From 832c83247a598b66f39282ea5a84c96d30a510c9 Mon Sep 17 00:00:00 2001 From: Dennis Klein Date: Fri, 2 Sep 2022 11:42:37 +0200 Subject: [PATCH] build(metaUpdater): Deduplicate `_handle_person_list_file()` --- meta_update.py | 80 +++++++++++++++++++++++--------------------------- 1 file changed, 37 insertions(+), 43 deletions(-) diff --git a/meta_update.py b/meta_update.py index 1dce54d4..1ae55dde 100755 --- a/meta_update.py +++ b/meta_update.py @@ -11,6 +11,9 @@ from collections import OrderedDict class Manipulator(object): + def __init__(self, regex): + self.findregex = re.compile(regex) + def __str__(self): return self.__class__.__name__ @@ -27,6 +30,19 @@ class Manipulator(object): else: return False + def _handle_person_list_file(self, filename, cm_field_name, **kwargs): + fp = open(filename, 'r', encoding='utf8') + person_list = self.data.setdefault(cm_field_name, []) + for line in fp: + line = line.strip() + m = self.findregex.match(line) + if m is None: + raise RuntimeError("Could not analyze line %r" % line) + found_entry = self._find_person_entry(person_list, m.groupdict()) + entry = self.update_person_entry(found_entry, m.groupdict(), **kwargs) + if found_entry is None: + person_list.append(entry) + def save(self, filename, indent=2): with open(filename, 'w', encoding='utf8') as fp: json.dump(self.data, fp, indent=indent) @@ -34,11 +50,17 @@ class Manipulator(object): class CodeMetaManipulator(Manipulator): + def __init__(self): + super().__init__(r'^(?P[-\w\s]*[-\w]),\s*' + r'(?P[-\w\s]*[-\w])\s*' + r'(?:<(?P\S+@\S+)>)?\s*' + r'(\[(?P\S+)\])?$') + def load(self, filename='codemeta.json'): super().load(filename) @classmethod - def find_person_entry(cls, person_list, matchdict): + def _find_person_entry(cls, person_list, matchdict): # orcid is unique for entry in person_list: if cls._dict_entry_cmp(entry, matchdict, '@id', 'orcid'): @@ -52,7 +74,7 @@ class CodeMetaManipulator(Manipulator): return None @staticmethod - def update_person_entry(entry, matchdict): + def update_person_entry(entry, matchdict, **kwargs): if entry is None: entry = OrderedDict() entry['@type'] = 'Person' @@ -65,26 +87,9 @@ class CodeMetaManipulator(Manipulator): entry[field] = val return entry - def handle_person_list_file(self, filename, cm_field_name): - fp = open(filename, 'r', encoding='utf8') - findregex = re.compile(r'^(?P[-\w\s]*[-\w]),\s*' - r'(?P[-\w\s]*[-\w])\s*' - r'(?:<(?P\S+@\S+)>)?\s*' - r'(\[(?P\S+)\])?$') - person_list = self.data.setdefault(cm_field_name, []) - for line in fp: - line = line.strip() - m = findregex.match(line) - if m is None: - raise RuntimeError("Could not analyze line %r" % line) - found_entry = self.find_person_entry(person_list, m.groupdict()) - entry = self.update_person_entry(found_entry, m.groupdict()) - if found_entry is None: - person_list.append(entry) - def update_authors(self): - self.handle_person_list_file('AUTHORS', 'author') - self.handle_person_list_file('CONTRIBUTORS', 'contributor') + self._handle_person_list_file('AUTHORS', 'author') + self._handle_person_list_file('CONTRIBUTORS', 'contributor') def save(self, filename='codemeta.json'): super().save(filename) @@ -94,11 +99,16 @@ class CodeMetaManipulator(Manipulator): class ZenodoManipulator(Manipulator): + def __init__(self): + super().__init__(r'^(?P[-\w\s,]*[-\w])\s*' + r'(?:<(?P\S+@\S+)>)?\s*' + r'(\[https://orcid\.org/(?P\S+)\])?$') + def load(self, filename='.zenodo.json'): super().load(filename) @classmethod - def find_person_entry(cls, person_list, matchdict): + def _find_person_entry(cls, person_list, matchdict): # Match on orcid first for entry in person_list: if cls._dict_entry_cmp(entry, matchdict, 'orcid'): @@ -109,36 +119,20 @@ class ZenodoManipulator(Manipulator): return None @staticmethod - def update_person_entry(entry, matchdict, contributors = False): + def update_person_entry(entry, matchdict, **kwargs): if entry is None: entry = OrderedDict() - if contributors: - entry['type'] = 'Other' + if kwargs['contributor_type']: + entry['type'] = kwargs['contributor_type'] for field in ('name', 'orcid'): val = matchdict.get(field, None) if val is not None: entry[field] = val return entry - def handle_person_list_file(self, filename, cm_field_name, contributors = False): - fp = open(filename, 'r', encoding='utf8') - findregex = re.compile(r'^(?P[-\w\s,]*[-\w])\s*' - r'(?:<(?P\S+@\S+)>)?\s*' - r'(\[https://orcid\.org/(?P\S+)\])?$') - person_list = self.data.setdefault(cm_field_name, []) - for line in fp: - line = line.strip() - m = findregex.match(line) - if m is None: - raise RuntimeError("Could not analyze line %r" % line) - found_entry = self.find_person_entry(person_list, m.groupdict()) - entry = self.update_person_entry(found_entry, m.groupdict(), contributors) - if found_entry is None: - person_list.append(entry) - def update_authors(self): - self.handle_person_list_file('AUTHORS', 'creators') - self.handle_person_list_file('CONTRIBUTORS', 'contributors', True) + self._handle_person_list_file('AUTHORS', 'creators') + self._handle_person_list_file('CONTRIBUTORS', 'contributors', contributor_type='Other') def save(self, filename='.zenodo.json'): super().save(filename, 4)