mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
[enh] Replace print_* arguments by output_as in the cli
This commit is contained in:
parent
013447bbe7
commit
2e83d9d1ff
2 changed files with 16 additions and 15 deletions
|
@ -94,8 +94,7 @@ def api(namespaces, host='localhost', port=80, routes={},
|
||||||
'use_cache': use_cache })
|
'use_cache': use_cache })
|
||||||
moulinette.run(host, port)
|
moulinette.run(host, port)
|
||||||
|
|
||||||
def cli(namespaces, args, print_json=False, print_plain=False, use_cache=True,
|
def cli(namespaces, args, use_cache=True, output_as=None, parser_kwargs={}):
|
||||||
parser_kwargs={}):
|
|
||||||
"""Command line interface
|
"""Command line interface
|
||||||
|
|
||||||
Execute an action with the moulinette from the CLI and print its
|
Execute an action with the moulinette from the CLI and print its
|
||||||
|
@ -104,10 +103,10 @@ def cli(namespaces, args, print_json=False, print_plain=False, use_cache=True,
|
||||||
Keyword arguments:
|
Keyword arguments:
|
||||||
- namespaces -- The list of namespaces to use
|
- namespaces -- The list of namespaces to use
|
||||||
- args -- A list of argument strings
|
- args -- A list of argument strings
|
||||||
- print_json -- True to print result as a JSON encoded string
|
|
||||||
- print_plain -- True to print result as a script-usable string
|
|
||||||
- use_cache -- False if it should parse the actions map file
|
- use_cache -- False if it should parse the actions map file
|
||||||
instead of using the cached one
|
instead of using the cached one
|
||||||
|
- output_as -- Output result in another format, see
|
||||||
|
moulinette.interfaces.cli.Interface for possible values
|
||||||
- parser_kwargs -- A dict of arguments to pass to the parser
|
- parser_kwargs -- A dict of arguments to pass to the parser
|
||||||
class at construction
|
class at construction
|
||||||
|
|
||||||
|
@ -120,7 +119,7 @@ def cli(namespaces, args, print_json=False, print_plain=False, use_cache=True,
|
||||||
'parser_kwargs': parser_kwargs,
|
'parser_kwargs': parser_kwargs,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
moulinette.run(args, print_json, print_plain)
|
moulinette.run(args, output_as=output_as)
|
||||||
except MoulinetteError as e:
|
except MoulinetteError as e:
|
||||||
import logging
|
import logging
|
||||||
logging.getLogger('yunohost').error(e.strerror)
|
logging.getLogger('yunohost').error(e.strerror)
|
||||||
|
|
|
@ -278,7 +278,7 @@ class Interface(BaseInterface):
|
||||||
|
|
||||||
self.actionsmap = actionsmap
|
self.actionsmap = actionsmap
|
||||||
|
|
||||||
def run(self, args, print_json=False, print_plain=False):
|
def run(self, args, output_as=None):
|
||||||
"""Run the moulinette
|
"""Run the moulinette
|
||||||
|
|
||||||
Process the action corresponding to the given arguments 'args'
|
Process the action corresponding to the given arguments 'args'
|
||||||
|
@ -286,11 +286,12 @@ class Interface(BaseInterface):
|
||||||
|
|
||||||
Keyword arguments:
|
Keyword arguments:
|
||||||
- args -- A list of argument strings
|
- args -- A list of argument strings
|
||||||
- print_json -- True to print result as a JSON encoded string
|
- output_as -- Output result in another format. Possible values:
|
||||||
- print_plain -- True to print result as a script-usable string
|
- json: return a JSON encoded string
|
||||||
|
- plain: return a script-readable output
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if print_json and print_plain:
|
if output_as and output_as not in ['json', 'plain']:
|
||||||
raise MoulinetteError(errno.EINVAL, m18n.g('invalid_usage'))
|
raise MoulinetteError(errno.EINVAL, m18n.g('invalid_usage'))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -302,12 +303,13 @@ class Interface(BaseInterface):
|
||||||
return
|
return
|
||||||
|
|
||||||
# Format and print result
|
# Format and print result
|
||||||
if print_json:
|
if output_as:
|
||||||
import json
|
if output_as == 'json':
|
||||||
from moulinette.utils.serialize import JSONExtendedEncoder
|
import json
|
||||||
print(json.dumps(ret, cls=JSONExtendedEncoder))
|
from moulinette.utils.serialize import JSONExtendedEncoder
|
||||||
elif print_plain:
|
print(json.dumps(ret, cls=JSONExtendedEncoder))
|
||||||
plain_print_dict(ret)
|
else:
|
||||||
|
plain_print_dict(ret)
|
||||||
elif isinstance(ret, dict):
|
elif isinstance(ret, dict):
|
||||||
pretty_print_dict(ret)
|
pretty_print_dict(ret)
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Add table
Reference in a new issue