|
| 1 | +"""Snakemake descriptive logger plugin.""" |
| 2 | + |
| 3 | +import sys |
| 4 | +import textwrap |
| 5 | +from dataclasses import dataclass |
| 6 | + |
| 7 | +from snakemake.logging import ( |
| 8 | + ColorizingTextHandler, |
| 9 | + DefaultFormatter, |
| 10 | + timestamp, |
| 11 | +) |
| 12 | +from snakemake_interface_logger_plugins.base import LogHandlerBase |
| 13 | +from snakemake_interface_logger_plugins.settings import LogHandlerSettingsBase |
| 14 | + |
| 15 | + |
| 16 | +@dataclass |
| 17 | +class LogHandlerSettings(LogHandlerSettingsBase): |
| 18 | + pass |
| 19 | + |
| 20 | + |
| 21 | +class DescriptiveFormatter(DefaultFormatter): |
| 22 | + BOLD_SEQ = "\033[1m" |
| 23 | + NON_BOLD_SEQ = "\033[21m" |
| 24 | + |
| 25 | + def format_job_info(self, msg): |
| 26 | + """Format for job_info log.""" |
| 27 | + output = [] |
| 28 | + |
| 29 | + output.append(timestamp()) |
| 30 | + if msg["rule_msg"]: |
| 31 | + output.append(msg["rule_msg"]) |
| 32 | + output.append("\n".join(self._format_job_info(msg))) |
| 33 | + |
| 34 | + if msg.get("indent", False): |
| 35 | + return textwrap.indent("\n".join(output), " ") |
| 36 | + return "\n".join(output) |
| 37 | + |
| 38 | + |
| 39 | +class LogHandler(LogHandlerBase, ColorizingTextHandler): |
| 40 | + def __post_init__(self) -> None: |
| 41 | + # initialize additional attributes |
| 42 | + # Do not overwrite the __init__ method as this is kept in control of the base |
| 43 | + # class in order to simplify the update process. |
| 44 | + # See https://github.com/snakemake/snakemake-interface-logger-plugins/blob/main/src/snakemake_interface_logger_plugins/base.py # noqa: E501 |
| 45 | + # for attributes of the base class. |
| 46 | + # In particular, the settings of above LogHandlerSettings class are accessible via |
| 47 | + # self.settings. |
| 48 | + # You also have access to self.common_settings here, which are logging settings supplied by the caller in the form of OutputSettingsLoggerInterface. # noqa: E501 |
| 49 | + # See https://github.com/snakemake/snakemake-interface-logger-plugins/blob/main/src/snakemake_interface_logger_plugins/settings.py for more details # noqa: E501 |
| 50 | + ColorizingTextHandler.__init__( |
| 51 | + self, |
| 52 | + nocolor=self.common_settings.nocolor, |
| 53 | + stream=sys.stdout if self.common_settings.stdout else sys.stderr, |
| 54 | + ) |
| 55 | + self.setFormatter( |
| 56 | + DescriptiveFormatter( |
| 57 | + self.common_settings.quiet, self.common_settings.show_failed_logs |
| 58 | + ) |
| 59 | + ) |
| 60 | + |
| 61 | + # Here you can override logging.Handler methods to customize logging behavior. |
| 62 | + # Only an implementation of the emit() method is required. See the Python logging |
| 63 | + # documentation for details: |
| 64 | + # https://docs.python.org/3/library/logging.html#handler-objects |
| 65 | + |
| 66 | + # LogRecords from Snakemake carry contextual information in the record's attributes |
| 67 | + # Of particular interest is the 'event' attribute, which indicates the type of log information contained |
| 68 | + # See https://github.com/snakemake/snakemake-interface-logger-plugins/blob/2ab84cb31f0b92cf0b7ee3026e15d1209729d197/src/snakemake_interface_logger_plugins/common.py#L33 # noqa: E501 |
| 69 | + # For examples on parsing LogRecords, see https://github.com/cademirch/snakemake-logger-plugin-snkmt/blob/main/src/snakemake_logger_plugin_snkmt/parsers.py # noqa: E501 |
| 70 | + |
| 71 | + def emit(self, record): |
| 72 | + # Actually emit the record. Typically this will call self.format(record) to |
| 73 | + # convert the record to a formatted string. The result could then be written to |
| 74 | + # a stream or file. |
| 75 | + ColorizingTextHandler.emit(self, record) |
| 76 | + |
| 77 | + @property |
| 78 | + def writes_to_stream(self) -> bool: |
| 79 | + # Whether this plugin writes to stderr/stdout. |
| 80 | + # If your plugin writes to stderr/stdout, return |
| 81 | + # true so that Snakemake disables its stderr logging. |
| 82 | + return True |
| 83 | + |
| 84 | + @property |
| 85 | + def writes_to_file(self) -> bool: |
| 86 | + # Whether this plugin writes to a file. |
| 87 | + # If your plugin writes log output to a file, return |
| 88 | + # true so that Snakemake can report your logfile path at workflow end. |
| 89 | + # NOTE: Handlers that return True must provide a baseFilename attribute |
| 90 | + # containing the file path. |
| 91 | + return False |
| 92 | + |
| 93 | + @property |
| 94 | + def has_filter(self) -> bool: |
| 95 | + # Whether this plugin attaches its own filter. |
| 96 | + # Return true if your plugin provides custom log filtering logic. |
| 97 | + # If false is returned, Snakemake's DefaultFilter will be attached see: https://github.com/snakemake/snakemake/blob/960f6a89eaa31da6014e810dfcf08f635ac03a6e/src/snakemake/logging.py#L372 # noqa: E501 |
| 98 | + # See https://docs.python.org/3/library/logging.html#filter-objects for info on how to define and attach a Filter |
| 99 | + return False |
| 100 | + |
| 101 | + @property |
| 102 | + def has_formatter(self) -> bool: |
| 103 | + # Whether this plugin attaches its own formatter. |
| 104 | + # Return true if your plugin provides custom log formatting logic. |
| 105 | + # If false is returned, Snakemake's Defaultformatter will be attached see: https://github.com/snakemake/snakemake/blob/960f6a89eaa31da6014e810dfcf08f635ac03a6e/src/snakemake/logging.py#L132 # noqa: E501 |
| 106 | + # See https://docs.python.org/3/library/logging.html#formatter-objects for info on how to define and attach a Formatter |
| 107 | + return True |
| 108 | + |
| 109 | + @property |
| 110 | + def needs_rulegraph(self) -> bool: |
| 111 | + # Whether this plugin requires the DAG rulegraph. |
| 112 | + # Return true if your plugin needs access to the workflow's |
| 113 | + # directed acyclic graph for logging purposes. |
| 114 | + return False |
0 commit comments