Skip to content

Commit fdeecfc

Browse files
Fixed issue #2 in harvest
1 parent 54422e6 commit fdeecfc

1 file changed

Lines changed: 43 additions & 48 deletions

File tree

src/hermes_toml/harvest.py

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ class TomlHarvestSettings(BaseModel):
1212

1313
class TomlHarvestPlugin(HermesHarvestPlugin):
1414
settings_class = TomlHarvestSettings
15+
table_with_mapping = {
16+
"project": [
17+
("name", "name"), ("version", "version"), ("description", "description"),
18+
("runtimePlatform", "requires-python"), ("author", "authors"),
19+
("maintainer", "maintainers"), ("keywords", "keywords")
20+
],
21+
"tool.poetry": [
22+
("name", "name"), ("version", "version"), ("description", "description"),
23+
("author", "authors"), ("maintainer", "maintainers"), ("url", "homepage"),
24+
("codeRepository", "repository"), ("keywords", "keywords")
25+
]
26+
}
27+
allowed_keys_for_person = ["givenName", "lastName", "email", "@id", "@type"]
1528

1629
def __call__(self, command: HermesHarvestCommand):
1730
path = command.args.path
@@ -21,29 +34,33 @@ def __call__(self, command: HermesHarvestCommand):
2134

2235
data = self.read_from_toml(command.settings.toml.filename)
2336

24-
return data, {}
37+
if path != old_path:
38+
os.chdir(old_path)
39+
40+
return data, {"filename": command.settings.toml.filename}
2541

26-
def read_from_toml(self, file):
42+
@classmethod
43+
def read_from_toml(cls, file):
2744
data = toml.load(file)
2845
ret_data = {}
29-
field_to_property_mapping_in_project = [
30-
("name", "name"), ("version", "version"), ("description", "description"),
31-
("runtimePlatform", "requires-python"), ("author", "authors"),
32-
("maintainer", "maintainers"), ("keywords", "keywords")
33-
]
34-
field_to_property_mapping_in_poetry = [
35-
("name", "name"), ("version", "version"), ("description", "description"),
36-
("author", "authors"), ("maintainer", "maintainers"), ("url", "homepage"),
37-
("codeRepository", "repository"), ("keywords", "keywords")
38-
]
39-
project = data.get("project")
40-
if not project is None:
41-
for (field1, field2) in field_to_property_mapping_in_project:
42-
if not project.get(field2) is None:
46+
for table, mapping in cls.table_with_mapping.items():
47+
table = data.get(table)
48+
if not table is None:
49+
if len(ret_data.keys()) != 0:
50+
raise ValueError("Both project and tool.poetry table exist.")
51+
ret_data = cls.read_from_one_table(table, mapping)
52+
53+
return ret_data
54+
55+
@classmethod
56+
def read_from_one_table(cls, table, mapping):
57+
ret_data = {}
58+
for (field1, field2) in mapping:
59+
if not table.get(field2) is None:
4360
if field2 == "requires-python":
44-
ret_data[field1] = "Python " + project[field2]
61+
ret_data[field1] = "Python " + table[field2]
4562
elif field1 in ["author", "maintainer"]:
46-
temp = self.handle_person_in_unknown_format(project[field2])
63+
temp = cls.handle_person_in_unknown_format(table[field2])
4764
if isinstance(temp, dict) and len(temp.keys()) > 0:
4865
temp["@type"] = "Person"
4966
ret_data[field1] = temp
@@ -56,51 +73,29 @@ def read_from_toml(self, file):
5673
temp[0]["@type"] = "Person"
5774
ret_data[field1] = temp[0]
5875
else:
59-
ret_data[field1] = project[field2]
60-
61-
poetry = data.get("tool.poetry")
62-
if not poetry is None:
63-
if not project is None:
64-
raise ValueError("Both project and tool.poetry table exist.")
65-
for (field1, field2) in field_to_property_mapping_in_poetry:
66-
if not poetry.get(field2) is None:
67-
if field1 in ["author", "maintainer"]:
68-
temp = self.handle_person_in_unknown_format(poetry[field2])
69-
if isinstance(temp, dict) and len(temp.keys()) > 0:
70-
temp["@type"] = "Person"
71-
ret_data[field1] = temp
72-
elif isinstance(temp, list):
73-
if len(temp) > 1:
74-
for person in temp:
75-
person["@type"] = "Person"
76-
ret_data[field1] = temp
77-
elif len(temp) == 1:
78-
temp[0]["@type"] = "Person"
79-
ret_data[field1] = temp[0]
80-
else:
81-
ret_data[field1] = poetry[field2]
82-
76+
ret_data[field1] = table[field2]
8377
return ret_data
8478

85-
def handle_person_in_unknown_format(self, persons):
79+
@classmethod
80+
def handle_person_in_unknown_format(cls, persons):
8681
if isinstance(persons, list):
8782
return_list = []
8883
for person in persons:
8984
if isinstance(person, dict):
90-
temp = self.remove_forbidden_keys(person)
85+
temp = cls.remove_forbidden_keys(person)
9186
if len(temp.keys()) > 0:
9287
return_list.append(temp)
9388
else:
9489
raise ValueError("A person must be a dict.")
9590
return return_list
9691
if isinstance(persons, dict):
97-
return self.remove_forbidden_keys(persons)
92+
return cls.remove_forbidden_keys(persons)
9893
raise ValueError("A person must be a dict.")
9994

100-
def remove_forbidden_keys(self, person):
101-
allowed_keys = ["givenName", "lastName", "email", "@id", "@type"]
95+
@classmethod
96+
def remove_forbidden_keys(cls, person):
10297
keys = list(person.keys())
10398
for key in keys:
104-
if not key in allowed_keys:
99+
if not key in cls.allowed_keys_for_person:
105100
del person[key]
106101
return person

0 commit comments

Comments
 (0)