jsonrpc: add Python with statement support
The JSONRPCClient class now support the with statement. From now on the __del__() method is changed to close() and user need to call it manually at object disposal or use the 'with' Python statement. Change-Id: I74afd93d411b9596ab8f9523c54a4a7523eec5e5 Signed-off-by: Pawel Wodkowski <pawelx.wodkowski@intel.com> Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/444686 Tested-by: SPDK CI Jenkins <sys_sgci@intel.com> Reviewed-by: Ben Walker <benjamin.walker@intel.com> Reviewed-by: Jim Harris <james.r.harris@intel.com> Reviewed-by: Pawel Kaminski <pawelx.kaminski@intel.com>
This commit is contained in:
parent
a282588584
commit
5c6ca5b6e0
@ -1737,10 +1737,11 @@ Format: 'user:u1 secret:s1 muser:mu1 msecret:ms1,user:u2 secret:s2 muser:mu2 mse
|
|||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
try:
|
with rpc.client.JSONRPCClient(args.server_addr, args.port, args.timeout, log_level=getattr(logging, args.verbose.upper())) as client:
|
||||||
args.client = rpc.client.JSONRPCClient(args.server_addr, args.port, args.timeout, log_level=getattr(logging, args.verbose.upper()))
|
try:
|
||||||
args.func(args)
|
args.client = client
|
||||||
except JSONRPCException as ex:
|
args.func(args)
|
||||||
print("Exception:")
|
except JSONRPCException as ex:
|
||||||
print(ex.message)
|
print("Exception:")
|
||||||
exit(1)
|
print(ex.message)
|
||||||
|
exit(1)
|
||||||
|
@ -48,6 +48,12 @@ class JSONRPCClient(object):
|
|||||||
raise JSONRPCException("Error while connecting to %s\n"
|
raise JSONRPCException("Error while connecting to %s\n"
|
||||||
"Error details: %s" % (addr, ex))
|
"Error details: %s" % (addr, ex))
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, exception_type, exception_value, traceback):
|
||||||
|
self.close()
|
||||||
|
|
||||||
def get_logger(self):
|
def get_logger(self):
|
||||||
return self._logger
|
return self._logger
|
||||||
|
|
||||||
@ -61,9 +67,11 @@ class JSONRPCClient(object):
|
|||||||
self._logger.setLevel(lvl)
|
self._logger.setLevel(lvl)
|
||||||
self._logger.info("Log level set to %s", lvl)
|
self._logger.info("Log level set to %s", lvl)
|
||||||
|
|
||||||
def __del__(self):
|
def close(self):
|
||||||
if getattr(self, "sock", None):
|
if getattr(self, "sock", None):
|
||||||
|
self.sock.shutdown(socket.SHUT_RDWR)
|
||||||
self.sock.close()
|
self.sock.close()
|
||||||
|
self.sock = None
|
||||||
|
|
||||||
def add_request(self, method, params):
|
def add_request(self, method, params):
|
||||||
self._request_id += 1
|
self._request_id += 1
|
||||||
|
@ -6,6 +6,7 @@ from os import getuid
|
|||||||
from rpc.client import JSONRPCException
|
from rpc.client import JSONRPCException
|
||||||
from configshell_fb import ConfigShell, shell, ExecutionError
|
from configshell_fb import ConfigShell, shell, ExecutionError
|
||||||
from spdkcli import UIRoot
|
from spdkcli import UIRoot
|
||||||
|
import rpc.client
|
||||||
from pyparsing import (alphanums, Optional, Suppress, Word, Regex,
|
from pyparsing import (alphanums, Optional, Suppress, Word, Regex,
|
||||||
removeQuotes, dblQuotedString, OneOrMore)
|
removeQuotes, dblQuotedString, OneOrMore)
|
||||||
|
|
||||||
@ -43,29 +44,30 @@ def main():
|
|||||||
help="commands to execute by SPDKCli as one-line command")
|
help="commands to execute by SPDKCli as one-line command")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
root_node = UIRoot(args.socket, spdk_shell)
|
with rpc.client.JSONRPCClient(args.socket) as client:
|
||||||
root_node.verbose = args.verbose
|
root_node = UIRoot(client, spdk_shell)
|
||||||
try:
|
root_node.verbose = args.verbose
|
||||||
root_node.refresh()
|
|
||||||
except BaseException:
|
|
||||||
pass
|
|
||||||
|
|
||||||
if len(args.commands) > 0:
|
|
||||||
try:
|
try:
|
||||||
spdk_shell.interactive = False
|
root_node.refresh()
|
||||||
spdk_shell.run_cmdline(" ".join(args.commands))
|
except BaseException:
|
||||||
except Exception as e:
|
pass
|
||||||
sys.stderr.write("%s\n" % e)
|
|
||||||
sys.exit(1)
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
spdk_shell.con.display("SPDK CLI v0.1")
|
if len(args.commands) > 0:
|
||||||
spdk_shell.con.display("")
|
try:
|
||||||
while not spdk_shell._exit:
|
spdk_shell.interactive = False
|
||||||
try:
|
spdk_shell.run_cmdline(" ".join(args.commands))
|
||||||
spdk_shell.run_interactive()
|
except Exception as e:
|
||||||
except (JSONRPCException, ExecutionError) as e:
|
sys.stderr.write("%s\n" % e)
|
||||||
spdk_shell.log.error("%s" % e)
|
sys.exit(1)
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
spdk_shell.con.display("SPDK CLI v0.1")
|
||||||
|
spdk_shell.con.display("")
|
||||||
|
while not spdk_shell._exit:
|
||||||
|
try:
|
||||||
|
spdk_shell.run_interactive()
|
||||||
|
except (JSONRPCException, ExecutionError) as e:
|
||||||
|
spdk_shell.log.error("%s" % e)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -10,14 +10,14 @@ class UIRoot(UINode):
|
|||||||
"""
|
"""
|
||||||
Root node for CLI menu tree structure. Refreshes running config on startup.
|
Root node for CLI menu tree structure. Refreshes running config on startup.
|
||||||
"""
|
"""
|
||||||
def __init__(self, s, shell):
|
def __init__(self, client, shell):
|
||||||
UINode.__init__(self, "/", shell=shell)
|
UINode.__init__(self, "/", shell=shell)
|
||||||
self.current_bdevs = []
|
self.current_bdevs = []
|
||||||
self.current_lvol_stores = []
|
self.current_lvol_stores = []
|
||||||
self.current_vhost_ctrls = []
|
self.current_vhost_ctrls = []
|
||||||
self.current_nvmf_transports = []
|
self.current_nvmf_transports = []
|
||||||
self.current_nvmf_subsystems = []
|
self.current_nvmf_subsystems = []
|
||||||
self.set_rpc_target(s)
|
self.set_rpc_target(client)
|
||||||
self.verbose = False
|
self.verbose = False
|
||||||
self.is_init = self.check_init()
|
self.is_init = self.check_init()
|
||||||
self.methods = []
|
self.methods = []
|
||||||
@ -45,8 +45,8 @@ class UIRoot(UINode):
|
|||||||
if self.has_subsystem("iscsi"):
|
if self.has_subsystem("iscsi"):
|
||||||
UIISCSI(self)
|
UIISCSI(self)
|
||||||
|
|
||||||
def set_rpc_target(self, s):
|
def set_rpc_target(self, client):
|
||||||
self.client = rpc.client.JSONRPCClient(s)
|
self.client = client
|
||||||
|
|
||||||
def print_array(self, a):
|
def print_array(self, a):
|
||||||
return " ".join(a)
|
return " ".join(a)
|
||||||
|
@ -209,9 +209,10 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
try:
|
with rpc.client.JSONRPCClient(args.server_addr, args.port, args.timeout, log_level=getattr(logging, args.verbose.upper())) as client:
|
||||||
args.client = rpc.client.JSONRPCClient(args.server_addr, args.port, args.timeout, log_level=getattr(logging, args.verbose.upper()))
|
try:
|
||||||
except JSONRPCException as ex:
|
args.client = client
|
||||||
print((ex.message))
|
args.func(args)
|
||||||
exit(1)
|
except JSONRPCException as ex:
|
||||||
args.func(args)
|
print((ex.message))
|
||||||
|
exit(1)
|
||||||
|
Loading…
Reference in New Issue
Block a user