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
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
||||||
|
|
||||||
|
|
||||||
import argparse
|
from argparse import ArgumentParser
|
||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
|
||||||
|
|
||||||
class Manipulator(object):
|
class Manipulator(object):
|
||||||
def __init__(self, regex):
|
|
||||||
self.findregex = re.compile(regex)
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.__class__.__name__
|
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:
|
with open(filename, 'rb') as fp:
|
||||||
self.data = json.load(fp, object_pairs_hook=OrderedDict)
|
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
|
@staticmethod
|
||||||
def _dict_entry_cmp(dict1, dict2, field1, field2=None):
|
def _dict_entry_cmp(dict1, dict2, field1, field2=None):
|
||||||
if field2 is None:
|
if field2 is None:
|
||||||
|
@ -30,34 +36,27 @@ class Manipulator(object):
|
||||||
else:
|
else:
|
||||||
return False
|
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')
|
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:
|
for line in fp:
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
m = self.findregex.match(line)
|
m = self.findregex.match(line)
|
||||||
if m is None:
|
if m is None:
|
||||||
raise RuntimeError("Could not analyze line %r" % line)
|
raise RuntimeError("Could not analyze line %r" % line)
|
||||||
found_entry = self._find_person_entry(person_list, m.groupdict())
|
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:
|
if found_entry is None:
|
||||||
person_list.append(entry)
|
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):
|
class CodeMetaManipulator(Manipulator):
|
||||||
def __init__(self):
|
default_filename = 'codemeta.json'
|
||||||
super().__init__(r'^(?P<familyName>[-\w\s]*[-\w]),\s*'
|
findregex = re.compile(r'^(?P<familyName>[-\w\s]*[-\w]),\s*'
|
||||||
r'(?P<givenName>[-\w\s]*[-\w])\s*'
|
r'(?P<givenName>[-\w\s]*[-\w])\s*'
|
||||||
r'(?:<(?P<email>\S+@\S+)>)?\s*'
|
r'(?:<(?P<email>\S+@\S+)>)?\s*'
|
||||||
r'(\[(?P<orcid>\S+)\])?$')
|
r'(\[(?P<orcid>\S+)\])?$')
|
||||||
|
|
||||||
def load(self, filename='codemeta.json'):
|
|
||||||
super().load(filename)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _find_person_entry(cls, person_list, matchdict):
|
def _find_person_entry(cls, person_list, matchdict):
|
||||||
|
@ -74,7 +73,7 @@ class CodeMetaManipulator(Manipulator):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def update_person_entry(entry, matchdict, **kwargs):
|
def update_person_entry(entry, matchdict):
|
||||||
if entry is None:
|
if entry is None:
|
||||||
entry = OrderedDict()
|
entry = OrderedDict()
|
||||||
entry['@type'] = 'Person'
|
entry['@type'] = 'Person'
|
||||||
|
@ -91,21 +90,15 @@ class CodeMetaManipulator(Manipulator):
|
||||||
self._handle_person_list_file('AUTHORS', 'author')
|
self._handle_person_list_file('AUTHORS', 'author')
|
||||||
self._handle_person_list_file('CONTRIBUTORS', 'contributor')
|
self._handle_person_list_file('CONTRIBUTORS', 'contributor')
|
||||||
|
|
||||||
def save(self, filename='codemeta.json'):
|
|
||||||
super().save(filename)
|
|
||||||
|
|
||||||
def version(self, new_version):
|
def version(self, new_version):
|
||||||
self.data['softwareVersion'] = new_version
|
self.data['softwareVersion'] = new_version
|
||||||
|
|
||||||
|
|
||||||
class ZenodoManipulator(Manipulator):
|
class ZenodoManipulator(Manipulator):
|
||||||
def __init__(self):
|
default_filename = '.zenodo.json'
|
||||||
super().__init__(r'^(?P<name>[-\w\s,]*[-\w])\s*'
|
findregex = re.compile(r'^(?P<name>[-\w\s,]*[-\w])\s*'
|
||||||
r'(?:<(?P<email>\S+@\S+)>)?\s*'
|
r'(?:<(?P<email>\S+@\S+)>)?\s*'
|
||||||
r'(\[https://orcid\.org/(?P<orcid>\S+)\])?$')
|
r'(\[https://orcid\.org/(?P<orcid>\S+)\])?$')
|
||||||
|
|
||||||
def load(self, filename='.zenodo.json'):
|
|
||||||
super().load(filename)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _find_person_entry(cls, person_list, matchdict):
|
def _find_person_entry(cls, person_list, matchdict):
|
||||||
|
@ -132,9 +125,10 @@ class ZenodoManipulator(Manipulator):
|
||||||
|
|
||||||
def update_authors(self):
|
def update_authors(self):
|
||||||
self._handle_person_list_file('AUTHORS', 'creators')
|
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)
|
super().save(filename, 4)
|
||||||
|
|
||||||
def version(self, new_version):
|
def version(self, new_version):
|
||||||
|
@ -142,7 +136,8 @@ class ZenodoManipulator(Manipulator):
|
||||||
|
|
||||||
|
|
||||||
def main():
|
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')
|
parser.add_argument('--set-version', dest='newversion')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user