def update(pid, **kwargs): """Updates a package (aka dataset)""" kw = ft.Objectify(kwargs, type='dataset') verbose = not kw.quiet ckan_kwargs = {k: v for k, v in kwargs.items() if k in api.CKAN_KEYS} ckan = CKAN(**ckan_kwargs) licenses = it.imap(itemgetter('id'), ckan.license_list()) groups = ckan.group_list() raw_tags = filter(None, kw.tags.split(',')) if kw.tags else [] tags = [{'state': 'active', 'name': t} for t in raw_tags] if kw.start: start = parse(str(kw.start)).strftime('%m/%d/%Y') else: date = None if kw.start and kw.end: date = '%s-%s' % (start, parse(str(kw.end)).strftime('%m/%d/%Y')) elif kw.start: date = start if kw.location and kw.location in set(groups): group_list = [{'name': kw.location}] elif kw.location: sys.exit('group name: %s not found!' % kw.location) else: group_list = [] if kw.license_id and kw.license_id not in set(licenses): sys.exit('license id: %s not found!' % kw.license_id) package_kwargs = { 'title': kw.title, 'name': kw.name, 'license_id': kw.license_id, 'dataset_source': kw.source, 'notes': kw.description or kw.title, 'type': kw.type, 'tags': tags, 'groups': group_list, 'dataset_date': date, 'caveats': kw.caveats, 'methodology': methods.get(kw.methodology, 'Other'), 'methodology_other': methods.get(kw.methodology) or kw.methodology, } try: old_package = ckan.package_show(id=pid) except api.ValidationError as e: exit(e) if any(package_kwargs.values()): # combine keys by returning the last non-empty result pred = lambda key: True last = lambda pair: filter(None, pair)[-1] if any(pair) else None records = [old_package, package_kwargs] new_kwargs = pr.merge(records, pred=pred, op=last) if verbose: print('Submitting your package request.') pprint(new_kwargs) print('\n') package = ckan.package_update(**new_kwargs) else: package = old_package if kw.private: org = package['organization'] ckan.package_privatize(org_id=org['id'], datasets=[package['id']]) print(package['id']) print('\n')
def create(org_id, **kwargs): """Creates a package (aka dataset)""" kw = ft.Objectify(kwargs, type='dataset') verbose = not kw.quiet ckan_kwargs = {k: v for k, v in kwargs.items() if k in api.CKAN_KEYS} ckan = CKAN(**ckan_kwargs) licenses = it.imap(itemgetter('id'), ckan.license_list()) orgs = ckan.organization_list() org_ids = it.imap(itemgetter('id'), orgs) org_names = it.imap(itemgetter('name'), orgs) groups = ckan.group_list() name = kw.name or slugify(kw.title) raw_tags = filter(None, kw.tags.split(',')) tags = [{'state': 'active', 'name': t} for t in raw_tags] if kw.start: start = parse(str(kw.start)).strftime('%m/%d/%Y') else: date = None if kw.start and kw.end: date = '%s-%s' % (start, parse(str(kw.end)).strftime('%m/%d/%Y')) elif kw.start: date = start if kw.location in set(groups): group_list = [{'name': kw.location}] elif kw.location: sys.exit('group name: %s not found!' % kw.location) else: group_list = [] if org_id not in set(it.chain(org_ids, org_names)): sys.exit('organization id: %s not found!' % org_id) if kw.license_id not in set(licenses): sys.exit('license id: %s not found!' % kw.license_id) files = filter(None, kw.files.split(',')) names = filter(None, kw.names.split(',')) resource_list = list(it.starmap(make_rkwargs, zip(files, names))) or [] package_kwargs = { 'title': kw.title, 'name': name, 'license_id': kw.license_id, 'owner_org': org_id, 'dataset_source': kw.source, 'notes': kw.description or kw.title, 'type': kw.type, 'tags': tags, 'resources': resource_list, 'package_creator': ckan.user['name'], 'groups': group_list, 'dataset_date': date, 'caveats': kw.caveats, 'methodology': methods.get(kw.methodology, 'Other'), 'methodology_other': methods.get(kw.methodology) or kw.methodology, } if verbose: print('Submitting your package request.') pprint(package_kwargs) print('\n') try: package = ckan.package_create(**package_kwargs) except api.ValidationError as e: exit(e) if kw.private: org = package['organization'] ckan.package_privatize(org_id=org['id'], datasets=[package['id']]) if verbose: print('Your package response.') pprint(package) print('\n') print(package['id']) print('\n')