Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions unitpackage/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,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)
Expand All @@ -295,9 +340,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...

"""
Expand Down
Loading