From 2e27b055e4b9d4a304ffade6b52c9284ef8d4e09 Mon Sep 17 00:00:00 2001 From: Salamandar <6552989+Salamandar@users.noreply.github.com> Date: Wed, 18 Sep 2024 13:46:43 +0000 Subject: [PATCH] :art: Format Python code with Black --- lib/nginxparser/nginxparser.py | 46 ++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/lib/nginxparser/nginxparser.py b/lib/nginxparser/nginxparser.py index 77d6b6c..71854d1 100644 --- a/lib/nginxparser/nginxparser.py +++ b/lib/nginxparser/nginxparser.py @@ -1,11 +1,22 @@ """Very low-level nginx config parser based on pyparsing.""" + # Taken from https://github.com/certbot/certbot (Apache licensed) # Itself forked from https://github.com/fatiherikli/nginxparser (MIT Licensed) import copy import logging from pyparsing import ( - Literal, White, Forward, Group, Optional, OneOrMore, QuotedString, Regex, ZeroOrMore, Combine) + Literal, + White, + Forward, + Group, + Optional, + OneOrMore, + QuotedString, + Regex, + ZeroOrMore, + Combine, +) from pyparsing import stringEnd from pyparsing import restOfLine import six @@ -25,13 +36,13 @@ class RawNginxParser(object): left_bracket = Literal("{").suppress() right_bracket = space + Literal("}").suppress() semicolon = Literal(";").suppress() - dquoted = QuotedString('"', multiline=True, unquoteResults=False, escChar='\\') - squoted = QuotedString("'", multiline=True, unquoteResults=False, escChar='\\') + dquoted = QuotedString('"', multiline=True, unquoteResults=False, escChar="\\") + squoted = QuotedString("'", multiline=True, unquoteResults=False, escChar="\\") quoted = dquoted | squoted head_tokenchars = Regex(r"(\$\{)|[^{};\s'\"]") # if (last_space) tail_tokenchars = Regex(r"(\$\{)|[^{;\s]") # else tokenchars = Combine(head_tokenchars + ZeroOrMore(tail_tokenchars)) - paren_quote_extend = Combine(quoted + Literal(')') + ZeroOrMore(tail_tokenchars)) + paren_quote_extend = Combine(quoted + Literal(")") + ZeroOrMore(tail_tokenchars)) # note: ')' allows extension, but then we fall into else, not last_space. token = paren_quote_extend | tokenchars | quoted @@ -39,7 +50,7 @@ class RawNginxParser(object): whitespace_token_group = space + token + ZeroOrMore(required_space + token) + space assignment = whitespace_token_group + semicolon - comment = space + Literal('#') + restOfLine + comment = space + Literal("#") + restOfLine block = Forward() @@ -68,6 +79,7 @@ def as_list(self): class RawNginxDumper(object): # pylint: disable=too-few-public-methods """A class that dumps nginx configuration from the provided tree.""" + def __init__(self, blocks): self.blocks = blocks @@ -85,25 +97,28 @@ def __iter__(self, blocks=None): continue if isinstance(item[0], list): # block - yield "".join(item.pop(0)) + '{' + yield "".join(item.pop(0)) + "{" for parameter in item.pop(0): for line in self.__iter__([parameter]): # negate "for b0 in blocks" yield line - yield '}' + yield "}" else: # not a block - list of strings semicolon = ";" - if isinstance(item[0], six.string_types) and item[0].strip() == '#': # comment + if ( + isinstance(item[0], six.string_types) and item[0].strip() == "#" + ): # comment semicolon = "" yield "".join(item) + semicolon def __str__(self): """Return the parsed block as a string.""" - return ''.join(self) + return "".join(self) # Shortcut functions to respect Python's serialization interface # (like pyyaml, picker or json) + def loads(source): """Parses from a string. @@ -149,7 +164,8 @@ def dump(blocks, _file): return _file.write(dumps(blocks)) -def spacey(x): return (isinstance(x, six.string_types) and x.isspace()) or x == '' +def spacey(x): + return (isinstance(x, six.string_types) and x.isspace()) or x == "" class UnspacedList(list): @@ -182,7 +198,7 @@ def _coerce(self, inbound): :rtype: tuple """ - if not isinstance(inbound, list): # str or None + if not isinstance(inbound, list): # str or None return (inbound, inbound) else: if not hasattr(inbound, "spaced"): @@ -229,11 +245,15 @@ def sort(self, _cmp=None, _key=None, _Rev=None): raise NotImplementedError("UnspacedList.sort() not yet implemented") def __setslice__(self, _i, _j, _newslice): - raise NotImplementedError("Slice operations on UnspacedLists not yet implemented") + raise NotImplementedError( + "Slice operations on UnspacedLists not yet implemented" + ) def __setitem__(self, i, value): if isinstance(i, slice): - raise NotImplementedError("Slice operations on UnspacedLists not yet implemented") + raise NotImplementedError( + "Slice operations on UnspacedLists not yet implemented" + ) item, spaced_item = self._coerce(value) self.spaced.__setitem__(self._spaced_position(i), spaced_item) if not spacey(item):