Skip to content

Commit afddc40

Browse files
author
notactuallyfinn
committed
made documents flake8 compliant and added tests
1 parent 50b4a98 commit afddc40

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

src/hermes_toml/harvest.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,9 @@ def read_from_toml(cls, file, data):
9090

9191
# load the toml file as a dictionary
9292
try:
93-
if not isinstance(toml_data := toml.load(file), dict):
94-
return
95-
except Exception as exc:
96-
raise type(exc)(f"Something went wrong while reading the given file {file}") from exc
93+
toml_data = toml.load(file)
94+
except Exception:
95+
return
9796

9897
# harvest project table
9998
project_data = toml_data.get("project")
@@ -373,12 +372,11 @@ def handle_pypi_classifieres(cls, classifiers: str | list[str], data):
373372

374373
if not isinstance(classifiers, (str, list)):
375374
return
376-
if isinstance(classifiers, str):
375+
if isinstance(classifiers, str) and len(classifiers.split(" :: ")) > 1:
377376
classifiers = [classifiers]
378377
else:
379-
classifiers = [classifier for classifier in classifiers if isinstance(classifier, str)]
380-
if len(classifiers) == 0:
381-
return
378+
# remove non strings and to short classifiers
379+
classifiers = [clsf for clsf in classifiers if isinstance(clsf, str) and len(clsf.split(" :: ")) > 1]
382380

383381
# remove duplicates
384382
classifiers = list(set(classifiers))
@@ -390,15 +388,13 @@ def handle_pypi_classifieres(cls, classifiers: str | list[str], data):
390388
# iterate over all classifiers and put them into the correct buckets
391389
for classifier in classifiers:
392390
classifier = classifier.split(" :: ")
393-
if len(classifier) < 2:
394-
continue
395-
if (classifier[0] == "Operating System" and not (len(classifier) == 2 and classifier[1] == "Microsoft")):
391+
if classifier[0] == "Operating System" and not (len(classifier) == 2 and classifier[1] == "Microsoft"):
396392
temp = {"@type": "schema:SoftwareApplication", "schema:name": classifier[-1]}
397393
sorted_classifiers["schema:targetProduct"].append(temp)
398394
elif classifier[0] == "Intended Audience":
399395
temp = {"@type": "schema:Audience", "schema:name": classifier[-1]}
400396
sorted_classifiers["schema:audience"].append(temp)
401-
elif (classifier[0] == "License" and not (classifier[1] == "OSI Approved" and len(classifier) == 2)):
397+
elif classifier[0] == "License" and not (classifier[1] == "OSI Approved" and len(classifier) == 2):
402398
temp = {"@type": "schema:CreativeWork", "schema:name": classifier[-1]}
403399
sorted_classifiers["schema:license"].append(temp)
404400
elif classifier[0] == "Natural Language":
@@ -421,10 +417,9 @@ def handle_pypi_classifieres(cls, classifiers: str | list[str], data):
421417

422418
# add everything to the SoftwareMetadata object
423419
for key, value in sorted_classifiers.items():
424-
if len(value) > 1:
425-
data[key] = value
426-
elif len(value) == 1:
427-
data[key] = value[0]
420+
if len(value) == 0:
421+
continue
422+
data[key] = value if len(value) > 1 else value[0]
428423

429424
@classmethod
430425
def handle_urls(cls, urls: dict[str, str], data):

test/hermes_toml_test/test_harvest.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# SPDX-FileContributor: Michael Meinel
66
# SPDX-FileContributor: Michael Fritzsche
77

8+
import os
89
import pytest
910
from pytest_unordered import unordered
1011
import toml
@@ -279,6 +280,7 @@ def toml_file(tmp_path_factory):
279280

280281

281282
@pytest.mark.parametrize("in_data, out_data", [
283+
("", {}),
282284
({"project": {"name": "a", "version": "x.x.x", "description": "abc", "keywords": ["a", "b", "c"],
283285
"authors": [{"name": "ab", "email": "ab@ab.ab"}, {"name": "a", "email": "a@a.a"}],
284286
"maintainers": [{"name": "ab", "email": "ab@ab.ab"}, {"name": "a", "email": "a@a.a"}],
@@ -328,12 +330,32 @@ def test_read_from_toml_success(in_data, out_data, toml_file):
328330
assert data == out_data
329331

330332

333+
@pytest.fixture(scope="session")
334+
def txt_file(tmp_path_factory):
335+
fn = tmp_path_factory.mktemp("data") / "test.txt"
336+
return fn
337+
338+
339+
def test_read_from_toml_error(txt_file):
340+
pre = os.path.splitext(txt_file)[0]
341+
toml_file = pre + ".toml"
342+
with open(txt_file, "w") as temp:
343+
temp.write("a")
344+
# try non toml file
345+
TomlHarvestPlugin.read_from_toml(txt_file, data := {})
346+
assert data == {}
347+
# try non existing file
348+
TomlHarvestPlugin.read_from_toml(toml_file, data := {})
349+
assert data == {}
350+
os.rename(txt_file, toml_file)
351+
# try malformed file
352+
TomlHarvestPlugin.read_from_toml(toml_file, data := {})
353+
assert data == {}
354+
355+
331356
"""
332-
# create different types of invalid files and save them somehow then run read_from_toml and validate the raised errors
333-
@pytest.mark.parametrize("in_data", [
334-
])
335-
def test_read_from_toml_error(in_data, out_data, toml_file):
357+
test files with multiple tables e.g. project and poetry or with different data in fileds with the same meaning
336358
337-
# test files with multiple tables e.g. project and poetry or with different data in fileds with the same meaning
338-
# not tested because it is unknown how these cases should be treated
359+
not tested because it is unknown how these cases should be treated
360+
as the SoftwareMetadata object is not yet implemented nor its behaivior documented
339361
"""

0 commit comments

Comments
 (0)