Skip to content

Commit bfe5731

Browse files
authored
Merge pull request #947 from juanjemdIos/master
clean code with constants in contributors
2 parents c82bc1f + 1da7cf6 commit bfe5731

3 files changed

Lines changed: 39 additions & 30 deletions

File tree

src/somef/export/json_export.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ def format_date(date_string):
153153
year = repo_data[constants.CAT_COPYRIGHT][0][constants.PROP_RESULT].get(constants.PROP_YEAR)
154154
if holder:
155155
codemeta_output[constants.CAT_CODEMETA_COPYRIGHTHOLDER] = holder
156+
156157
if year:
157158
codemeta_output[constants.CAT_CODEMETA_COPYRIGHTYEAR] = year
158159
if constants.CAT_DOWNLOAD_URL in repo_data:
@@ -162,7 +163,6 @@ def format_date(date_string):
162163
if constants.CAT_LOGO in repo_data:
163164
codemeta_output[constants.CAT_CODEMETA_LOGO] = repo_data[constants.CAT_LOGO][0][constants.PROP_RESULT][constants.PROP_VALUE]
164165
if constants.CAT_KEYWORDS in repo_data:
165-
# codemeta_output[constants.CAT_CODEMETA_KEYWORDS] = repo_data[constants.CAT_KEYWORDS][0][constants.PROP_RESULT][constants.PROP_VALUE]
166166
codemeta_output[constants.CAT_CODEMETA_KEYWORDS] = []
167167
for key in repo_data[constants.CAT_KEYWORDS]:
168168
key_value = key[constants.PROP_RESULT][constants.PROP_VALUE]
@@ -703,25 +703,26 @@ def parse_contributors(raw):
703703
if name not in seen:
704704

705705
if re.search(constants.REGEXP_LTD_INC, name, re.IGNORECASE):
706-
type_contributor = "Organization"
706+
type_contributor = constants.TYPE_CONTRIBUTOR_ORGANIZATION
707707
else:
708-
type_contributor = "Person"
708+
type_contributor = constants.TYPE_CONTRIBUTOR_PERSON
709709

710710
contributor = {
711-
"@type": type_contributor,
712-
"name": name
711+
constants.PROP_CODEMETA_TYPE: type_contributor,
712+
constants.PROP_NAME: name
713713
}
714+
714715
if "given_name" in result:
715-
contributor["givenName"] = result["given_name"]
716+
contributor[constants.PROP_CODEMETA_GIVENAME] = result["given_name"]
716717

717718
if "last_name" in result:
718-
contributor["familyName"] = result["last_name"]
719+
contributor[constants.PROP_CODEMETA_FAMILYNAME] = result["last_name"]
719720

720721
if "email" in result:
721-
contributor["email"] = result["email"]
722+
contributor[constants.PROP_EMAIL] = result["email"]
722723

723724
if "identifier" in result:
724-
contributor["@id"] = result["identifier"]
725+
contributor[constants.PROP_CODEMETA_ID] = result["identifier"]
725726

726727
contributors.append(contributor)
727728
seen.add(name)
@@ -741,13 +742,13 @@ def parse_contributors(raw):
741742
continue
742743

743744
if re.search(constants.REGEXP_LTD_INC, line, re.IGNORECASE):
744-
type_contributor = "Organization"
745+
type_contributor = constants.TYPE_CONTRIBUTOR_ORGANIZATION
745746
else:
746-
type_contributor = "Person"
747+
type_contributor = constants.TYPE_CONTRIBUTOR_PERSON
747748

748749
contributors.append({
749-
"@type": type_contributor,
750-
"name": line
750+
constants.PROP_CODEMETA_TYPE: type_contributor,
751+
constants.PROP_NAME: line
751752
})
752753

753754
seen.add(line)

src/somef/parser/codemeta_parser.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,9 @@ def parse_contributors(contributors_data):
260260

261261
if isinstance(contributor, dict):
262262

263-
given = contributor.get("givenName")
264-
family = contributor.get("familyName")
265-
name = contributor.get("name")
263+
given = contributor.get(constants.PROP_CODEMETA_GIVENAME)
264+
family = contributor.get(constants.PROP_CODEMETA_FAMILYNAME)
265+
name = contributor.get(constants.PROP_NAME)
266266

267267
if given and family:
268268
full_name = f"{given} {family}"
@@ -272,39 +272,39 @@ def parse_contributors(contributors_data):
272272
continue
273273

274274
contributor_info = {
275-
"value": full_name,
276-
"name": full_name,
277-
"type": constants.AGENT
275+
constants.PROP_VALUE: full_name,
276+
constants.PROP_NAME: full_name,
277+
constants.PROP_TYPE: constants.AGENT
278278
}
279279

280280
if given:
281-
contributor_info["given_name"] = given
281+
contributor_info[constants.PROP_GIVEN_NAME] = given
282282

283283
if family:
284-
contributor_info["last_name"] = family
284+
contributor_info[constants.PROP_LAST_NAME] = family
285285

286286
if "email" in contributor:
287-
contributor_info["email"] = contributor["email"]
287+
contributor_info[constants.PROP_EMAIL] = contributor[constants.PROP_EMAIL]
288288

289-
affil = contributor.get("affiliation")
289+
affil = contributor.get(constants.PROP_AFFILIATION)
290290
if affil:
291-
if isinstance(affil, dict) and affil.get("name"):
292-
contributor_info["affiliation"] = affil["name"]
291+
if isinstance(affil, dict) and affil.get(constants.PROP_NAME):
292+
contributor_info[constants.PROP_AFFILIATION] = affil[constants.PROP_NAME]
293293
elif isinstance(affil, str):
294-
contributor_info["affiliation"] = affil
294+
contributor_info[constants.PROP_AFFILIATION] = affil
295295

296-
identifier = contributor.get("identifier") or contributor.get("@id")
296+
identifier = contributor.get(constants.PROP_IDENTIFIER) or contributor.get(constants.PROP_CODEMETA_ID)
297297
if identifier:
298-
contributor_info["identifier"] = identifier
298+
contributor_info[constants.PROP_IDENTIFIER] = identifier
299299

300300
contributors_list.append(contributor_info)
301301

302302
elif isinstance(contributor, str):
303303
name = contributor.strip()
304304
if name:
305305
contributors_list.append({
306-
"value": name,
307-
"type": constants.AGENT
306+
constants.PROP_VALUE: name,
307+
constants.PROP_TYPE: constants.AGENT
308308
})
309309

310310
return contributors_list

src/somef/utils/constants.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,10 @@
229229
PROP_DEPENDENCY_TYPE = "dependency_type"
230230
PROP_DEPENDENCY_RESOLVER = "dependency_resolver"
231231
PROP_EMAIL = "email"
232+
PROP_GIVEN_NAME = "given_name"
232233
PROP_HTML_URL = "html_url"
233234
PROP_IDENTIFIER = "identifier"
235+
PROP_LAST_NAME = "last_name"
234236
PROP_NAME = "name"
235237
PROP_ORIGINAL_HEADER = "original_header"
236238
PROP_PARENT_HEADER = "parent_header"
@@ -450,6 +452,12 @@ class RepositoryType(Enum):
450452
CAT_CODEMETA_SOFTWAREVERSION = "softwareVersion"
451453
CAT_CODEMETA_URL = "url"
452454

455+
PROP_CODEMETA_GIVENAME = "givenName"
456+
PROP_CODEMETA_FAMILYNAME = "familyName"
457+
PROP_CODEMETA_ID= "@id"
458+
PROP_CODEMETA_TYPE = "@type"
459+
TYPE_CONTRIBUTOR_ORGANIZATION = "Organization"
460+
TYPE_CONTRIBUTOR_PERSON = "Person"
453461

454462
# DOCKER labels maintainer
455463
# REGEXP_MAINTAINER_LABEL_OCI = r'^\s*LABEL\s+org\.opencontainers\.image\.authors\s*=\s*["\']?(.+?)["\']?\s*$'

0 commit comments

Comments
 (0)