From 44b99489d775ad4094e0da4f708fdbcca4d68804 Mon Sep 17 00:00:00 2001 From: Daniel Neuwirth <daniel.neuwirth@dataport.de> Date: Thu, 21 Nov 2019 12:24:29 +0000 Subject: [PATCH] bugfix tools --- ckanext/odsh/tools.py | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/ckanext/odsh/tools.py b/ckanext/odsh/tools.py index 9fbd4f90..ab7c1ba6 100644 --- a/ckanext/odsh/tools.py +++ b/ckanext/odsh/tools.py @@ -13,22 +13,33 @@ def add_attributes_resources(context, resource): for item in resources: if item.get('id') == resource.get('id'): path = get_filepath_to_resource(resource) - with open(path, 'rb') as file: - if not item.get('size'): - resource_size = os.path.getsize(path) - item.update({'size': resource_size}) - file.seek(0) - hash = calculate_hash(file) - item.update({'hash':hash}) - file_type = magic.from_file(path, mime = True) - if file_type == 'application/pdf': - file.seek(0) - pdf = pdftotext.PDF(file) - number_of_pages = len(pdf) - item.update({'number_of_pages':number_of_pages}) - item.update({'hash_algorithm': 'http://dcat-ap.de/def/hashAlgorithms/md/5'}) - resources[i] = item - break + if os.path.exists(path): + with open(path, 'rb') as file: + + #size + if not item.get('size'): + resource_size = os.path.getsize(path) + item.update({'size': resource_size}) + + #hash + file.seek(0) + hash = calculate_hash(file) + item.update({'hash':hash}) + + #hash algorithm + item.update({'hash_algorithm': 'http://dcat-ap.de/def/hashAlgorithms/md/5'}) + + + #number of pages + file_type = magic.from_file(path, mime = True) + if file_type == 'application/pdf': + file.seek(0) + pdf = pdftotext.PDF(file) + number_of_pages = len(pdf) + item.update({'number_of_pages':number_of_pages}) + + resources[i] = item + break i = i + 1 package.update({'resources':resources}) toolkit.get_action('package_update')(context, package) -- GitLab