Repository: incubator-ariatosca Updated Branches: refs/heads/ARIA-146-Support-colorful-execution-logging e3b25d7ad -> 193f3738a
removed class Project: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/commit/193f3738 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/tree/193f3738 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/diff/193f3738 Branch: refs/heads/ARIA-146-Support-colorful-execution-logging Commit: 193f3738a39f5588bdcfe5902868a8a6c22da205 Parents: e3b25d7 Author: max-orlov <[email protected]> Authored: Wed Apr 26 17:09:30 2017 +0300 Committer: max-orlov <[email protected]> Committed: Wed Apr 26 17:09:30 2017 +0300 ---------------------------------------------------------------------- aria/cli/color.py | 48 +++---- aria/cli/config/config.py | 1 - aria/cli/execution_logging.py | 248 ++++++++++++++++++------------------- 3 files changed, 147 insertions(+), 150 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/193f3738/aria/cli/color.py ---------------------------------------------------------------------- diff --git a/aria/cli/color.py b/aria/cli/color.py index 99d584f..7307e8a 100644 --- a/aria/cli/color.py +++ b/aria/cli/color.py @@ -13,9 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. from StringIO import StringIO +import re import colorama -import re colorama.init() @@ -27,36 +27,36 @@ def _get_colors(color_type): class StylizedString(object): - def __init__(self, str_, schema=None): - self._str = str_ - self._schema = schema + def __init__(self, str_, schema=None): + self._str = str_ + self._schema = schema - def __repr__(self): - if self._schema: - return '{schema}{str}{reset}'.format( - schema=self._schema, str=str(self._str), reset=Color.Style.RESET_ALL) - return self._str + def __repr__(self): + if self._schema: + return '{schema}{str}{reset}'.format( + schema=self._schema, str=str(self._str), reset=Color.Style.RESET_ALL) + return self._str - def __add__(self, other): - return str(self) + other + def __add__(self, other): + return str(self) + other - def __radd__(self, other): - return other + str(self) + def __radd__(self, other): + return other + str(self) - def color(self, schema): - self._schema = schema + def color(self, schema): + self._schema = schema - def replace(self, old, new, **kwargs): - self._str = self._str.replace(str(old), str(new), **kwargs) + def replace(self, old, new, **kwargs): + self._str = self._str.replace(str(old), str(new), **kwargs) - def format(self, *args, **kwargs): - self._str = self._str.format(*args, **kwargs) + def format(self, *args, **kwargs): + self._str = self._str.format(*args, **kwargs) - def highlight(self, original_value, schema, original_schema, pattern=None): - if pattern is None: - return - for match in set(re.findall(re.compile(pattern), str(original_value))): - self.replace(match, schema + match + Color.Style.RESET_ALL + original_schema) + def highlight(self, original_value, schema, original_schema, pattern=None): + if pattern is None: + return + for match in set(re.findall(re.compile(pattern), str(original_value))): + self.replace(match, schema + match + Color.Style.RESET_ALL + original_schema) class Color(object): http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/193f3738/aria/cli/config/config.py ---------------------------------------------------------------------- diff --git a/aria/cli/config/config.py b/aria/cli/config/config.py index 01e757c..d664518 100644 --- a/aria/cli/config/config.py +++ b/aria/cli/config/config.py @@ -83,4 +83,3 @@ class CliConfig(object): @property def formats(self): return self._logging.get('execution', {}).get('formats', {}) - http://git-wip-us.apache.org/repos/asf/incubator-ariatosca/blob/193f3738/aria/cli/execution_logging.py ---------------------------------------------------------------------- diff --git a/aria/cli/execution_logging.py b/aria/cli/execution_logging.py index 022eeeb..7aa7b64 100644 --- a/aria/cli/execution_logging.py +++ b/aria/cli/execution_logging.py @@ -15,7 +15,6 @@ import os import re from StringIO import StringIO -from contextlib import contextmanager from . import logger from .color import Color @@ -99,134 +98,41 @@ _DEFAULT_STYLE = { MARKER: 'lightyellow_ex' } -class _StylizedLogs(object): - - def _update_implementation(self, str_, implementation, log_item, mark_pattern=None): - schema = self._stylize(str_, implementation, log_item, IMPLEMENTATION, mark_pattern) - - def _update_inputs(self, str_, inputs, log_item, mark_pattern=None): - self._stylize(str_, inputs, log_item, INPUTS, mark_pattern) - - def _update_timestamp(self, str_, timestamp, log_item): - self._stylize(str_, timestamp, log_item, TIMESTAMP) - - def _update_message(self, str_, message, log_item, mark_pattern=None): - self._stylize(str_, message, log_item, MESSAGE, mark_pattern) - - def _update_level(self, str_, log_item): - self._stylize(str_, log_item.level[0], log_item, LEVEL) - - def _stylize(self, stylized_str, msg, log_item, msg_type, mark_pattern=None): - matched_str = self._find_pattern(_PATTERNS[FIELD_TYPE][msg_type], stylized_str ._str) - if not matched_str: - return stylized_str - - # if Styling was disabled - if not self._is_styling_enabled: - return stylized_str.replace(matched_str, matched_str.format(**{msg_type: msg})) - - # If the log is not of a final workflow message, it could be configured through the - # config file. Thus it should be instantiated from the dict provided. - schema = self._resolve_schema(msg_type, log_item) - substring = Color.stylize(matched_str) - substring.color(schema or '') - substring.format(**{msg_type: msg}) - substring.highlight(msg, self._marker_schema, schema, mark_pattern) - stylized_str.replace(matched_str, substring) - - def _get_traceback(self, traceback, log_item, mark_pattern): - schema = Color.Schema(**self._styles[TRACEBACK].get(log_item.level.lower(), {})) - stylized_string = Color.stylize(traceback, schema) - stylized_string.highlight(traceback, self._marker_schema, schema, mark_pattern) - return stylized_string - - def _end_execution_schema(self, original_message): - if not self.is_workflow_log: - return - for state, pattern in _PATTERNS[FINAL_STATES].items(): - if re.match(pattern, original_message): - return _FINAL_STATES[state] - - def _resolve_schema(self, msg_type, log_item): - schema = self._end_execution_schema(log_item.msg) - - if schema is None: - typed_schema_config = self._styles[msg_type].get(log_item.level.lower()) - user_default_schema_config = self._styles[msg_type].get('default') - aria_schema_config = _DEFAULT_STYLE[msg_type].get(log_item.level.lower()) - schema = Color.Schema(**(typed_schema_config or - user_default_schema_config or - aria_schema_config)) - - return schema - - - @staticmethod - def _find_pattern(pattern, field_value): - # TODO: this finds the matching field type according to a pattern - match = re.match(pattern, field_value) - if match: - return match.group(1) - - @property - def _verbosity_level(self): - return env.logging.verbosity_level - - @property - def _format(self): - return env.config.logging.formats[self._verbosity_level] - - @property - def _styles(self): - return env.config.logging.styles - - @property - def _is_styling_enabled(self): - return env.config.logging.styling_enabled - - @property - def _marker_schema(self): - return Color.Schema(back=self._styles[MARKER]) - - def __call__(self, item, mark_pattern): - - # implementation - if item.task: - # operation task - implementation = item.task.implementation - inputs = dict(i.unwrap() for i in item.task.inputs.values()) - self.is_workflow_log = False - else: - # execution task - implementation = item.execution.workflow_name - inputs = dict(i.unwrap() for i in item.execution.inputs.values()) - self.is_workflow_log = True - - # TODO: use the is_workflow_log - stylized_str = Color.stylize(self._format) - self._update_level(stylized_str, item) - self._update_timestamp(stylized_str, item.created_at, item) - self._update_message(stylized_str, item.msg, item, mark_pattern) - self._update_inputs(stylized_str, inputs, item, mark_pattern) - self._update_implementation(stylized_str, implementation, item, mark_pattern) - - msg = StringIO() - msg.write(str(stylized_str)) - # Add the exception and the error msg. - if item.traceback and env.logging.verbosity_level >= logger.MEDIUM_VERBOSE: - msg.write(os.linesep) - msg.writelines(self._get_traceback('\t' + '|' + line, item, mark_pattern) - for line in item.traceback.splitlines(True)) - - return msg.getvalue() - - -_stylize_log = _StylizedLogs() + +def stylize_log(item, mark_pattern): + + # implementation + if item.task: + # operation task + implementation = item.task.implementation + inputs = dict(i.unwrap() for i in item.task.inputs.values()) + else: + # execution task + implementation = item.execution.workflow_name + inputs = dict(i.unwrap() for i in item.execution.inputs.values()) + + # TODO: use the is_workflow_log + stylized_str = Color.stylize(_get_format()) + _update_level(stylized_str, item) + _update_timestamp(stylized_str, item.created_at, item) + _update_message(stylized_str, item.msg, item, mark_pattern) + _update_inputs(stylized_str, inputs, item, mark_pattern) + _update_implementation(stylized_str, implementation, item, mark_pattern) + + msg = StringIO() + msg.write(str(stylized_str)) + # Add the exception and the error msg. + if item.traceback and env.logging.verbosity_level >= logger.MEDIUM_VERBOSE: + msg.write(os.linesep) + msg.writelines(_get_traceback('\t' + '|' + line, item, mark_pattern) + for line in item.traceback.splitlines(True)) + + return msg.getvalue() def log(item, mark_pattern=None, *args, **kwargs): leveled_log = getattr(env.logging.logger, item.level.lower()) - return leveled_log(_stylize_log(item, mark_pattern), *args, **kwargs) + return leveled_log(stylize_log(item, mark_pattern), *args, **kwargs) def log_list(iterator, mark_pattern=None): @@ -235,3 +141,95 @@ def log_list(iterator, mark_pattern=None): log(item, mark_pattern) any_logs = True return any_logs + + +def _find_pattern(pattern, field_value): + # TODO: this finds the matching field type according to a pattern + match = re.match(pattern, field_value) + if match: + return match.group(1) + + +def _get_format(): + return env.config.logging.formats[env.logging.verbosity_level] + + +def _styles(field_type): + return env.config.logging.styles[field_type] + + +def _is_styling_enabled(): + return env.config.logging.styling_enabled + + +def _get_marker_schema(): + return Color.Schema(back=_styles(MARKER)) + + +def _update_implementation(str_, implementation, log_item, mark_pattern=None): + _stylize(str_, implementation, log_item, IMPLEMENTATION, mark_pattern) + + +def _update_inputs(str_, inputs, log_item, mark_pattern=None): + _stylize(str_, inputs, log_item, INPUTS, mark_pattern) + + +def _update_timestamp(str_, timestamp, log_item): + _stylize(str_, timestamp, log_item, TIMESTAMP) + + +def _update_message(str_, message, log_item, mark_pattern=None): + _stylize(str_, message, log_item, MESSAGE, mark_pattern) + + +def _update_level(str_, log_item): + _stylize(str_, log_item.level[0], log_item, LEVEL) + + +def _stylize(stylized_str, msg, log_item, msg_type, mark_pattern=None): + matched_str = _find_pattern(_PATTERNS[FIELD_TYPE][msg_type], stylized_str._str) + if not matched_str: + return stylized_str + + # if Styling was disabled + if not _is_styling_enabled(): + return stylized_str.replace(matched_str, matched_str.format(**{msg_type: msg})) + + # If the log is not of a final workflow message, it could be configured through the + # config file. Thus it should be instantiated from the dict provided. + schema = _resolve_schema(msg_type, log_item) + substring = Color.stylize(matched_str) + substring.color(schema or '') + substring.format(**{msg_type: msg}) + substring.highlight(msg, _get_marker_schema(), schema, mark_pattern) + stylized_str.replace(matched_str, substring) + + +def _get_traceback(traceback, log_item, mark_pattern): + schema = Color.Schema(**_styles(TRACEBACK).get(log_item.level.lower(), {})) + stylized_string = Color.stylize(traceback, schema) + stylized_string.highlight(traceback, _get_marker_schema(), schema, mark_pattern) + return stylized_string + + +def _end_execution_schema(log_item): + if log_item.task: + # This can't be an end workflow log + return + for state, pattern in _PATTERNS[FINAL_STATES].items(): + if re.match(pattern, log_item.msg): + return _FINAL_STATES[state] + + +def _resolve_schema(msg_type, log_item): + schema = _end_execution_schema(log_item) + + if schema is None: + typed_schema_config = _styles(msg_type).get(log_item.level.lower()) + user_default_schema_config = _styles(msg_type).get('default') + aria_schema_config = _DEFAULT_STYLE[msg_type].get(log_item.level.lower()) + schema = Color.Schema(**(typed_schema_config or + user_default_schema_config or + aria_schema_config)) + + return schema
