From 648c4605e1d3b8f262c6543577f24e3951c859d9 Mon Sep 17 00:00:00 2001 From: AlbertEngstfeld Date: Tue, 18 Feb 2025 14:56:26 +0100 Subject: [PATCH] Save collection as Data Package --- unitpackage/collection.py | 49 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/unitpackage/collection.py b/unitpackage/collection.py index 209470fc..dfd862eb 100644 --- a/unitpackage/collection.py +++ b/unitpackage/collection.py @@ -264,6 +264,51 @@ def __getitem__(self, identifier): return self.Entry(self.package.get_resource(identifier)) + def save(self, package_name=None, outdir=None): + r""" + Save this collection as a frictionless Data Package (CSV and JSON) + to the output directory :param outdir: and :param package_name:. + + In the following example we first generate some sample datapackages containing individual entries + in the specified directory. From these we generate a single Data Package, containing resources + for all CSV in that folder. + + EXAMPLES:: + + >>> collection = Collection.create_example() + >>> collection.save_entries(outdir='./test/generated/saved_collection/entries') + >>> package = collection.package.to_dict() + >>> collection.save(outdir='./test/generated/saved_collection') + >>> import glob + >>> glob.glob('test/generated/saved_collection/datapackage.json') # doctest: +NORMALIZE_WHITESPACE + ['test/generated/saved_collection/datapackage.json'] + + """ + if not outdir: + outdir = "." + + if not package_name: + package_name = "datapackage" + + import os + + if not os.path.isdir(outdir): + os.makedirs(outdir) + + json_name = os.path.join(outdir, package_name + ".json") + + import copy + package = copy.deepcopy(self.package.to_dict()) + + for resource in package["resources"]: + del resource["MutableResource"] + + from unitpackage.local import write_metadata + + with open(json_name, mode="w", encoding="utf-8") as json: + write_metadata(json, package) + + def save_entries(self, outdir=None): r""" Save the entries of this collection as Data Packages (CSV and JSON) @@ -272,9 +317,9 @@ def save_entries(self, outdir=None): EXAMPLES:: >>> db = Collection.create_example() - >>> db.save_entries(outdir='./test/generated/saved_collection') + >>> db.save_entries(outdir='./test/generated/saved_entries') >>> import glob - >>> glob.glob('test/generated/saved_collection/**.json') # doctest: +NORMALIZE_WHITESPACE + >>> glob.glob('test/generated/saved_entries/**.json') # doctest: +NORMALIZE_WHITESPACE ['test... """