##// END OF EJS Templates
v2.9.2 :: Add 'tags' in search option - dataset
eynilupu -
r22:3ecf3c51fc50
parent child
Show More
@@ -1,502 +1,514
1 from ckanapi import RemoteCKAN
1 from ckanapi import RemoteCKAN
2 from datetime import datetime
2 from datetime import datetime
3 from jrodb import download
3 from jrodb import download
4 from jrodb import resource
4 from jrodb import resource
5 #from ckanapi.errors import NotAuthorized, NotFound, ValidationError, SearchQueryError, SearchError, CKANAPIError, ServerIncompatibleError
5 #from ckanapi.errors import NotAuthorized, NotFound, ValidationError, SearchQueryError, SearchError, CKANAPIError, ServerIncompatibleError
6 import sys
6 import sys
7 import platform
7 import platform
8 import os
8 import os
9 import requests
9 import requests
10
10
11 class Api():
11 class Api():
12 """
12 """
13 FINALIDAD:
13 FINALIDAD:
14 Script para administrar y obtener la data del repositorio por medio de APIs.
14 Script para administrar y obtener la data del repositorio por medio de APIs.
15
15
16 REQUISITIOS PREVIOS:
16 REQUISITIOS PREVIOS:
17 - Paso 1: Tener "pip [Python 2]" o "pip3 [Python 3]" instalado:
17 - Paso 1: Tener "pip [Python 2]" o "pip3 [Python 3]" instalado:
18 - Paso 2: Instalar los siguientes paquetes:
18 - Paso 2: Instalar los siguientes paquetes:
19 En Python 2
19 En Python 2
20 - pip install -e git+http://intranet.igp.gob.pe:8082/DATABASES/ckanext-jro/api-cliente#egg=jrodb
20 - pip install -e git+http://intranet.igp.gob.pe:8082/DATABASES/ckanext-jro/api-cliente#egg=jrodb
21 En Python 3
21 En Python 3
22 - pip3 install -e git+http://intranet.igp.gob.pe:8082/DATABASES/ckanext-jro/api-cliente#egg=jrodb
22 - pip3 install -e git+http://intranet.igp.gob.pe:8082/DATABASES/ckanext-jro/api-cliente#egg=jrodb
23
23
24 FUNCIONES DISPONIBLES:
24 FUNCIONES DISPONIBLES:
25 - action
25 - action
26 - show
26 - show
27 - search
27 - search
28 - create
28 - create
29 - patch
29 - patch
30 - delete
30 - delete
31 - download
31 - download
32
32
33 EJEMPLOS:
33 EJEMPLOS:
34 #1:
34 #1:
35 with Api('http://demo.example.com', Authorization='#########') as <access_name>:
35 with Api('http://demo.example.com', Authorization='#########') as <access_name>:
36 ... some operation(s) ...
36 ... some operation(s) ...
37 #2:
37 #2:
38 <access_name> = Api('http://example.com', Authorization='#########')
38 <access_name> = Api('http://example.com', Authorization='#########')
39 ... some operation(s) ...
39 ... some operation(s) ...
40 <access_name>.ckan.close()
40 <access_name>.ckan.close()
41
41
42 REPORTAR ALGUN PROBLEMA:
42 REPORTAR ALGUN PROBLEMA:
43 Debe enviar un correo a eynilupu@igp.gob.pe detallando los siguientes pasos:
43 Debe enviar un correo a eynilupu@igp.gob.pe detallando los siguientes pasos:
44 1) Correo para contactarlo
44 1) Correo para contactarlo
45 2) Descripcion del problema
45 2) Descripcion del problema
46 3) ΒΏEn que paso o seccion encontro el problema?
46 3) ΒΏEn que paso o seccion encontro el problema?
47 4) ΒΏCual era el resultado que usted esperaba?
47 4) ΒΏCual era el resultado que usted esperaba?
48 """
48 """
49 def __init__(self, url, Authorization=None, secure=True):
49 def __init__(self, url, Authorization=None, secure=True):
50 #-------- Check Secure -------#
50 #-------- Check Secure -------#
51 self.verify = secure
51 self.verify = secure
52 if not secure and isinstance(secure, bool):
52 if not secure and isinstance(secure, bool):
53 session = requests.Session()
53 session = requests.Session()
54 session.verify = False
54 session.verify = False
55 else:
55 else:
56 session = None
56 session = None
57 #------------------------------#
57 #------------------------------#
58 self.url = url
58 self.url = url
59 ua = 'CKAN_JRO/2.9.2 (+'+str(self.url)+')'
59 ua = 'CKAN_JRO/2.9.2 (+'+str(self.url)+')'
60 #ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'
60 #ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'
61 self.ckan = RemoteCKAN(self.url, apikey=Authorization, user_agent=ua, session=session)
61 self.ckan = RemoteCKAN(self.url, apikey=Authorization, user_agent=ua, session=session)
62 #self.ckan = RemoteCKAN(self.url, apikey=Authorization)
62 #self.ckan = RemoteCKAN(self.url, apikey=Authorization)
63 self.Authorization = Authorization
63 self.Authorization = Authorization
64 # Change for --> self.separator = os.sep
64 # Change for --> self.separator = os.sep
65 if platform.system() == 'Windows':
65 if platform.system() == 'Windows':
66 self.separator = '\\'
66 self.separator = '\\'
67 else:
67 else:
68 self.separator = '/'
68 self.separator = '/'
69
69
70 self.chunk_size = 1024
70 self.chunk_size = 1024
71 self.list = []
71 self.list = []
72 self.dict = {}
72 self.dict = {}
73 self.str = ''
73 self.str = ''
74 self.check = 1
74 self.check = 1
75 self.cont = 0
75 self.cont = 0
76
76
77 def __enter__(self):
77 def __enter__(self):
78 return self
78 return self
79
79
80 def __exit__(self, *args):
80 def __exit__(self, *args):
81 self.ckan.close()
81 self.ckan.close()
82
82
83 def action(self, action, **kwargs):
83 def action(self, action, **kwargs):
84 """
84 """
85 FINALIDAD:
85 FINALIDAD:
86 Funcion para llamar a las APIs disponibles
86 Funcion para llamar a las APIs disponibles
87
87
88 APIs DISPONIBLES:
88 APIs DISPONIBLES:
89 CONSULTAR: "GUIA DE SCRIPT.pdf"
89 CONSULTAR: "GUIA DE SCRIPT.pdf"
90
90
91 EJEMPLO:
91 EJEMPLO:
92 <access_name>.action(<consuming API>, param_1 = <class 'param_1'>, ...)
92 <access_name>.action(<consuming API>, param_1 = <class 'param_1'>, ...)
93 """
93 """
94 #--------------- CASE: PACKAGE SEARCH ---------------#
94 #--------------- CASE: PACKAGE SEARCH ---------------#
95 if kwargs is not None:
95 if kwargs is not None:
96 if action == 'package_search':
96 if action == 'package_search':
97 self.list = ['facet_mincount', 'facet_limit', 'facet_field']
97 self.list = ['facet_mincount', 'facet_limit', 'facet_field']
98 for facet in self.list:
98 for facet in self.list:
99 if facet in kwargs:
99 if facet in kwargs:
100 kwargs[facet.replace('_', '.')] = kwargs[facet]
100 kwargs[facet.replace('_', '.')] = kwargs[facet]
101 kwargs.pop(facet)
101 kwargs.pop(facet)
102 #----------------------------------------------------#
102 #----------------------------------------------------#
103 try:
103 try:
104 return getattr(self.ckan.action, action)(**kwargs)
104 return getattr(self.ckan.action, action)(**kwargs)
105 except:
105 except:
106 _, exc_value, _ = sys.exc_info()
106 _, exc_value, _ = sys.exc_info()
107 return exc_value
107 return exc_value
108
108
109 def show(self, type_option, id, **kwargs):
109 def show(self, type_option, id, **kwargs):
110 '''
110 '''
111 FINALIDAD:
111 FINALIDAD:
112 Funcion personalizada para una busqueda en especifico.
112 Funcion personalizada para una busqueda en especifico.
113
113
114 PARAMETROS DISPONIBLES:
114 PARAMETROS DISPONIBLES:
115 CONSULTAR: "GUIA DE SCRIPT.pdf"
115 CONSULTAR: "GUIA DE SCRIPT.pdf"
116
116
117 ESTRUCTURA:
117 ESTRUCTURA:
118 <access_name>.show(type_option = <class 'str'>, id = <class 'str'>, param_1 = <class 'param_1'>, ...)
118 <access_name>.show(type_option = <class 'str'>, id = <class 'str'>, param_1 = <class 'param_1'>, ...)
119 '''
119 '''
120 if type(type_option) is str:
120 if type(type_option) is str:
121 try:
121 try:
122 if type_option == 'dataset':
122 if type_option == 'dataset':
123 return getattr(self.ckan.action, 'package_show')(id=id, **kwargs)
123 return getattr(self.ckan.action, 'package_show')(id=id, **kwargs)
124 elif type_option == 'resource':
124 elif type_option == 'resource':
125 return getattr(self.ckan.action, 'resource_show')(id=id, **kwargs)
125 return getattr(self.ckan.action, 'resource_show')(id=id, **kwargs)
126 elif type_option == 'project':
126 elif type_option == 'project':
127 return getattr(self.ckan.action, 'organization_show')(id=id, **kwargs)
127 return getattr(self.ckan.action, 'organization_show')(id=id, **kwargs)
128 elif type_option == 'collaborator':
128 elif type_option == 'collaborator':
129 return getattr(self.ckan.action, 'package_collaborator_list_for_user')(id=id, **kwargs)
129 return getattr(self.ckan.action, 'package_collaborator_list_for_user')(id=id, **kwargs)
130 elif type_option == 'member':
130 elif type_option == 'member':
131 return getattr(self.ckan.action, 'organization_list_for_user')(id=id, **kwargs)
131 return getattr(self.ckan.action, 'organization_list_for_user')(id=id, **kwargs)
132 elif type_option == 'vocabulary':
132 elif type_option == 'vocabulary':
133 return getattr(self.ckan.action, 'vocabulary_show')(id=id, **kwargs)
133 return getattr(self.ckan.action, 'vocabulary_show')(id=id, **kwargs)
134 elif type_option == 'tag':
134 elif type_option == 'tag':
135 if not 'vocabulary_id' in kwargs:
135 if not 'vocabulary_id' in kwargs:
136 print('Missing "vocabulary_id" value: assume it is a free tag')
136 print('Missing "vocabulary_id" value: assume it is a free tag')
137 return getattr(self.ckan.action, 'tag_show')(id=id, **kwargs)
137 return getattr(self.ckan.action, 'tag_show')(id=id, **kwargs)
138 elif type_option == 'user':
138 elif type_option == 'user':
139 return getattr(self.ckan.action, 'user_show')(id=id, **kwargs)
139 return getattr(self.ckan.action, 'user_show')(id=id, **kwargs)
140 elif type_option == 'job':
140 elif type_option == 'job':
141 return getattr(self.ckan.action, 'job_show')(id=id, **kwargs)
141 return getattr(self.ckan.action, 'job_show')(id=id, **kwargs)
142 else:
142 else:
143 return 'ERROR:: "type_option = %s" is not accepted' % (type_option)
143 return 'ERROR:: "type_option = %s" is not accepted' % (type_option)
144 except:
144 except:
145 _, exc_value, _ = sys.exc_info()
145 _, exc_value, _ = sys.exc_info()
146 return exc_value
146 return exc_value
147 else:
147 else:
148 return 'ERROR:: "type_option" must be a str'
148 return 'ERROR:: "type_option" must be a str'
149
149
150 def search(self, type_option, query=None, **kwargs):
150 def search(self, type_option, query=None, **kwargs):
151 '''
151 '''
152 FINALIDAD:
152 FINALIDAD:
153 Funcion personalizada para busquedas que satisfagan algun criterio.
153 Funcion personalizada para busquedas que satisfagan algun criterio.
154
154
155 PARAMETROS DISPONIBLES:
155 PARAMETROS DISPONIBLES:
156 CONSULTAR: "GUIA DE SCRIPT.pdf"
156 CONSULTAR: "GUIA DE SCRIPT.pdf"
157
157
158 ESTRUCTURA:
158 ESTRUCTURA:
159 <access_name>.search(type_option = <class 'str'>, query = <class 'dict'>, param_1 = <class 'param_1'>, ...)
159 <access_name>.search(type_option = <class 'str'>, query = <class 'dict'>, param_1 = <class 'param_1'>, ...)
160 '''
160 '''
161 if type(type_option) is str:
161 if type(type_option) is str:
162 try:
162 try:
163 if type_option == 'dataset':
163 if type_option == 'dataset':
164 key_replace = ['fq', 'fq_list', 'include_private']
164 key_replace = ['fq', 'fq_list', 'include_private']
165 key_point = ['facet_mincount', 'facet_limit', 'facet_field']
165 key_point = ['facet_mincount', 'facet_limit', 'facet_field']
166 for key1, value1 in kwargs.items():
166 for key1, value1 in kwargs.items():
167 if not key1 in key_replace:
167 if not key1 in key_replace:
168 if key1 in key_point:
168 if key1 in key_point:
169 self.dict[key1.replace('_', '.')] = value1
169 self.dict[key1.replace('_', '.')] = value1
170 else:
170 else:
171 self.dict[key1] = value1
171 self.dict[key1] = value1
172
172
173 if query is not None:
173 if query is not None:
174 if type(query) is dict:
174 if type(query) is dict:
175 self.dict['fq_list'] = []
175 self.dict['fq_list'] = []
176 #NUM_RESOURCES_MIN / NUM_RESOURCES_MAX
176 #NUM_RESOURCES_MIN / NUM_RESOURCES_MAX
177 #----------------------------------------------------#
177 #----------------------------------------------------#
178 if 'dataset_start_date' in query:
178 if 'dataset_start_date' in query:
179 if type(query['dataset_start_date']) is str:
179 if type(query['dataset_start_date']) is str:
180 try:
180 try:
181 datetime.strptime(query['dataset_start_date'], '%Y-%m-%d')
181 datetime.strptime(query['dataset_start_date'], '%Y-%m-%d')
182 if len(query['dataset_start_date']) != 10:
182 if len(query['dataset_start_date']) != 10:
183 return '"dataset_start_date", must be: <YYYY-MM-DD>'
183 return '"dataset_start_date", must be: <YYYY-MM-DD>'
184 self.dict['fq_list'].append('dataset_start_date:"'+query['dataset_start_date']+'"')
184 self.dict['fq_list'].append('dataset_start_date:"'+query['dataset_start_date']+'"')
185 self.list.append('dataset_start_date')
185 self.list.append('dataset_start_date')
186 except:
186 except:
187 return '"dataset_start_date" incorrect: "%s"' % (query['dataset_start_date'])
187 return '"dataset_start_date" incorrect: "%s"' % (query['dataset_start_date'])
188 else:
188 else:
189 return '"dataset_start_date" must be <str>'
189 return '"dataset_start_date" must be <str>'
190 #----------------------------------------------------#
190 #----------------------------------------------------#
191 if 'dataset_end_date' in query:
191 if 'dataset_end_date' in query:
192 if type(query['dataset_end_date']) is str:
192 if type(query['dataset_end_date']) is str:
193 try:
193 try:
194 datetime.strptime(query['dataset_end_date'], '%Y-%m-%d')
194 datetime.strptime(query['dataset_end_date'], '%Y-%m-%d')
195 if len(query['dataset_end_date']) != 10:
195 if len(query['dataset_end_date']) != 10:
196 return '"dataset_end_date", must be: <YYYY-MM-DD>'
196 return '"dataset_end_date", must be: <YYYY-MM-DD>'
197
197
198 if 'dataset_start_date' in query:
198 if 'dataset_start_date' in query:
199 if query['dataset_start_date'] > query['dataset_end_date']:
199 if query['dataset_start_date'] > query['dataset_end_date']:
200 return '"dataset_end_date" must be greater than "dataset_start_date"'
200 return '"dataset_end_date" must be greater than "dataset_start_date"'
201
201
202 self.dict['fq_list'].append('dataset_end_date:"'+query['dataset_end_date']+'"')
202 self.dict['fq_list'].append('dataset_end_date:"'+query['dataset_end_date']+'"')
203 self.list.append('dataset_end_date')
203 self.list.append('dataset_end_date')
204 except:
204 except:
205 return '"dataset_end_date" incorrect: "%s"' % (query['dataset_end_date'])
205 return '"dataset_end_date" incorrect: "%s"' % (query['dataset_end_date'])
206 else:
206 else:
207 return '"dataset_end_date" must be <str>'
207 return '"dataset_end_date" must be <str>'
208 #----------------------------------------------------#
208 #----------------------------------------------------#
209 if 'tags' in query:
210 if isinstance(query['tags'], (int, float, str, list)):
211 if type(query['tags']) is list:
212 for u in query['tags']:
213 self.dict['fq_list'].append('tags:"'+str(u)+'"')
214 else:
215 self.dict['fq_list'].append('tags:"'+str(query['tags'])+'"')
216
217 self.list.append('tags')
218 else:
219 return '"tags" must be <list> or <float> or <int> or <str>'
220 #----------------------------------------------------#
209 for key, value in query.items():
221 for key, value in query.items():
210 if value is not None and not key in self.list:
222 if value is not None and not key in self.list:
211 self.dict['fq_list'].append(str(key)+':"'+str(value)+'"')
223 self.dict['fq_list'].append(str(key)+':"'+str(value)+'"')
212 else:
224 else:
213 return '"query" must be <dict>'
225 return '"query" must be <dict>'
214
226
215 return getattr(self.ckan.action, 'package_search')(include_private=True, **self.dict)
227 return getattr(self.ckan.action, 'package_search')(include_private=True, **self.dict)
216
228
217 elif type_option == 'resource':
229 elif type_option == 'resource':
218 for key1, value1 in kwargs.items():
230 for key1, value1 in kwargs.items():
219 if key1 != 'fields':
231 if key1 != 'fields':
220 self.dict[key1] = value1
232 self.dict[key1] = value1
221
233
222 if query is not None:
234 if query is not None:
223 if type(query) is dict:
235 if type(query) is dict:
224 #----------------------------------------------------#
236 #----------------------------------------------------#
225 if 'file_date_min' in query:
237 if 'file_date_min' in query:
226 if type(query['file_date_min']) is str:
238 if type(query['file_date_min']) is str:
227 try:
239 try:
228 datetime.strptime(query['file_date_min'], '%Y-%m-%d')
240 datetime.strptime(query['file_date_min'], '%Y-%m-%d')
229 if len(query['file_date_min']) != 10:
241 if len(query['file_date_min']) != 10:
230 return '"file_date_min", must be: <YYYY-MM-DD>'
242 return '"file_date_min", must be: <YYYY-MM-DD>'
231 except:
243 except:
232 return '"file_date_min" incorrect: "%s"' % (query['file_date_min'])
244 return '"file_date_min" incorrect: "%s"' % (query['file_date_min'])
233 else:
245 else:
234 return '"file_date_min" must be <str>'
246 return '"file_date_min" must be <str>'
235 #----------------------------------------------------#
247 #----------------------------------------------------#
236 if 'file_date_max' in query:
248 if 'file_date_max' in query:
237 if type(query['file_date_max']) is str:
249 if type(query['file_date_max']) is str:
238 try:
250 try:
239 datetime.strptime(query['file_date_max'], '%Y-%m-%d')
251 datetime.strptime(query['file_date_max'], '%Y-%m-%d')
240 if len(query['file_date_max']) != 10:
252 if len(query['file_date_max']) != 10:
241 return '"file_date_max", must be: <YYYY-MM-DD>'
253 return '"file_date_max", must be: <YYYY-MM-DD>'
242
254
243 if 'file_date_min' in query:
255 if 'file_date_min' in query:
244 if query['file_date_min'] > query['file_date_max']:
256 if query['file_date_min'] > query['file_date_max']:
245 return '"file_date_max" must be greater than "file_date_min"'
257 return '"file_date_max" must be greater than "file_date_min"'
246 except:
258 except:
247 return '"file_date_max" incorrect: "%s"' % (query['file_date_max'])
259 return '"file_date_max" incorrect: "%s"' % (query['file_date_max'])
248 else:
260 else:
249 return '"file_date_max" must be <str>'
261 return '"file_date_max" must be <str>'
250 #----------------------------------------------------#
262 #----------------------------------------------------#
251 self.dict['query'] = query
263 self.dict['query'] = query
252 else:
264 else:
253 return '"query" must be <dict>'
265 return '"query" must be <dict>'
254 return getattr(self.ckan.action, 'resources_search')(**self.dict)
266 return getattr(self.ckan.action, 'resources_search')(**self.dict)
255
267
256 elif type_option == 'tag':
268 elif type_option == 'tag':
257 for key1, value1 in kwargs.items():
269 for key1, value1 in kwargs.items():
258 if key1 != 'fields':
270 if key1 != 'fields':
259 self.dict[key1] = value1
271 self.dict[key1] = value1
260
272
261 if not 'vocabulary_id' in kwargs:
273 if not 'vocabulary_id' in kwargs:
262 print('Missing "vocabulary_id" value: tags that don’t belong to any vocabulary')
274 print('Missing "vocabulary_id" value: tags that don’t belong to any vocabulary')
263 else:
275 else:
264 print('Only tags that belong to "{}" vocabulary'.format(kwargs['vocabulary_id']))
276 print('Only tags that belong to "{}" vocabulary'.format(kwargs['vocabulary_id']))
265
277
266 if query is not None:
278 if query is not None:
267 if type(query) is dict:
279 if type(query) is dict:
268 if 'search' in query:
280 if 'search' in query:
269 if type(query['search']) is list or type(query['search']) is str:
281 if type(query['search']) is list or type(query['search']) is str:
270 self.dict['query'] = query['search']
282 self.dict['query'] = query['search']
271 else:
283 else:
272 return '"search" must be <list> or <str>'
284 return '"search" must be <list> or <str>'
273 else:
285 else:
274 return '"query" must be <dict>'
286 return '"query" must be <dict>'
275 return getattr(self.ckan.action, 'tag_search')(**self.dict)
287 return getattr(self.ckan.action, 'tag_search')(**self.dict)
276
288
277 else:
289 else:
278 return 'ERROR:: "type_option = %s" is not accepted' % (type_option)
290 return 'ERROR:: "type_option = %s" is not accepted' % (type_option)
279
291
280 except:
292 except:
281 _, exc_value, _ = sys.exc_info()
293 _, exc_value, _ = sys.exc_info()
282 return exc_value
294 return exc_value
283 else:
295 else:
284 return 'ERROR:: "type_option" must be <str>'
296 return 'ERROR:: "type_option" must be <str>'
285
297
286 def create(self, type_option, select=None, **kwargs):
298 def create(self, type_option, select=None, **kwargs):
287 '''
299 '''
288 FINALIDAD:
300 FINALIDAD:
289 Funcion personalizada para crear.
301 Funcion personalizada para crear.
290
302
291 PARAMETROS DISPONIBLES:
303 PARAMETROS DISPONIBLES:
292 CONSULTAR: "GUIA DE SCRIPT.pdf"
304 CONSULTAR: "GUIA DE SCRIPT.pdf"
293
305
294 ESTRUCTURA:
306 ESTRUCTURA:
295 <access_name>.create(type_option = <class 'str'>, param_1 = <class 'param_1'>, ...)
307 <access_name>.create(type_option = <class 'str'>, param_1 = <class 'param_1'>, ...)
296 '''
308 '''
297 if type(type_option) is str:
309 if type(type_option) is str:
298 try:
310 try:
299 if type_option == 'dataset':
311 if type_option == 'dataset':
300 return getattr(self.ckan.action, 'package_create')(**kwargs)
312 return getattr(self.ckan.action, 'package_create')(**kwargs)
301 if type_option == 'resource':
313 if type_option == 'resource':
302 return resource.resource_create(self, **kwargs)
314 return resource.resource_create(self, **kwargs)
303 elif type_option == 'project':
315 elif type_option == 'project':
304 return getattr(self.ckan.action, 'organization_create')(**kwargs)
316 return getattr(self.ckan.action, 'organization_create')(**kwargs)
305 elif type_option == 'member':
317 elif type_option == 'member':
306 return getattr(self.ckan.action, 'organization_member_create')(**kwargs)
318 return getattr(self.ckan.action, 'organization_member_create')(**kwargs)
307 elif type_option == 'collaborator':
319 elif type_option == 'collaborator':
308 return getattr(self.ckan.action, 'package_collaborator_create')(**kwargs)
320 return getattr(self.ckan.action, 'package_collaborator_create')(**kwargs)
309 elif type_option == 'vocabulary':
321 elif type_option == 'vocabulary':
310 return getattr(self.ckan.action, 'vocabulary_create')(**kwargs)
322 return getattr(self.ckan.action, 'vocabulary_create')(**kwargs)
311 elif type_option == 'tag':
323 elif type_option == 'tag':
312 return getattr(self.ckan.action, 'tag_create')(**kwargs)
324 return getattr(self.ckan.action, 'tag_create')(**kwargs)
313 elif type_option == 'user':
325 elif type_option == 'user':
314 return getattr(self.ckan.action, 'user_create')(**kwargs)
326 return getattr(self.ckan.action, 'user_create')(**kwargs)
315 elif type_option == 'views':
327 elif type_option == 'views':
316 if 'resource' == select:
328 if 'resource' == select:
317 self.list = ['package']
329 self.list = ['package']
318 for key1, value1 in kwargs.items():
330 for key1, value1 in kwargs.items():
319 if not key1 in self.list:
331 if not key1 in self.list:
320 self.dict[key1] = value1
332 self.dict[key1] = value1
321 return getattr(self.ckan.action, 'resource_create_default_resource_views')(**self.dict)
333 return getattr(self.ckan.action, 'resource_create_default_resource_views')(**self.dict)
322 elif 'dataset' == select:
334 elif 'dataset' == select:
323 return getattr(self.ckan.action, 'package_create_default_resource_views')(**kwargs)
335 return getattr(self.ckan.action, 'package_create_default_resource_views')(**kwargs)
324 else:
336 else:
325 return 'ERROR:: "select = %s" is not accepted' % (select)
337 return 'ERROR:: "select = %s" is not accepted' % (select)
326 else:
338 else:
327 return 'ERROR:: "type_option = %s" is not accepted' % (type_option)
339 return 'ERROR:: "type_option = %s" is not accepted' % (type_option)
328 except:
340 except:
329 _, exc_value, _ = sys.exc_info()
341 _, exc_value, _ = sys.exc_info()
330 return exc_value
342 return exc_value
331 else:
343 else:
332 return 'ERROR:: "type_option" must be <str>'
344 return 'ERROR:: "type_option" must be <str>'
333
345
334 def patch(self, type_option, **kwargs):
346 def patch(self, type_option, **kwargs):
335 '''
347 '''
336 FINALIDAD:
348 FINALIDAD:
337 Funciones personalizadas para actualizar
349 Funciones personalizadas para actualizar
338
350
339 PARAMETROS DISPONIBLES:
351 PARAMETROS DISPONIBLES:
340 CONSULTAR: "GUIA DE SCRIPT.pdf"
352 CONSULTAR: "GUIA DE SCRIPT.pdf"
341
353
342 ESTRUCTURA:
354 ESTRUCTURA:
343 <access_name>.patch(type_option = <class 'str'>, param_1 = <class 'param_1'>, ...)
355 <access_name>.patch(type_option = <class 'str'>, param_1 = <class 'param_1'>, ...)
344 '''
356 '''
345 if type(type_option) is str:
357 if type(type_option) is str:
346 try:
358 try:
347 if type_option == 'dataset':
359 if type_option == 'dataset':
348 #Agregar que solo se debe modificar parΓ‘metros del Dataset y que no incluya Resources
360 #Agregar que solo se debe modificar parΓ‘metros del Dataset y que no incluya Resources
349 return getattr(self.ckan.action, 'package_patch')(**kwargs)
361 return getattr(self.ckan.action, 'package_patch')(**kwargs)
350 elif type_option == 'project':
362 elif type_option == 'project':
351 return getattr(self.ckan.action, 'organization_patch')(**kwargs)
363 return getattr(self.ckan.action, 'organization_patch')(**kwargs)
352 elif type_option == 'resource':
364 elif type_option == 'resource':
353 return resource.resource_patch(self, **kwargs)
365 return resource.resource_patch(self, **kwargs)
354 elif type_option == 'member':
366 elif type_option == 'member':
355 return getattr(self.ckan.action, 'organization_member_create')(**kwargs)
367 return getattr(self.ckan.action, 'organization_member_create')(**kwargs)
356 elif type_option == 'collaborator':
368 elif type_option == 'collaborator':
357 return getattr(self.ckan.action, 'package_collaborator_create')(**kwargs)
369 return getattr(self.ckan.action, 'package_collaborator_create')(**kwargs)
358 else:
370 else:
359 return 'ERROR:: "type_option = %s" is not accepted' % (type_option)
371 return 'ERROR:: "type_option = %s" is not accepted' % (type_option)
360 except:
372 except:
361 _, exc_value, _ = sys.exc_info()
373 _, exc_value, _ = sys.exc_info()
362 return exc_value
374 return exc_value
363 else:
375 else:
364 return 'ERROR:: "type_option" must be <str>'
376 return 'ERROR:: "type_option" must be <str>'
365
377
366 def delete(self, type_option, select=None, **kwargs):
378 def delete(self, type_option, select=None, **kwargs):
367 '''
379 '''
368 FINALIDAD:
380 FINALIDAD:
369 FunciΓ³n personalizada para eliminar y/o purgar.
381 FunciΓ³n personalizada para eliminar y/o purgar.
370
382
371 PARAMETROS DISPONIBLES:
383 PARAMETROS DISPONIBLES:
372 CONSULTAR: "GUIA DE SCRIPT.pdf"
384 CONSULTAR: "GUIA DE SCRIPT.pdf"
373
385
374 ESTRUCTURA:
386 ESTRUCTURA:
375 <access_name>.delete(type_option = <class 'str'>, param_1 = <class 'param_1'>, ...)
387 <access_name>.delete(type_option = <class 'str'>, param_1 = <class 'param_1'>, ...)
376 '''
388 '''
377 if type(type_option) is str:
389 if type(type_option) is str:
378 try:
390 try:
379 if type_option == 'dataset':
391 if type_option == 'dataset':
380 if select is None:
392 if select is None:
381 return 'ERROR:: "select" must not be "None"'
393 return 'ERROR:: "select" must not be "None"'
382 else:
394 else:
383 if 'delete' == select:
395 if 'delete' == select:
384 return getattr(self.ckan.action, 'package_delete')(**kwargs)
396 return getattr(self.ckan.action, 'package_delete')(**kwargs)
385 elif 'purge' == select:
397 elif 'purge' == select:
386 return getattr(self.ckan.action, 'dataset_purge')(**kwargs)
398 return getattr(self.ckan.action, 'dataset_purge')(**kwargs)
387 else:
399 else:
388 return 'ERROR:: "select = %s" is not accepted' % (select)
400 return 'ERROR:: "select = %s" is not accepted' % (select)
389 elif type_option == 'project':
401 elif type_option == 'project':
390 if select is None:
402 if select is None:
391 return 'ERROR:: "select" must not be "None"'
403 return 'ERROR:: "select" must not be "None"'
392 else:
404 else:
393 if 'delete' == select:
405 if 'delete' == select:
394 return getattr(self.ckan.action, 'organization_delete')(**kwargs)
406 return getattr(self.ckan.action, 'organization_delete')(**kwargs)
395 elif 'purge' == select:
407 elif 'purge' == select:
396 return getattr(self.ckan.action, 'organization_purge')(**kwargs)
408 return getattr(self.ckan.action, 'organization_purge')(**kwargs)
397 else:
409 else:
398 return 'ERROR:: "select = %s" is not accepted' % (select)
410 return 'ERROR:: "select = %s" is not accepted' % (select)
399 elif type_option == 'resource':
411 elif type_option == 'resource':
400 if select is None:
412 if select is None:
401 return 'ERROR:: "select" must not be "None"'
413 return 'ERROR:: "select" must not be "None"'
402 else:
414 else:
403 return resource.resource_delete(self, select, **kwargs)
415 return resource.resource_delete(self, select, **kwargs)
404 elif type_option == 'vocabulary':
416 elif type_option == 'vocabulary':
405 return getattr(self.ckan.action, 'vocabulary_delete')(**kwargs)
417 return getattr(self.ckan.action, 'vocabulary_delete')(**kwargs)
406 elif type_option == 'tag':
418 elif type_option == 'tag':
407 return getattr(self.ckan.action, 'tag_delete')(**kwargs)
419 return getattr(self.ckan.action, 'tag_delete')(**kwargs)
408 elif type_option == 'user':
420 elif type_option == 'user':
409 return getattr(self.ckan.action, 'user_delete')(**kwargs)
421 return getattr(self.ckan.action, 'user_delete')(**kwargs)
410 else:
422 else:
411 return 'ERROR:: "type_option = %s" is not accepted' % (type_option)
423 return 'ERROR:: "type_option = %s" is not accepted' % (type_option)
412 except:
424 except:
413 _, exc_value, _ = sys.exc_info()
425 _, exc_value, _ = sys.exc_info()
414 return exc_value
426 return exc_value
415 else:
427 else:
416 return 'ERROR:: "type_option" must be <str>'
428 return 'ERROR:: "type_option" must be <str>'
417
429
418 def download(self, id, processes=1, path=os.path.expanduser("~"), **kwargs):
430 def download(self, id, processes=1, path=os.path.expanduser("~"), **kwargs):
419 '''
431 '''
420 FINALIDAD:
432 FINALIDAD:
421 Funcion personalizada avanzada para la descarga de archivos existentes de un(os) dataset(s).
433 Funcion personalizada avanzada para la descarga de archivos existentes de un(os) dataset(s).
422
434
423 PARAMETROS DISPONIBLES:
435 PARAMETROS DISPONIBLES:
424 CONSULTAR: "GUIA DE SCRIPT.pdf"
436 CONSULTAR: "GUIA DE SCRIPT.pdf"
425
437
426 ESTRUCTURA:
438 ESTRUCTURA:
427 <access_name>.download(id = <class 'str' or 'list'>, param_1 = <class 'param_1'>, ...)
439 <access_name>.download(id = <class 'str' or 'list'>, param_1 = <class 'param_1'>, ...)
428 '''
440 '''
429 #------------------ PATH ----------------------#
441 #------------------ PATH ----------------------#
430 if isinstance(path, str):
442 if isinstance(path, str):
431 if os.path.isdir(path):
443 if os.path.isdir(path):
432 if not path.endswith(os.sep):
444 if not path.endswith(os.sep):
433 path = path + os.sep
445 path = path + os.sep
434 test_txt = path + datetime.now().strftime("%Y_%m_%d_%H_%M_%S_%f")+'.txt'
446 test_txt = path + datetime.now().strftime("%Y_%m_%d_%H_%M_%S_%f")+'.txt'
435 try:
447 try:
436 file_txt = open(test_txt, 'w')
448 file_txt = open(test_txt, 'w')
437 file_txt.close()
449 file_txt.close()
438 os.remove(test_txt)
450 os.remove(test_txt)
439 except:
451 except:
440 return 'ERROR:: Access denied, you are not authorized to write files: "%s"' % (path)
452 return 'ERROR:: Access denied, you are not authorized to write files: "%s"' % (path)
441 else:
453 else:
442 return 'ERROR:: "path" does not exist'
454 return 'ERROR:: "path" does not exist'
443 else:
455 else:
444 return 'ERROR:: "path" must be: <class "str">'
456 return 'ERROR:: "path" must be: <class "str">'
445
457
446 #------------------ PROCESSES -----------------#
458 #------------------ PROCESSES -----------------#
447 if not isinstance(processes, int):
459 if not isinstance(processes, int):
448 return 'ERROR:: "processes" must be: <class "int">'
460 return 'ERROR:: "processes" must be: <class "int">'
449
461
450 #------------------ ID OR NAME ----------------#
462 #------------------ ID OR NAME ----------------#
451 if isinstance(id, str):
463 if isinstance(id, str):
452 id = [id]
464 id = [id]
453 elif isinstance(id, list):
465 elif isinstance(id, list):
454 id = list(map(str, id))
466 id = list(map(str, id))
455 else:
467 else:
456 return 'ERROR:: dataset "id" must be: <class "str" or "list">'
468 return 'ERROR:: dataset "id" must be: <class "str" or "list">'
457 #----------------------------------------------#
469 #----------------------------------------------#
458 arguments = {
470 arguments = {
459 '--apikey': self.Authorization,
471 '--apikey': self.Authorization,
460 '--ckan-user': None,
472 '--ckan-user': None,
461 '--config': None,
473 '--config': None,
462 '--datapackages': path,
474 '--datapackages': path,
463 '--datastore-fields': False,
475 '--datastore-fields': False,
464 '--get-request': False,
476 '--get-request': False,
465 '--insecure': not self.verify,
477 '--insecure': not self.verify,
466 '--processes': str(processes),
478 '--processes': str(processes),
467 '--quiet': False,
479 '--quiet': False,
468 '--remote': self.url,
480 '--remote': self.url,
469 '--worker': False,
481 '--worker': False,
470 #'--log': 'log.txt',
482 #'--log': 'log.txt',
471 #'--all': False,
483 #'--all': False,
472 #'--gzip': False,
484 #'--gzip': False,
473 #'--output': None,
485 #'--output': None,
474 #'--max-records': None,
486 #'--max-records': None,
475 #'--output-json': False,
487 #'--output-json': False,
476 #'--output-jsonl': False,
488 #'--output-jsonl': False,
477 #'--create-only': False,
489 #'--create-only': False,
478 #'--help': False,
490 #'--help': False,
479 #'--input': None,
491 #'--input': None,
480 #'--input-json': False,
492 #'--input-json': False,
481 #'--start-record': '1',
493 #'--start-record': '1',
482 #'--update-only': False,
494 #'--update-only': False,
483 #'--upload-logo': False,
495 #'--upload-logo': False,
484 #'--upload-resources': False,
496 #'--upload-resources': False,
485 #'--version': False,
497 #'--version': False,
486 'ID_OR_NAME': id,
498 'ID_OR_NAME': id,
487 'datasets': True,
499 'datasets': True,
488 'dump': True,
500 'dump': True,
489 #'ACTION_NAME': None,
501 #'ACTION_NAME': None,
490 #'KEY:JSON': [],
502 #'KEY:JSON': [],
491 #'KEY=STRING': [],
503 #'KEY=STRING': [],
492 #'KEY@FILE': [],
504 #'KEY@FILE': [],
493 #'action': False,
505 #'action': False,
494 #'delete': False,
506 #'delete': False,
495 #'groups': False,
507 #'groups': False,
496 #'load': False,
508 #'load': False,
497 #'organizations': False,
509 #'organizations': False,
498 #'related': False,
510 #'related': False,
499 #'search': False,
511 #'search': False,
500 #'users': False
512 #'users': False
501 }
513 }
502 return download.dump_things_change(self.ckan, 'datasets', arguments, **kwargs) No newline at end of file
514 return download.dump_things_change(self.ckan, 'datasets', arguments, **kwargs)
General Comments 0
You need to be logged in to leave comments. Login now