From 3dc9bde70bf685a59fc45d9b02e445c0caf7bab3 Mon Sep 17 00:00:00 2001 From: Andrew Krasichkov Date: Sat, 29 Apr 2017 11:19:37 +0300 Subject: [PATCH] Minor code style improvements --- gixy/cli/argparser.py | 17 ++++++++--------- gixy/core/builtin_variables.py | 1 - gixy/core/config.py | 1 - gixy/core/context.py | 1 - gixy/core/issue.py | 1 - gixy/core/plugins_manager.py | 3 +-- gixy/core/sre_parse/sre_constants.py | 28 ++++++++++++++++------------ gixy/core/variable.py | 1 - gixy/directives/__init__.py | 1 - gixy/parser/nginx_parser.py | 2 -- gixy/parser/raw_parser.py | 2 -- 11 files changed, 25 insertions(+), 33 deletions(-) diff --git a/gixy/cli/argparser.py b/gixy/cli/argparser.py index 9107bc4..40998f4 100644 --- a/gixy/cli/argparser.py +++ b/gixy/cli/argparser.py @@ -5,7 +5,6 @@ from six.moves import StringIO from gixy.core.plugins_manager import PluginsManager - # used while parsing args to keep track of where they came from _COMMAND_LINE_SOURCE_KEY = 'command_line' _ENV_VAR_SOURCE_KEY = 'environment_variables' @@ -32,8 +31,8 @@ class GixyConfigParser(DefaultConfigFileParser): white_space = '\\s*' key = '(?P[^:=;#\s]+?)' - value = white_space+'[:=\s]'+white_space+'(?P.+?)' - comment = white_space+'(?P\\s[;#].*)?' + value = white_space + '[:=\s]' + white_space + '(?P.+?)' + comment = white_space + '(?P\\s[;#].*)?' key_only_match = re.match('^' + key + comment + '$', line) if key_only_match: @@ -41,7 +40,7 @@ class GixyConfigParser(DefaultConfigFileParser): items[key] = 'true' continue - key_value_match = re.match('^'+key+value+comment+'$', line) + key_value_match = re.match('^' + key + value + comment + '$', line) if key_value_match: key = key_value_match.group('key') value = key_value_match.group('value') @@ -54,7 +53,7 @@ class GixyConfigParser(DefaultConfigFileParser): continue raise ConfigFileParserException('Unexpected line %s in %s: %s' % (i, - getattr(stream, 'name', 'stream'), line)) + getattr(stream, 'name', 'stream'), line)) return items def serialize(self, items): @@ -95,7 +94,7 @@ class ArgsParser(ArgumentParser): for arg in action.option_strings: if arg in {'--config', '--write-config', '--version'}: continue - if any([arg.startswith(2*c) for c in self.prefix_chars]): + if any([arg.startswith(2 * c) for c in self.prefix_chars]): keys += [arg[2:], arg] # eg. for '--bla' return ['bla', '--bla'] return keys @@ -119,8 +118,8 @@ class ArgsParser(ArgumentParser): for action in self._actions: config_file_keys = self.get_possible_config_keys(action) if config_file_keys and not action.is_positional_arg and \ - already_on_command_line(existing_command_line_args, - action.option_strings): + already_on_command_line(existing_command_line_args, + action.option_strings): value = getattr(parsed_namespace, action.dest, None) if value is not None: if type(value) is bool: @@ -157,4 +156,4 @@ def create_parser(): args_for_setting_config_path=['-c', '--config'], args_for_writing_out_config_file=['--write-config'], add_config_file_help=False - ) \ No newline at end of file + ) diff --git a/gixy/core/builtin_variables.py b/gixy/core/builtin_variables.py index 1e98b54..5fbdd13 100644 --- a/gixy/core/builtin_variables.py +++ b/gixy/core/builtin_variables.py @@ -1,7 +1,6 @@ from gixy.core.regexp import Regexp from gixy.core.variable import Variable - BUILTIN_VARIABLES = { # http://nginx.org/en/docs/http/ngx_http_core_module.html#var_uri 'uri': '/[^\x20\t]*', diff --git a/gixy/core/config.py b/gixy/core/config.py index 8237ef4..ab217f8 100644 --- a/gixy/core/config.py +++ b/gixy/core/config.py @@ -9,7 +9,6 @@ class Config(object): output_format=None, output_file=None, allow_includes=True): - self.severity = severity self.output_format = output_format self.output_file = output_file diff --git a/gixy/core/context.py b/gixy/core/context.py index 0a938e8..fc447d2 100644 --- a/gixy/core/context.py +++ b/gixy/core/context.py @@ -3,7 +3,6 @@ import copy from gixy.core.utils import is_indexed_name - LOG = logging.getLogger(__name__) CONTEXTS = [] diff --git a/gixy/core/issue.py b/gixy/core/issue.py index fe86cfe..949cc2b 100644 --- a/gixy/core/issue.py +++ b/gixy/core/issue.py @@ -1,5 +1,4 @@ class Issue(object): - def __init__(self, plugin, summary=None, description=None, severity=None, reason=None, help_url=None, directives=None): self.plugin = plugin diff --git a/gixy/core/plugins_manager.py b/gixy/core/plugins_manager.py index 98c0eae..6af4d34 100644 --- a/gixy/core/plugins_manager.py +++ b/gixy/core/plugins_manager.py @@ -5,7 +5,6 @@ from gixy.plugins.plugin import Plugin class PluginsManager(object): - def __init__(self, config=None): self.imported = False self.config = config @@ -19,7 +18,7 @@ class PluginsManager(object): for plugin_file in files_list: if not plugin_file.endswith('.py') or plugin_file.startswith('_'): continue - __import__('gixy.plugins.'+os.path.splitext(plugin_file)[0], None, None, ['']) + __import__('gixy.plugins.' + os.path.splitext(plugin_file)[0], None, None, ['']) self.imported = True diff --git a/gixy/core/sre_parse/sre_constants.py b/gixy/core/sre_parse/sre_constants.py index 51f2e71..670deeb 100644 --- a/gixy/core/sre_parse/sre_constants.py +++ b/gixy/core/sre_parse/sre_constants.py @@ -21,14 +21,17 @@ try: from _sre import MAXREPEAT except ImportError: import _sre + MAXREPEAT = _sre.MAXREPEAT = 65535 + # SRE standard exception (access as sre.error) # should this really be here? class error(Exception): pass + # operators FAILURE = "failure" @@ -148,6 +151,7 @@ CHCODES = [ CATEGORY_UNI_NOT_LINEBREAK ] + def makedict(list): d = {} i = 0 @@ -156,6 +160,7 @@ def makedict(list): i = i + 1 return d + OPCODES = makedict(OPCODES) ATCODES = makedict(ATCODES) CHCODES = makedict(CHCODES) @@ -206,17 +211,16 @@ CH_UNICODE = { } # flags -SRE_FLAG_TEMPLATE = 1 # template mode (disable backtracking) -SRE_FLAG_IGNORECASE = 2 # case insensitive -SRE_FLAG_LOCALE = 4 # honour system locale -SRE_FLAG_MULTILINE = 8 # treat target as multiline string -SRE_FLAG_DOTALL = 16 # treat target as a single string -SRE_FLAG_UNICODE = 32 # use unicode locale -SRE_FLAG_VERBOSE = 64 # ignore whitespace and comments -SRE_FLAG_DEBUG = 128 # debugging +SRE_FLAG_TEMPLATE = 1 # template mode (disable backtracking) +SRE_FLAG_IGNORECASE = 2 # case insensitive +SRE_FLAG_LOCALE = 4 # honour system locale +SRE_FLAG_MULTILINE = 8 # treat target as multiline string +SRE_FLAG_DOTALL = 16 # treat target as a single string +SRE_FLAG_UNICODE = 32 # use unicode locale +SRE_FLAG_VERBOSE = 64 # ignore whitespace and comments +SRE_FLAG_DEBUG = 128 # debugging # flags for INFO primitive -SRE_INFO_PREFIX = 1 # has prefix -SRE_INFO_LITERAL = 2 # entire pattern is literal (given by prefix) -SRE_INFO_CHARSET = 4 # pattern starts with character from given set - +SRE_INFO_PREFIX = 1 # has prefix +SRE_INFO_LITERAL = 2 # entire pattern is literal (given by prefix) +SRE_INFO_CHARSET = 4 # pattern starts with character from given set diff --git a/gixy/core/variable.py b/gixy/core/variable.py index 274907c..2b94d3c 100644 --- a/gixy/core/variable.py +++ b/gixy/core/variable.py @@ -4,7 +4,6 @@ import logging from gixy.core.regexp import Regexp from gixy.core.context import get_context - LOG = logging.getLogger(__name__) # See ngx_http_script_compile in http/ngx_http_script.c EXTRACT_RE = re.compile(r'\$([1-9]|[a-z_][a-z0-9_]*|\{[a-z0-9_]+\})', re.IGNORECASE) diff --git a/gixy/directives/__init__.py b/gixy/directives/__init__.py index ae08050..f2a4c0e 100644 --- a/gixy/directives/__init__.py +++ b/gixy/directives/__init__.py @@ -1,7 +1,6 @@ import os from gixy.directives.directive import Directive - DIRECTIVES = {} diff --git a/gixy/parser/nginx_parser.py b/gixy/parser/nginx_parser.py index 7922384..7a30c26 100644 --- a/gixy/parser/nginx_parser.py +++ b/gixy/parser/nginx_parser.py @@ -1,4 +1,3 @@ -"""NginxParser is a member object of the NginxConfigurator class.""" import os import glob import logging @@ -13,7 +12,6 @@ LOG = logging.getLogger(__name__) class NginxParser(object): - def __init__(self, file_path, allow_includes=True): self.base_file_path = file_path self.cwd = os.path.dirname(file_path) diff --git a/gixy/parser/raw_parser.py b/gixy/parser/raw_parser.py index 99946fd..eb066e0 100644 --- a/gixy/parser/raw_parser.py +++ b/gixy/parser/raw_parser.py @@ -1,4 +1,3 @@ -"""Very low-level nginx config parser based on pyparsing.""" import re import logging from cached_property import cached_property @@ -7,7 +6,6 @@ from pyparsing import ( Literal, Suppress, White, Word, alphanums, Forward, Group, Optional, Combine, Keyword, OneOrMore, ZeroOrMore, Regex, QuotedString, nestedExpr) - LOG = logging.getLogger(__name__)