Skip to content

Commit 992d446

Browse files
committed
[terminology.py] Add Refresh Cache Function
1 parent 99f532a commit 992d446

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

odml/terminology.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,29 @@ def cache_load(url):
5858
return open(cache_file)
5959

6060

61+
def refresh(url):
62+
"""
63+
Deletes the current odML terminology file from cache and reloads
64+
it from the given URL.
65+
66+
:param url: URL from where to load an odML terminology file from.
67+
"""
68+
filename = '.'.join([md5(url.encode()).hexdigest(), os.path.basename(url)])
69+
cache_dir = os.path.join(tempfile.gettempdir(), "odml.cache")
70+
cache_file = os.path.join(cache_dir, filename)
71+
try:
72+
urllib2.urlopen(url).read()
73+
except Exception as exc:
74+
print('Cache for file "%s" could not be refreshed. Failed loading "%s": %s'
75+
% (filename, url, exc))
76+
return
77+
78+
if os.path.exists(cache_file):
79+
os.remove(cache_file)
80+
81+
cache_load(url)
82+
83+
6184
class Terminologies(dict):
6285
"""
6386
Terminologies facilitates synchronous and deferred loading, caching,

0 commit comments

Comments
 (0)