1 | NO CONTENT: modified file, binary diff hidden |
|
NO CONTENT: modified file, binary diff hidden |
@@ -1,4 +1,6 | |||||
1 | from ckanapi import RemoteCKAN |
|
1 | from ckanapi import RemoteCKAN | |
|
2 | from datetime import datetime | |||
|
3 | from tqdm import tqdm | |||
2 | #from ckanapi.errors import NotAuthorized, NotFound, ValidationError, SearchQueryError, SearchError, CKANAPIError, ServerIncompatibleError |
|
4 | #from ckanapi.errors import NotAuthorized, NotFound, ValidationError, SearchQueryError, SearchError, CKANAPIError, ServerIncompatibleError | |
3 | import sys |
|
5 | import sys | |
4 | import platform |
|
6 | import platform | |
@@ -11,8 +13,6 import requests | |||||
11 | import json |
|
13 | import json | |
12 | import pathlib |
|
14 | import pathlib | |
13 | import uuid |
|
15 | import uuid | |
14 | from datetime import datetime |
|
|||
15 | from tqdm import tqdm |
|
|||
16 |
|
16 | |||
17 | class JROAPI(): |
|
17 | class JROAPI(): | |
18 | """ |
|
18 | """ | |
@@ -25,8 +25,10 class JROAPI(): | |||||
25 | En Python 2 |
|
25 | En Python 2 | |
26 | - pip install ckanapi==4.5 |
|
26 | - pip install ckanapi==4.5 | |
27 | - pip install requests |
|
27 | - pip install requests | |
|
28 | - pip install pathlib | |||
|
29 | - pip install futures | |||
28 | - pip install tqdm |
|
30 | - pip install tqdm | |
29 | En Python 3 |
|
31 | En Python > 3 | |
30 | - pip3 install ckanapi==4.5 |
|
32 | - pip3 install ckanapi==4.5 | |
31 | - pip3 install requests |
|
33 | - pip3 install requests | |
32 | - pip3 install tqdm |
|
34 | - pip3 install tqdm | |
@@ -54,10 +56,10 class JROAPI(): | |||||
54 |
|
56 | |||
55 | REPORTAR ALGUN PROBLEMA: |
|
57 | REPORTAR ALGUN PROBLEMA: | |
56 | Debe enviar un correo a eynilupu@igp.gob.pe detallando los siguientes pasos: |
|
58 | Debe enviar un correo a eynilupu@igp.gob.pe detallando los siguientes pasos: | |
57 | 1) Identifiquese |
|
59 | 1) Correo para contactarlo | |
58 |
2) Descri |
|
60 | 2) Descripcion del problema | |
59 |
3) ¿En que |
|
61 | 3) ¿En que paso o seccion encontro el problema? | |
60 | 4) ¿Que esperaba que hiciera la funcion sin el problema? |
|
62 | 4) ¿Cual era el resultado que usted esperaba? | |
61 | """ |
|
63 | """ | |
62 | def __init__(self, url, Authorization=None): |
|
64 | def __init__(self, url, Authorization=None): | |
63 | ua = 'CKAN_JRO/1.1 (+'+str(url)+')' |
|
65 | ua = 'CKAN_JRO/1.1 (+'+str(url)+')' | |
@@ -110,6 +112,7 class JROAPI(): | |||||
110 | return exc_value |
|
112 | return exc_value | |
111 |
|
113 | |||
112 | def upload_file(self, dataset_id, file_path, file_date, file_type, **kwargs): |
|
114 | def upload_file(self, dataset_id, file_path, file_date, file_type, **kwargs): | |
|
115 | # Agregar si es interruptido por teclado | |||
113 | ''' |
|
116 | ''' | |
114 | FINALIDAD: |
|
117 | FINALIDAD: | |
115 | Funcion para subir un unico archivo al repositorio del ROJ. |
|
118 | Funcion para subir un unico archivo al repositorio del ROJ. | |
@@ -147,8 +150,8 class JROAPI(): | |||||
147 | _, exc_value, _ = sys.exc_info() |
|
150 | _, exc_value, _ = sys.exc_info() | |
148 | return exc_value |
|
151 | return exc_value | |
149 |
|
152 | |||
150 |
|
153 | def upload_multiple_files_advance(self, dataset_id, path_files, file_date, file_type, max_size=100, ignore_repetition=False, **kwargs): | ||
151 | def upload_multiple_files_advance(self, dataset_id, path_files, file_date, file_type, **kwargs): |
|
154 | # Agregar si es interruptido por teclado | |
152 | ''' |
|
155 | ''' | |
153 | FINALIDAD: |
|
156 | FINALIDAD: | |
154 | Funcion para subir multiples archivos al repositorio del ROJ. |
|
157 | Funcion para subir multiples archivos al repositorio del ROJ. | |
@@ -159,41 +162,59 class JROAPI(): | |||||
159 | ESTRUCTURA: |
|
162 | ESTRUCTURA: | |
160 | <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'>, ...) |
|
163 | <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'>, ...) | |
161 | ''' |
|
164 | ''' | |
|
165 | #-------------------------PACKAGE SHOW-----------------------# | |||
|
166 | try: | |||
|
167 | dataset_show = getattr(self.ckan.action, 'package_show')(id=dataset_id)['resources'] | |||
|
168 | except: | |||
|
169 | _, exc_value, _ = sys.exc_info() | |||
|
170 | print('ERROR obtaining metadata dataset:: Use the "print" for more information') | |||
|
171 | return exc_value | |||
|
172 | #------------------------------------------------------------# | |||
|
173 | resources_name = [] | |||
|
174 | for u in dataset_show: | |||
|
175 | resources_name.append(u['name'].lower()) | |||
|
176 | #------------------------------------------------------------# | |||
162 | self.list = ['package_id', 'upload', 'voc_file_type', 'name'] |
|
177 | self.list = ['package_id', 'upload', 'voc_file_type', 'name'] | |
163 |
|
||||
164 | for key1, value1 in kwargs.items(): |
|
178 | for key1, value1 in kwargs.items(): | |
165 | if not key1 in self.list: |
|
179 | if not key1 in self.list: | |
166 | self.dict[key1] = value1 |
|
180 | self.dict[key1] = value1 | |
167 |
|
181 | #------------------------------------------------------------# | ||
168 | #---------------------------# |
|
|||
169 | if not 'others' in kwargs: |
|
182 | if not 'others' in kwargs: | |
170 | self.dict['others'] = '' |
|
183 | self.dict['others'] = '' | |
171 | else: |
|
184 | else: | |
172 | if isinstance(kwargs['others'], list): |
|
185 | if isinstance(kwargs['others'], list): | |
173 | self.dict['others'] = json.dumps(kwargs['others']) |
|
186 | self.dict['others'] = json.dumps(kwargs['others']) | |
174 | #---------------------------# |
|
187 | #------------------------------------------------------------# | |
175 |
|
188 | total_list = [] | ||
176 | params_dict = {'upload':[], 'name':[]} |
|
|||
177 | #---------------CASO : "path" or "path_list"-----------------# |
|
189 | #---------------CASO : "path" or "path_list"-----------------# | |
178 | if type(path_files) is list: |
|
190 | if type(path_files) is list: | |
179 | if len(path_files) != 0: |
|
191 | if len(path_files) != 0: | |
180 | path_files.sort() |
|
192 | path_files.sort() | |
181 | for u in path_files: |
|
193 | for u in path_files: | |
182 | if os.path.isfile(u): |
|
194 | if os.path.isfile(u): | |
183 | params_dict['upload'].append(open(u, 'rb')) |
|
195 | if pathlib.Path(u).name.lower() in resources_name: | |
184 | params_dict['name'].append(pathlib.Path(u).name) |
|
196 | if not ignore_repetition: | |
|
197 | return 'ERROR:: "%s" file already exist in this dataset' % (pathlib.Path(u).name) | |||
|
198 | print('WARRING:: "'+ str(pathlib.Path(u).name) +'" file was ignored because already exist in this dataset') | |||
|
199 | else: | |||
|
200 | total_list.append({'name':pathlib.Path(u).name, 'size': os.stat(u).st_size, 'upload':open(u, 'rb')}) | |||
185 | else: |
|
201 | else: | |
186 | return 'File "%s" does not exist' % (u) |
|
202 | return 'File "%s" does not exist' % (u) | |
187 | else: |
|
203 | else: | |
188 | return 'ERROR:: "path_list is empty"' |
|
204 | return 'ERROR:: "path_list is empty"' | |
|
205 | ||||
189 | elif type(path_files) is str: |
|
206 | elif type(path_files) is str: | |
190 | if os.path.isdir(path_files): |
|
207 | if os.path.isdir(path_files): | |
191 | path_order = [f for f in os.listdir(path_files) if os.path.isfile(os.path.join(path_files, f))] |
|
208 | path_order = [f for f in os.listdir(path_files) if os.path.isfile(os.path.join(path_files, f))] | |
192 | path_order.sort() |
|
209 | path_order.sort() | |
193 | if path_order: |
|
210 | if path_order: | |
194 | for name in path_order: |
|
211 | for name in path_order: | |
195 | params_dict['upload'].append(open(os.path.join(path_files, name), 'rb')) |
|
212 | if name.lower() in resources_name: | |
196 | params_dict['name'].append(name) |
|
213 | if not ignore_repetition: | |
|
214 | return 'ERROR:: "%s" file already exist in this dataset' % (name) | |||
|
215 | print('WARRING:: "'+ name +'" file was ignored because already exist in this dataset') | |||
|
216 | else: | |||
|
217 | 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')}) | |||
197 | else: |
|
218 | else: | |
198 | return "ERROR:: There aren't files in this directory" |
|
219 | return "ERROR:: There aren't files in this directory" | |
199 | else: |
|
220 | else: | |
@@ -201,42 +222,67 class JROAPI(): | |||||
201 | else: |
|
222 | else: | |
202 | return 'ERROR:: "path_files" must be a str or list' |
|
223 | return 'ERROR:: "path_files" must be a str or list' | |
203 | #------------------------------------------------------------# |
|
224 | #------------------------------------------------------------# | |
204 | resource_extend = [] |
|
|||
205 | files_dict = {} |
|
|||
206 | for count, name in enumerate(params_dict['name']): |
|
|||
207 | param_list = {"name": name, "file_date": file_date, "voc_file_type": file_type} |
|
|||
208 | param_list.update(self.dict) |
|
|||
209 |
|
||||
210 | if not 'format' in param_list: |
|
|||
211 | format = ''.join(pathlib.Path(name).suffixes) |
|
|||
212 | if len(format) > 0: |
|
|||
213 | param_list['format'] = format.upper()[1:] |
|
|||
214 |
|
||||
215 | resource_extend.append(param_list) |
|
|||
216 | files_dict['update__resources__-'+ str(len(params_dict['name'])-count) +'__upload'] = (name, params_dict['upload'][count]) |
|
|||
217 |
|
||||
218 | #------------------------------------------------------------# |
|
|||
219 | try: |
|
225 | try: | |
220 | uuid.UUID(str(dataset_id), version=4) |
|
226 | uuid.UUID(str(dataset_id), version=4) | |
221 | package_id_or_name = '"id": "' + str(dataset_id) + '"' |
|
227 | package_id_or_name = '"id": "' + str(dataset_id) + '"' | |
222 | except ValueError: |
|
228 | except ValueError: | |
223 | package_id_or_name = '"name": "' + str(dataset_id) + '"' |
|
229 | package_id_or_name = '"name": "' + str(dataset_id) + '"' | |
224 | #------------------------------------------------------------# |
|
230 | #------------------------------------------------------------# | |
225 | print('"{}" file(s) found >> uploading'.format(len(params_dict['name']))) |
|
231 | blocks = [[]] | |
226 | try: |
|
232 | size_file = 0 | |
227 | result = self.ckan.call_action( |
|
233 | inter_num = 0 | |
228 | 'package_revise', |
|
234 | for value in total_list: | |
229 | {'match': '{'+ str(package_id_or_name) +'}', 'update__resources__extend': json.dumps(resource_extend)}, |
|
235 | if value['size'] > 1048576 * float(max_size): | |
230 | files=files_dict |
|
236 | return 'ERROR:: The size of the "%s" file is %sMB, please change "max_size" value' % (value['name'], str(round(value['size']/1048576, 1))) | |
231 | ) |
|
237 | size_file = size_file + value['size'] | |
232 | print('Uploaded file(s) successfully') |
|
238 | if size_file <= 1048576 * float(max_size): | |
233 | return result |
|
239 | del value['size'] | |
234 | except: |
|
240 | blocks[inter_num].append(value) | |
235 | print('ERROR :: Use the "print" option for more information') |
|
241 | else: | |
236 | _, exc_value, _ = sys.exc_info() |
|
242 | inter_num = inter_num + 1 | |
237 | return exc_value |
|
243 | blocks.append([]) | |
|
244 | del value['size'] | |||
|
245 | blocks[inter_num].append(value) | |||
|
246 | #------------------------------------------------------------# | |||
|
247 | if len(blocks[0]) > 0: | |||
|
248 | print('BLOCK(S) IN TOTAL:: {}'.format(len(blocks))) | |||
|
249 | for count1, block in enumerate(blocks): | |||
|
250 | print('---- BLOCK N°{} ----'.format(count1 + 1)) | |||
|
251 | resource_extend = [] | |||
|
252 | files_dict = {} | |||
|
253 | for count2, value2 in enumerate(block): | |||
|
254 | value2['file_date'] = file_date | |||
|
255 | value2['voc_file_type'] = file_type | |||
|
256 | value2.update(self.dict) | |||
|
257 | ||||
|
258 | if not 'format' in value2: | |||
|
259 | format = ''.join(pathlib.Path(value2['name']).suffixes) | |||
|
260 | if len(format) > 0: | |||
|
261 | value2['format'] = format.upper()[1:] | |||
|
262 | ||||
|
263 | files_dict['update__resources__-'+ str(len(block)-count2) +'__upload'] = (value2['name'], value2['upload']) | |||
|
264 | del value2['upload'] | |||
|
265 | resource_extend.append(value2) | |||
|
266 | ||||
|
267 | print('BLOCK N°{} :: "{}" file(s) found >> uploading'.format(count1 + 1, len(block))) | |||
|
268 | try: | |||
|
269 | result = self.ckan.call_action( | |||
|
270 | 'package_revise', | |||
|
271 | {'match': '{'+ str(package_id_or_name) +'}', 'update__resources__extend': json.dumps(resource_extend)}, | |||
|
272 | files=files_dict | |||
|
273 | ) | |||
|
274 | print('BLOCK N°{} :: Uploaded file(s) successfully'.format(count1 + 1)) | |||
|
275 | if len(blocks) == count1 + 1: | |||
|
276 | return result | |||
|
277 | except: | |||
|
278 | print('ERROR :: Use the "print" for more information') | |||
|
279 | _, exc_value, _ = sys.exc_info() | |||
|
280 | return exc_value | |||
|
281 | else: | |||
|
282 | return "ERROR:: No file(s) found to upload" | |||
238 |
|
283 | |||
239 | def upload_multiple_files(self, dataset_id, path_files, date_files, type_files, **kwargs): |
|
284 | def upload_multiple_files(self, dataset_id, path_files, date_files, type_files, **kwargs): | |
|
285 | # Agregar si es interruptido por teclado | |||
240 | ''' |
|
286 | ''' | |
241 | FINALIDAD: |
|
287 | FINALIDAD: | |
242 | Funcion para subir multiples archivos al repositorio del ROJ. |
|
288 | Funcion para subir multiples archivos al repositorio del ROJ. |
General Comments 0
You need to be logged in to leave comments.
Login now