From 8546f4a533b7cda4e9bf2f8ad7351b390c534439 Mon Sep 17 00:00:00 2001 From: anonymous <anonymous> Date: Mon, 3 Jun 2019 11:13:18 +0200 Subject: [PATCH] changed fallback-filepath --- ckanext/odsh/profiles.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/ckanext/odsh/profiles.py b/ckanext/odsh/profiles.py index 34ed6957..084be5e8 100644 --- a/ckanext/odsh/profiles.py +++ b/ckanext/odsh/profiles.py @@ -110,23 +110,29 @@ def resource_formats(): urlresponse = urllib.request.urlopen(urllib.request.Request(format_european_url)) g.parse(urlresponse) # At the moment, there are 143 different file types listed, - # if less than 120 are found, something went wrong. - assert len(set([s for s in g.subjects()])) > 120 + # if less than 120 are found, something went wrong. + if len(set([s for s in g.subjects()])) > 120: + raise ValueError("Not enough subjects") # Save the content as backup if sys.version_info[0] == 2: urlresponse = urllib2.urlopen(urllib2.Request(format_european_url)) elif sys.version_info[0] == 3: # >=Python3.1 urlresponse = urllib.request.urlopen(urllib.request.Request(format_european_url)) - err_msg = "Could not write to /usr/lib/ckan/default/src/ckanext-odsh/ckanext/odsh/fileformats.rdf" - f = open('/usr/lib/ckan/default/src/ckanext-odsh/ckanext/odsh/fileformats.rdf', 'w') + fallback_filepath = config.get('ckan.odsh.resource_formats_fallback_filepath') + if not fallback_filepath: + log.warning("Could not find config setting: 'ckan.odsh.resource_formats_fallback_filepath', using fallback instead.") + fallback_filepath = "/tmp/fileformats.rdf" + err_msg = "Could not write to " + fallback_filepath + f = open(fallback_filepath, 'w') f.write(urlresponse.read()) f.close() except: + log.exception("failed to process resource_formats") # Something went wrong with trying to get the file formats online, try to use backup instead try: - g.parse('/usr/lib/ckan/default/src/ckanext-odsh/ckanext/odsh/fileformats.rdf') + g.parse(fallback_filepath) assert len(set([s for s in g.subjects()])) > 120 - log.warning("Could not get file formats from " + format_european_url + ", using fallback instead.") + log.info("Could not get file formats from " + format_european_url + ", using fallback instead.") except: raise Exception(err_msg) file_types = [subj.decode() for subj in g.subjects()] @@ -180,4 +186,4 @@ def get_language(): for language_line in language_mapping_table: _LANGUAGES[language_line[0]] = language_line[1] - return _LANGUAGES + return _LANGUAGES \ No newline at end of file -- GitLab