-
Notifications
You must be signed in to change notification settings - Fork 40
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
80 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,45 @@ | ||
from ..helpers.resource_matcher import ResourceMatcher | ||
|
||
|
||
def process_resource(rows, fields): | ||
fields = set(fields) | ||
for row in rows: | ||
row = dict( | ||
(k, v) | ||
for k, v in row.items() | ||
if k in fields | ||
) | ||
yield row | ||
|
||
|
||
def select_fields(fields, resources=None): | ||
|
||
def func(package): | ||
matcher = ResourceMatcher(resources, package.pkg) | ||
dp_resources = package.pkg.descriptor.get('resources', []) | ||
for resource in dp_resources: | ||
if matcher.match(resource['name']): | ||
dp_fields = resource['schema'].get('fields', []) | ||
dp_fields = dict( | ||
(f['name'], f) | ||
for f in dp_fields | ||
) | ||
non_existing = [f for f in fields if f not in dp_fields] | ||
|
||
assert len(non_existing) == 0, \ | ||
"Can't find following field(s): %s" % '; '.join(non_existing) | ||
|
||
new_fields = [ | ||
dp_fields.get(f) | ||
for f in fields | ||
] | ||
resource['schema']['fields'] = new_fields | ||
yield package.pkg | ||
|
||
for resource in package: | ||
if not matcher.match(resource.res.name): | ||
yield resource | ||
else: | ||
yield process_resource(resource, fields) | ||
|
||
return func |
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