MCPcopy
hub / github.com/hugapi/hug / cli

Function cli

hug/test.py:109–150  ·  view source on GitHub ↗

Simulates testing a hug cli method from the command line

(method, *args, api=None, module=None, **arguments)

Source from the content-addressed store, hash-verified

107
108
109def cli(method, *args, api=None, module=None, **arguments):
110 """Simulates testing a hug cli method from the command line"""
111 collect_output = arguments.pop("collect_output", True)
112 if api and module:
113 raise ValueError("Please specify an API OR a Module that contains the API, not both")
114 elif api or module:
115 method = API(api or module).cli.commands[method].interface._function
116
117 command_args = [method.__name__] + list(args)
118 for name, values in arguments.items():
119 if not isinstance(values, (tuple, list)):
120 values = (values,)
121 for value in values:
122 command_args.append("--{0}".format(name))
123 if not value in (True, False):
124 command_args.append("{0}".format(value))
125
126 old_sys_argv = sys.argv
127 sys.argv = [str(part) for part in command_args]
128
129 old_outputs = method.interface.cli.outputs
130 if collect_output:
131 method.interface.cli.outputs = lambda data: to_return.append(old_outputs(data))
132 to_return = []
133
134 try:
135 method.interface.cli()
136 except Exception as e:
137 to_return = (e,)
138
139 method.interface.cli.outputs = old_outputs
140 sys.argv = old_sys_argv
141 if to_return:
142 result = _internal_result(to_return)
143 try:
144 result = json.loads(result)
145 except Exception:
146 try:
147 result = ast.literal_eval(result)
148 except Exception:
149 pass
150 return result

Callers 3

__call__Method · 0.70
decoratorMethod · 0.70
cliMethod · 0.70

Calls 4

APIClass · 0.90
_internal_resultFunction · 0.85
cliMethod · 0.45
loadsMethod · 0.45

Tested by

no test coverage detected