##// END OF EJS Templates
v2.9.2 :: Update 'isfile' create resource and download [bugs]
eynilupu -
r17:187bc9eb8a7d
parent child
Show More
1 NO CONTENT: modified file, binary diff hidden
NO CONTENT: modified file, binary diff hidden
@@ -1,1044 +1,1044
1 from ckanapi import RemoteCKAN
1 from ckanapi import RemoteCKAN
2 from datetime import datetime
2 from datetime import datetime
3 from tqdm import tqdm
3 from tqdm import tqdm
4 from CKAN_JRO import logic_download
4 from CKAN_JRO import logic_download
5 from CKAN_JRO import resource
5 from CKAN_JRO import resource
6 #from ckanapi.errors import NotAuthorized, NotFound, ValidationError, SearchQueryError, SearchError, CKANAPIError, ServerIncompatibleError
6 #from ckanapi.errors import NotAuthorized, NotFound, ValidationError, SearchQueryError, SearchError, CKANAPIError, ServerIncompatibleError
7 import sys
7 import sys
8 import platform
8 import platform
9 import os
9 import os
10 import tempfile
10 import tempfile
11 import shutil
11 import shutil
12 import zipfile
12 import zipfile
13 import concurrent.futures
13 import concurrent.futures
14 import requests
14 import requests
15 import json
15 import json
16 #import pathlib
16 #import pathlib
17 import uuid
17 import uuid
18
18
19 if sys.version_info.major == 3:
19 if sys.version_info.major == 3:
20 from urllib.parse import urlparse
20 from urllib.parse import urlparse
21 else:
21 else:
22 import urlparse
22 import urlparse
23
23
24 class JROAPI():
24 class JROAPI():
25 """
25 """
26 FINALIDAD:
26 FINALIDAD:
27 Script para administrar y obtener la data del repositorio por medio de APIs.
27 Script para administrar y obtener la data del repositorio por medio de APIs.
28
28
29 REQUISITIOS PREVIOS:
29 REQUISITIOS PREVIOS:
30 - Paso 1: Tener "pip [Python 2]" o "pip3 [Python 3]" instalado:
30 - Paso 1: Tener "pip [Python 2]" o "pip3 [Python 3]" instalado:
31 - Paso 2: Instalar lo siguiente como admininstrador:
31 - Paso 2: Instalar lo siguiente como admininstrador:
32 En Python 2
32 En Python 2
33 - pip install ckanapi==4.5
33 - pip install ckanapi==4.5
34 - pip install requests
34 - pip install requests
35 - pip install futures
35 - pip install futures
36 - pip install tqdm
36 - pip install tqdm
37 En Python > 3
37 En Python > 3
38 - pip3 install ckanapi==4.5
38 - pip3 install ckanapi==4.5
39 - pip3 install requests
39 - pip3 install requests
40 - pip3 install tqdm
40 - pip3 install tqdm
41
41
42 FUNCIONES DISPONIBLES:
42 FUNCIONES DISPONIBLES:
43 - action
43 - action
44 - upload_file
44 - upload_file
45 - upload_multiple_files
45 - upload_multiple_files
46 - upload_multiple_files_advance
46 - upload_multiple_files_advance
47 - show
47 - show
48 - search
48 - search
49 - create
49 - create
50 - patch
50 - patch
51 - delete
51 - delete
52 - download_files
52 - download_files
53
53
54 EJEMPLOS:
54 EJEMPLOS:
55 #1:
55 #1:
56 with JROAPI('http://demo.example.com', Authorization='#########') as <access_name>:
56 with JROAPI('http://demo.example.com', Authorization='#########') as <access_name>:
57 ... some operation(s) ...
57 ... some operation(s) ...
58 #2:
58 #2:
59 <access_name> = JROAPI('http://example.com', Authorization='#########')
59 <access_name> = JROAPI('http://example.com', Authorization='#########')
60 ... some operation(s) ...
60 ... some operation(s) ...
61 <access_name>.ckan.close()
61 <access_name>.ckan.close()
62
62
63 REPORTAR ALGUN PROBLEMA:
63 REPORTAR ALGUN PROBLEMA:
64 Debe enviar un correo a eynilupu@igp.gob.pe detallando los siguientes pasos:
64 Debe enviar un correo a eynilupu@igp.gob.pe detallando los siguientes pasos:
65 1) Correo para contactarlo
65 1) Correo para contactarlo
66 2) Descripcion del problema
66 2) Descripcion del problema
67 3) ¿En que paso o seccion encontro el problema?
67 3) ¿En que paso o seccion encontro el problema?
68 4) ¿Cual era el resultado que usted esperaba?
68 4) ¿Cual era el resultado que usted esperaba?
69 """
69 """
70 def __init__(self, url, Authorization=None, secure=True):
70 def __init__(self, url, Authorization=None, secure=True):
71 #-------- Check Secure -------#
71 #-------- Check Secure -------#
72 self.verify = secure
72 self.verify = secure
73 if not secure and isinstance(secure, bool):
73 if not secure and isinstance(secure, bool):
74 session = requests.Session()
74 session = requests.Session()
75 session.verify = False
75 session.verify = False
76 else:
76 else:
77 session = None
77 session = None
78 #------------------------------#
78 #------------------------------#
79 self.url = url
79 self.url = url
80 ua = 'CKAN_JRO/2.9.2 (+'+str(self.url)+')'
80 ua = 'CKAN_JRO/2.9.2 (+'+str(self.url)+')'
81 #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'
81 #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'
82 self.ckan = RemoteCKAN(self.url, apikey=Authorization, user_agent=ua, session=session)
82 self.ckan = RemoteCKAN(self.url, apikey=Authorization, user_agent=ua, session=session)
83 #self.ckan = RemoteCKAN(self.url, apikey=Authorization)
83 #self.ckan = RemoteCKAN(self.url, apikey=Authorization)
84 self.Authorization = Authorization
84 self.Authorization = Authorization
85 # Change for --> self.separator = os.sep
85 # Change for --> self.separator = os.sep
86 if platform.system() == 'Windows':
86 if platform.system() == 'Windows':
87 self.separator = '\\'
87 self.separator = '\\'
88 else:
88 else:
89 self.separator = '/'
89 self.separator = '/'
90
90
91 self.chunk_size = 1024
91 self.chunk_size = 1024
92 self.list = []
92 self.list = []
93 self.dict = {}
93 self.dict = {}
94 self.str = ''
94 self.str = ''
95 self.check = 1
95 self.check = 1
96 self.cont = 0
96 self.cont = 0
97
97
98 def __enter__(self):
98 def __enter__(self):
99 return self
99 return self
100
100
101 def __exit__(self, *args):
101 def __exit__(self, *args):
102 self.ckan.close()
102 self.ckan.close()
103
103
104 def action(self, action, **kwargs):
104 def action(self, action, **kwargs):
105 """
105 """
106 FINALIDAD:
106 FINALIDAD:
107 Funcion para llamar a las APIs disponibles
107 Funcion para llamar a las APIs disponibles
108
108
109 APIs DISPONIBLES:
109 APIs DISPONIBLES:
110 CONSULTAR: "GUIA DE SCRIPT.pdf"
110 CONSULTAR: "GUIA DE SCRIPT.pdf"
111
111
112 EJEMPLO:
112 EJEMPLO:
113 <access_name>.action(<consuming API>, param_1 = <class 'param_1'>, ...)
113 <access_name>.action(<consuming API>, param_1 = <class 'param_1'>, ...)
114 """
114 """
115 #--------------- CASE: PACKAGE SEARCH ---------------#
115 #--------------- CASE: PACKAGE SEARCH ---------------#
116 if kwargs is not None:
116 if kwargs is not None:
117 if action == 'package_search':
117 if action == 'package_search':
118 self.list = ['facet_mincount', 'facet_limit', 'facet_field']
118 self.list = ['facet_mincount', 'facet_limit', 'facet_field']
119 for facet in self.list:
119 for facet in self.list:
120 if facet in kwargs:
120 if facet in kwargs:
121 kwargs[facet.replace('_', '.')] = kwargs[facet]
121 kwargs[facet.replace('_', '.')] = kwargs[facet]
122 kwargs.pop(facet)
122 kwargs.pop(facet)
123 #----------------------------------------------------#
123 #----------------------------------------------------#
124 try:
124 try:
125 return getattr(self.ckan.action, action)(**kwargs)
125 return getattr(self.ckan.action, action)(**kwargs)
126 except:
126 except:
127 _, exc_value, _ = sys.exc_info()
127 _, exc_value, _ = sys.exc_info()
128 return exc_value
128 return exc_value
129
129
130 def upload_file(self, dataset_id, file_date, file_type, file_path=False, url_or_path=False, ignore_repetition=False, **kwargs):
130 def upload_file(self, dataset_id, file_date, file_type, file_path=False, url_or_path=False, ignore_repetition=False, **kwargs):
131 # Agregar si es interruptido por teclado
131 # Agregar si es interruptido por teclado
132 '''
132 '''
133 FINALIDAD:
133 FINALIDAD:
134 Funcion para crear un unico recurso (puede incluir un archivo asociado) al repositorio del ROJ.
134 Funcion para crear un unico recurso (puede incluir un archivo asociado) al repositorio del ROJ.
135
135
136 PARAMETROS DISPONIBLES:
136 PARAMETROS DISPONIBLES:
137 CONSULTAR: "GUIA DE SCRIPT.pdf"
137 CONSULTAR: "GUIA DE SCRIPT.pdf"
138
138
139 ESTRUCTURA:
139 ESTRUCTURA:
140 <access_name>.upload_file(dataset_id = <class 'str'>, file_date = <class 'str'>, file_type = <class 'str'>, file_path = <class 'str'>, url_or_path = <class 'str'>, param_1 = <class 'param_1'>, ...)
140 <access_name>.upload_file(dataset_id = <class 'str'>, file_date = <class 'str'>, file_type = <class 'str'>, file_path = <class 'str'>, url_or_path = <class 'str'>, param_1 = <class 'param_1'>, ...)
141 '''
141 '''
142 #self.list = ['package_id', 'upload', 'voc_file_type', 'name'] #file_date
142 #self.list = ['package_id', 'upload', 'voc_file_type', 'name'] #file_date
143 self.list = ['package_id', 'upload', 'voc_file_type'] #file_date
143 self.list = ['package_id', 'upload', 'voc_file_type'] #file_date
144 for key1, value1 in kwargs.items():
144 for key1, value1 in kwargs.items():
145 if not key1 in self.list:
145 if not key1 in self.list:
146 self.dict[key1] = value1
146 self.dict[key1] = value1
147
147
148 #---------------------------#
148 #---------------------------#
149 if not 'others' in kwargs:
149 if not 'others' in kwargs:
150 self.dict['others'] = ''
150 self.dict['others'] = ''
151 else:
151 else:
152 if isinstance(kwargs['others'], list):
152 if isinstance(kwargs['others'], list):
153 self.dict['others'] = json.dumps(kwargs['others'])
153 self.dict['others'] = json.dumps(kwargs['others'])
154 #---------------------------#
154 #---------------------------#
155
155
156 if isinstance(file_path, str) and isinstance(url_or_path, str):
156 if isinstance(file_path, str) and isinstance(url_or_path, str):
157 return 'ERROR:: Choose one: "file_path" or "url_or_path" parameters'
157 return 'ERROR:: Choose one: "file_path" or "url_or_path" parameters'
158
158
159 if isinstance(file_path, str):
159 if isinstance(file_path, str):
160 if not os.path.isfile(file_path):
160 if not os.path.isfile(file_path):
161 return 'File "%s" not exist' % (file_path)
161 return 'File "%s" not exist' % (file_path)
162
162
163 self.dict['upload'] = open(file_path, 'rb')
163 self.dict['upload'] = open(file_path, 'rb')
164 self.dict['name'] = os.path.basename(file_path)
164 self.dict['name'] = os.path.basename(file_path)
165 elif isinstance(url_or_path, str):
165 elif isinstance(url_or_path, str):
166 self.dict['url'] = url_or_path
166 self.dict['url'] = url_or_path
167 if not 'name' in self.dict:
167 if not 'name' in self.dict:
168 self.dict['name'] = os.path.basename(url_or_path)
168 self.dict['name'] = os.path.basename(url_or_path)
169 else:
169 else:
170 return 'ERROR: Verify "file_path" or "url_or_path" parameters: <class "str"> or choose one'
170 return 'ERROR: Verify "file_path" or "url_or_path" parameters: <class "str"> or choose one'
171
171
172 #if not 'format' in self.dict:
172 #if not 'format' in self.dict:
173 # self.str = ''.join(pathlib.Path(file_path).suffixes)
173 # self.str = ''.join(pathlib.Path(file_path).suffixes)
174 # if len(self.str) > 0:
174 # if len(self.str) > 0:
175 # self.dict['format'] = self.str.upper()[1:]
175 # self.dict['format'] = self.str.upper()[1:]
176
176
177 #-------------------------PACKAGE SHOW-----------------------#
177 #-------------------------PACKAGE SHOW-----------------------#
178 try:
178 try:
179 dataset_show = getattr(self.ckan.action, 'package_show')(id=dataset_id)['resources']
179 dataset_show = getattr(self.ckan.action, 'package_show')(id=dataset_id)['resources']
180 except:
180 except:
181 _, exc_value, _ = sys.exc_info()
181 _, exc_value, _ = sys.exc_info()
182 print('ERROR obtaining metadata dataset:: Use the "print" for more information')
182 print('ERROR obtaining metadata dataset:: Use the "print" for more information')
183 return exc_value
183 return exc_value
184
184
185 resources_name = []
185 resources_name = []
186 for u in dataset_show:
186 for u in dataset_show:
187 resources_name.append(u['name'].lower())
187 resources_name.append(u['name'].lower())
188
188
189 if self.dict['name'].lower() in resources_name:
189 if self.dict['name'].lower() in resources_name:
190 if not ignore_repetition:
190 if not ignore_repetition:
191 return 'ERROR:: "%s" resource already exist in this dataset' % (self.dict['name'])
191 return 'ERROR:: "%s" resource already exist in this dataset' % (self.dict['name'])
192 print('WARRING:: "'+ str(self.dict['name']) +'" resource already exist in this dataset')
192 print('WARRING:: "'+ str(self.dict['name']) +'" resource already exist in this dataset')
193 #------------------------------------------------------------#
193 #------------------------------------------------------------#
194 try:
194 try:
195 return getattr(self.ckan.action, 'resource_create')(package_id=dataset_id, file_date=file_date, voc_file_type=file_type, **self.dict)
195 return getattr(self.ckan.action, 'resource_create')(package_id=dataset_id, file_date=file_date, voc_file_type=file_type, **self.dict)
196 except:
196 except:
197 _, exc_value, _ = sys.exc_info()
197 _, exc_value, _ = sys.exc_info()
198 return exc_value
198 return exc_value
199
199
200 def upload_multiple_files_advance(self, dataset_id, path_files, file_date, file_type, max_size=100, max_count=500, ignore_repetition=False, **kwargs):
200 def upload_multiple_files_advance(self, dataset_id, path_files, file_date, file_type, max_size=100, max_count=500, ignore_repetition=False, **kwargs):
201 # Agregar si es interruptido por teclado
201 # Agregar si es interruptido por teclado
202 '''
202 '''
203 FINALIDAD:
203 FINALIDAD:
204 Funcion para subir multiples archivos al repositorio del ROJ.
204 Funcion para subir multiples archivos al repositorio del ROJ.
205
205
206 PARAMETROS DISPONIBLES:
206 PARAMETROS DISPONIBLES:
207 CONSULTAR: "GUIA DE SCRIPT.pdf"
207 CONSULTAR: "GUIA DE SCRIPT.pdf"
208
208
209 ESTRUCTURA:
209 ESTRUCTURA:
210 <access_name>.upload_multiple_files_advance(dataset_id = <class 'str'>, path_files = <class 'list of strings'>, file_date = <class 'str'>, file_type = <class 'str'>, param_1 = <class 'param_1'>, ...)
210 <access_name>.upload_multiple_files_advance(dataset_id = <class 'str'>, path_files = <class 'list of strings'>, file_date = <class 'str'>, file_type = <class 'str'>, param_1 = <class 'param_1'>, ...)
211 '''
211 '''
212 #-------------------------PACKAGE SHOW-----------------------#
212 #-------------------------PACKAGE SHOW-----------------------#
213 try:
213 try:
214 dataset_show = getattr(self.ckan.action, 'package_show')(id=dataset_id)['resources']
214 dataset_show = getattr(self.ckan.action, 'package_show')(id=dataset_id)['resources']
215 except:
215 except:
216 _, exc_value, _ = sys.exc_info()
216 _, exc_value, _ = sys.exc_info()
217 print('ERROR obtaining metadata dataset:: Use the "print" for more information')
217 print('ERROR obtaining metadata dataset:: Use the "print" for more information')
218 return exc_value
218 return exc_value
219 #------------------------------------------------------------#
219 #------------------------------------------------------------#
220 resources_name = []
220 resources_name = []
221 for u in dataset_show:
221 for u in dataset_show:
222 resources_name.append(u['name'].lower())
222 resources_name.append(u['name'].lower())
223 #------------------------------------------------------------#
223 #------------------------------------------------------------#
224 self.list = ['package_id', 'upload', 'voc_file_type', 'name']
224 self.list = ['package_id', 'upload', 'voc_file_type', 'name']
225 for key1, value1 in kwargs.items():
225 for key1, value1 in kwargs.items():
226 if not key1 in self.list:
226 if not key1 in self.list:
227 self.dict[key1] = value1
227 self.dict[key1] = value1
228 #------------------------------------------------------------#
228 #------------------------------------------------------------#
229 if not 'others' in kwargs:
229 if not 'others' in kwargs:
230 self.dict['others'] = ''
230 self.dict['others'] = ''
231 else:
231 else:
232 if isinstance(kwargs['others'], list):
232 if isinstance(kwargs['others'], list):
233 self.dict['others'] = json.dumps(kwargs['others'])
233 self.dict['others'] = json.dumps(kwargs['others'])
234 #------------------------------------------------------------#
234 #------------------------------------------------------------#
235 total_list = []
235 total_list = []
236 #---------------CASO : "path" or "path_list"-----------------#
236 #---------------CASO : "path" or "path_list"-----------------#
237 if type(path_files) is list:
237 if type(path_files) is list:
238 if len(path_files) != 0:
238 if len(path_files) != 0:
239 path_files.sort()
239 path_files.sort()
240 for u in path_files:
240 for u in path_files:
241 if os.path.isfile(u):
241 if os.path.isfile(u):
242 if os.path.basename(u).lower() in resources_name:
242 if os.path.basename(u).lower() in resources_name:
243 if not ignore_repetition:
243 if not ignore_repetition:
244 return 'ERROR:: "%s" file already exist in this dataset' % (os.path.basename(u))
244 return 'ERROR:: "%s" file already exist in this dataset' % (os.path.basename(u))
245 print('WARRING:: "'+ str(os.path.basename(u)) +'" file was ignored because already exist in this dataset')
245 print('WARRING:: "'+ str(os.path.basename(u)) +'" file was ignored because already exist in this dataset')
246 else:
246 else:
247 total_list.append({'name':os.path.basename(u), 'size': os.stat(u).st_size, 'upload':open(u, 'rb')})
247 total_list.append({'name':os.path.basename(u), 'size': os.stat(u).st_size, 'upload':open(u, 'rb')})
248 else:
248 else:
249 return 'File "%s" does not exist' % (u)
249 return 'File "%s" does not exist' % (u)
250 else:
250 else:
251 return 'ERROR:: "path_list is empty"'
251 return 'ERROR:: "path_list is empty"'
252
252
253 elif type(path_files) is str:
253 elif type(path_files) is str:
254 if os.path.isdir(path_files):
254 if os.path.isdir(path_files):
255 path_order = [f for f in os.listdir(path_files) if os.path.isfile(os.path.join(path_files, f))]
255 path_order = [f for f in os.listdir(path_files) if os.path.isfile(os.path.join(path_files, f))]
256 path_order.sort()
256 path_order.sort()
257 if path_order:
257 if path_order:
258 for name in path_order:
258 for name in path_order:
259 if name.lower() in resources_name:
259 if name.lower() in resources_name:
260 if not ignore_repetition:
260 if not ignore_repetition:
261 return 'ERROR:: "%s" file already exist in this dataset' % (name)
261 return 'ERROR:: "%s" file already exist in this dataset' % (name)
262 print('WARRING:: "'+ name +'" file was ignored because already exist in this dataset')
262 print('WARRING:: "'+ name +'" file was ignored because already exist in this dataset')
263 else:
263 else:
264 total_list.append({'name':name, 'size': os.stat(os.path.join(path_files, name)).st_size, 'upload':open(os.path.join(path_files, name), 'rb')})
264 total_list.append({'name':name, 'size': os.stat(os.path.join(path_files, name)).st_size, 'upload':open(os.path.join(path_files, name), 'rb')})
265 else:
265 else:
266 return "ERROR:: There aren't files in this directory"
266 return "ERROR:: There aren't files in this directory"
267 else:
267 else:
268 return 'ERROR:: Directory "%s" does not exist' % (path_files)
268 return 'ERROR:: Directory "%s" does not exist' % (path_files)
269 else:
269 else:
270 return 'ERROR:: "path_files" must be a str or list'
270 return 'ERROR:: "path_files" must be a str or list'
271 #------------------------------------------------------------#
271 #------------------------------------------------------------#
272 try:
272 try:
273 uuid.UUID(str(dataset_id), version=4)
273 uuid.UUID(str(dataset_id), version=4)
274 package_id_or_name = '"id": "' + str(dataset_id) + '"'
274 package_id_or_name = '"id": "' + str(dataset_id) + '"'
275 except ValueError:
275 except ValueError:
276 package_id_or_name = '"name": "' + str(dataset_id) + '"'
276 package_id_or_name = '"name": "' + str(dataset_id) + '"'
277 #------------------------------------------------------------#
277 #------------------------------------------------------------#
278 blocks = [[]]
278 blocks = [[]]
279 size_file = 0
279 size_file = 0
280 count_file = 0
280 count_file = 0
281 inter_num = 0
281 inter_num = 0
282 for value in total_list:
282 for value in total_list:
283 if value['size'] > 1024 * 1024 * float(max_size):
283 if value['size'] > 1024 * 1024 * float(max_size):
284 return 'ERROR:: The size of the "%s" file is %sMB aprox, please change "max_size" value' % (value['name'], str(round(value['size']/(1024 * 1024), 2)))
284 return 'ERROR:: The size of the "%s" file is %sMB aprox, please change "max_size" value' % (value['name'], str(round(value['size']/(1024 * 1024), 2)))
285 if not 1 <= int(max_count) <= 999:
285 if not 1 <= int(max_count) <= 999:
286 return 'ERROR:: The count of the number of files must be between 1 and 999, please change "max_count" value'
286 return 'ERROR:: The count of the number of files must be between 1 and 999, please change "max_count" value'
287
287
288 size_file = size_file + value['size']
288 size_file = size_file + value['size']
289 count_file = count_file + 1
289 count_file = count_file + 1
290 if size_file <= 1024 * 1024 * float(max_size) and count_file <= int(max_count):
290 if size_file <= 1024 * 1024 * float(max_size) and count_file <= int(max_count):
291 del value['size']
291 del value['size']
292 blocks[inter_num].append(value)
292 blocks[inter_num].append(value)
293 else:
293 else:
294 inter_num = inter_num + 1
294 inter_num = inter_num + 1
295 size_file = value['size']
295 size_file = value['size']
296 count_file = 1
296 count_file = 1
297 blocks.append([])
297 blocks.append([])
298 del value['size']
298 del value['size']
299 blocks[inter_num].append(value)
299 blocks[inter_num].append(value)
300 #------------------------------------------------------------#
300 #------------------------------------------------------------#
301 if len(blocks[0]) > 0:
301 if len(blocks[0]) > 0:
302 print('BLOCK(S) IN TOTAL:: {}'.format(len(blocks)))
302 print('BLOCK(S) IN TOTAL:: {}'.format(len(blocks)))
303 for count1, block in enumerate(blocks):
303 for count1, block in enumerate(blocks):
304 print('---- BLOCK N°{} ----'.format(count1 + 1))
304 print('---- BLOCK N°{} ----'.format(count1 + 1))
305 resource_extend = []
305 resource_extend = []
306 files_dict = {}
306 files_dict = {}
307 for count2, value2 in enumerate(block):
307 for count2, value2 in enumerate(block):
308 value2['file_date'] = file_date
308 value2['file_date'] = file_date
309 value2['voc_file_type'] = file_type
309 value2['voc_file_type'] = file_type
310 value2.update(self.dict)
310 value2.update(self.dict)
311
311
312 #if not 'format' in value2:
312 #if not 'format' in value2:
313 # format = ''.join(pathlib.Path(value2['name']).suffixes)
313 # format = ''.join(pathlib.Path(value2['name']).suffixes)
314 # if len(format) > 0:
314 # if len(format) > 0:
315 # value2['format'] = format.upper()[1:]
315 # value2['format'] = format.upper()[1:]
316
316
317 files_dict['update__resources__-'+ str(len(block)-count2) +'__upload'] = (value2['name'], value2['upload'])
317 files_dict['update__resources__-'+ str(len(block)-count2) +'__upload'] = (value2['name'], value2['upload'])
318 del value2['upload']
318 del value2['upload']
319 resource_extend.append(value2)
319 resource_extend.append(value2)
320
320
321 print('BLOCK N°{} :: "{}" file(s) found >> uploading'.format(count1 + 1, len(block)))
321 print('BLOCK N°{} :: "{}" file(s) found >> uploading'.format(count1 + 1, len(block)))
322 try:
322 try:
323 result = self.ckan.call_action(
323 result = self.ckan.call_action(
324 'package_revise',
324 'package_revise',
325 {'match': '{'+ str(package_id_or_name) +'}', 'update__resources__extend': json.dumps(resource_extend)},
325 {'match': '{'+ str(package_id_or_name) +'}', 'update__resources__extend': json.dumps(resource_extend)},
326 files=files_dict
326 files=files_dict
327 )
327 )
328 print('BLOCK N°{} :: Uploaded file(s) successfully'.format(count1 + 1))
328 print('BLOCK N°{} :: Uploaded file(s) successfully'.format(count1 + 1))
329 if len(blocks) == count1 + 1:
329 if len(blocks) == count1 + 1:
330 return result
330 return result
331 except:
331 except:
332 print('ERROR :: Use the "print" for more information')
332 print('ERROR :: Use the "print" for more information')
333 _, exc_value, _ = sys.exc_info()
333 _, exc_value, _ = sys.exc_info()
334 return exc_value
334 return exc_value
335 else:
335 else:
336 return "ERROR:: No file(s) found to upload"
336 return "ERROR:: No file(s) found to upload"
337
337
338 def upload_multiple_files(self, dataset_id, path_files, date_files, type_files, ignore_repetition=False, **kwargs):
338 def upload_multiple_files(self, dataset_id, path_files, date_files, type_files, ignore_repetition=False, **kwargs):
339 # Agregar si es interruptido por teclado
339 # Agregar si es interruptido por teclado
340 '''
340 '''
341 FINALIDAD:
341 FINALIDAD:
342 Funcion para subir multiples archivos al repositorio del ROJ.
342 Funcion para subir multiples archivos al repositorio del ROJ.
343
343
344 PARAMETROS DISPONIBLES:
344 PARAMETROS DISPONIBLES:
345 CONSULTAR: "GUIA DE SCRIPT.pdf"
345 CONSULTAR: "GUIA DE SCRIPT.pdf"
346
346
347 ESTRUCTURA:
347 ESTRUCTURA:
348 <access_name>.upload_multiple_files(dataset_id = <class 'str'>, path_files = <class 'str'> or <class 'list of strings'>, date_files = <class 'str'> or <class 'list of strings'>, type_files = <class 'str'> or <class 'list of strings'>, param_1 = <class 'param_1'>, ...)
348 <access_name>.upload_multiple_files(dataset_id = <class 'str'>, path_files = <class 'str'> or <class 'list of strings'>, date_files = <class 'str'> or <class 'list of strings'>, type_files = <class 'str'> or <class 'list of strings'>, param_1 = <class 'param_1'>, ...)
349 '''
349 '''
350 #-------------------------PACKAGE SHOW-----------------------#
350 #-------------------------PACKAGE SHOW-----------------------#
351 try:
351 try:
352 dataset_show = getattr(self.ckan.action, 'package_show')(id=dataset_id)['resources']
352 dataset_show = getattr(self.ckan.action, 'package_show')(id=dataset_id)['resources']
353 except:
353 except:
354 _, exc_value, _ = sys.exc_info()
354 _, exc_value, _ = sys.exc_info()
355 print('ERROR obtaining metadata dataset:: Use the "print" for more information')
355 print('ERROR obtaining metadata dataset:: Use the "print" for more information')
356 return exc_value
356 return exc_value
357 #------------------------------------------------------------#
357 #------------------------------------------------------------#
358 resources_name = []
358 resources_name = []
359 for u in dataset_show:
359 for u in dataset_show:
360 resources_name.append(u['name'].lower())
360 resources_name.append(u['name'].lower())
361 #------------------------------------------------------------#
361 #------------------------------------------------------------#
362
362
363 params_dict = {'upload':[], 'name':[]}
363 params_dict = {'upload':[], 'name':[]}
364 #if not 'format' in kwargs:
364 #if not 'format' in kwargs:
365 # params_dict.update({'format':[]})
365 # params_dict.update({'format':[]})
366 #---------------CASO : "path" or "path_list"-----------------#
366 #---------------CASO : "path" or "path_list"-----------------#
367 if type(path_files) is list:
367 if type(path_files) is list:
368 if len(path_files) != 0:
368 if len(path_files) != 0:
369 path_files.sort()
369 path_files.sort()
370 for u in path_files:
370 for u in path_files:
371 if os.path.isfile(u):
371 if os.path.isfile(u):
372 if os.path.basename(u).lower() in resources_name:
372 if os.path.basename(u).lower() in resources_name:
373 if not ignore_repetition:
373 if not ignore_repetition:
374 return 'ERROR:: "%s" file already exist in this dataset' % (os.path.basename(u))
374 return 'ERROR:: "%s" file already exist in this dataset' % (os.path.basename(u))
375 print('WARRING:: "'+ str(os.path.basename(u)) +'" file was ignored because already exist in this dataset')
375 print('WARRING:: "'+ str(os.path.basename(u)) +'" file was ignored because already exist in this dataset')
376 else:
376 else:
377 params_dict['upload'].append(open(u, 'rb'))
377 params_dict['upload'].append(open(u, 'rb'))
378 params_dict['name'].append(os.path.basename(u))
378 params_dict['name'].append(os.path.basename(u))
379 #if not 'format' in kwargs:
379 #if not 'format' in kwargs:
380 # format = ''.join(pathlib.Path(u).suffixes)
380 # format = ''.join(pathlib.Path(u).suffixes)
381 # if len(format) > 0:
381 # if len(format) > 0:
382 # params_dict['format'].append(format.upper()[1:])
382 # params_dict['format'].append(format.upper()[1:])
383 # else:
383 # else:
384 # params_dict['format'].append('')
384 # params_dict['format'].append('')
385 else:
385 else:
386 return 'File "%s" does not exist' % (u)
386 return 'File "%s" does not exist' % (u)
387 else:
387 else:
388 return 'ERROR:: "path_list is empty"'
388 return 'ERROR:: "path_list is empty"'
389 elif type(path_files) is str:
389 elif type(path_files) is str:
390 if os.path.isdir(path_files):
390 if os.path.isdir(path_files):
391 path_order = [f for f in os.listdir(path_files) if os.path.isfile(os.path.join(path_files, f))]
391 path_order = [f for f in os.listdir(path_files) if os.path.isfile(os.path.join(path_files, f))]
392 path_order.sort()
392 path_order.sort()
393 if path_order:
393 if path_order:
394 for name in path_order:
394 for name in path_order:
395 if name.lower() in resources_name:
395 if name.lower() in resources_name:
396 if not ignore_repetition:
396 if not ignore_repetition:
397 return 'ERROR:: "%s" file already exist in this dataset' % (name)
397 return 'ERROR:: "%s" file already exist in this dataset' % (name)
398 print('WARRING:: "'+ str(name) +'" file was ignored because already exist in this dataset')
398 print('WARRING:: "'+ str(name) +'" file was ignored because already exist in this dataset')
399 else:
399 else:
400 params_dict['upload'].append(open(os.path.join(path_files, name), 'rb'))
400 params_dict['upload'].append(open(os.path.join(path_files, name), 'rb'))
401 params_dict['name'].append(name)
401 params_dict['name'].append(name)
402 #if not 'format' in kwargs:
402 #if not 'format' in kwargs:
403 # format = ''.join(pathlib.Path(name).suffixes)
403 # format = ''.join(pathlib.Path(name).suffixes)
404 # if len(format) > 0:
404 # if len(format) > 0:
405 # params_dict['format'].append(format.upper()[1:])
405 # params_dict['format'].append(format.upper()[1:])
406 # else:
406 # else:
407 # params_dict['format'].append('')
407 # params_dict['format'].append('')
408 else:
408 else:
409 return "ERROR:: There aren't files in this directory"
409 return "ERROR:: There aren't files in this directory"
410 else:
410 else:
411 return 'ERROR:: Directory "%s" does not exist' % (path_files)
411 return 'ERROR:: Directory "%s" does not exist' % (path_files)
412 else:
412 else:
413 return 'ERROR:: "path_files" must be a str or list'
413 return 'ERROR:: "path_files" must be a str or list'
414 #------------------------------------------------------------#
414 #------------------------------------------------------------#
415 params_no_dict = {'package_id': dataset_id}
415 params_no_dict = {'package_id': dataset_id}
416 if type(date_files) is list:
416 if type(date_files) is list:
417 params_dict['file_date'] = date_files
417 params_dict['file_date'] = date_files
418 else:
418 else:
419 params_no_dict['file_date'] = date_files
419 params_no_dict['file_date'] = date_files
420
420
421 if type(type_files) is list:
421 if type(type_files) is list:
422 params_dict['voc_file_type'] = type_files
422 params_dict['voc_file_type'] = type_files
423 else:
423 else:
424 params_no_dict['voc_file_type'] = type_files
424 params_no_dict['voc_file_type'] = type_files
425
425
426 for key1, value1 in kwargs.items():
426 for key1, value1 in kwargs.items():
427 if not key1 in params_dict and not key1 in params_no_dict and key1 != 'others':
427 if not key1 in params_dict and not key1 in params_no_dict and key1 != 'others':
428 if type(value1) is list:
428 if type(value1) is list:
429 params_dict[key1] = value1
429 params_dict[key1] = value1
430 else:
430 else:
431 params_no_dict[key1] = value1
431 params_no_dict[key1] = value1
432 #------------------------------------------#
432 #------------------------------------------#
433 if not 'others' in kwargs:
433 if not 'others' in kwargs:
434 params_no_dict['others'] = ''
434 params_no_dict['others'] = ''
435 else:
435 else:
436 if isinstance(kwargs['others'], tuple):
436 if isinstance(kwargs['others'], tuple):
437 params_dict['others'] = [json.dumps(w) for w in kwargs['others']]
437 params_dict['others'] = [json.dumps(w) for w in kwargs['others']]
438 elif isinstance(kwargs['others'], list):
438 elif isinstance(kwargs['others'], list):
439 params_no_dict['others'] = json.dumps(kwargs['others'])
439 params_no_dict['others'] = json.dumps(kwargs['others'])
440 elif isinstance(kwargs['others'], str):
440 elif isinstance(kwargs['others'], str):
441 params_no_dict['others'] = kwargs['others']
441 params_no_dict['others'] = kwargs['others']
442 else:
442 else:
443 return 'ERROR:: "others" must be a tuple, list or str'
443 return 'ERROR:: "others" must be a tuple, list or str'
444 #------------------------------------------#
444 #------------------------------------------#
445 len_params_dict = []
445 len_params_dict = []
446 for value2 in params_dict.values():
446 for value2 in params_dict.values():
447 len_params_dict.append(len(value2))
447 len_params_dict.append(len(value2))
448
448
449 if len(list(set(len_params_dict))) > 1:
449 if len(list(set(len_params_dict))) > 1:
450 return 'ERROR:: All lists must be the same length: %s' % (len(params_dict['name']))
450 return 'ERROR:: All lists must be the same length: %s' % (len(params_dict['name']))
451 #------------------------------------------------------------#
451 #------------------------------------------------------------#
452 print('"{}" file(s) found >> uploading'.format(len(params_dict['name'])))
452 print('"{}" file(s) found >> uploading'.format(len(params_dict['name'])))
453 for v in range(len(params_dict['name'])):
453 for v in range(len(params_dict['name'])):
454 try:
454 try:
455 send = {}
455 send = {}
456 for key_dict, value_dict in params_dict.items():
456 for key_dict, value_dict in params_dict.items():
457 send[key_dict] = value_dict[v]
457 send[key_dict] = value_dict[v]
458 for key_no_dict, value_no_dict in params_no_dict.items():
458 for key_no_dict, value_no_dict in params_no_dict.items():
459 send[key_no_dict] = value_no_dict
459 send[key_no_dict] = value_no_dict
460
460
461 self.list.append(getattr(self.ckan.action, 'resource_create')(**send))
461 self.list.append(getattr(self.ckan.action, 'resource_create')(**send))
462 print('File #{} :: "{}" was uploaded successfully'.format(v+1, params_dict['name'][v]))
462 print('File #{} :: "{}" was uploaded successfully'.format(v+1, params_dict['name'][v]))
463 except:
463 except:
464 _, exc_value, _ = sys.exc_info()
464 _, exc_value, _ = sys.exc_info()
465 self.list.append(exc_value)
465 self.list.append(exc_value)
466 print('File #{} :: Error uploading "{}" file'.format(v+1, params_dict['name'][v]))
466 print('File #{} :: Error uploading "{}" file'.format(v+1, params_dict['name'][v]))
467 return self.list
467 return self.list
468 #------------------------------------------------------------#
468 #------------------------------------------------------------#
469
469
470 def show(self, type_option, id, **kwargs):
470 def show(self, type_option, id, **kwargs):
471 '''
471 '''
472 FINALIDAD:
472 FINALIDAD:
473 Funcion personalizada para una busqueda en especifico.
473 Funcion personalizada para una busqueda en especifico.
474
474
475 PARAMETROS DISPONIBLES:
475 PARAMETROS DISPONIBLES:
476 CONSULTAR: "GUIA DE SCRIPT.pdf"
476 CONSULTAR: "GUIA DE SCRIPT.pdf"
477
477
478 ESTRUCTURA:
478 ESTRUCTURA:
479 <access_name>.show(type_option = <class 'str'>, id = <class 'str'>, param_1 = <class 'param_1'>, ...)
479 <access_name>.show(type_option = <class 'str'>, id = <class 'str'>, param_1 = <class 'param_1'>, ...)
480 '''
480 '''
481 if type(type_option) is str:
481 if type(type_option) is str:
482 try:
482 try:
483 if type_option == 'dataset':
483 if type_option == 'dataset':
484 return getattr(self.ckan.action, 'package_show')(id=id, **kwargs)
484 return getattr(self.ckan.action, 'package_show')(id=id, **kwargs)
485 elif type_option == 'resource':
485 elif type_option == 'resource':
486 return getattr(self.ckan.action, 'resource_show')(id=id, **kwargs)
486 return getattr(self.ckan.action, 'resource_show')(id=id, **kwargs)
487 elif type_option == 'project':
487 elif type_option == 'project':
488 return getattr(self.ckan.action, 'organization_show')(id=id, **kwargs)
488 return getattr(self.ckan.action, 'organization_show')(id=id, **kwargs)
489 elif type_option == 'collaborator':
489 elif type_option == 'collaborator':
490 return getattr(self.ckan.action, 'package_collaborator_list_for_user')(id=id, **kwargs)
490 return getattr(self.ckan.action, 'package_collaborator_list_for_user')(id=id, **kwargs)
491 elif type_option == 'member':
491 elif type_option == 'member':
492 return getattr(self.ckan.action, 'organization_list_for_user')(id=id, **kwargs)
492 return getattr(self.ckan.action, 'organization_list_for_user')(id=id, **kwargs)
493 elif type_option == 'vocabulary':
493 elif type_option == 'vocabulary':
494 return getattr(self.ckan.action, 'vocabulary_show')(id=id, **kwargs)
494 return getattr(self.ckan.action, 'vocabulary_show')(id=id, **kwargs)
495 elif type_option == 'tag':
495 elif type_option == 'tag':
496 if not 'vocabulary_id' in kwargs:
496 if not 'vocabulary_id' in kwargs:
497 print('Missing "vocabulary_id" value: assume it is a free tag')
497 print('Missing "vocabulary_id" value: assume it is a free tag')
498 return getattr(self.ckan.action, 'tag_show')(id=id, **kwargs)
498 return getattr(self.ckan.action, 'tag_show')(id=id, **kwargs)
499 elif type_option == 'user':
499 elif type_option == 'user':
500 return getattr(self.ckan.action, 'user_show')(id=id, **kwargs)
500 return getattr(self.ckan.action, 'user_show')(id=id, **kwargs)
501 elif type_option == 'job':
501 elif type_option == 'job':
502 return getattr(self.ckan.action, 'job_show')(id=id, **kwargs)
502 return getattr(self.ckan.action, 'job_show')(id=id, **kwargs)
503 else:
503 else:
504 return 'ERROR:: "type_option = %s" is not accepted' % (type_option)
504 return 'ERROR:: "type_option = %s" is not accepted' % (type_option)
505 except:
505 except:
506 _, exc_value, _ = sys.exc_info()
506 _, exc_value, _ = sys.exc_info()
507 return exc_value
507 return exc_value
508 else:
508 else:
509 return 'ERROR:: "type_option" must be a str'
509 return 'ERROR:: "type_option" must be a str'
510
510
511 def search(self, type_option, query=None, **kwargs):
511 def search(self, type_option, query=None, **kwargs):
512 '''
512 '''
513 FINALIDAD:
513 FINALIDAD:
514 Funcion personalizada para busquedas que satisfagan algun criterio.
514 Funcion personalizada para busquedas que satisfagan algun criterio.
515
515
516 PARAMETROS DISPONIBLES:
516 PARAMETROS DISPONIBLES:
517 CONSULTAR: "GUIA DE SCRIPT.pdf"
517 CONSULTAR: "GUIA DE SCRIPT.pdf"
518
518
519 ESTRUCTURA:
519 ESTRUCTURA:
520 <access_name>.search(type_option = <class 'str'>, query = <class 'dict'>, param_1 = <class 'param_1'>, ...)
520 <access_name>.search(type_option = <class 'str'>, query = <class 'dict'>, param_1 = <class 'param_1'>, ...)
521 '''
521 '''
522 if type(type_option) is str:
522 if type(type_option) is str:
523 try:
523 try:
524 if type_option == 'dataset':
524 if type_option == 'dataset':
525 key_replace = ['fq', 'fq_list', 'include_private']
525 key_replace = ['fq', 'fq_list', 'include_private']
526 key_point = ['facet_mincount', 'facet_limit', 'facet_field']
526 key_point = ['facet_mincount', 'facet_limit', 'facet_field']
527 for key1, value1 in kwargs.items():
527 for key1, value1 in kwargs.items():
528 if not key1 in key_replace:
528 if not key1 in key_replace:
529 if key1 in key_point:
529 if key1 in key_point:
530 self.dict[key1.replace('_', '.')] = value1
530 self.dict[key1.replace('_', '.')] = value1
531 else:
531 else:
532 self.dict[key1] = value1
532 self.dict[key1] = value1
533
533
534 if query is not None:
534 if query is not None:
535 if type(query) is dict:
535 if type(query) is dict:
536 self.dict['fq_list'] = []
536 self.dict['fq_list'] = []
537 #NUM_RESOURCES_MIN / NUM_RESOURCES_MAX
537 #NUM_RESOURCES_MIN / NUM_RESOURCES_MAX
538 #----------------------------------------------------#
538 #----------------------------------------------------#
539 if 'dataset_start_date' in query:
539 if 'dataset_start_date' in query:
540 if type(query['dataset_start_date']) is str:
540 if type(query['dataset_start_date']) is str:
541 try:
541 try:
542 datetime.strptime(query['dataset_start_date'], '%Y-%m-%d')
542 datetime.strptime(query['dataset_start_date'], '%Y-%m-%d')
543 if len(query['dataset_start_date']) != 10:
543 if len(query['dataset_start_date']) != 10:
544 return '"dataset_start_date", must be: <YYYY-MM-DD>'
544 return '"dataset_start_date", must be: <YYYY-MM-DD>'
545 self.dict['fq_list'].append('dataset_start_date:"'+query['dataset_start_date']+'"')
545 self.dict['fq_list'].append('dataset_start_date:"'+query['dataset_start_date']+'"')
546 self.list.append('dataset_start_date')
546 self.list.append('dataset_start_date')
547 except:
547 except:
548 return '"dataset_start_date" incorrect: "%s"' % (query['dataset_start_date'])
548 return '"dataset_start_date" incorrect: "%s"' % (query['dataset_start_date'])
549 else:
549 else:
550 return '"dataset_start_date" must be <str>'
550 return '"dataset_start_date" must be <str>'
551 #----------------------------------------------------#
551 #----------------------------------------------------#
552 if 'dataset_end_date' in query:
552 if 'dataset_end_date' in query:
553 if type(query['dataset_end_date']) is str:
553 if type(query['dataset_end_date']) is str:
554 try:
554 try:
555 datetime.strptime(query['dataset_end_date'], '%Y-%m-%d')
555 datetime.strptime(query['dataset_end_date'], '%Y-%m-%d')
556 if len(query['dataset_end_date']) != 10:
556 if len(query['dataset_end_date']) != 10:
557 return '"dataset_end_date", must be: <YYYY-MM-DD>'
557 return '"dataset_end_date", must be: <YYYY-MM-DD>'
558
558
559 if 'dataset_start_date' in query:
559 if 'dataset_start_date' in query:
560 if query['dataset_start_date'] > query['dataset_end_date']:
560 if query['dataset_start_date'] > query['dataset_end_date']:
561 return '"dataset_end_date" must be greater than "dataset_start_date"'
561 return '"dataset_end_date" must be greater than "dataset_start_date"'
562
562
563 self.dict['fq_list'].append('dataset_end_date:"'+query['dataset_end_date']+'"')
563 self.dict['fq_list'].append('dataset_end_date:"'+query['dataset_end_date']+'"')
564 self.list.append('dataset_end_date')
564 self.list.append('dataset_end_date')
565 except:
565 except:
566 return '"dataset_end_date" incorrect: "%s"' % (query['dataset_end_date'])
566 return '"dataset_end_date" incorrect: "%s"' % (query['dataset_end_date'])
567 else:
567 else:
568 return '"dataset_end_date" must be <str>'
568 return '"dataset_end_date" must be <str>'
569 #----------------------------------------------------#
569 #----------------------------------------------------#
570 for key, value in query.items():
570 for key, value in query.items():
571 if value is not None and not key in self.list:
571 if value is not None and not key in self.list:
572 self.dict['fq_list'].append(str(key)+':"'+str(value)+'"')
572 self.dict['fq_list'].append(str(key)+':"'+str(value)+'"')
573 else:
573 else:
574 return '"query" must be <dict>'
574 return '"query" must be <dict>'
575
575
576 return getattr(self.ckan.action, 'package_search')(include_private=True, **self.dict)
576 return getattr(self.ckan.action, 'package_search')(include_private=True, **self.dict)
577
577
578 elif type_option == 'resource':
578 elif type_option == 'resource':
579 for key1, value1 in kwargs.items():
579 for key1, value1 in kwargs.items():
580 if key1 != 'fields':
580 if key1 != 'fields':
581 self.dict[key1] = value1
581 self.dict[key1] = value1
582
582
583 if query is not None:
583 if query is not None:
584 if type(query) is dict:
584 if type(query) is dict:
585 #----------------------------------------------------#
585 #----------------------------------------------------#
586 if 'file_date_min' in query:
586 if 'file_date_min' in query:
587 if type(query['file_date_min']) is str:
587 if type(query['file_date_min']) is str:
588 try:
588 try:
589 datetime.strptime(query['file_date_min'], '%Y-%m-%d')
589 datetime.strptime(query['file_date_min'], '%Y-%m-%d')
590 if len(query['file_date_min']) != 10:
590 if len(query['file_date_min']) != 10:
591 return '"file_date_min", must be: <YYYY-MM-DD>'
591 return '"file_date_min", must be: <YYYY-MM-DD>'
592 except:
592 except:
593 return '"file_date_min" incorrect: "%s"' % (query['file_date_min'])
593 return '"file_date_min" incorrect: "%s"' % (query['file_date_min'])
594 else:
594 else:
595 return '"file_date_min" must be <str>'
595 return '"file_date_min" must be <str>'
596 #----------------------------------------------------#
596 #----------------------------------------------------#
597 if 'file_date_max' in query:
597 if 'file_date_max' in query:
598 if type(query['file_date_max']) is str:
598 if type(query['file_date_max']) is str:
599 try:
599 try:
600 datetime.strptime(query['file_date_max'], '%Y-%m-%d')
600 datetime.strptime(query['file_date_max'], '%Y-%m-%d')
601 if len(query['file_date_max']) != 10:
601 if len(query['file_date_max']) != 10:
602 return '"file_date_max", must be: <YYYY-MM-DD>'
602 return '"file_date_max", must be: <YYYY-MM-DD>'
603
603
604 if 'file_date_min' in query:
604 if 'file_date_min' in query:
605 if query['file_date_min'] > query['file_date_max']:
605 if query['file_date_min'] > query['file_date_max']:
606 return '"file_date_max" must be greater than "file_date_min"'
606 return '"file_date_max" must be greater than "file_date_min"'
607 except:
607 except:
608 return '"file_date_max" incorrect: "%s"' % (query['file_date_max'])
608 return '"file_date_max" incorrect: "%s"' % (query['file_date_max'])
609 else:
609 else:
610 return '"file_date_max" must be <str>'
610 return '"file_date_max" must be <str>'
611 #----------------------------------------------------#
611 #----------------------------------------------------#
612 self.dict['query'] = query
612 self.dict['query'] = query
613 else:
613 else:
614 return '"query" must be <dict>'
614 return '"query" must be <dict>'
615 return getattr(self.ckan.action, 'resources_search')(**self.dict)
615 return getattr(self.ckan.action, 'resources_search')(**self.dict)
616
616
617 elif type_option == 'tag':
617 elif type_option == 'tag':
618 for key1, value1 in kwargs.items():
618 for key1, value1 in kwargs.items():
619 if key1 != 'fields':
619 if key1 != 'fields':
620 self.dict[key1] = value1
620 self.dict[key1] = value1
621
621
622 if not 'vocabulary_id' in kwargs:
622 if not 'vocabulary_id' in kwargs:
623 print('Missing "vocabulary_id" value: tags that don’t belong to any vocabulary')
623 print('Missing "vocabulary_id" value: tags that don’t belong to any vocabulary')
624 else:
624 else:
625 print('Only tags that belong to "{}" vocabulary'.format(kwargs['vocabulary_id']))
625 print('Only tags that belong to "{}" vocabulary'.format(kwargs['vocabulary_id']))
626
626
627 if query is not None:
627 if query is not None:
628 if type(query) is dict:
628 if type(query) is dict:
629 if 'search' in query:
629 if 'search' in query:
630 if type(query['search']) is list or type(query['search']) is str:
630 if type(query['search']) is list or type(query['search']) is str:
631 self.dict['query'] = query['search']
631 self.dict['query'] = query['search']
632 else:
632 else:
633 return '"search" must be <list> or <str>'
633 return '"search" must be <list> or <str>'
634 else:
634 else:
635 return '"query" must be <dict>'
635 return '"query" must be <dict>'
636 return getattr(self.ckan.action, 'tag_search')(**self.dict)
636 return getattr(self.ckan.action, 'tag_search')(**self.dict)
637
637
638 else:
638 else:
639 return 'ERROR:: "type_option = %s" is not accepted' % (type_option)
639 return 'ERROR:: "type_option = %s" is not accepted' % (type_option)
640
640
641 except:
641 except:
642 _, exc_value, _ = sys.exc_info()
642 _, exc_value, _ = sys.exc_info()
643 return exc_value
643 return exc_value
644 else:
644 else:
645 return 'ERROR:: "type_option" must be <str>'
645 return 'ERROR:: "type_option" must be <str>'
646
646
647 def create(self, type_option, select=None, **kwargs):
647 def create(self, type_option, select=None, **kwargs):
648 '''
648 '''
649 FINALIDAD:
649 FINALIDAD:
650 Funcion personalizada para crear.
650 Funcion personalizada para crear.
651
651
652 PARAMETROS DISPONIBLES:
652 PARAMETROS DISPONIBLES:
653 CONSULTAR: "GUIA DE SCRIPT.pdf"
653 CONSULTAR: "GUIA DE SCRIPT.pdf"
654
654
655 ESTRUCTURA:
655 ESTRUCTURA:
656 <access_name>.create(type_option = <class 'str'>, param_1 = <class 'param_1'>, ...)
656 <access_name>.create(type_option = <class 'str'>, param_1 = <class 'param_1'>, ...)
657 '''
657 '''
658 if type(type_option) is str:
658 if type(type_option) is str:
659 try:
659 try:
660 if type_option == 'dataset':
660 if type_option == 'dataset':
661 return getattr(self.ckan.action, 'package_create')(**kwargs)
661 return getattr(self.ckan.action, 'package_create')(**kwargs)
662 if type_option == 'resource':
662 if type_option == 'resource':
663 return resource.resource_create(self, **kwargs)
663 return resource.resource_create(self, **kwargs)
664 elif type_option == 'project':
664 elif type_option == 'project':
665 return getattr(self.ckan.action, 'organization_create')(**kwargs)
665 return getattr(self.ckan.action, 'organization_create')(**kwargs)
666 elif type_option == 'member':
666 elif type_option == 'member':
667 return getattr(self.ckan.action, 'organization_member_create')(**kwargs)
667 return getattr(self.ckan.action, 'organization_member_create')(**kwargs)
668 elif type_option == 'collaborator':
668 elif type_option == 'collaborator':
669 return getattr(self.ckan.action, 'package_collaborator_create')(**kwargs)
669 return getattr(self.ckan.action, 'package_collaborator_create')(**kwargs)
670 elif type_option == 'vocabulary':
670 elif type_option == 'vocabulary':
671 return getattr(self.ckan.action, 'vocabulary_create')(**kwargs)
671 return getattr(self.ckan.action, 'vocabulary_create')(**kwargs)
672 elif type_option == 'tag':
672 elif type_option == 'tag':
673 return getattr(self.ckan.action, 'tag_create')(**kwargs)
673 return getattr(self.ckan.action, 'tag_create')(**kwargs)
674 elif type_option == 'user':
674 elif type_option == 'user':
675 return getattr(self.ckan.action, 'user_create')(**kwargs)
675 return getattr(self.ckan.action, 'user_create')(**kwargs)
676 elif type_option == 'views':
676 elif type_option == 'views':
677 if 'resource' == select:
677 if 'resource' == select:
678 self.list = ['package']
678 self.list = ['package']
679 for key1, value1 in kwargs.items():
679 for key1, value1 in kwargs.items():
680 if not key1 in self.list:
680 if not key1 in self.list:
681 self.dict[key1] = value1
681 self.dict[key1] = value1
682 return getattr(self.ckan.action, 'resource_create_default_resource_views')(**self.dict)
682 return getattr(self.ckan.action, 'resource_create_default_resource_views')(**self.dict)
683 elif 'dataset' == select:
683 elif 'dataset' == select:
684 return getattr(self.ckan.action, 'package_create_default_resource_views')(**kwargs)
684 return getattr(self.ckan.action, 'package_create_default_resource_views')(**kwargs)
685 else:
685 else:
686 return 'ERROR:: "select = %s" is not accepted' % (select)
686 return 'ERROR:: "select = %s" is not accepted' % (select)
687 else:
687 else:
688 return 'ERROR:: "type_option = %s" is not accepted' % (type_option)
688 return 'ERROR:: "type_option = %s" is not accepted' % (type_option)
689 except:
689 except:
690 _, exc_value, _ = sys.exc_info()
690 _, exc_value, _ = sys.exc_info()
691 return exc_value
691 return exc_value
692 else:
692 else:
693 return 'ERROR:: "type_option" must be <str>'
693 return 'ERROR:: "type_option" must be <str>'
694
694
695 def patch(self, type_option, **kwargs):
695 def patch(self, type_option, **kwargs):
696 '''
696 '''
697 FINALIDAD:
697 FINALIDAD:
698 Funciones personalizadas para actualizar
698 Funciones personalizadas para actualizar
699
699
700 PARAMETROS DISPONIBLES:
700 PARAMETROS DISPONIBLES:
701 CONSULTAR: "GUIA DE SCRIPT.pdf"
701 CONSULTAR: "GUIA DE SCRIPT.pdf"
702
702
703 ESTRUCTURA:
703 ESTRUCTURA:
704 <access_name>.patch(type_option = <class 'str'>, param_1 = <class 'param_1'>, ...)
704 <access_name>.patch(type_option = <class 'str'>, param_1 = <class 'param_1'>, ...)
705 '''
705 '''
706 if type(type_option) is str:
706 if type(type_option) is str:
707 try:
707 try:
708 if type_option == 'dataset':
708 if type_option == 'dataset':
709 #Agregar que solo se debe modificar parámetros del Dataset y que no incluya Resources
709 #Agregar que solo se debe modificar parámetros del Dataset y que no incluya Resources
710 return getattr(self.ckan.action, 'package_patch')(**kwargs)
710 return getattr(self.ckan.action, 'package_patch')(**kwargs)
711 elif type_option == 'project':
711 elif type_option == 'project':
712 return getattr(self.ckan.action, 'organization_patch')(**kwargs)
712 return getattr(self.ckan.action, 'organization_patch')(**kwargs)
713 elif type_option == 'resource':
713 elif type_option == 'resource':
714 return resource.resource_patch(self, **kwargs)
714 return resource.resource_patch(self, **kwargs)
715 elif type_option == 'member':
715 elif type_option == 'member':
716 return getattr(self.ckan.action, 'organization_member_create')(**kwargs)
716 return getattr(self.ckan.action, 'organization_member_create')(**kwargs)
717 elif type_option == 'collaborator':
717 elif type_option == 'collaborator':
718 return getattr(self.ckan.action, 'package_collaborator_create')(**kwargs)
718 return getattr(self.ckan.action, 'package_collaborator_create')(**kwargs)
719 else:
719 else:
720 return 'ERROR:: "type_option = %s" is not accepted' % (type_option)
720 return 'ERROR:: "type_option = %s" is not accepted' % (type_option)
721 except:
721 except:
722 _, exc_value, _ = sys.exc_info()
722 _, exc_value, _ = sys.exc_info()
723 return exc_value
723 return exc_value
724 else:
724 else:
725 return 'ERROR:: "type_option" must be <str>'
725 return 'ERROR:: "type_option" must be <str>'
726
726
727 def delete(self, type_option, select=None, **kwargs):
727 def delete(self, type_option, select=None, **kwargs):
728 '''
728 '''
729 FINALIDAD:
729 FINALIDAD:
730 Función personalizada para eliminar y/o purgar.
730 Función personalizada para eliminar y/o purgar.
731
731
732 PARAMETROS DISPONIBLES:
732 PARAMETROS DISPONIBLES:
733 CONSULTAR: "GUIA DE SCRIPT.pdf"
733 CONSULTAR: "GUIA DE SCRIPT.pdf"
734
734
735 ESTRUCTURA:
735 ESTRUCTURA:
736 <access_name>.delete(type_option = <class 'str'>, param_1 = <class 'param_1'>, ...)
736 <access_name>.delete(type_option = <class 'str'>, param_1 = <class 'param_1'>, ...)
737 '''
737 '''
738 if type(type_option) is str:
738 if type(type_option) is str:
739 try:
739 try:
740 if type_option == 'dataset':
740 if type_option == 'dataset':
741 if select is None:
741 if select is None:
742 return 'ERROR:: "select" must not be "None"'
742 return 'ERROR:: "select" must not be "None"'
743 else:
743 else:
744 if 'delete' == select:
744 if 'delete' == select:
745 return getattr(self.ckan.action, 'package_delete')(**kwargs)
745 return getattr(self.ckan.action, 'package_delete')(**kwargs)
746 elif 'purge' == select:
746 elif 'purge' == select:
747 return getattr(self.ckan.action, 'dataset_purge')(**kwargs)
747 return getattr(self.ckan.action, 'dataset_purge')(**kwargs)
748 else:
748 else:
749 return 'ERROR:: "select = %s" is not accepted' % (select)
749 return 'ERROR:: "select = %s" is not accepted' % (select)
750 elif type_option == 'project':
750 elif type_option == 'project':
751 if select is None:
751 if select is None:
752 return 'ERROR:: "select" must not be "None"'
752 return 'ERROR:: "select" must not be "None"'
753 else:
753 else:
754 if 'delete' == select:
754 if 'delete' == select:
755 return getattr(self.ckan.action, 'organization_delete')(**kwargs)
755 return getattr(self.ckan.action, 'organization_delete')(**kwargs)
756 elif 'purge' == select:
756 elif 'purge' == select:
757 return getattr(self.ckan.action, 'organization_purge')(**kwargs)
757 return getattr(self.ckan.action, 'organization_purge')(**kwargs)
758 else:
758 else:
759 return 'ERROR:: "select = %s" is not accepted' % (select)
759 return 'ERROR:: "select = %s" is not accepted' % (select)
760 elif type_option == 'resource':
760 elif type_option == 'resource':
761 if select is None:
761 if select is None:
762 return 'ERROR:: "select" must not be "None"'
762 return 'ERROR:: "select" must not be "None"'
763 else:
763 else:
764 return resource.resource_delete(self, select, **kwargs)
764 return resource.resource_delete(self, select, **kwargs)
765 elif type_option == 'vocabulary':
765 elif type_option == 'vocabulary':
766 return getattr(self.ckan.action, 'vocabulary_delete')(**kwargs)
766 return getattr(self.ckan.action, 'vocabulary_delete')(**kwargs)
767 elif type_option == 'tag':
767 elif type_option == 'tag':
768 return getattr(self.ckan.action, 'tag_delete')(**kwargs)
768 return getattr(self.ckan.action, 'tag_delete')(**kwargs)
769 elif type_option == 'user':
769 elif type_option == 'user':
770 return getattr(self.ckan.action, 'user_delete')(**kwargs)
770 return getattr(self.ckan.action, 'user_delete')(**kwargs)
771 else:
771 else:
772 return 'ERROR:: "type_option = %s" is not accepted' % (type_option)
772 return 'ERROR:: "type_option = %s" is not accepted' % (type_option)
773 except:
773 except:
774 _, exc_value, _ = sys.exc_info()
774 _, exc_value, _ = sys.exc_info()
775 return exc_value
775 return exc_value
776 else:
776 else:
777 return 'ERROR:: "type_option" must be <str>'
777 return 'ERROR:: "type_option" must be <str>'
778
778
779 def f_status_note(self, total, result, path):
779 def f_status_note(self, total, result, path):
780 file_txt = open(path+'status_note.txt', 'w')
780 file_txt = open(path+'status_note.txt', 'w')
781 file_txt = open(path+'status_note.txt', 'a')
781 file_txt = open(path+'status_note.txt', 'a')
782
782
783 file_txt.write('DOWNLOADED FILE(S): "%s"' % (len(result['name'])))
783 file_txt.write('DOWNLOADED FILE(S): "%s"' % (len(result['name'])))
784 file_txt.write(''+ os.linesep)
784 file_txt.write(''+ os.linesep)
785 for u in result['name']:
785 for u in result['name']:
786 file_txt.write(' - '+ u + os.linesep)
786 file_txt.write(' - '+ u + os.linesep)
787 file_txt.write(''+ os.linesep)
787 file_txt.write(''+ os.linesep)
788
788
789 file_txt.write('FAILED FILE(S): "%s"' % (len(total['name'])-len(result['name'])))
789 file_txt.write('FAILED FILE(S): "%s"' % (len(total['name'])-len(result['name'])))
790 file_txt.write(''+ os.linesep)
790 file_txt.write(''+ os.linesep)
791 if len(total['name'])-len(result['name']) != 0:
791 if len(total['name'])-len(result['name']) != 0:
792 for u in total['name']:
792 for u in total['name']:
793 if not u in result['name']:
793 if not u in result['name']:
794 file_txt.write(' - '+ u + os.linesep)
794 file_txt.write(' - '+ u + os.linesep)
795 else:
795 else:
796 file_txt.write(' "None"'+ os.linesep)
796 file_txt.write(' "None"'+ os.linesep)
797
797
798 def f_name(self, name_dataset, ext, tempdir):
798 def f_name(self, name_dataset, ext, tempdir):
799 while self.check:
799 while self.check:
800 self.str = ''
800 self.str = ''
801 if self.cont == 0:
801 if self.cont == 0:
802 if os.path.exists(tempdir + name_dataset + ext):
802 if os.path.exists(tempdir + name_dataset + ext):
803 self.str = name_dataset+'('+str(self.cont+1)+')'+ext
803 self.str = name_dataset+'('+str(self.cont+1)+')'+ext
804 else:
804 else:
805 self.check = self.check * 0
805 self.check = self.check * 0
806 self.str = name_dataset + ext
806 self.str = name_dataset + ext
807 else:
807 else:
808 if not os.path.exists(tempdir + name_dataset+'('+str(self.cont)+')'+ext):
808 if not os.path.exists(tempdir + name_dataset+'('+str(self.cont)+')'+ext):
809 self.check = self.check * 0
809 self.check = self.check * 0
810 self.str = name_dataset+'('+str(self.cont)+')'+ ext
810 self.str = name_dataset+'('+str(self.cont)+')'+ ext
811 self.cont = self.cont+1
811 self.cont = self.cont+1
812 return self.str
812 return self.str
813
813
814 def f_zipdir(self, path, ziph, zip_name):
814 def f_zipdir(self, path, ziph, zip_name):
815 for root, _, files in os.walk(path):
815 for root, _, files in os.walk(path):
816 print('.....')
816 print('.....')
817 print('Creating: "{}" >>'.format(zip_name))
817 print('Creating: "{}" >>'.format(zip_name))
818 for __file in tqdm(iterable=files, total=len(files)):
818 for __file in tqdm(iterable=files, total=len(files)):
819 new_dir = os.path.relpath(os.path.join(root, __file), os.path.join(path, '..'))
819 new_dir = os.path.relpath(os.path.join(root, __file), os.path.join(path, '..'))
820 ziph.write(os.path.join(root, __file), new_dir)
820 ziph.write(os.path.join(root, __file), new_dir)
821 print('Created >>')
821 print('Created >>')
822
822
823 def download_by_step(self, response, tempdir_name):
823 def download_by_step(self, response, tempdir_name):
824 try:
824 try:
825 # ---------- REPLACE URL --------- #
825 # ---------- REPLACE URL --------- #
826 if urlparse(self.url).netloc != 'www.igp.gob.pe' and urlparse(response['url']).netloc == 'www.igp.gob.pe':
826 if urlparse(self.url).netloc != 'www.igp.gob.pe' and urlparse(response['url']).netloc == 'www.igp.gob.pe':
827 response['url'] = response['url'].replace(urlparse(response['url']).scheme + '://' + urlparse(response['url']).netloc,
827 response['url'] = response['url'].replace(urlparse(response['url']).scheme + '://' + urlparse(response['url']).netloc,
828 urlparse(self.url).scheme + '://' + urlparse(self.url).netloc)
828 urlparse(self.url).scheme + '://' + urlparse(self.url).netloc)
829 #----------------------------------#
829 #----------------------------------#
830 with requests.get(response['url'], stream=True, headers={'Authorization': self.Authorization}, verify=self.verify) as resp:
830 with requests.get(response['url'], stream=True, headers={'Authorization': self.Authorization}, verify=self.verify) as resp:
831 if resp.status_code == 200:
831 if resp.status_code == 200:
832 with open(tempdir_name+response['name'], 'wb') as file:
832 with open(tempdir_name+response['name'], 'wb') as file:
833 for chunk in resp.iter_content(chunk_size = self.chunk_size):
833 for chunk in resp.iter_content(chunk_size = self.chunk_size):
834 if chunk:
834 if chunk:
835 file.write(chunk)
835 file.write(chunk)
836 except requests.exceptions.RequestException:
836 except requests.exceptions.RequestException:
837 pass
837 pass
838
838
839 def download_files(self, **kwargs):
839 def download_files(self, **kwargs):
840 '''
840 '''
841 FINALIDAD:
841 FINALIDAD:
842 Funcion personalizada para la descarga de archivos existentes de un dataset.
842 Funcion personalizada para la descarga de archivos existentes de un dataset.
843
843
844 PARAMETROS DISPONIBLES:
844 PARAMETROS DISPONIBLES:
845 CONSULTAR: "GUIA DE SCRIPT.pdf"
845 CONSULTAR: "GUIA DE SCRIPT.pdf"
846
846
847 ESTRUCTURA:
847 ESTRUCTURA:
848 <access_name>.download_files(id = <class 'str'>, param_1 = <class 'param_1'>, ...)
848 <access_name>.download_files(id = <class 'str'>, param_1 = <class 'param_1'>, ...)
849 '''
849 '''
850 dict_local = {}
850 dict_local = {}
851 #----------------------------------------------#
851 #----------------------------------------------#
852 if 'zip' in kwargs:
852 if 'zip' in kwargs:
853 if type(kwargs['zip']) is not bool:
853 if type(kwargs['zip']) is not bool:
854 return 'ERROR:: "zip" must be: <class "bool">'
854 return 'ERROR:: "zip" must be: <class "bool">'
855 else:
855 else:
856 dict_local['zip'] = kwargs['zip']
856 dict_local['zip'] = kwargs['zip']
857 else:
857 else:
858 dict_local['zip'] = False
858 dict_local['zip'] = False
859 #----------------------------------------------#
859 #----------------------------------------------#
860 if 'status_note' in kwargs:
860 if 'status_note' in kwargs:
861 if type(kwargs['status_note']) is not bool:
861 if type(kwargs['status_note']) is not bool:
862 return 'ERROR:: "status_note" must be: <class "bool">'
862 return 'ERROR:: "status_note" must be: <class "bool">'
863 else:
863 else:
864 dict_local['status_note'] = kwargs['status_note']
864 dict_local['status_note'] = kwargs['status_note']
865 else:
865 else:
866 dict_local['status_note'] = False
866 dict_local['status_note'] = False
867 #----------------------------------------------#
867 #----------------------------------------------#
868 if 'path' in kwargs:
868 if 'path' in kwargs:
869 if type(kwargs['path']) is str:
869 if type(kwargs['path']) is str:
870 if os.path.isdir(kwargs['path']) == False:
870 if os.path.isdir(kwargs['path']) == False:
871 return 'ERROR:: "path" does not exist'
871 return 'ERROR:: "path" does not exist'
872 else:
872 else:
873 if kwargs['path'][-1:] != self.separator:
873 if kwargs['path'][-1:] != self.separator:
874 dict_local['path'] = kwargs['path']+self.separator
874 dict_local['path'] = kwargs['path']+self.separator
875 else:
875 else:
876 dict_local['path'] = kwargs['path']
876 dict_local['path'] = kwargs['path']
877
877
878 txt = dict_local['path']+datetime.now().strftime("%Y_%m_%d_%H_%M_%S_%f")+'.txt'
878 txt = dict_local['path']+datetime.now().strftime("%Y_%m_%d_%H_%M_%S_%f")+'.txt'
879 if int(platform.python_version()[0]) == 3:
879 if int(platform.python_version()[0]) == 3:
880 try:
880 try:
881 file_txt = open(txt, 'w')
881 file_txt = open(txt, 'w')
882 file_txt.close()
882 file_txt.close()
883 os.remove(txt)
883 os.remove(txt)
884 except PermissionError:
884 except PermissionError:
885 return 'ERROR:: Access denied, you are not authorized to write files: "%s"' % (dict_local['path'])
885 return 'ERROR:: Access denied, you are not authorized to write files: "%s"' % (dict_local['path'])
886 else:
886 else:
887 try:
887 try:
888 file_txt = open(txt, 'w')
888 file_txt = open(txt, 'w')
889 file_txt.close()
889 file_txt.close()
890 os.remove(txt)
890 os.remove(txt)
891 except:
891 except:
892 return 'ERROR:: Access denied, you are not authorized to write files: "%s"' % (dict_local['path'])
892 return 'ERROR:: Access denied, you are not authorized to write files: "%s"' % (dict_local['path'])
893 else:
893 else:
894 return 'ERROR:: "path" must be: <class "str">'
894 return 'ERROR:: "path" must be: <class "str">'
895 else:
895 else:
896 dict_local['path'] = ''
896 dict_local['path'] = ''
897 #----------------------------------------------#
897 #----------------------------------------------#
898 for key, value in kwargs.items():
898 for key, value in kwargs.items():
899 if not key in dict_local:
899 if not key in dict_local:
900 self.dict[key] = value
900 self.dict[key] = value
901 try:
901 try:
902 response = getattr(self.ckan.action, 'url_resources')(**self.dict)
902 response = getattr(self.ckan.action, 'url_resources')(**self.dict)
903 except:
903 except:
904 _, exc_value, _ = sys.exc_info()
904 _, exc_value, _ = sys.exc_info()
905 return exc_value
905 return exc_value
906
906
907 if len(response) != 0:
907 if len(response) != 0:
908 #--------------TEMP PATH---------------#
908 #--------------TEMP PATH---------------#
909 if dict_local['zip']:
909 if dict_local['zip']:
910 tempdir = tempfile.mkdtemp(prefix=kwargs['id']+'-')+self.separator
910 tempdir = tempfile.mkdtemp(prefix=kwargs['id']+'-')+self.separator
911 os.mkdir(tempdir+kwargs['id'])
911 os.mkdir(tempdir+kwargs['id'])
912 dir_name = tempdir + kwargs['id'] + self.separator
912 dir_name = tempdir + kwargs['id'] + self.separator
913 else:
913 else:
914 dir = self.f_name(kwargs['id'], '', dict_local['path'])
914 dir = self.f_name(kwargs['id'], '', dict_local['path'])
915 os.mkdir(dict_local['path'] + dir)
915 os.mkdir(dict_local['path'] + dir)
916 dir_name = dict_local['path'] + dir + self.separator
916 dir_name = dict_local['path'] + dir + self.separator
917 #-----------DOWNLOAD FILES-------------#
917 #-----------DOWNLOAD FILES-------------#
918 print('.....')
918 print('.....')
919 print('Downloading "{}" file(s) >>'.format(len(response)))
919 print('Downloading "{}" file(s) >>'.format(len(response)))
920 name_total = {'name': []}
920 name_total = {'name': []}
921 with concurrent.futures.ThreadPoolExecutor() as executor:
921 with concurrent.futures.ThreadPoolExecutor() as executor:
922 for u in tqdm(iterable=response, total=len(response)):
922 for u in tqdm(iterable=response, total=len(response)):
923 name_total['name'].append(u['name'])
923 name_total['name'].append(u['name'])
924 executor.submit(self.download_by_step, u, dir_name)
924 executor.submit(self.download_by_step, u, dir_name)
925 name_check = {}
925 name_check = {}
926 name_check['name'] = [f for f in os.listdir(dir_name) if os.path.isfile(os.path.join(dir_name, f))]
926 name_check['name'] = [f for f in os.listdir(dir_name) if os.path.isfile(os.path.join(dir_name, f))]
927 print('"{}" downloaded file(s) successfully >>'.format(len(name_check['name'])))
927 print('"{}" downloaded file(s) successfully >>'.format(len(name_check['name'])))
928 #--------------------------------------#
928 #--------------------------------------#
929 if len(name_check['name']) != 0:
929 if len(name_check['name']) != 0:
930 #----------Status Note---------#
930 #----------Status Note---------#
931 if dict_local['status_note']:
931 if dict_local['status_note']:
932 print('.....')
932 print('.....')
933 print('Creating: "status_note.txt" >>')
933 print('Creating: "status_note.txt" >>')
934 self.f_status_note(name_total, name_check, dir_name)
934 self.f_status_note(name_total, name_check, dir_name)
935 print('Created>>')
935 print('Created>>')
936 #----------ZIP CREATE----------#
936 #----------ZIP CREATE----------#
937 if dict_local['zip']:
937 if dict_local['zip']:
938 zip_name = self.f_name(kwargs['id'], '.zip', dict_local['path'])
938 zip_name = self.f_name(kwargs['id'], '.zip', dict_local['path'])
939 ziph = zipfile.ZipFile(dict_local['path'] + zip_name, 'w', zipfile.ZIP_DEFLATED, allowZip64=True)
939 ziph = zipfile.ZipFile(dict_local['path'] + zip_name, 'w', zipfile.ZIP_DEFLATED, allowZip64=True)
940 self.f_zipdir(dir_name, ziph, zip_name)
940 self.f_zipdir(dir_name, ziph, zip_name)
941 ziph.close()
941 ziph.close()
942 #Delete Temporal Path
942 #Delete Temporal Path
943 if os.path.exists(tempdir[:-1]):
943 if os.path.exists(tempdir[:-1]):
944 shutil.rmtree(tempdir[:-1])
944 shutil.rmtree(tempdir[:-1])
945 #------------------------------#
945 #------------------------------#
946 print('.....')
946 print('.....')
947 return 'DOWNLOAD FINISHED'
947 return 'DOWNLOAD FINISHED'
948 else:
948 else:
949 #Delete Temporal Path
949 #Delete Temporal Path
950 if dict_local['zip']:
950 if dict_local['zip']:
951 if os.path.exists(tempdir[:-1]):
951 if os.path.exists(tempdir[:-1]):
952 shutil.rmtree(tempdir[:-1])
952 shutil.rmtree(tempdir[:-1])
953 else:
953 else:
954 if os.path.exists(dir_name[:-1]):
954 if os.path.exists(dir_name[:-1]):
955 shutil.rmtree(dir_name[:-1])
955 shutil.rmtree(dir_name[:-1])
956 return 'NO FILES WERE DOWNLOADED'
956 return 'NO FILES WERE DOWNLOADED'
957 else:
957 else:
958 return 'FILES NOT FOUND'
958 return 'FILES NOT FOUND'
959
959
960 def download_files_advance(self, id_or_name, processes=1, path=os.path.expanduser("~"), **kwargs):
960 def download_files_advance(self, id_or_name, processes=1, path=os.path.expanduser("~"), **kwargs):
961 '''
961 '''
962 FINALIDAD:
962 FINALIDAD:
963 Funcion personalizada avanzada para la descarga de archivos existentes de un(os) dataset(s).
963 Funcion personalizada avanzada para la descarga de archivos existentes de un(os) dataset(s).
964
964
965 PARAMETROS DISPONIBLES:
965 PARAMETROS DISPONIBLES:
966 CONSULTAR: "GUIA DE SCRIPT.pdf"
966 CONSULTAR: "GUIA DE SCRIPT.pdf"
967
967
968 ESTRUCTURA:
968 ESTRUCTURA:
969 <access_name>.download_files_advance(id_or_name= <class 'str' or 'list'>, param_1 = <class 'param_1'>, ...)
969 <access_name>.download_files_advance(id_or_name= <class 'str' or 'list'>, param_1 = <class 'param_1'>, ...)
970 '''
970 '''
971 #------------------ PATH ----------------------#
971 #------------------ PATH ----------------------#
972 if isinstance(path, str):
972 if isinstance(path, str):
973 if os.path.isdir(path):
973 if os.path.isdir(path):
974 if not path.endswith(os.sep):
974 if not path.endswith(os.sep):
975 path = path + os.sep
975 path = path + os.sep
976 test_txt = path + datetime.now().strftime("%Y_%m_%d_%H_%M_%S_%f")+'.txt'
976 test_txt = path + datetime.now().strftime("%Y_%m_%d_%H_%M_%S_%f")+'.txt'
977 try:
977 try:
978 file_txt = open(test_txt, 'w')
978 file_txt = open(test_txt, 'w')
979 file_txt.close()
979 file_txt.close()
980 os.remove(test_txt)
980 os.remove(test_txt)
981 except:
981 except:
982 return 'ERROR:: Access denied, you are not authorized to write files: "%s"' % (path)
982 return 'ERROR:: Access denied, you are not authorized to write files: "%s"' % (path)
983 else:
983 else:
984 return 'ERROR:: "path" does not exist'
984 return 'ERROR:: "path" does not exist'
985 else:
985 else:
986 return 'ERROR:: "path" must be: <class "str">'
986 return 'ERROR:: "path" must be: <class "str">'
987
987
988 #------------------ PROCESSES -----------------#
988 #------------------ PROCESSES -----------------#
989 if not isinstance(processes, int):
989 if not isinstance(processes, int):
990 return 'ERROR:: "processes" must be: <class "int">'
990 return 'ERROR:: "processes" must be: <class "int">'
991
991
992 #------------------ ID OR NAME ----------------#
992 #------------------ ID OR NAME ----------------#
993 if isinstance(id_or_name, str):
993 if isinstance(id_or_name, str):
994 id_or_name = [id_or_name]
994 id_or_name = [id_or_name]
995 elif isinstance(id_or_name, list):
995 elif isinstance(id_or_name, list):
996 id_or_name = list(map(str, id_or_name))
996 id_or_name = list(map(str, id_or_name))
997 else:
997 else:
998 return 'ERROR:: dataset "id_or_name" must be: <class "str" or "list">'
998 return 'ERROR:: dataset "id_or_name" must be: <class "str" or "list">'
999 #----------------------------------------------#
999 #----------------------------------------------#
1000 arguments = {
1000 arguments = {
1001 '--apikey': self.Authorization,
1001 '--apikey': self.Authorization,
1002 '--ckan-user': None,
1002 '--ckan-user': None,
1003 '--config': None,
1003 '--config': None,
1004 '--datapackages': path,
1004 '--datapackages': path,
1005 '--datastore-fields': False,
1005 '--datastore-fields': False,
1006 '--get-request': False,
1006 '--get-request': False,
1007 '--insecure': not self.verify,
1007 '--insecure': not self.verify,
1008 '--log': '/home/soporte/DUMP/download.txt',
1009 '--processes': str(processes),
1008 '--processes': str(processes),
1010 '--quiet': False,
1009 '--quiet': False,
1011 '--remote': self.url,
1010 '--remote': self.url,
1012 '--worker': False,
1011 '--worker': False,
1012 #'--log': 'log.txt',
1013 #'--all': False,
1013 #'--all': False,
1014 #'--gzip': False,
1014 #'--gzip': False,
1015 #'--output': None,
1015 #'--output': None,
1016 #'--max-records': None,
1016 #'--max-records': None,
1017 #'--output-json': False,
1017 #'--output-json': False,
1018 #'--output-jsonl': False,
1018 #'--output-jsonl': False,
1019 #'--create-only': False,
1019 #'--create-only': False,
1020 #'--help': False,
1020 #'--help': False,
1021 #'--input': None,
1021 #'--input': None,
1022 #'--input-json': False,
1022 #'--input-json': False,
1023 #'--start-record': '1',
1023 #'--start-record': '1',
1024 #'--update-only': False,
1024 #'--update-only': False,
1025 #'--upload-logo': False,
1025 #'--upload-logo': False,
1026 #'--upload-resources': False,
1026 #'--upload-resources': False,
1027 #'--version': False,
1027 #'--version': False,
1028 'ID_OR_NAME': id_or_name,
1028 'ID_OR_NAME': id_or_name,
1029 'datasets': True,
1029 'datasets': True,
1030 'dump': True,
1030 'dump': True,
1031 #'ACTION_NAME': None,
1031 #'ACTION_NAME': None,
1032 #'KEY:JSON': [],
1032 #'KEY:JSON': [],
1033 #'KEY=STRING': [],
1033 #'KEY=STRING': [],
1034 #'KEY@FILE': [],
1034 #'KEY@FILE': [],
1035 #'action': False,
1035 #'action': False,
1036 #'delete': False,
1036 #'delete': False,
1037 #'groups': False,
1037 #'groups': False,
1038 #'load': False,
1038 #'load': False,
1039 #'organizations': False,
1039 #'organizations': False,
1040 #'related': False,
1040 #'related': False,
1041 #'search': False,
1041 #'search': False,
1042 #'users': False
1042 #'users': False
1043 }
1043 }
1044 return logic_download.dump_things_change(self.ckan, 'datasets', arguments, **kwargs) No newline at end of file
1044 return logic_download.dump_things_change(self.ckan, 'datasets', arguments, **kwargs)
@@ -1,226 +1,234
1 #from ckanapi.datapackage import populate_schema_from_datastore
1 #from ckanapi.datapackage import populate_schema_from_datastore
2 from ckanapi.cli import workers, dump
2 from ckanapi.cli import workers, dump
3 from ckanapi.cli.utils import pretty_json, completion_stats, compact_json, quiet_int_pipe
3 from ckanapi.cli.utils import pretty_json, completion_stats, compact_json, quiet_int_pipe
4 from datetime import datetime
4 from datetime import datetime
5 import sys
5 import sys
6 import json
6 import json
7 import os
7 import os
8 import requests
8 import requests
9 import six
9 import six
10
10
11 if sys.version_info.major == 3:
11 if sys.version_info.major == 3:
12 from urllib.parse import urlparse
12 from urllib.parse import urlparse
13 else:
13 else:
14 import urlparse
14 import urlparse
15
15
16 DL_CHUNK_SIZE = 100 * 1024
16 DL_CHUNK_SIZE = 100 * 1024
17
17
18 def dump_things_change(ckan, thing, arguments, worker_pool=None, stdout=None, stderr=None, **kwargs):
18 def dump_things_change(ckan, thing, arguments, worker_pool=None, stdout=None, stderr=None, **kwargs):
19 if worker_pool is None:
19 if worker_pool is None:
20 worker_pool = workers.worker_pool
20 worker_pool = workers.worker_pool
21 if stdout is None:
21 if stdout is None:
22 stdout = getattr(sys.__stdout__, 'buffer', sys.__stdout__)
22 stdout = getattr(sys.__stdout__, 'buffer', sys.__stdout__)
23 if stderr is None:
23 if stderr is None:
24 stderr = getattr(sys.stderr, 'buffer', sys.stderr)
24 stderr = getattr(sys.stderr, 'buffer', sys.stderr)
25
25
26 if arguments['--worker']:
26 if arguments['--worker']:
27 return dump.dump_things_worker(ckan, thing, arguments)
27 return dump.dump_things_worker(ckan, thing, arguments)
28 '''
28 '''
29 log = None
29 log = None
30 if arguments['--log']:
30 if arguments['--log']:
31 log = open(arguments['--log'], 'a')
31 log = open(arguments['--log'], 'a')
32 '''
32 '''
33 jsonl_output = stdout
33 jsonl_output = stdout
34 if arguments['--datapackages']:
34 if arguments['--datapackages']:
35 jsonl_output = open(os.devnull, 'wb')
35 jsonl_output = open(os.devnull, 'wb')
36
36
37 names = arguments['ID_OR_NAME']
37 names = arguments['ID_OR_NAME']
38
38
39 if names and isinstance(names[0], dict):
39 if names and isinstance(names[0], dict):
40 names = [rec.get('name',rec.get('id')) for rec in names]
40 names = [rec.get('name',rec.get('id')) for rec in names]
41 '''
41 '''
42 if arguments['--datapackages']:
42 if arguments['--datapackages']:
43 arguments['--datastore-fields'] = True
43 arguments['--datastore-fields'] = True
44 '''
44 '''
45 #----------------------------#
45 #----------------------------#
46 filtered_urls = {}
46 filtered_urls = {}
47 for name in names:
47 for val in names:
48 try:
48 try:
49 response = getattr(ckan.action, 'url_resources')(id=name, **kwargs)
49 filtered_urls[val] = getattr(ckan.action, 'url_resources')(id=val, **kwargs)
50 except:
50 except:
51 _, exc_value, _ = sys.exc_info()
51 _, exc_value, _ = sys.exc_info()
52 return exc_value
52 return exc_value
53 filtered_urls[name] = response
54 #----------------------------#
53 #----------------------------#
55
54
56 cmd = dump._worker_command_line(thing, arguments)
55 cmd = dump._worker_command_line(thing, arguments)
57 processes = int(arguments['--processes'])
56 processes = int(arguments['--processes'])
58 if hasattr(ckan, 'parallel_limit'):
57 if hasattr(ckan, 'parallel_limit'):
59 processes = min(processes, ckan.parallel_limit)
58 processes = min(processes, ckan.parallel_limit)
60 stats = completion_stats(processes)
59 stats = completion_stats(processes)
61 pool = worker_pool(cmd, processes, enumerate(compact_json(n) + b'\n' for n in names))
60 pool = worker_pool(cmd, processes, enumerate(compact_json(n) + b'\n' for n in names))
62
61
63 results = {}
62 results = {}
64 expecting_number = 0
63 expecting_number = 0
65 with quiet_int_pipe() as errors:
64 with quiet_int_pipe() as errors:
66 for job_ids, finished, result in pool:
65 for job_ids, finished, result in pool:
67 if not result:
66 if not result:
68 return 1
67 return 1
69 timestamp, error, record = json.loads(result.decode('utf-8'))
68 timestamp, error, record = json.loads(result.decode('utf-8'))
70 results[finished] = record
69 results[finished] = record
71
70
71 #----------------------------------------#
72 datapackages_path = arguments['--datapackages']
73 datapackage_dir = name_no_repetition(record.get('name', ''), datapackages_path)
74 #----------------------------------------#
72 if not arguments['--quiet']:
75 if not arguments['--quiet']:
73 stderr.write('** Finished: {0} | Job IDs: {1} | Next Report: {2} | Error: {3} | Dataset Name: {4}\n'.format(
76 stderr.write('** Finished: {0} | Job IDs: {1} | Next Report: {2} | Error: {3} | Path: {4} | Dataset Name: {5}\n'.format(
74 finished,
77 finished,
75 job_ids,
78 job_ids,
76 next(stats),
79 next(stats),
77 error,
80 error,
81 datapackage_dir,
78 record.get('name', '') if record else '',
82 record.get('name', '') if record else '',
79 ).encode('utf-8'))
83 ).encode('utf-8'))
80 '''
84 '''
81 if log:
85 if log:
82 log.write(compact_json([
86 log.write(compact_json([
83 timestamp,
87 timestamp,
84 finished,
88 finished,
85 error,
89 error,
86 record.get('name', '') if record else None,
90 record.get('name', '') if record else None,
87 ]) + b'\n')
91 ]) + b'\n')
88 '''
92 '''
89 datapackages_path = arguments['--datapackages']
90 if datapackages_path:
93 if datapackages_path:
91 create_datapackage_change(record, filtered_urls[record.get('name', '')], datapackages_path, stderr, arguments['--apikey'], arguments['--remote'], arguments['--insecure'])
94 try:
95 filter_url = filtered_urls[record.get('name', '')]
96 except:
97 filter_url = filtered_urls[record.get('id', '')]
98 create_datapackage_change(record, filter_url, datapackage_dir, stderr, arguments['--apikey'], arguments['--remote'], arguments['--insecure'])
99
92 while expecting_number in results:
100 while expecting_number in results:
93 record = results.pop(expecting_number)
101 record = results.pop(expecting_number)
94 if record:
102 if record:
95 jsonl_output.write(compact_json(record, sort_keys=True) + b'\n')
103 jsonl_output.write(compact_json(record, sort_keys=True) + b'\n')
96 expecting_number += 1
104 expecting_number += 1
97 if 'pipe' in errors:
105 if 'pipe' in errors:
98 return 1
106 return 1
99 if 'interrupt' in errors:
107 if 'interrupt' in errors:
100 return 2
108 return 2
101
109
102 def create_datapackage_change(record, filtered_url, base_path, stderr, apikey, host_url, insecure):
110 def create_datapackage_change(record, filtered_url, datapackage_dir, stderr, apikey, host_url, insecure):
103 resource_formats_to_ignore = ['API', 'api']
111 resource_formats_to_ignore = ['API', 'api']
104 #----------------------------------------#
112
105 datapackage_dir = name_no_repetition(record.get('name', ''), base_path)
106 #----------------------------------------#
107 os.makedirs(os.path.join(datapackage_dir, 'data'))
113 os.makedirs(os.path.join(datapackage_dir, 'data'))
108 record['path'] = datapackage_dir
114 record['path'] = datapackage_dir
109
115
110 ckan_resources = []
116 ckan_resources = []
111 for resource in record.get('resources', []):
117 for resource in record.get('resources', []):
112 if resource['format'] in resource_formats_to_ignore:
118 if resource['format'] in resource_formats_to_ignore:
113 continue
119 continue
114
120
115 if not {'name': resource['name'], 'url': resource['url']} in filtered_url:
121 if not {'name': resource['name'], 'url': resource['url']} in filtered_url:
116 continue
122 continue
117
123
118 if len(resource['url']) == 0:
124 if len(resource['url']) == 0:
119 continue
125 continue
120
126
121 filename = name_no_repetition(resource['name'], os.path.join(datapackage_dir, 'data'), 'resource')
127 filename = name_no_repetition(resource['name'], os.path.join(datapackage_dir, 'data'), 'resource')
122 resource['path'] = os.path.join(datapackage_dir, 'data', filename)
128 resource['path'] = os.path.join(datapackage_dir, 'data', filename)
123
129
124 cres = create_resource_change(resource, stderr, apikey, host_url, insecure)
130 cres = create_resource_change(resource, stderr, apikey, host_url, insecure)
125 if not cres:
131 if not cres:
126 continue
132 continue
127 '''
133 '''
128 #----------------------------------------#
134 #----------------------------------------#
129 dres = {'path': os.path.join('data', filename),
135 dres = {'path': os.path.join('data', filename),
130 'description': cres.get('description', ''),
136 'description': cres.get('description', ''),
131 'format': cres.get('format', ''),
137 'format': cres.get('format', ''),
132 'name': cres.get('name', ''),
138 'name': cres.get('name', ''),
133 'title': cres.get('name', '').title()}
139 'title': cres.get('name', '').title()}
134 #----------------------------------------#
140 #----------------------------------------#
135 populate_schema_from_datastore(cres, dres)
141 populate_schema_from_datastore(cres, dres)
136 '''
142 '''
137 ckan_resources.append(resource)
143 ckan_resources.append(resource)
138
144
139 dataset = dict(record, resources=ckan_resources)
145 dataset = dict(record, resources=ckan_resources)
140 datapackage = dataset_to_datapackage_change(dataset)
146 datapackage = dataset_to_datapackage_change(dataset)
141
147
142 json_path = os.path.join(datapackage_dir, 'datapackage.json')
148 json_path = os.path.join(datapackage_dir, 'datapackage.json')
143 with open(json_path, 'wb') as out:
149 with open(json_path, 'wb') as out:
144 out.write(pretty_json(datapackage))
150 out.write(pretty_json(datapackage))
145
151
146 return datapackage_dir, datapackage, json_path
152 return datapackage_dir, datapackage, json_path
147
153
148 def create_resource_change(resource, stderr, apikey, host_url, insecure):
154 def create_resource_change(resource, stderr, apikey, host_url, insecure):
149 # ---------- REPLACE URL --------- #
155 # ---------- REPLACE URL --------- #
150 if urlparse(host_url).netloc != 'www.igp.gob.pe' and urlparse(resource['url']).netloc == 'www.igp.gob.pe':
156 if urlparse(host_url).netloc != 'www.igp.gob.pe' and urlparse(resource['url']).netloc == 'www.igp.gob.pe':
151 resource['url'] = resource['url'].replace(urlparse(resource['url']).scheme + '://' + urlparse(resource['url']).netloc,
157 resource['url'] = resource['url'].replace(urlparse(resource['url']).scheme + '://' + urlparse(resource['url']).netloc,
152 urlparse(host_url).scheme + '://' + urlparse(host_url).netloc)
158 urlparse(host_url).scheme + '://' + urlparse(host_url).netloc)
153 #----------------------------------#
159 #----------------------------------#
154 try:
160 try:
155 r = requests.get(resource['url'], headers={'Authorization': apikey}, stream=True, verify=not insecure)
161 r = requests.get(resource['url'], headers={'Authorization': apikey}, stream=True, verify=not insecure)
156 #---------------------------------------#
162 #---------------------------------------#
157 try:
163 try:
158 r.raise_for_status()
164 r.raise_for_status()
159 except requests.exceptions.HTTPError as e:
165 except requests.exceptions.HTTPError as e:
160 return False
166 return False
161 #---------------------------------------#
167 #---------------------------------------#
162 with open(resource['path'], 'wb') as f:
168 with open(resource['path'], 'wb') as f:
163 for chunk in r.iter_content(chunk_size=DL_CHUNK_SIZE):
169 for chunk in r.iter_content(chunk_size=DL_CHUNK_SIZE):
164 if chunk:
170 if chunk:
165 f.write(chunk)
171 f.write(chunk)
166
172
167 except requests.ConnectionError:
173 except requests.ConnectionError:
168 stderr.write('URL {0} refused connection. The resource will not be downloaded\n'.format(resource['url']).encode('utf-8'))
174 stderr.write('URL {0} refused connection. The resource will not be downloaded\n'.format(resource['url']).encode('utf-8'))
169 except requests.exceptions.RequestException as e:
175 except requests.exceptions.RequestException as e:
170 stderr.write('{0}\n'.format(str(e.args[0]) if len(e.args) > 0 else '').encode('utf-8'))
176 stderr.write('{0}\n'.format(str(e.args[0]) if len(e.args) > 0 else '').encode('utf-8'))
171 except Exception as e:
177 except Exception as e:
172 stderr.write('{0}'.format(str(e.args[0]) if len(e.args) > 0 else '').encode('utf-8'))
178 stderr.write('{0}'.format(str(e.args[0]) if len(e.args) > 0 else '').encode('utf-8'))
173 return resource
179 return resource
174
180
175 def dataset_to_datapackage_change(dataset_dict):
181 def dataset_to_datapackage_change(dataset_dict):
176 dp = {'name': dataset_dict['name'],
182 dp = {'name': dataset_dict['name'],
177 'id': dataset_dict['id'],
183 'id': dataset_dict['id'],
178 'path': dataset_dict['path'],
184 'path': dataset_dict['path'],
179 'last_update': datetime.strptime(dataset_dict['metadata_modified'], "%Y-%m-%dT%H:%M:%S.%f").strftime("%d-%b-%Y %I.%M %p")}
185 'last_update': datetime.strptime(dataset_dict['metadata_modified'], "%Y-%m-%dT%H:%M:%S.%f").strftime("%d-%b-%Y %I.%M %p")}
180
186
181 resources = dataset_dict.get('resources')
187 resources = dataset_dict.get('resources')
182 if resources:
188 if resources:
183 dp['resources'] = [convert_to_datapackage_resource_change(r)
189 dp['resources'] = [convert_to_datapackage_resource_change(r)
184 for r in resources]
190 for r in resources]
185 return dp
191 return dp
186
192
187 def convert_to_datapackage_resource_change(resource_dict):
193 def convert_to_datapackage_resource_change(resource_dict):
188 resource = {}
194 resource = {}
189
195
190 if resource_dict.get('id'):
196 if resource_dict.get('id'):
191 resource['id'] = resource_dict['id']
197 resource['id'] = resource_dict['id']
192
198
193 if resource_dict.get('name'):
199 if resource_dict.get('name'):
194 resource['name'] = resource_dict['name']
200 resource['name'] = resource_dict['name']
195
201
196 if resource_dict.get('path'):
202 if resource_dict.get('path'):
197 resource['path'] = resource_dict['path']
203 if os.path.isfile(resource_dict['path']):
204 resource['path'] = resource_dict['path']
205 else:
206 resource['url'] = resource_dict['url']
198
207
199 schema = resource_dict.get('schema')
208 schema = resource_dict.get('schema')
200 if isinstance(schema, six.string_types):
209 if isinstance(schema, six.string_types):
201 try:
210 try:
202 resource['schema'] = json.loads(schema)
211 resource['schema'] = json.loads(schema)
203 except ValueError:
212 except ValueError:
204 resource['schema'] = schema
213 resource['schema'] = schema
205 elif isinstance(schema, dict):
214 elif isinstance(schema, dict):
206 resource['schema'] = schema
215 resource['schema'] = schema
207
208 return resource
216 return resource
209
217
210 def name_no_repetition(name, dir, option=''):
218 def name_no_repetition(name, dir, option=''):
211 count = 0
219 count = 0
212 while True:
220 while True:
213 count = count + 1
221 count = count + 1
214 if not os.path.exists(os.path.join(dir, name)):
222 if not os.path.exists(os.path.join(dir, name)):
215 if option == 'resource':
223 if option == 'resource':
216 return name
224 return name
217 else:
225 else:
218 return os.path.join(dir, name)
226 return os.path.join(dir, name)
219
227
220 elif not os.path.exists(os.path.join(dir, '('+str(count)+')'+name)):
228 elif not os.path.exists(os.path.join(dir, '('+str(count)+')'+name)):
221 if option == 'resource':
229 if option == 'resource':
222 return '('+str(count)+')'+name
230 return '('+str(count)+')'+name
223 else:
231 else:
224 return os.path.join(dir, '('+str(count)+')'+name)
232 return os.path.join(dir, '('+str(count)+')'+name)
225 else:
233 else:
226 pass No newline at end of file
234 pass
@@ -1,343 +1,345
1 import json
1 import json
2 import uuid
2 import uuid
3 import os
3 import os
4 import sys
4 import sys
5
5
6 def resource_create(self, package_id, file_type, others='', max_size=100, max_count=500, ignore_repetition=False, **kwargs):
6 def resource_create(self, package_id, file_type, others='', max_size=100, max_count=500, ignore_repetition=False, **kwargs):
7 #---------------------------------------------------------------------#
7 #---------------------------------------------------------------------#
8 kwargs['voc_file_type'] = file_type
8 kwargs['voc_file_type'] = file_type
9 kwargs['others'] = others
9 kwargs['others'] = others
10
10
11 if 'clear_upload' in kwargs:
11 if 'clear_upload' in kwargs:
12 del kwargs['clear_upload']
12 del kwargs['clear_upload']
13 #---------------------------------------------------------------------#
13 #---------------------------------------------------------------------#
14
14
15 url_upload = {}
15 url_upload = {}
16 if 'upload' in kwargs and 'url' in kwargs:
16 if 'upload' in kwargs and 'url' in kwargs:
17 return 'ERROR:: Choose one: "upload" or "url" parameters'
17 return 'ERROR:: Choose one: "upload" or "url" parameters'
18 elif 'upload' in kwargs:
18 elif 'upload' in kwargs:
19 url_upload['upload'] = kwargs['upload']
19 url_upload['upload'] = kwargs['upload']
20 elif 'url' in kwargs:
20 elif 'url' in kwargs:
21 url_upload['url'] = kwargs['url']
21 url_upload['url'] = kwargs['url']
22 else:
22 else:
23 return 'ERROR:: Missing value: "upload" or "url" parameters'
23 return 'ERROR:: Missing value: "upload" or "url" parameters'
24
24
25 value_u = list(url_upload.values())[0]
25 value_u = list(url_upload.values())[0]
26 key_u = list(url_upload.keys())[0]
26 key_u = list(url_upload.keys())[0]
27
27
28 if not isinstance(value_u, list) and not isinstance(value_u, str):
28 if not isinstance(value_u, list) and not isinstance(value_u, str):
29 return 'ERROR:: "%s" must be <str> or <list>' % (key_u)
29 return 'ERROR:: "%s" must be <str> or <list>' % (key_u)
30 #---------------------------------------------------------------------#
30 #---------------------------------------------------------------------#
31
31
32 if isinstance(value_u, str):
32 if isinstance(value_u, str):
33 if len(value_u) != 0:
33 if len(value_u) != 0:
34 if key_u == 'upload':
34 if key_u == 'upload':
35 if os.path.isdir(value_u):
35 if os.path.isdir(value_u):
36 path_order = [f for f in os.listdir(value_u) if os.path.isfile(os.path.join(value_u, f))]
36 path_order = [f for f in os.listdir(value_u) if os.path.isfile(os.path.join(value_u, f))]
37 path_order.sort()
37 path_order.sort()
38 if path_order:
38 if path_order:
39 kwargs['upload'] = []
39 kwargs['upload'] = []
40 for name in path_order:
40 for name in path_order:
41 kwargs['upload'].append(os.path.join(value_u, name))
41 kwargs['upload'].append(os.path.join(value_u, name))
42 else:
42 else:
43 return "ERROR:: There aren't files in this directory"
43 return "ERROR:: There aren't files in this directory"
44 elif os.path.isfile(value_u):
45 pass
44 else:
46 else:
45 return 'ERROR:: Directory or File does not exist'
47 return 'ERROR:: Directory or File does not exist'
46 else:
48 else:
47 return 'ERROR:: "path_list is empty"'
49 return 'ERROR:: "path_list is empty"'
48 #---------------------------------------------------------------------#
50 #---------------------------------------------------------------------#
49
51
50 if not isinstance(kwargs[key_u], list):
52 if not isinstance(kwargs[key_u], list):
51 kwargs[key_u] = [kwargs[key_u]]
53 kwargs[key_u] = [kwargs[key_u]]
52
54
53 if kwargs.get('upload', None):
55 if kwargs.get('upload', None):
54 if len(kwargs['upload']) != len(set(kwargs['upload'])):
56 if len(kwargs['upload']) != len(set(kwargs['upload'])):
55 return 'Duplicate files found in "upload" parameter'
57 return 'Duplicate files found in "upload" parameter'
56 #---------------------------------------------------------------------#
58 #---------------------------------------------------------------------#
57
59
58 change_kwargs = {}
60 change_kwargs = {}
59 for key1, value1 in kwargs.items():
61 for key1, value1 in kwargs.items():
60 if key1 == 'others':
62 if key1 == 'others':
61 if isinstance(value1, tuple):
63 if isinstance(value1, tuple):
62 if len(value1) != len(kwargs[key_u]):
64 if len(value1) != len(kwargs[key_u]):
63 return 'ERROR:: "%s" value(s) must be same length as "%s" value(s)' % (key1, key_u)
65 return 'ERROR:: "%s" value(s) must be same length as "%s" value(s)' % (key1, key_u)
64 else:
66 else:
65 change_kwargs[key1] = value1
67 change_kwargs[key1] = value1
66 else:
68 else:
67 change_kwargs[key1] = (value1,) * len(kwargs[key_u])
69 change_kwargs[key1] = (value1,) * len(kwargs[key_u])
68 else:
70 else:
69 if isinstance(value1, list):
71 if isinstance(value1, list):
70 if len(value1) != len(kwargs[key_u]):
72 if len(value1) != len(kwargs[key_u]):
71 return 'ERROR:: "%s" value(s) must be same length as "%s" value(s)' % (key1, key_u)
73 return 'ERROR:: "%s" value(s) must be same length as "%s" value(s)' % (key1, key_u)
72 else:
74 else:
73 change_kwargs[key1] = value1
75 change_kwargs[key1] = value1
74 else:
76 else:
75 change_kwargs[key1] = [value1] * len(kwargs[key_u])
77 change_kwargs[key1] = [value1] * len(kwargs[key_u])
76 #---------------------------------------------------------------------#
78 #---------------------------------------------------------------------#
77
79
78 try:
80 try:
79 dataset_show = getattr(self.ckan.action, 'package_show')(id=package_id)['resources']
81 dataset_show = getattr(self.ckan.action, 'package_show')(id=package_id)['resources']
80 resources_name = []
82 resources_name = []
81 for u in dataset_show:
83 for u in dataset_show:
82 resources_name.append(u['name'].lower())
84 resources_name.append(u['name'].lower())
83 except:
85 except:
84 _, exc_value, _ = sys.exc_info()
86 _, exc_value, _ = sys.exc_info()
85 print('ERROR obtaining metadata dataset:: Use the "print" for more information')
87 print('ERROR obtaining metadata dataset:: Use the "print" for more information')
86 return exc_value
88 return exc_value
87 #---------------------------------------------------------------------#
89 #---------------------------------------------------------------------#
88
90
89 for c1 in range(len(kwargs[key_u])):
91 for c1 in range(len(kwargs[key_u])):
90 new_kwargs = {}
92 new_kwargs = {}
91 for k2, v2 in change_kwargs.items():
93 for k2, v2 in change_kwargs.items():
92 #-------------------------------------------------------------#
94 #-------------------------------------------------------------#
93 if k2 == 'upload':
95 if k2 == 'upload':
94 if not os.path.isfile(v2[c1]):
96 if not os.path.isfile(v2[c1]):
95 return 'File "%s" does not exist' % (v2[c1])
97 return 'File "%s" does not exist' % (v2[c1])
96
98
97 if not kwargs.get('size', None):
99 if not kwargs.get('size', None):
98 new_kwargs['size'] = os.stat(v2[c1]).st_size
100 new_kwargs['size'] = os.stat(v2[c1]).st_size
99 #-------------------------------------------------------------#
101 #-------------------------------------------------------------#
100 new_kwargs[k2] = v2[c1]
102 new_kwargs[k2] = v2[c1]
101 #-----------------------------------------------------------------#
103 #-----------------------------------------------------------------#
102
104
103 if not kwargs.get('name', None):
105 if not kwargs.get('name', None):
104 new_kwargs['name'] = os.path.basename(new_kwargs[key_u])
106 new_kwargs['name'] = os.path.basename(new_kwargs[key_u])
105
107
106 if new_kwargs['name'].lower() in resources_name:
108 if new_kwargs['name'].lower() in resources_name:
107 if not ignore_repetition:
109 if not ignore_repetition:
108 return 'ERROR:: "%s" resource already exist in this dataset' % (new_kwargs['name'])
110 return 'ERROR:: "%s" resource already exist in this dataset' % (new_kwargs['name'])
109 print('WARRING:: "'+ new_kwargs['name'] +'" resource was ignored because already exist in this dataset')
111 print('WARRING:: "'+ new_kwargs['name'] +'" resource was ignored because already exist in this dataset')
110 else:
112 else:
111 self.list.append(new_kwargs)
113 self.list.append(new_kwargs)
112 #---------------------------------------------------------------------#
114 #---------------------------------------------------------------------#
113
115
114 try:
116 try:
115 uuid.UUID(package_id, version=4)
117 uuid.UUID(package_id, version=4)
116 self.dict['match'] = json.dumps({'id': package_id})
118 self.dict['match'] = json.dumps({'id': package_id})
117 except ValueError:
119 except ValueError:
118 self.dict['match'] = json.dumps({'name': package_id})
120 self.dict['match'] = json.dumps({'name': package_id})
119 #---------------------------------------------------------------------#
121 #---------------------------------------------------------------------#
120
122
121 if kwargs.get('upload', None):
123 if kwargs.get('upload', None):
122 blocks = [[]]
124 blocks = [[]]
123 size_file = 0
125 size_file = 0
124 count_file = 0
126 count_file = 0
125 inter_num = 0
127 inter_num = 0
126 for value in self.list:
128 for value in self.list:
127 if value['size'] > 1024 * 1024 * float(max_size):
129 if value['size'] > 1024 * 1024 * float(max_size):
128 return 'ERROR:: The size of the "%s" file is %sMB aprox, please change "max_size" value' % (value['name'], str(round(value['size']/(1024 * 1024), 2)))
130 return 'ERROR:: The size of the "%s" file is %sMB aprox, please change "max_size" value' % (value['name'], str(round(value['size']/(1024 * 1024), 2)))
129 if not 1 <= int(max_count) <= 999:
131 if not 1 <= int(max_count) <= 999:
130 return 'ERROR:: The count of the number of files must be between 1 and 999, please change "max_count" value'
132 return 'ERROR:: The count of the number of files must be between 1 and 999, please change "max_count" value'
131
133
132 size_file = size_file + value['size']
134 size_file = size_file + value['size']
133 count_file = count_file + 1
135 count_file = count_file + 1
134 if size_file > 1024 * 1024 * float(max_size) or count_file > int(max_count):
136 if size_file > 1024 * 1024 * float(max_size) or count_file > int(max_count):
135 inter_num = inter_num + 1
137 inter_num = inter_num + 1
136 size_file = value['size']
138 size_file = value['size']
137 count_file = 1
139 count_file = 1
138 blocks.append([])
140 blocks.append([])
139
141
140 del value['size']
142 del value['size']
141 blocks[inter_num].append(value)
143 blocks[inter_num].append(value)
142 #------------------------------------------------------------#
144 #------------------------------------------------------------#
143
145
144 if len(blocks[0]) > 0:
146 if len(blocks[0]) > 0:
145 print('BLOCK(S) IN TOTAL:: {}'.format(len(blocks)))
147 print('BLOCK(S) IN TOTAL:: {}'.format(len(blocks)))
146
148
147 for count1, block in enumerate(blocks):
149 for count1, block in enumerate(blocks):
148 upload_files = {}
150 upload_files = {}
149 for count2, value2 in enumerate(block):
151 for count2, value2 in enumerate(block):
150 upload_files['update__resources__-'+ str(len(block)-count2) +'__upload'] = (value2['name'], open(value2['upload'], 'rb'))
152 upload_files['update__resources__-'+ str(len(block)-count2) +'__upload'] = (value2['name'], open(value2['upload'], 'rb'))
151 del value2['upload']
153 del value2['upload']
152
154
153 print('---- BLOCK N°{} ----'.format(count1 + 1))
155 print('---- BLOCK N°{} ----'.format(count1 + 1))
154 print('BLOCK N°{} :: "{}" file(s) found >> uploading'.format(count1 + 1, len(block)))
156 print('BLOCK N°{} :: "{}" file(s) found >> uploading'.format(count1 + 1, len(block)))
155
157
156 try:
158 try:
157 result = self.ckan.call_action('package_revise', {'match': self.dict['match'], 'update__resources__extend': json.dumps(block)}, files=upload_files)
159 result = self.ckan.call_action('package_revise', {'match': self.dict['match'], 'update__resources__extend': json.dumps(block)}, files=upload_files)
158 print('BLOCK N°{} :: Uploaded file(s) successfully'.format(count1 + 1))
160 print('BLOCK N°{} :: Uploaded file(s) successfully'.format(count1 + 1))
159 if len(blocks) == count1 + 1:
161 if len(blocks) == count1 + 1:
160 return result
162 return result
161 except:
163 except:
162 print('ERROR :: Use the "print" for more information')
164 print('ERROR :: Use the "print" for more information')
163 _, exc_value, _ = sys.exc_info()
165 _, exc_value, _ = sys.exc_info()
164 return exc_value
166 return exc_value
165 else:
167 else:
166 return "ERROR:: No file(s) found to upload"
168 return "ERROR:: No file(s) found to upload"
167 else:
169 else:
168 if len(self.list) > 0:
170 if len(self.list) > 0:
169 return self.ckan.call_action('package_revise', {'match': self.dict['match'], 'update__resources__extend': json.dumps(self.list)})
171 return self.ckan.call_action('package_revise', {'match': self.dict['match'], 'update__resources__extend': json.dumps(self.list)})
170 else:
172 else:
171 return "ERROR:: No resource(s) found to create"
173 return "ERROR:: No resource(s) found to create"
172
174
173
175
174 def resource_patch(self, id, package_id, max_size=100, max_count=500, **kwargs):
176 def resource_patch(self, id, package_id, max_size=100, max_count=500, **kwargs):
175 #Cambiar el nombre al actualizar con un nuevo archivo
177 #Cambiar el nombre al actualizar con un nuevo archivo
176
178
177 if 'file_type' in kwargs:
179 if 'file_type' in kwargs:
178 kwargs['voc_file_type'] = kwargs['file_type']
180 kwargs['voc_file_type'] = kwargs['file_type']
179 del kwargs['file_type']
181 del kwargs['file_type']
180
182
181 if 'upload' in kwargs and 'url' in kwargs:
183 if 'upload' in kwargs and 'url' in kwargs:
182 return 'ERROR:: Choose one: "upload" or "url" parameters'
184 return 'ERROR:: Choose one: "upload" or "url" parameters'
183 #---------------------------------------------------------------------#
185 #---------------------------------------------------------------------#
184
186
185 if not isinstance(id, list) and not isinstance(id, str):
187 if not isinstance(id, list) and not isinstance(id, str):
186 return 'ERROR:: "id" must be <str> or <list>'
188 return 'ERROR:: "id" must be <str> or <list>'
187
189
188 if isinstance(id, str):
190 if isinstance(id, str):
189 id = [id]
191 id = [id]
190
192
191 change_kwargs = {}
193 change_kwargs = {}
192 for key1, value1 in kwargs.items():
194 for key1, value1 in kwargs.items():
193 if key1 == 'others':
195 if key1 == 'others':
194 if isinstance(value1, tuple):
196 if isinstance(value1, tuple):
195 if len(value1) != len(id):
197 if len(value1) != len(id):
196 return 'ERROR:: "%s" value(s) must be same length as "id" value(s)' % (key1)
198 return 'ERROR:: "%s" value(s) must be same length as "id" value(s)' % (key1)
197 else:
199 else:
198 change_kwargs[key1] = value1
200 change_kwargs[key1] = value1
199 else:
201 else:
200 change_kwargs[key1] = (value1,) * len(id)
202 change_kwargs[key1] = (value1,) * len(id)
201 else:
203 else:
202 if isinstance(value1, list):
204 if isinstance(value1, list):
203 if len(value1) != len(id):
205 if len(value1) != len(id):
204 return 'ERROR:: "%s" value(s) must be same length as "id" value(s)' % (key1)
206 return 'ERROR:: "%s" value(s) must be same length as "id" value(s)' % (key1)
205 else:
207 else:
206 change_kwargs[key1] = value1
208 change_kwargs[key1] = value1
207 else:
209 else:
208 change_kwargs[key1] = [value1] * len(id)
210 change_kwargs[key1] = [value1] * len(id)
209 #---------------------------------------------------------------------#
211 #---------------------------------------------------------------------#
210
212
211 for c1, v1 in enumerate(id):
213 for c1, v1 in enumerate(id):
212 new_kwargs = {}
214 new_kwargs = {}
213 for k2, v2 in change_kwargs.items():
215 for k2, v2 in change_kwargs.items():
214 #-------------------------------------------------------------#
216 #-------------------------------------------------------------#
215 if k2 == 'upload':
217 if k2 == 'upload':
216 if not os.path.isfile(v2[c1]):
218 if not os.path.isfile(v2[c1]):
217 return 'File "%s" does not exist' % (v2[c1])
219 return 'File "%s" does not exist' % (v2[c1])
218
220
219 new_kwargs['size'] = os.stat(v2[c1]).st_size
221 new_kwargs['size'] = os.stat(v2[c1]).st_size
220
222
221 if k2 == 'url':
223 if k2 == 'url':
222 new_kwargs['clear_upload'] = True
224 new_kwargs['clear_upload'] = True
223 new_kwargs['size'] = 0
225 new_kwargs['size'] = 0
224 new_kwargs['mimetype'] = None
226 new_kwargs['mimetype'] = None
225 #-------------------------------------------------------------#
227 #-------------------------------------------------------------#
226 new_kwargs[k2] = v2[c1]
228 new_kwargs[k2] = v2[c1]
227
229
228 if new_kwargs.get('upload', None):
230 if new_kwargs.get('upload', None):
229 #-------------------------------------------------------------#
231 #-------------------------------------------------------------#
230 if new_kwargs.get('clear_upload', None):
232 if new_kwargs.get('clear_upload', None):
231 del new_kwargs['clear_upload']
233 del new_kwargs['clear_upload']
232 #-------------------------------------------------------------#
234 #-------------------------------------------------------------#
233 self.dict['update__resources__'+v1[:6]] = new_kwargs
235 self.dict['update__resources__'+v1[:6]] = new_kwargs
234 else:
236 else:
235 self.dict['update__resources__'+v1[:6]] = json.dumps(new_kwargs)
237 self.dict['update__resources__'+v1[:6]] = json.dumps(new_kwargs)
236 #---------------------------------------------------------------------#
238 #---------------------------------------------------------------------#
237
239
238 try:
240 try:
239 uuid.UUID(package_id, version=4)
241 uuid.UUID(package_id, version=4)
240 package_id = json.dumps({'id': package_id})
242 package_id = json.dumps({'id': package_id})
241 except ValueError:
243 except ValueError:
242 package_id = json.dumps({'name': package_id})
244 package_id = json.dumps({'name': package_id})
243 #---------------------------------------------------------------------#
245 #---------------------------------------------------------------------#
244
246
245 if kwargs.get('upload', None):
247 if kwargs.get('upload', None):
246 blocks = [{}]
248 blocks = [{}]
247 upload_files = [{}]
249 upload_files = [{}]
248 size_file = 0
250 size_file = 0
249 count_file = 0
251 count_file = 0
250 inter_num = 0
252 inter_num = 0
251
253
252 for dict_key, dict_value in self.dict.items():
254 for dict_key, dict_value in self.dict.items():
253 if dict_value['size'] > 1024 * 1024 * float(max_size):
255 if dict_value['size'] > 1024 * 1024 * float(max_size):
254 return 'ERROR:: "%s" size out of limit' % (dict_value['upload'])
256 return 'ERROR:: "%s" size out of limit' % (dict_value['upload'])
255
257
256 if not 1 <= int(max_count) <= 999:
258 if not 1 <= int(max_count) <= 999:
257 return 'ERROR:: The count of the number of files must be between 1 and 999, please change "max_count" value'
259 return 'ERROR:: The count of the number of files must be between 1 and 999, please change "max_count" value'
258
260
259 size_file = size_file + dict_value['size']
261 size_file = size_file + dict_value['size']
260 count_file = count_file + 1
262 count_file = count_file + 1
261 if size_file <= 1024 * 1024 * float(max_size) and count_file <= int(max_count):
263 if size_file <= 1024 * 1024 * float(max_size) and count_file <= int(max_count):
262 upload_files[inter_num][dict_key+'__upload'] = (os.path.basename(dict_value['upload']), open(dict_value['upload'], 'rb'))
264 upload_files[inter_num][dict_key+'__upload'] = (os.path.basename(dict_value['upload']), open(dict_value['upload'], 'rb'))
263
265
264 else:
266 else:
265 inter_num = inter_num + 1
267 inter_num = inter_num + 1
266 size_file = dict_value['size']
268 size_file = dict_value['size']
267 count_file = 1
269 count_file = 1
268 upload_files.append({dict_key+'__upload': (os.path.basename(dict_value['upload']), open(dict_value['upload'], 'rb'))})
270 upload_files.append({dict_key+'__upload': (os.path.basename(dict_value['upload']), open(dict_value['upload'], 'rb'))})
269 blocks.append({})
271 blocks.append({})
270
272
271 del dict_value['upload']
273 del dict_value['upload']
272 del dict_value['size']
274 del dict_value['size']
273 blocks[inter_num]['match'] = package_id
275 blocks[inter_num]['match'] = package_id
274 blocks[inter_num][dict_key] = json.dumps(dict_value)
276 blocks[inter_num][dict_key] = json.dumps(dict_value)
275
277
276 #------------------------------------------------------------#
278 #------------------------------------------------------------#
277 if len(blocks[0]) > 0:
279 if len(blocks[0]) > 0:
278 print('BLOCK(S) IN TOTAL:: {}'.format(len(blocks)))
280 print('BLOCK(S) IN TOTAL:: {}'.format(len(blocks)))
279 for count1, block in enumerate(blocks):
281 for count1, block in enumerate(blocks):
280 print('---- BLOCK N°{} ----'.format(count1 + 1))
282 print('---- BLOCK N°{} ----'.format(count1 + 1))
281 print('BLOCK N°{} :: "{}" file(s) found >> uploading'.format(count1 + 1, len(block)-1))
283 print('BLOCK N°{} :: "{}" file(s) found >> uploading'.format(count1 + 1, len(block)-1))
282 try:
284 try:
283 result = self.ckan.call_action('package_revise', block, files=upload_files[count1])
285 result = self.ckan.call_action('package_revise', block, files=upload_files[count1])
284
286
285 print('BLOCK N°{} :: Uploaded file(s) successfully'.format(count1 + 1))
287 print('BLOCK N°{} :: Uploaded file(s) successfully'.format(count1 + 1))
286 if len(blocks) == count1 + 1:
288 if len(blocks) == count1 + 1:
287 return result
289 return result
288 except:
290 except:
289 print('ERROR :: Use the "print" for more information')
291 print('ERROR :: Use the "print" for more information')
290 _, exc_value, _ = sys.exc_info()
292 _, exc_value, _ = sys.exc_info()
291 return exc_value
293 return exc_value
292 else:
294 else:
293 return "ERROR:: No file(s) found to upload"
295 return "ERROR:: No file(s) found to upload"
294 else:
296 else:
295 self.dict['match'] = package_id
297 self.dict['match'] = package_id
296 return self.ckan.call_action('package_revise', self.dict)
298 return self.ckan.call_action('package_revise', self.dict)
297
299
298
300
299 def resource_delete(self, select, id, **kwargs):
301 def resource_delete(self, select, id, **kwargs):
300
302
301 if not isinstance(id, list) and not isinstance(id, str):
303 if not isinstance(id, list) and not isinstance(id, str):
302 return 'ERROR:: "id" must be <str> or <list>'
304 return 'ERROR:: "id" must be <str> or <list>'
303
305
304 if isinstance(id, list):
306 if isinstance(id, list):
305 if not 'package_id' in kwargs:
307 if not 'package_id' in kwargs:
306 return "ERROR:: 'package_id' parameter is empty"
308 return "ERROR:: 'package_id' parameter is empty"
307 #---------------------------------------------------------------------#
309 #---------------------------------------------------------------------#
308
310
309 if 'delete' == select:
311 if 'delete' == select:
310 if kwargs.get('package_id', None):
312 if kwargs.get('package_id', None):
311 pkg_dict = getattr(self.ckan.action, 'package_show')(id=kwargs['package_id'])
313 pkg_dict = getattr(self.ckan.action, 'package_show')(id=kwargs['package_id'])
312
314
313 if pkg_dict.get('resources'):
315 if pkg_dict.get('resources'):
314 pkg_dict['resources'] = [res for res in pkg_dict['resources'] if not
316 pkg_dict['resources'] = [res for res in pkg_dict['resources'] if not
315 res['id'] in id]
317 res['id'] in id]
316 if pkg_dict['num_resources'] - len(pkg_dict['resources']) == len(id):
318 if pkg_dict['num_resources'] - len(pkg_dict['resources']) == len(id):
317 return getattr(self.ckan.action, 'package_update')(**pkg_dict)
319 return getattr(self.ckan.action, 'package_update')(**pkg_dict)
318 else:
320 else:
319 return "ERROR:: No changes have been applied"
321 return "ERROR:: No changes have been applied"
320 else:
322 else:
321 return getattr(self.ckan.action, 'resource_delete')(id=id)
323 return getattr(self.ckan.action, 'resource_delete')(id=id)
322
324
323 elif 'purge' == select:
325 elif 'purge' == select:
324 if kwargs.get('package_id', None):
326 if kwargs.get('package_id', None):
325 pkg_dict = getattr(self.ckan.action, 'package_show')(id=kwargs['package_id'])
327 pkg_dict = getattr(self.ckan.action, 'package_show')(id=kwargs['package_id'])
326 if pkg_dict.get('resources'):
328 if pkg_dict.get('resources'):
327 pkg_dict['resources'] = [res for res in pkg_dict['resources'] if not
329 pkg_dict['resources'] = [res for res in pkg_dict['resources'] if not
328 res['id'] in id]
330 res['id'] in id]
329 if pkg_dict['num_resources'] - len(pkg_dict['resources']) == len(id):
331 if pkg_dict['num_resources'] - len(pkg_dict['resources']) == len(id):
330 print('[DELETING FILES]')
332 print('[DELETING FILES]')
331 resource_patch(self=self, id=id, package_id=kwargs['package_id'], clear_upload=True)
333 resource_patch(self=self, id=id, package_id=kwargs['package_id'], clear_upload=True)
332 print('[DELETING RESOURCES]')
334 print('[DELETING RESOURCES]')
333 return getattr(self.ckan.action, 'package_update')(**pkg_dict)
335 return getattr(self.ckan.action, 'package_update')(**pkg_dict)
334 else:
336 else:
335 return "ERROR:: No changes have been applied, please check 'id' parameter"
337 return "ERROR:: No changes have been applied, please check 'id' parameter"
336
338
337 else:
339 else:
338 print('[DELETING FILE]')
340 print('[DELETING FILE]')
339 getattr(self.ckan.action, 'resource_patch')(id=id, clear_upload=True)
341 getattr(self.ckan.action, 'resource_patch')(id=id, clear_upload=True)
340 print('[DELETING RESOURCE]')
342 print('[DELETING RESOURCE]')
341 return getattr(self.ckan.action, 'resource_delete')(id=id)
343 return getattr(self.ckan.action, 'resource_delete')(id=id)
342 else:
344 else:
343 return 'ERROR:: "select = %s" is not accepted' % (select) No newline at end of file
345 return 'ERROR:: "select = %s" is not accepted' % (select)
General Comments 0
You need to be logged in to leave comments. Login now