mirror of
https://github.com/FairRootGroup/FairMQ.git
synced 2025-10-13 08:41:16 +00:00
build(metaUpdater): Simplify some code
* Use class attributes instead of inheritance * Remove unused arguments * Reformat for PEP-8
This commit is contained in:
parent
fc82f262dc
commit
4b31ea3332
|
@ -4,23 +4,29 @@
|
|||
# SPDX-License-Identifier: LGPL-3.0-or-later
|
||||
|
||||
|
||||
import argparse
|
||||
from argparse import ArgumentParser
|
||||
import json
|
||||
import re
|
||||
from collections import OrderedDict
|
||||
|
||||
|
||||
class Manipulator(object):
|
||||
def __init__(self, regex):
|
||||
self.findregex = re.compile(regex)
|
||||
|
||||
def __str__(self):
|
||||
return self.__class__.__name__
|
||||
|
||||
def load(self, filename):
|
||||
def load(self, filename=None):
|
||||
if filename is None:
|
||||
filename = self.default_filename
|
||||
with open(filename, 'rb') as fp:
|
||||
self.data = json.load(fp, object_pairs_hook=OrderedDict)
|
||||
|
||||
def save(self, filename=None, indent=2):
|
||||
if filename is None:
|
||||
filename = self.default_filename
|
||||
with open(filename, 'w', encoding='utf8') as fp:
|
||||
json.dump(self.data, fp, indent=indent)
|
||||
fp.write('\n')
|
||||
|
||||
@staticmethod
|
||||
def _dict_entry_cmp(dict1, dict2, field1, field2=None):
|
||||
if field2 is None:
|
||||
|
@ -30,35 +36,28 @@ class Manipulator(object):
|
|||
else:
|
||||
return False
|
||||
|
||||
def _handle_person_list_file(self, filename, cm_field_name, **kwargs):
|
||||
def _handle_person_list_file(self, filename, field_name, **kwargs):
|
||||
fp = open(filename, 'r', encoding='utf8')
|
||||
person_list = self.data.setdefault(cm_field_name, [])
|
||||
person_list = self.data.setdefault(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)
|
||||
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)
|
||||
fp.write('\n')
|
||||
|
||||
|
||||
class CodeMetaManipulator(Manipulator):
|
||||
def __init__(self):
|
||||
super().__init__(r'^(?P<familyName>[-\w\s]*[-\w]),\s*'
|
||||
default_filename = 'codemeta.json'
|
||||
findregex = re.compile(r'^(?P<familyName>[-\w\s]*[-\w]),\s*'
|
||||
r'(?P<givenName>[-\w\s]*[-\w])\s*'
|
||||
r'(?:<(?P<email>\S+@\S+)>)?\s*'
|
||||
r'(\[(?P<orcid>\S+)\])?$')
|
||||
|
||||
def load(self, filename='codemeta.json'):
|
||||
super().load(filename)
|
||||
|
||||
@classmethod
|
||||
def _find_person_entry(cls, person_list, matchdict):
|
||||
# orcid is unique
|
||||
|
@ -74,7 +73,7 @@ class CodeMetaManipulator(Manipulator):
|
|||
return None
|
||||
|
||||
@staticmethod
|
||||
def update_person_entry(entry, matchdict, **kwargs):
|
||||
def update_person_entry(entry, matchdict):
|
||||
if entry is None:
|
||||
entry = OrderedDict()
|
||||
entry['@type'] = 'Person'
|
||||
|
@ -91,22 +90,16 @@ class CodeMetaManipulator(Manipulator):
|
|||
self._handle_person_list_file('AUTHORS', 'author')
|
||||
self._handle_person_list_file('CONTRIBUTORS', 'contributor')
|
||||
|
||||
def save(self, filename='codemeta.json'):
|
||||
super().save(filename)
|
||||
|
||||
def version(self, new_version):
|
||||
self.data['softwareVersion'] = new_version
|
||||
|
||||
|
||||
class ZenodoManipulator(Manipulator):
|
||||
def __init__(self):
|
||||
super().__init__(r'^(?P<name>[-\w\s,]*[-\w])\s*'
|
||||
default_filename = '.zenodo.json'
|
||||
findregex = re.compile(r'^(?P<name>[-\w\s,]*[-\w])\s*'
|
||||
r'(?:<(?P<email>\S+@\S+)>)?\s*'
|
||||
r'(\[https://orcid\.org/(?P<orcid>\S+)\])?$')
|
||||
|
||||
def load(self, filename='.zenodo.json'):
|
||||
super().load(filename)
|
||||
|
||||
@classmethod
|
||||
def _find_person_entry(cls, person_list, matchdict):
|
||||
# Match on orcid first
|
||||
|
@ -132,9 +125,10 @@ class ZenodoManipulator(Manipulator):
|
|||
|
||||
def update_authors(self):
|
||||
self._handle_person_list_file('AUTHORS', 'creators')
|
||||
self._handle_person_list_file('CONTRIBUTORS', 'contributors', contributor_type='Other')
|
||||
self._handle_person_list_file('CONTRIBUTORS', 'contributors',
|
||||
contributor_type='Other')
|
||||
|
||||
def save(self, filename='.zenodo.json'):
|
||||
def save(self, filename=None):
|
||||
super().save(filename, 4)
|
||||
|
||||
def version(self, new_version):
|
||||
|
@ -142,7 +136,8 @@ class ZenodoManipulator(Manipulator):
|
|||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description='Update codemeta.json and .zenodo.json')
|
||||
parser = ArgumentParser(description='Update codemeta.json and '
|
||||
'.zenodo.json')
|
||||
parser.add_argument('--set-version', dest='newversion')
|
||||
args = parser.parse_args()
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user