-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlbu_cli.py
More file actions
executable file
·50 lines (46 loc) · 2 KB
/
Copy pathlbu_cli.py
File metadata and controls
executable file
·50 lines (46 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python3
from logging import info, warn, error
import logging
import subprocess
from lbu_common import cli_func, BadArgumentsError
logging.getLogger().setLevel(logging.INFO)
_log_colors=dict([(k_v[0], "\033[%sm"%(k_v[1],)) for k_v in list(dict(blue="34", red="1;31", yellow="33", reset="1;0").items())])
try: subprocess.check_call(["tty","-s"])
except subprocess.CalledProcessError: pass
else:
logging.addLevelName(logging.INFO, "{blue}{level}{reset}".format(level=logging.getLevelName(logging.INFO), **_log_colors))
logging.addLevelName(logging.WARNING, "{yellow}{level}{reset}".format(level=logging.getLevelName(logging.WARNING), **_log_colors))
logging.addLevelName(logging.ERROR, "{red}{level}{reset}".format(level=logging.getLevelName(logging.ERROR), **_log_colors))
if __name__ == '__main__':
import sys, os
args = sys.argv[:]
arg0=os.path.basename(args.pop(0))
try: command=args.pop(0)
except IndexError:
warn("Usage: %s [{--debug|--quiet}] <command> [<args..>]", arg0)
info("Supported commands:%s",
"".join(["\n\t%s\t%s"%(n_f[0], getattr(n_f[1], "_cli_desc", "")) for n_f in sorted(cli_func.commands.items())]))
raise SystemExit(1)
if command=='--debug':
logging.getLogger().setLevel(logging.DEBUG)
command=args.pop(0)
elif command=="--quiet":
logging.getLogger().setLevel(logging.WARN)
command=args.pop(0)
logging.getLogger().name=command
try: cmd_func=cli_func.commands[command]
except KeyError:
error("Unknown command: %s", command)
raise SystemExit(1)
try: ret=cmd_func.cli_call(args)
except BadArgumentsError as e:
error("Execution error: %s", e)
info("Usage: %s %s %s", arg0, command, cmd_func.__doc__)
raise SystemExit(1)
if ret is not None:
if isinstance(ret, list):
for e in ret: print(e)
elif isinstance(ret, dict):
print(__import__("json").dumps(ret, indent=True))
else:
print(ret)