/
cli.py
102 lines (82 loc) · 3.16 KB
/
cli.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# Command line interface for the coloredlogs package.
#
# Author: Peter Odding <peter@peterodding.com>
# Last Change: October 14, 2015
# URL: https://coloredlogs.readthedocs.org
"""
Usage: coloredlogs [OPTIONS] [ARGS]
The coloredlogs program provides a simple command line interface for the Python
package by the same name.
Supported options:
-c, --convert, --to-html
Capture the output of an external command (given by the positional
arguments) and convert ANSI escape sequences in the output to HTML.
If the `coloredlogs' program is attached to an interactive terminal it will
write the generated HTML to a temporary file and open that file in a web
browser, otherwise the generated HTML will be written to standard output.
This requires the `script' program to fake the external command into
thinking that it's attached to an interactive terminal (in order to enable
output of ANSI escape sequences).
-d, --demo
Perform a simple demonstration of the coloredlogs package to show the
colored logging on an interactive terminal.
-h, --help
Show this message and exit.
"""
# Standard library modules.
import functools
import getopt
import logging
import sys
import tempfile
import webbrowser
# External dependencies.
from humanfriendly.terminal import connected_to_terminal, usage
# Modules included in our package.
from coloredlogs.converter import capture, convert
from coloredlogs.demo import demonstrate_colored_logging
# Initialize a logger for this module.
logger = logging.getLogger(__name__)
def main():
"""Command line interface for the `coloredlogs` program."""
actions = []
try:
# Parse the command line arguments.
options, arguments = getopt.getopt(sys.argv[1:], 'cdh', [
'convert', 'to-html', 'demo', 'help',
])
# Map command line options to actions.
for option, value in options:
if option in ('-c', '--convert', '--to-html'):
actions.append(functools.partial(convert_command_output, *arguments))
arguments = []
elif option in ('-d', '--demo'):
actions.append(demonstrate_colored_logging)
elif option in ('-h', '--help'):
usage(__doc__)
return
else:
assert False, "Programming error: Unhandled option!"
if not actions:
usage(__doc__)
return
except Exception as e:
sys.stderr.write("Error: %s\n" % e)
sys.exit(1)
for function in actions:
function()
def convert_command_output(*command):
"""
Command line interface for ``coloredlogs --to-html``.
Takes a command (and its arguments) and runs the program under ``script``
(emulating an interactive terminal), intercepts the output of the command
and converts ANSI escape sequences in the output to HTML.
"""
html_output = convert(capture(command))
if connected_to_terminal():
fd, temporary_file = tempfile.mkstemp(suffix='.html')
with open(temporary_file, 'w') as handle:
handle.write(html_output)
webbrowser.open(temporary_file)
else:
print(html_output)