]> Frank Brehm's Git Trees - pixelpark/puppet-tools.git/commitdiff
Make the linter happy
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 15 Feb 2023 17:21:34 +0000 (18:21 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 15 Feb 2023 17:21:34 +0000 (18:21 +0100)
lib/dpx_puppettools/acceptparse.py
lib/dpx_puppettools/pfile_moduleinfo.py

index 229b4d52c325d4f781d44d9f13ff4686386d47d1..af15c98a0f1f34441472bb2574372d2449fc4f63 100644 (file)
@@ -58,23 +58,18 @@ def _item_qvalue_pair_to_header_element(pair):
 def _list_0_or_more__compiled_re(element_re):
     # RFC 7230 Section 7 "ABNF List Extension: #rule":
     # #element => [ ( "," / element ) *( OWS "," [ OWS element ] ) ]
-    return re.compile(
-        '^(?:$)|' +
-        '(?:' +
-        '(?:,|(?:' + element_re + '))' +
-        '(?:' + OWS_re + ',(?:' + OWS_re + element_re + ')?)*' +
-        ')$',
-    )
+    pat = '^(?:$)|(?:(?:,|(?:' + element_re + '))(?:'
+    pat += OWS_re + ',(?:' + OWS_re + element_re + ')?)*)$'
+    return re.compile(pat)
 
 
 def _list_1_or_more__compiled_re(element_re):
     # RFC 7230 Section 7 "ABNF List Extension: #rule":
     # 1#element => *( "," OWS ) element *( OWS "," [ OWS element ] )
     # and RFC 7230 Errata ID: 4169
-    return re.compile(
-        '^(?:,' + OWS_re + ')*' + element_re +
-        '(?:' + OWS_re + ',(?:' + OWS_re + element_re + ')?)*$',
-    )
+    pat = '^(?:,' + OWS_re + ')*' + element_re
+    pat += '(?:' + OWS_re + ',(?:' + OWS_re + element_re + ')?)*$'
+    return re.compile(pat)
 
 
 class AcceptOffer(namedtuple('AcceptOffer', ['type', 'subtype', 'params'])):
@@ -142,27 +137,15 @@ class Accept(object):
     #                  / ( type "/" "*" )
     #                  / ( type "/" subtype )
     #                  ) *( OWS ";" OWS parameter )
-    media_range_re = (
-        '(' +
-        '(?:' + type_re + '/' + subtype_re + ')' +
-        # '*' is included through type_re and subtype_re, so this covers */*
-        # and type/*
-        ')' +
-        '(' +
-        '(?:' + OWS_re + ';' + OWS_re +
-        '(?![qQ]=)' +  # media type parameter cannot be named "q"
-        parameter_re + ')*' +
-        ')'
-    )
+    media_range_re = '((?:' + type_re + '/' + subtype_re + '))((?:'
+    media_range_re += OWS_re + ';' + OWS_re + '(?![qQ]=)' + parameter_re + ')*)'
     # accept-params  = weight *( accept-ext )
     # accept-ext = OWS ";" OWS token [ "=" ( token / quoted-string ) ]
-    accept_ext_re = (
-        OWS_re + ';' + OWS_re + token_re + '(?:' +
-        '=(?:' +
-        '(?:' + token_re + ')|(?:' + quoted_string_re + ')' +
-        ')' +
-        ')?'
-    )
+
+    accept_ext_re = OWS_re + ';' + OWS_re + token_re
+    accept_ext_re += '(?:' + '=(?:' + '(?:'
+    accept_ext_re += token_re + ')|(?:' + quoted_string_re + ')))?'
+
     accept_params_re = weight_re + '((?:' + accept_ext_re + ')*)'
 
     media_range_n_accept_params_re = media_range_re + '(?:' + \
@@ -178,20 +161,14 @@ class Accept(object):
     # For parsing repeated groups within the media type parameters and
     # extension parameters segments
     parameters_compiled_re = re.compile(
-        OWS_re + ';' + OWS_re + '(' + token_re + ')=(' + token_re + '|' +
-        quoted_string_re + ')',
-    )
-    accept_ext_compiled_re = re.compile(
-        OWS_re + ';' + OWS_re + '(' + token_re + ')' +
-        '(?:' +
-        '=(' +
-        '(?:' +
-        '(?:' + token_re + ')|(?:' + quoted_string_re + ')' +
-        ')' +
-        ')' +
-        ')?',
+        OWS_re + ';' + OWS_re + '(' + token_re + ')=(' + token_re + '|' + quoted_string_re + ')',
     )
 
+    pattern = OWS_re + ';' + OWS_re + '(' + token_re + ')'
+    pattern += '(?:=((?:(?:' + token_re + ')|(?:' + quoted_string_re
+    pattern += '))))?'
+    accept_ext_compiled_re = re.compile(pattern)
+
     # For parsing the media types in the `offers` argument to
     # .acceptable_offers(), we re-use the media range regex for media types.
     # This is not intended to be a validation of the offers; its main purpose
@@ -379,6 +356,7 @@ class Accept(object):
         # to do this in steps using multiple regexes.
         if cls.accept_compiled_re.match(value) is None:
             raise ValueError('Invalid value for an Accept header.')
+
         def generator(value):
             for match in (
                 cls.media_range_n_accept_params_compiled_re.finditer(value)
@@ -412,10 +390,7 @@ class Accept(object):
                         extension_params
                     ):
                         if token_value:
-                            if (
-                                token_value.startswith('"') and
-                                token_value.endswith('"')
-                            ):
+                            if (token_value.startswith('"') and token_value.endswith('"')):
                                 token_value = cls._process_quoted_string_token(
                                     token=token_value,
                                 )
@@ -780,9 +755,7 @@ class AcceptValidHeader(Accept):
         of the public APIs that uses this method.
         """
         # Match if comparisons are the same or either is a complete wildcard
-        if (mask.lower() == offer.lower() or
-                '*/*' in (mask, offer) or
-                '*' == offer):
+        if (mask.lower() == offer.lower() or '*/*' in (mask, offer) or '*' == offer):
             return True
 
         # Set mask type with wildcard subtype for malformed masks
@@ -901,10 +874,7 @@ class AcceptValidHeader(Accept):
                 # items in reverse order, so specificity 4, 3, 2, 1 correspond
                 # to 1, 2, 3, 4 in the list, respectively (so that higher
                 # specificity has higher precedence).
-                if (
-                    offer_type == range_type
-                    and offer_subtype == range_subtype
-                ):
+                if (offer_type == range_type and offer_subtype == range_subtype):
                     if range_media_type_params == ():
                         # If offer_media_type_params == () the offer and the
                         # range match exactly, with neither having media type
@@ -1803,6 +1773,7 @@ class AcceptCharset(object):
         # to use one regex to check the match, and another to get the groups.
         if cls.accept_charset_compiled_re.match(value) is None:
             raise ValueError('Invalid value for an Accept-Charset header.')
+
         def generator(value):
             for match in (cls.charset_n_weight_compiled_re.finditer(value)):
                 charset = match.group(1)
@@ -1970,7 +1941,7 @@ class AcceptCharsetValidHeader(AcceptCharset):
             'future.',
             DeprecationWarning,
         )
-        for m,q in sorted(
+        for m, q in sorted(
             self._parsed_nonzero,
             key=lambda i: i[1],
             reverse=True
@@ -2794,6 +2765,7 @@ class AcceptEncoding(object):
         # to use one regex to check the match, and another to get the groups.
         if cls.accept_encoding_compiled_re.match(value) is None:
             raise ValueError('Invalid value for an Accept-Encoding header.')
+
         def generator(value):
             for match in (cls.codings_n_weight_compiled_re.finditer(value)):
                 codings = match.group(1)
@@ -2978,7 +2950,7 @@ class AcceptEncodingValidHeader(AcceptEncoding):
             DeprecationWarning,
         )
 
-        for m,q in sorted(
+        for m, q in sorted(
             self._parsed_nonzero,
             key=lambda i: i[1],
             reverse=True
@@ -3841,6 +3813,7 @@ class AcceptLanguage(object):
         # to use one regex to check the match, and another to get the groups.
         if cls.accept_language_compiled_re.match(value) is None:
             raise ValueError('Invalid value for an Accept-Language header.')
+
         def generator(value):
             for match in (
                 cls.lang_range_n_weight_compiled_re.finditer(value)
@@ -4151,11 +4124,9 @@ class AcceptLanguageValidHeader(AcceptLanguage):
         """
         item = item.replace('_', '-').lower()
         mask = mask.lower()
-        return (mask == '*'
-            or item == mask
-            or item.split('-')[0] == mask
-            or item == mask.split('-')[0]
-        )
+        return (
+            mask == '*' or item == mask or                                  # noqa
+            item.split('-')[0] == mask or item == mask.split('-')[0])
 
     def basic_filtering(self, language_tags):
         """
@@ -4463,7 +4434,7 @@ class AcceptLanguageValidHeader(AcceptLanguage):
                     matched_by = mask
         return best_offer
 
-    def lookup(
+    def lookup(                                                             # noqa
         self, language_tags, default_range=None, default_tag=None,
         default=None,
     ):
@@ -4639,7 +4610,7 @@ class AcceptLanguageValidHeader(AcceptLanguage):
                 else:  # {non-* range};q=0
                     not_acceptable_ranges.append(range_.lower())
             elif not asterisk_q0_found and range_ == '*':  # *;q={not 0}
-                asterisk_non0_found = True
+                asterisk_non0_found = True                  # noqa
                 # if asterisk_q0_found, then it does not matter whether
                 # asterisk_non0_found
             else:  # {non-* range};q={not 0}
@@ -4677,8 +4648,7 @@ class AcceptLanguageValidHeader(AcceptLanguage):
                     break
                 # len(subtags) >= 2
                 if len(subtag_before_this) == 1 and (
-                    subtag_before_this.isdigit() or
-                    subtag_before_this.isalpha()
+                    subtag_before_this.isdigit() or subtag_before_this.isalpha()
                 ):  # if subtag_before_this is a single-letter or -digit subtag
                     subtags.pop(-1)  # pop twice instead of once
                 subtags.pop(-1)
index c0b51bd3878cd3fd8552001f557e12f46ae82d77..8a4a66ca0600ebce5b76a53f1c7304337f6cb27f 100644 (file)
@@ -21,7 +21,7 @@ from .xlate import XLATOR
 from .errors import BaseModuleInfoError
 from .base_moduleinfo import BaseModuleInfo
 
-__version__ = '0.4.1'
+__version__ = '0.4.2'
 
 LOG = logging.getLogger(__name__)
 
@@ -466,7 +466,7 @@ class PuppetfileModuleInfo(BaseModuleInfo):
 
     # -------------------------------------------------------------------------
     @classmethod
-    def init_from_puppetfile_line(cls, line, appname=None, verbose=0, base_dir=None):
+    def init_from_puppetfile_line(cls, line, appname=None, verbose=0, base_dir=None):   # noqa
 
         # Removing all newlines
         mline = cls.re_newline.sub(' ', line)