m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.4 KiB

6 months ago
  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. Usage: coloredlogs [OPTIONS] [ARGS]
  8. The coloredlogs program provides a simple command line interface for the Python
  9. package by the same name.
  10. Supported options:
  11. -c, --convert, --to-html
  12. Capture the output of an external command (given by the positional
  13. arguments) and convert ANSI escape sequences in the output to HTML.
  14. If the `coloredlogs' program is attached to an interactive terminal it will
  15. write the generated HTML to a temporary file and open that file in a web
  16. browser, otherwise the generated HTML will be written to standard output.
  17. This requires the `script' program to fake the external command into
  18. thinking that it's attached to an interactive terminal (in order to enable
  19. output of ANSI escape sequences).
  20. If the command didn't produce any output then no HTML will be produced on
  21. standard output, this is to avoid empty emails from cron jobs.
  22. -d, --demo
  23. Perform a simple demonstration of the coloredlogs package to show the
  24. colored logging on an interactive terminal.
  25. -h, --help
  26. Show this message and exit.
  27. """
  28. # Standard library modules.
  29. import functools
  30. import getopt
  31. import logging
  32. import sys
  33. import tempfile
  34. import webbrowser
  35. # External dependencies.
  36. from humanfriendly.terminal import connected_to_terminal, output, usage, warning
  37. # Modules included in our package.
  38. from coloredlogs.converter import capture, convert
  39. from coloredlogs.demo import demonstrate_colored_logging
  40. # Initialize a logger for this module.
  41. logger = logging.getLogger(__name__)
  42. def main():
  43. """Command line interface for the ``coloredlogs`` program."""
  44. actions = []
  45. try:
  46. # Parse the command line arguments.
  47. options, arguments = getopt.getopt(sys.argv[1:], 'cdh', [
  48. 'convert', 'to-html', 'demo', 'help',
  49. ])
  50. # Map command line options to actions.
  51. for option, value in options:
  52. if option in ('-c', '--convert', '--to-html'):
  53. actions.append(functools.partial(convert_command_output, *arguments))
  54. arguments = []
  55. elif option in ('-d', '--demo'):
  56. actions.append(demonstrate_colored_logging)
  57. elif option in ('-h', '--help'):
  58. usage(__doc__)
  59. return
  60. else:
  61. assert False, "Programming error: Unhandled option!"
  62. if not actions:
  63. usage(__doc__)
  64. return
  65. except Exception as e:
  66. warning("Error: %s", e)
  67. sys.exit(1)
  68. for function in actions:
  69. function()
  70. def convert_command_output(*command):
  71. """
  72. Command line interface for ``coloredlogs --to-html``.
  73. Takes a command (and its arguments) and runs the program under ``script``
  74. (emulating an interactive terminal), intercepts the output of the command
  75. and converts ANSI escape sequences in the output to HTML.
  76. """
  77. captured_output = capture(command)
  78. converted_output = convert(captured_output)
  79. if connected_to_terminal():
  80. fd, temporary_file = tempfile.mkstemp(suffix='.html')
  81. with open(temporary_file, 'w') as handle:
  82. handle.write(converted_output)
  83. webbrowser.open(temporary_file)
  84. elif captured_output and not captured_output.isspace():
  85. output(converted_output)