Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/coloredlogs/cli.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 # Command line interface for the coloredlogs package. | |
2 # | |
3 # Author: Peter Odding <peter@peterodding.com> | |
4 # Last Change: December 15, 2017 | |
5 # URL: https://coloredlogs.readthedocs.io | |
6 | |
7 """ | |
8 Usage: coloredlogs [OPTIONS] [ARGS] | |
9 | |
10 The coloredlogs program provides a simple command line interface for the Python | |
11 package by the same name. | |
12 | |
13 Supported options: | |
14 | |
15 -c, --convert, --to-html | |
16 | |
17 Capture the output of an external command (given by the positional | |
18 arguments) and convert ANSI escape sequences in the output to HTML. | |
19 | |
20 If the `coloredlogs' program is attached to an interactive terminal it will | |
21 write the generated HTML to a temporary file and open that file in a web | |
22 browser, otherwise the generated HTML will be written to standard output. | |
23 | |
24 This requires the `script' program to fake the external command into | |
25 thinking that it's attached to an interactive terminal (in order to enable | |
26 output of ANSI escape sequences). | |
27 | |
28 If the command didn't produce any output then no HTML will be produced on | |
29 standard output, this is to avoid empty emails from cron jobs. | |
30 | |
31 -d, --demo | |
32 | |
33 Perform a simple demonstration of the coloredlogs package to show the | |
34 colored logging on an interactive terminal. | |
35 | |
36 -h, --help | |
37 | |
38 Show this message and exit. | |
39 """ | |
40 | |
41 # Standard library modules. | |
42 import functools | |
43 import getopt | |
44 import logging | |
45 import sys | |
46 import tempfile | |
47 import webbrowser | |
48 | |
49 # External dependencies. | |
50 from humanfriendly.terminal import connected_to_terminal, output, usage, warning | |
51 | |
52 # Modules included in our package. | |
53 from coloredlogs.converter import capture, convert | |
54 from coloredlogs.demo import demonstrate_colored_logging | |
55 | |
56 # Initialize a logger for this module. | |
57 logger = logging.getLogger(__name__) | |
58 | |
59 | |
60 def main(): | |
61 """Command line interface for the ``coloredlogs`` program.""" | |
62 actions = [] | |
63 try: | |
64 # Parse the command line arguments. | |
65 options, arguments = getopt.getopt(sys.argv[1:], 'cdh', [ | |
66 'convert', 'to-html', 'demo', 'help', | |
67 ]) | |
68 # Map command line options to actions. | |
69 for option, value in options: | |
70 if option in ('-c', '--convert', '--to-html'): | |
71 actions.append(functools.partial(convert_command_output, *arguments)) | |
72 arguments = [] | |
73 elif option in ('-d', '--demo'): | |
74 actions.append(demonstrate_colored_logging) | |
75 elif option in ('-h', '--help'): | |
76 usage(__doc__) | |
77 return | |
78 else: | |
79 assert False, "Programming error: Unhandled option!" | |
80 if not actions: | |
81 usage(__doc__) | |
82 return | |
83 except Exception as e: | |
84 warning("Error: %s", e) | |
85 sys.exit(1) | |
86 for function in actions: | |
87 function() | |
88 | |
89 | |
90 def convert_command_output(*command): | |
91 """ | |
92 Command line interface for ``coloredlogs --to-html``. | |
93 | |
94 Takes a command (and its arguments) and runs the program under ``script`` | |
95 (emulating an interactive terminal), intercepts the output of the command | |
96 and converts ANSI escape sequences in the output to HTML. | |
97 """ | |
98 captured_output = capture(command) | |
99 converted_output = convert(captured_output) | |
100 if connected_to_terminal(): | |
101 fd, temporary_file = tempfile.mkstemp(suffix='.html') | |
102 with open(temporary_file, 'w') as handle: | |
103 handle.write(converted_output) | |
104 webbrowser.open(temporary_file) | |
105 elif captured_output and not captured_output.isspace(): | |
106 output(converted_output) |