From e42bca636e25e9c6db7e5d543831840f7a369d87 Mon Sep 17 00:00:00 2001 From: Robert Alonso <17463757+rgalonso@users.noreply.github.com> Date: Mon, 11 Nov 2024 17:52:38 +0000 Subject: [PATCH] refactor: add optional argument to TodoParser to set configuration Currently, this is just used to set identifiers without needing to modify the environment, but this could (should?) be extended to other options --- TodoParser.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/TodoParser.py b/TodoParser.py index 6101704..af4dc05 100644 --- a/TodoParser.py +++ b/TodoParser.py @@ -24,22 +24,30 @@ class TodoParser(object): ISSUE_URL_PATTERN = re.compile(r'(?<=Issue URL:\s).+', re.IGNORECASE) ISSUE_NUMBER_PATTERN = re.compile(r'/issues/(\d+)', re.IGNORECASE) - def __init__(self): + def __init__(self, options=dict()): # Determine if the issues should be escaped. self.should_escape = os.getenv('INPUT_ESCAPE', 'true') == 'true' - # Load any custom identifiers, otherwise use the default. + # Load any custom identifiers specified by the environment, + # falling back to any specified by the constructor argument, + # otherwise using the default. custom_identifiers = os.getenv('INPUT_IDENTIFIERS') self.identifiers = ['TODO'] self.identifiers_dict = None if custom_identifiers: try: custom_identifiers_dict = json.loads(custom_identifiers) + except json.JSONDecodeError: + print('Invalid identifiers dict, ignoring.') + else: + custom_identifiers_dict = options.get("identifiers", None) + if custom_identifiers_dict: + try: for identifier_dict in custom_identifiers_dict: if type(identifier_dict['name']) is not str or type(identifier_dict['labels']) is not list: raise TypeError self.identifiers = [identifier['name'] for identifier in custom_identifiers_dict] self.identifiers_dict = custom_identifiers_dict - except (json.JSONDecodeError, KeyError, TypeError): + except (KeyError, TypeError): print('Invalid identifiers dict, ignoring.') self.languages_dict = None