22from json import dumps , loads
33from json .decoder import JSONDecodeError
44from sys import stdout
5- from typing import BinaryIO
5+ from typing import BinaryIO , TextIO
66
77from . import doc_split , usage , version
88from .extras import json_utf8_decode , json_utf8_encode
@@ -48,22 +48,28 @@ def _loads(string: str) -> str:
4848 return string
4949
5050
51- def rpc_list (handle : BinaryIO , device : str , baudrate : int , wait : int ) -> None :
51+ def rpc_list (
52+ handle : BinaryIO , device : str , baudrate : int , wait : int , save : TextIO
53+ ) -> None :
5254 """List the device methods.
5355
5456 :arg handle: Output handle.
5557 :arg device: Device.
5658 :arg baudrate: Baud rate.
5759 :arg wait: Time in seconds before communication starts.
60+ :arg save: Interface definition file.
5861 """
5962 with Interface (device , baudrate , wait ) as interface :
60- for method in interface .methods .values ():
61- handle .write (_describe_method (method ) + '\n \n \n ' )
63+ if not save :
64+ for method in interface .methods .values ():
65+ handle .write (_describe_method (method ) + '\n \n \n ' )
66+ else :
67+ interface .save (save )
6268
6369
6470def rpc_call (
6571 handle : BinaryIO , device : str , baudrate : int , wait : int , name : str ,
66- args : list ) -> None :
72+ args : list , load : TextIO ) -> None :
6773 """Execute a method.
6874
6975 :arg handle: Output handle.
@@ -72,10 +78,11 @@ def rpc_call(
7278 :arg wait: Time in seconds before communication starts.
7379 :arg name: Method name.
7480 :arg args: Method parameters.
81+ :arg load: Interface definition file.
7582 """
7683 args_ = list (map (lambda x : json_utf8_encode (_loads (x )), args ))
7784
78- with Interface (device , baudrate , wait ) as interface :
85+ with Interface (device , baudrate , wait , True , load ) as interface :
7986 result = interface .call_method (name , * args_ )
8087
8188 if result is not None :
@@ -110,16 +117,22 @@ def _arg_parser() -> object:
110117 subparser = subparsers .add_parser (
111118 'list' , formatter_class = ArgumentDefaultsHelpFormatter ,
112119 parents = [common_parser ], description = doc_split (rpc_list ))
120+ subparser .add_argument (
121+ '-s' , dest = 'save' , type = FileType ('w' ), default = None ,
122+ help = 'interface definition file' )
113123 subparser .set_defaults (func = rpc_list )
114124
115125 subparser = subparsers .add_parser (
116126 'call' , formatter_class = ArgumentDefaultsHelpFormatter ,
117127 parents = [common_parser ], description = doc_split (rpc_call ))
118- subparser .set_defaults (func = rpc_call )
119128 subparser .add_argument (
120129 'name' , metavar = 'NAME' , type = str , help = 'command name' )
121130 subparser .add_argument (
122131 'args' , metavar = 'ARG' , type = str , nargs = '*' , help = 'command parameter' )
132+ subparser .add_argument (
133+ '-l' , dest = 'load' , type = FileType ('r' ), default = None ,
134+ help = 'interface definition file' )
135+ subparser .set_defaults (func = rpc_call )
123136
124137 return parser
125138
0 commit comments