-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import os | ||
from datapackage import Package | ||
from dataflows import PackageWrapper, DataStream, Flow | ||
from . import dump_to_path | ||
|
||
|
||
def cache(cache_path, *steps): | ||
cache_package_json_path = os.path.join(cache_path, 'datapackage.json') | ||
|
||
def processor(package: PackageWrapper): | ||
if os.path.exists(cache_package_json_path): | ||
print('using cache data from {}'.format(cache_path)) | ||
dp = Package(cache_package_json_path) | ||
res_iter = (resource.iter(keyed=True) for resource in dp.resources) | ||
else: | ||
print('loading fresh data, saving cache to: {}'.format(cache_path)) | ||
ds: DataStream = Flow(*steps + (dump_to_path(cache_path),))._chain()._process() | ||
dp = ds.dp | ||
res_iter = ds.res_iter | ||
for resource in dp.resources: | ||
package.pkg.add_resource(resource.descriptor) | ||
yield package.pkg | ||
yield from package | ||
yield from res_iter | ||
|
||
return processor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters