Skip to content

Commit c83ff19

Browse files
committed
[templates] Refactor cache_load
1 parent dcd11d2 commit c83ff19

1 file changed

Lines changed: 19 additions & 17 deletions

File tree

odml/templates.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Handles (deferred) loading of odML templates
33
"""
44

5-
import datetime
65
import os
76
import sys
87
import tempfile
@@ -18,6 +17,7 @@
1817
from urlparse import urljoin
1918

2019
from datetime import datetime as dati
20+
from datetime import timedelta
2121
from hashlib import md5
2222

2323
from .tools.parser_utils import ParserException
@@ -27,7 +27,7 @@
2727
REPOSITORY_BASE = 'https://templates.g-node.org/'
2828
REPOSITORY = urljoin(REPOSITORY_BASE, 'templates.xml')
2929

30-
CACHE_AGE = datetime.timedelta(days=1)
30+
CACHE_AGE = timedelta(days=1)
3131
CACHE_DIR = "odml.cache"
3232

3333

@@ -45,6 +45,7 @@ def cache_load(url):
4545
odML files without breaking of one of the child files is unavailable.
4646
4747
:param url: location of an odML template XML file.
48+
:return: Local file location of the requested file.
4849
"""
4950

5051
filename = '.'.join([md5(url.encode()).hexdigest(), os.path.basename(url)])
@@ -66,14 +67,13 @@ def cache_load(url):
6667
if sys.version_info.major > 2:
6768
data = data.decode("utf-8")
6869
except (ValueError, URLError) as exc:
69-
print("Could not load template from '%s': %s" % (url, exc))
70-
return
70+
print("Failed to load resource from '%s': %s" % (url, exc))
71+
raise
7172

72-
fp = open(cache_file, "w")
73-
fp.write(str(data))
74-
fp.close()
73+
with open(cache_file, "w") as local_file:
74+
local_file.write(str(data))
7575

76-
return open(cache_file)
76+
return cache_file
7777

7878

7979
class TemplateHandler(dict):
@@ -90,7 +90,7 @@ def browse(self, url):
9090
doc = self.load(url)
9191

9292
if not doc:
93-
raise ValueError("Could not load template from '%s'" % url)
93+
raise ValueError("Failed to load resource from '%s'" % url)
9494

9595
doc.pprint(max_depth=0)
9696

@@ -114,7 +114,7 @@ def clone_section(self, url, section_name, children=True, keep_id=False):
114114
"""
115115
doc = self.load(url)
116116
if not doc:
117-
raise ValueError("Could not load template from '%s'" % url)
117+
raise ValueError("Failed to load resource from '%s'" % url)
118118

119119
try:
120120
sec = doc[section_name]
@@ -148,22 +148,24 @@ def load(self, url):
148148
def _load(self, url):
149149
"""
150150
Cache loads an odML template for a URL and returns
151-
the parsed result odML document.
151+
the result as a parsed odML document.
152152
153153
:param url: location of an odML template XML file.
154154
:return: The odML document loaded from url.
155+
It will silently return None, if any exceptions
156+
occur to enable loading of nested odML files.
155157
"""
156-
fp = cache_load(url)
157-
if fp is None:
158-
print("Unable to load '%s'" % url)
159-
return
158+
try:
159+
local_file = cache_load(url)
160+
except (ValueError, URLError):
161+
return None
160162

161163
try:
162-
doc = XMLReader(filename=url, ignore_errors=True).from_file(fp)
164+
doc = XMLReader(filename=url, ignore_errors=True).from_file(local_file)
163165
doc.finalize()
164166
except ParserException as exc:
165167
print("Failed to load '%s' due to parser errors:\n %s" % (url, exc))
166-
doc = None
168+
return None
167169

168170
self[url] = doc
169171
return doc

0 commit comments

Comments
 (0)