Skip to content

Commit

Permalink
Merge pull request #81 from oemof/feature/sorted-metadata
Browse files Browse the repository at this point in the history
Deterministic metadata
  • Loading branch information
jnnr committed Jan 17, 2023
2 parents 9c41e03 + df03eb5 commit 005fdac
Show file tree
Hide file tree
Showing 8 changed files with 408 additions and 291 deletions.
22 changes: 15 additions & 7 deletions src/oemof/tabular/datapackage/building.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def infer_resources(directory="data/elements"):
os.makedirs("resources")

# create meta data resources
for f in os.listdir(directory):
for f in sorted(os.listdir(directory)):
r = Resource({"path": os.path.join(directory, f)})
r.infer()
r.save(os.path.join("resources", f.replace(".csv", ".json")))
Expand All @@ -42,7 +42,7 @@ def update_package_descriptor():
""" """
p = Package("datapackage.json")

for f in os.listdir("resources"):
for f in sorted(os.listdir("resources")):
path = os.path.join("resources", f)

r = Resource(path)
Expand All @@ -63,6 +63,7 @@ def infer_metadata(
keep_resources=False,
foreign_keys=None,
path=None,
metadata_filename="datapackage.json",
):
"""Add basic meta data for a datapackage
Expand All @@ -80,6 +81,8 @@ def infer_metadata(
strings with the name of the resources
path: string
Absolute path to root-folder of the datapackage
metadata_filename: basestring
Name of the inferred metadata string.
"""
foreign_keys = foreign_keys or config.FOREIGN_KEYS

Expand All @@ -103,7 +106,7 @@ def infer_metadata(
)
)
else:
for f in os.listdir("data/elements"):
for f in sorted(os.listdir("data/elements")):
r = Resource(
{"path": str(pathlib.PurePosixPath("data", "elements", f))}
)
Expand Down Expand Up @@ -139,6 +142,11 @@ def infer_metadata(
}
)

# sort foreign_key entries by alphabetically by fields
r.descriptor["schema"]["foreignKeys"].sort(
key=lambda x: x["fields"]
)

r.commit()
r.save(
pathlib.PurePosixPath("resources", f.replace(".csv", ".json"))
Expand All @@ -153,7 +161,7 @@ def infer_metadata(
)
)
else:
for f in os.listdir("data/sequences"):
for f in sorted(os.listdir("data/sequences")):
r = Resource(
{"path": str(pathlib.PurePosixPath("data", "sequences", f))}
)
Expand All @@ -171,7 +179,7 @@ def infer_metadata(
)
)
else:
for f in os.listdir("data/geometries"):
for f in sorted(os.listdir("data/geometries")):
r = Resource(
{"path": str(pathlib.PurePosixPath("data", "geometries", f))}
)
Expand All @@ -183,7 +191,7 @@ def infer_metadata(
p.add_resource(r.descriptor)

p.commit()
p.save("datapackage.json")
p.save(metadata_filename)

if not keep_resources:
shutil.rmtree("resources")
Expand All @@ -209,7 +217,7 @@ def package_from_resources(resource_path, output_path, clean=True):
p.descriptor["profile"] = "tabular-data-package"
p.commit()

for f in os.listdir(resource_path):
for f in sorted(os.listdir(resource_path)):
path = os.path.join(resource_path, f)

r = Resource(path)
Expand Down

0 comments on commit 005fdac

Please sign in to comment.