From 1da7cf6e87b2e24afbf9220d22ecd3a4d0e93695 Mon Sep 17 00:00:00 2001 From: Juanje Mendoza Date: Tue, 7 Apr 2026 09:39:19 +0200 Subject: [PATCH] clean code with constants in contributors --- src/somef/export/json_export.py | 27 ++++++++++++----------- src/somef/parser/codemeta_parser.py | 34 ++++++++++++++--------------- src/somef/utils/constants.py | 8 +++++++ 3 files changed, 39 insertions(+), 30 deletions(-) diff --git a/src/somef/export/json_export.py b/src/somef/export/json_export.py index 02477d80..be1d66b1 100644 --- a/src/somef/export/json_export.py +++ b/src/somef/export/json_export.py @@ -153,6 +153,7 @@ def format_date(date_string): year = repo_data[constants.CAT_COPYRIGHT][0][constants.PROP_RESULT].get(constants.PROP_YEAR) if holder: codemeta_output[constants.CAT_CODEMETA_COPYRIGHTHOLDER] = holder + if year: codemeta_output[constants.CAT_CODEMETA_COPYRIGHTYEAR] = year if constants.CAT_DOWNLOAD_URL in repo_data: @@ -162,7 +163,6 @@ def format_date(date_string): if constants.CAT_LOGO in repo_data: codemeta_output[constants.CAT_CODEMETA_LOGO] = repo_data[constants.CAT_LOGO][0][constants.PROP_RESULT][constants.PROP_VALUE] if constants.CAT_KEYWORDS in repo_data: - # codemeta_output[constants.CAT_CODEMETA_KEYWORDS] = repo_data[constants.CAT_KEYWORDS][0][constants.PROP_RESULT][constants.PROP_VALUE] codemeta_output[constants.CAT_CODEMETA_KEYWORDS] = [] for key in repo_data[constants.CAT_KEYWORDS]: key_value = key[constants.PROP_RESULT][constants.PROP_VALUE] @@ -703,25 +703,26 @@ def parse_contributors(raw): if name not in seen: if re.search(constants.REGEXP_LTD_INC, name, re.IGNORECASE): - type_contributor = "Organization" + type_contributor = constants.TYPE_CONTRIBUTOR_ORGANIZATION else: - type_contributor = "Person" + type_contributor = constants.TYPE_CONTRIBUTOR_PERSON contributor = { - "@type": type_contributor, - "name": name + constants.PROP_CODEMETA_TYPE: type_contributor, + constants.PROP_NAME: name } + if "given_name" in result: - contributor["givenName"] = result["given_name"] + contributor[constants.PROP_CODEMETA_GIVENAME] = result["given_name"] if "last_name" in result: - contributor["familyName"] = result["last_name"] + contributor[constants.PROP_CODEMETA_FAMILYNAME] = result["last_name"] if "email" in result: - contributor["email"] = result["email"] + contributor[constants.PROP_EMAIL] = result["email"] if "identifier" in result: - contributor["@id"] = result["identifier"] + contributor[constants.PROP_CODEMETA_ID] = result["identifier"] contributors.append(contributor) seen.add(name) @@ -741,13 +742,13 @@ def parse_contributors(raw): continue if re.search(constants.REGEXP_LTD_INC, line, re.IGNORECASE): - type_contributor = "Organization" + type_contributor = constants.TYPE_CONTRIBUTOR_ORGANIZATION else: - type_contributor = "Person" + type_contributor = constants.TYPE_CONTRIBUTOR_PERSON contributors.append({ - "@type": type_contributor, - "name": line + constants.PROP_CODEMETA_TYPE: type_contributor, + constants.PROP_NAME: line }) seen.add(line) diff --git a/src/somef/parser/codemeta_parser.py b/src/somef/parser/codemeta_parser.py index 86ebad36..a3f6028e 100644 --- a/src/somef/parser/codemeta_parser.py +++ b/src/somef/parser/codemeta_parser.py @@ -260,9 +260,9 @@ def parse_contributors(contributors_data): if isinstance(contributor, dict): - given = contributor.get("givenName") - family = contributor.get("familyName") - name = contributor.get("name") + given = contributor.get(constants.PROP_CODEMETA_GIVENAME) + family = contributor.get(constants.PROP_CODEMETA_FAMILYNAME) + name = contributor.get(constants.PROP_NAME) if given and family: full_name = f"{given} {family}" @@ -272,30 +272,30 @@ def parse_contributors(contributors_data): continue contributor_info = { - "value": full_name, - "name": full_name, - "type": constants.AGENT + constants.PROP_VALUE: full_name, + constants.PROP_NAME: full_name, + constants.PROP_TYPE: constants.AGENT } if given: - contributor_info["given_name"] = given + contributor_info[constants.PROP_GIVEN_NAME] = given if family: - contributor_info["last_name"] = family + contributor_info[constants.PROP_LAST_NAME] = family if "email" in contributor: - contributor_info["email"] = contributor["email"] + contributor_info[constants.PROP_EMAIL] = contributor[constants.PROP_EMAIL] - affil = contributor.get("affiliation") + affil = contributor.get(constants.PROP_AFFILIATION) if affil: - if isinstance(affil, dict) and affil.get("name"): - contributor_info["affiliation"] = affil["name"] + if isinstance(affil, dict) and affil.get(constants.PROP_NAME): + contributor_info[constants.PROP_AFFILIATION] = affil[constants.PROP_NAME] elif isinstance(affil, str): - contributor_info["affiliation"] = affil + contributor_info[constants.PROP_AFFILIATION] = affil - identifier = contributor.get("identifier") or contributor.get("@id") + identifier = contributor.get(constants.PROP_IDENTIFIER) or contributor.get(constants.PROP_CODEMETA_ID) if identifier: - contributor_info["identifier"] = identifier + contributor_info[constants.PROP_IDENTIFIER] = identifier contributors_list.append(contributor_info) @@ -303,8 +303,8 @@ def parse_contributors(contributors_data): name = contributor.strip() if name: contributors_list.append({ - "value": name, - "type": constants.AGENT + constants.PROP_VALUE: name, + constants.PROP_TYPE: constants.AGENT }) return contributors_list diff --git a/src/somef/utils/constants.py b/src/somef/utils/constants.py index abda6638..4d2c476e 100644 --- a/src/somef/utils/constants.py +++ b/src/somef/utils/constants.py @@ -229,8 +229,10 @@ PROP_DEPENDENCY_TYPE = "dependency_type" PROP_DEPENDENCY_RESOLVER = "dependency_resolver" PROP_EMAIL = "email" +PROP_GIVEN_NAME = "given_name" PROP_HTML_URL = "html_url" PROP_IDENTIFIER = "identifier" +PROP_LAST_NAME = "last_name" PROP_NAME = "name" PROP_ORIGINAL_HEADER = "original_header" PROP_PARENT_HEADER = "parent_header" @@ -450,6 +452,12 @@ class RepositoryType(Enum): CAT_CODEMETA_SOFTWAREVERSION = "softwareVersion" CAT_CODEMETA_URL = "url" +PROP_CODEMETA_GIVENAME = "givenName" +PROP_CODEMETA_FAMILYNAME = "familyName" +PROP_CODEMETA_ID= "@id" +PROP_CODEMETA_TYPE = "@type" +TYPE_CONTRIBUTOR_ORGANIZATION = "Organization" +TYPE_CONTRIBUTOR_PERSON = "Person" # DOCKER labels maintainer # REGEXP_MAINTAINER_LABEL_OCI = r'^\s*LABEL\s+org\.opencontainers\.image\.authors\s*=\s*["\']?(.+?)["\']?\s*$'