Skip to content
This repository has been archived by the owner on May 26, 2021. It is now read-only.

Commit

Permalink
Add sources to datapackage after generate
Browse files Browse the repository at this point in the history
  • Loading branch information
georgiana-b committed Jul 13, 2016
1 parent cc19338 commit dc27a10
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 101 deletions.
1 change: 1 addition & 0 deletions data_quality/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def generate(generator_name, endpoint, config_file_path, generator_class_path, f

generator = tasks.GeneratorManager(config)
generator.run(generator_name, endpoint, generator_class_path, file_types)
generator.update_datapackage_sources()


@cli.command()
Expand Down
21 changes: 20 additions & 1 deletion data_quality/tasks/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def __init__(self, config):

def run(self, generator_name, endpoint, generator_path, file_types, simulate=False):
"""Delegate the generation processes to the chosen generator
Args:
generator_name: Name of the generator (ex: ckan)
endpoint: Url where the generator should get the data from
Expand Down Expand Up @@ -59,3 +58,23 @@ def run(self, generator_name, endpoint, generator_path, file_types, simulate=Fal

generator.generate_publishers(self.publisher_file)
generator.generate_sources(self.source_file, file_types=file_types)

def update_datapackage_sources(self):
"""Update the 'sources' property of datapackage with the new sources"""

datapackage_check = DataPackageChecker(self.config)
required_resources = [self.source_file, self.publisher_file]
datapackage_check.check_database_completeness(required_resources)
datapackage_check.run()
self.datapackage.metadata['sources'] = []
datapkg_path = os.path.join(self.datapackage.base_path, 'datapackage.json')

with compat.UnicodeDictReader(self.source_file) as sources_file:
for source in sources_file:
src_info = {'name': source['title'], 'web': source[self.data_key]}
self.datapackage.metadata['sources'].append(src_info)

with io.open(datapkg_path, mode='w+', encoding='utf-8') as datapkg_file:
new_datapkg = json.dumps(self.datapackage.to_dict(), indent=4,
sort_keys=True)
datapkg_file.write(compat.str(new_datapkg))

0 comments on commit dc27a10

Please sign in to comment.