Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format Python code with Black #161

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions lib/nginxparser/nginxparser.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,21 +36,21 @@ 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

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()

Expand Down Expand Up @@ -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

Expand All @@ -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.

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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"):
Expand Down Expand Up @@ -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):
Expand Down