当前位置: 首页>>代码示例>>Python>>正文


Python Filter.print_stack方法代码示例

本文整理汇总了Python中filter.Filter.print_stack方法的典型用法代码示例。如果您正苦于以下问题:Python Filter.print_stack方法的具体用法?Python Filter.print_stack怎么用?Python Filter.print_stack使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在filter.Filter的用法示例。


在下文中一共展示了Filter.print_stack方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from filter import Filter [as 别名]
# 或者: from filter.Filter import print_stack [as 别名]
class Unlog:
    """Filter the output of a command or a log file according to pattern passed
    in the *args* argument or according to a config file.
    """

    def __init__(self, args):
        """    **PARAMETERS**

        * *args* - an ArgumentParser object containing all the option. Look at
          :py:mod:`unlog.main` for the list of opitons.
      """
        self._args = args
        self._check_args()
        if args.start_pattern:
            self._filter_from_args()
        else:
            self._filter_from_config()

    def _check_args(self):
        """Verify that the arguments are coherent. Exit with error code 2 if
        incoherences are fonud.
        """
        if not self._args.files and not self._args.start_pattern and not self._args.use_config_section:
            sys.stderr.write("You must give a file or a start pattern.\n")
            sys.exit(2)
        if (self._args.start_group_pattern and not self._args.end_group_pattern) or (
            not self._args.start_group_pattern and self._args.end_group_pattern
        ):
            sys.stderr.write("You must --start-group and --end-group.")
            sys.exit(2)

    def _filter_from_args(self):
        """Filter the files or stdin according to the patterns give by the
        arguments provided on the command line.
        """
        config = copy.copy(self._args.__dict__)
        # Must not be passed to filter (unuseful)
        del config["files"]
        # The following key are only used when processing from a config file
        del config["config_file"]
        del config["use_config_section"]
        # The filter manipulates string in the proper encoding. No need to pass it.
        del config["log_encoding"]
        self._output_filter = Filter(**config)
        # If no files are provided, read from stdin
        if self._args.files:
            self._files = self._args.files
            self.process_files()
        else:
            self.process_stdin()

    def process_files(self):
        """Loop on each file given on the command line and process them.
        """
        for file in self._files:
            self.process_file(file, log_encoding=self._args.log_encoding)

    def process_file(self, file_name, log_encoding="utf-8"):
        """Open file_name and process it with :py:meth:`unlog.filter.Filter.process_file`
        """
        try:
            with open(file_name, "r", encoding=log_encoding) as file:
                self._output_filter.process_file(file)
        except IOError as e:
            sys.stderr.write(str(e))
            sys.stderr.write("\n")

    def process_stdin(self):
        """Process each line on the stdin with
        :py:meth:`unlog.filter.Filter.process_line`
        """
        for line in iter(sys.stdin.readline, ""):
            self._output_filter.process_line(line)
        # We must print the stack when we reach the last line of stdin so that the
        # errors located at the end are displayed.
        self._output_filter.print_stack()
        self._output_filter.send_mail()

    def _filter_from_config(self):
        """Filter the files according to the patterns defined in the
        configuration file.
        """
        self._config = Config(self._args)
        if self._args.files:
            self.process_files_from_config()
        else:
            self._output_filter = self._config.get_filter()
            self.process_stdin()

    def process_files_from_config(self):
        """Loop over each file given on the command line and process them
        according to the actions defined in the associated config file. The file
        is then passed to :py:meth:`process_file_filter_from_config`.
        """
        for file_name in self._args.files:
            file_name = self._correct_path_input_file(file_name)
            self.process_file_filter_from_config(file_name)

    def _correct_path_input_file(self, file_name):
        """Expand the ~ variable and transform a relative path into an absolute
#.........这里部分代码省略.........
开发者ID:Jenselme,项目名称:unlog,代码行数:103,代码来源:unlog.py


注:本文中的filter.Filter.print_stack方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。