-
Notifications
You must be signed in to change notification settings - Fork 8
/
ckan_utils.py
143 lines (120 loc) · 5.01 KB
/
ckan_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import re
from datetime import time
from dateutil import parser, tz
from .helpers import title_to_name
def append_attribute_to_extra(package, dataset, attribute, serialize=False):
value = dataset.get(attribute)
if value:
if serialize:
value = json.dumps(value)
package['extras'].append({'key': attribute, 'value': value})
def map_dataset_to_package(catalog, dataset, owner_org, catalog_id=None,
demote_superThemes=True, demote_themes=True):
package = dict()
package['extras'] = []
# Obligatorios
package['id'] = catalog_id + '_' + \
dataset['identifier'] if catalog_id else dataset['identifier']
package['name'] = title_to_name(
catalog_id + '-' + dataset['title'] if catalog_id else dataset['title']
)
package['title'] = dataset['title']
package['private'] = False
package['notes'] = dataset['description']
package['author'] = dataset['publisher']['name']
package['owner_org'] = owner_org
append_attribute_to_extra(package, dataset, 'issued')
append_attribute_to_extra(package, dataset, 'accrualPeriodicity')
distributions = dataset['distribution']
package['resources'] = map_distributions_to_resources(
distributions, catalog_id)
super_themes = dataset['superTheme']
append_attribute_to_extra(package, dataset, 'superTheme', serialize=True)
if demote_superThemes:
package['groups'] = [
{'name': title_to_name(super_theme, decode=False)}
for super_theme in super_themes
]
# Recomendados y opcionales
package['url'] = dataset.get('landingPage')
package['author_email'] = dataset['publisher'].get('mbox')
append_attribute_to_extra(package, dataset, 'modified')
append_attribute_to_extra(package, dataset, 'temporal')
append_attribute_to_extra(package, dataset, 'source')
append_attribute_to_extra(package, dataset, 'language', serialize=True)
spatial = dataset.get('spatial')
if spatial:
serializable = type(spatial) is list
append_attribute_to_extra(package, dataset, 'spatial', serializable)
contact_point = dataset.get('contactPoint')
if contact_point:
package['maintainer'] = contact_point.get('fn')
package['maintainer_email'] = contact_point.get('hasEmail')
keywords = dataset.get('keyword')
if keywords:
package['tags'] = [{'name': keyword} for keyword in keywords]
# Move themes to keywords
themes = dataset.get('theme', [])
if themes and demote_themes:
package['tags'] = package.get('tags', [])
for theme in themes:
try:
label = catalog.get_theme(identifier=theme)['label']
except:
label = catalog.get_theme(label=theme)['label']
label = re.sub(r'[^\wá-úÁ-ÚñÑ .-]+', '', label, flags=re.UNICODE)
package['tags'].append({'name': label})
else:
package['groups'] = package.get('groups', []) + [
{'name': title_to_name(theme, decode=False)}
for theme in themes
]
return package
def convert_iso_string_to_utc(date_string):
date_time = parser.parse(date_string)
if date_time.time() == time(0):
return date_string
if date_time.tzinfo is not None:
utc_date_time = date_time.astimezone(tz.tzutc())
else:
utc_date_time = date_time
utc_date_time = utc_date_time.replace(tzinfo=None)
return utc_date_time.isoformat()
def map_distributions_to_resources(distributions, catalog_id=None):
resources = []
for distribution in distributions:
resource = dict()
# Obligatorios
resource['id'] = catalog_id + '_' + \
distribution['identifier'] if catalog_id else distribution[
'identifier']
resource['name'] = distribution['title']
resource['url'] = distribution['downloadURL']
resource['created'] = convert_iso_string_to_utc(distribution['issued'])
# Recomendados y opcionales
resource['description'] = distribution.get('description')
resource['format'] = distribution.get('format')
last_modified = distribution.get('modified')
if last_modified:
resource['last_modified'] = convert_iso_string_to_utc(
last_modified)
resource['mimetype'] = distribution.get('mediaType')
resource['size'] = distribution.get('byteSize')
resource['accessURL'] = distribution.get('accessURL')
fileName = distribution.get('fileName')
if fileName:
resource['fileName'] = fileName
dist_fields = distribution.get('field')
if dist_fields:
resource['attributesDescription'] = json.dumps(dist_fields)
resources.append(resource)
return resources
def map_theme_to_group(theme):
return {
"name": title_to_name(theme.get('id') or theme['label']),
"title": theme.get('label'),
"description": theme.get('description'),
}