From 2e9caea9cdcb2ee54de2e092bce8745ced0c5d31 Mon Sep 17 00:00:00 2001 From: Fini Jastrow Date: Fri, 26 May 2023 12:09:44 +0200 Subject: [PATCH] name-parser: Simplify for CodeClimate [why] Function get_name_token has a Cognitive Complexity of 12 (exceeds 9 allowed). Consider refactoring. [how] Remove not really needed special case. Signed-off-by: Fini Jastrow --- bin/scripts/name_parser/FontnameTools.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/bin/scripts/name_parser/FontnameTools.py b/bin/scripts/name_parser/FontnameTools.py index d5ce1bd40..3836627f7 100644 --- a/bin/scripts/name_parser/FontnameTools.py +++ b/bin/scripts/name_parser/FontnameTools.py @@ -135,7 +135,7 @@ class FontnameTools: return (weights, styles) @staticmethod - def get_name_token(name, tokens, allow_regex_token = False): + def get_name_token(name, tokens): """Try to find any case insensitive token from tokens in the name, return tuple with found token-list and rest""" # The default mode (allow_regex_token = False) will try to find any verbatim string in the # tokens list (case insensitive matching) and give that tokens list item back with @@ -150,10 +150,9 @@ class FontnameTools: all_tokens = [] j = 1 token_regex = '|'.join(tokens) - if not allow_regex_token: - # Allow a dash between CamelCase token word parts, i.e. Camel-Case - # This allows for styles like Extra-Bold - token_regex = re.sub(r'(?<=[a-z])(?=[A-Z])', '-?', token_regex) + # Allow a dash between CamelCase token word parts, i.e. Camel-Case + # This allows for styles like Extra-Bold + token_regex = re.sub(r'(?<=[a-z])(?=[A-Z])', '-?', token_regex) regex = re.compile('(.*?)(' + token_regex + ')(.*)', re.IGNORECASE) while j: j = regex.match(name) @@ -163,9 +162,7 @@ class FontnameTools: sys.exit('Malformed regex in FontnameTools.get_name_token()') not_matched += ' ' + j.groups()[0] # Blanc prevents unwanted concatenation of unmatched substrings tok = j.groups()[1].lower() - if not allow_regex_token: - # Remove dashes between CamelCase token words - tok = tok.replace('-', '') + tok = tok.replace('-', '') # Remove dashes between CamelCase token words if tok in lower_tokens: tok = tokens[lower_tokens.index(tok)] tok = FontnameTools.unify_style_names(tok) @@ -354,7 +351,7 @@ class FontnameTools: ( style, weight_token ) = FontnameTools.get_name_token(style, weights) ( style, style_token ) = FontnameTools.get_name_token(style, styles) - ( style, other_token ) = FontnameTools.get_name_token(style, other, True) + ( style, other_token ) = FontnameTools.get_name_token(style, other) while 'Regular' in style_token and len(style_token) > 1: # Correct situation where "Regular" and something else is given style_token.remove('Regular')