代码之家  ›  专栏  ›  技术社区  ›  Michael A

Python类中的字符串存储包括换行符

  •  0
  • Michael A  · 技术社区  · 6 年前

    我有一个输出类,如下所示(有点经过编辑):

    from colorclass import Color
    from colorclass import disable_all_colors, enable_all_colors, is_enabled
    from time import localtime, strftime
    from ApplicationFiles.lib.core.__version__ import __version__
    from enum import IntEnum
    
    
    class OutputHelper(object):
        def __init__(self, arguments, app):
    
            if arguments.nocolor:
                disable_all_colors()
    
            self.domain = "undefined"
            self.severity_print = arguments.severity
            self.verbose = arguments.verbose
            self.silent = arguments.silent
            self.seperator = "=============================================="
            self.modules_count = app.modules_count()
    
        # this is here to allow easier redirection of output to other
        # interfaces later on in the project
        def write(self, severity, message):
            self.terminal(severity, message)
    
        def terminal(self, severity, message):
            if severity == 0 and not self.verbose:
                return
    
            if severity < self.severity_print:
                return
    
            formatting = {
               0: Color('{autoblue}[VERBOSE]{/autoblue}')
            }
    
            leader = formatting.get(severity, '[#]')
    
            format_args = {
               'time': strftime("%H:%M:%S", localtime()),
               'domain': self.domain,
               'leader': leader,
               'message': message,
            }
    
            template = '[{time}] [{domain}] {leader} {message}'
            print(template.format(**format_args))
    
    
    class Level(IntEnum):
        VERBOSE = 0
    

    我正在从一个文件中读取一个域列表,并通过这个类将其传递给以下对象:

    for domain in arguments.domain_list:
        domains.append(domain)
    
    for domain in domains:
        output.domain = domain
        app.modules['example_module'].run(arguments, output)
    

    然后依次为:

    def run(arguments, output):
    
        output.write(Level.VERBOSE, 'TEST DOMAIN')
    

    1 modules loaded
    ==============================================
    [16:57:25] [testone.com
    ] [VERBOSE] TEST DOMAIN
    [16:57:25] [testtwo.com
    ] [VERBOSE] TEST DOMAIN
    [16:57:25] [testthree.com
    ] [VERBOSE] TEST DOMAIN
    

    其内容应为:

    1 modules loaded
    ==============================================
    [16:57:25] [testone.com] [VERBOSE] TEST DOMAIN
    [16:57:25] [testtwo.com] [VERBOSE] TEST DOMAIN
    [16:57:25] [testthree.com] [VERBOSE] TEST DOMAIN
    

    我知道我对我的对象犯了一个错误,或者我是如何在某处引用它们的——但是我很难看到它。我不明白什么?在我的TerminalOutput()函数中处理这个问题似乎很愚蠢,我宁愿在域存储时对它进行排序。

    def readable_file(parser, arg):
        if not os.path.exists(arg):
            parser.error("The file %s does not exist!" % arg)
        else:
            return open(arg, 'r')  # return an open file handle
    

    它在argparse类中调用,使用:

    domains.add_argument(
        '-dL', dest='domain_list', required=False,
        help='Specify a list of target domain names.',
        metavar="FILE",
        type=lambda x: CliFileHelper.readable_file(parser, x)
    )
    
    1 回复  |  直到 6 年前
        1
  •  4
  •   kabanus    6 年前

    您没有向我们展示如何阅读域,而是使用:

    for domain in domainFileDescriptor:
         domains.append(domain)
    
    #or
    domains=domainFileDescriptor.readlines()
    

    for domain in domainFileDescriptor:
         domains.append(domain.strip())
    

    提示是换行符的位置(在域之后)。注意:在这个上下文中,文件描述符和句柄是相同的。