24
24
CACHE_AGE = datetime .timedelta (days = 1 )
25
25
26
26
27
- def cache_load (url ):
27
+ def cache_load (url , replace_file = False ):
28
28
"""
29
29
Loads the url and store it in a temporary cache directory
30
30
subsequent requests for this url will use the cached version.
31
31
32
32
:param url: URL from where to load an odML terminology file from.
33
+ :param replace_file: True, if file should be reloaded
33
34
"""
34
35
filename = '.' .join ([md5 (url .encode ()).hexdigest (), os .path .basename (url )])
35
36
cache_dir = os .path .join (tempfile .gettempdir (), "odml.cache" )
@@ -40,9 +41,12 @@ def cache_load(url):
40
41
if not os .path .exists (cache_dir ):
41
42
raise
42
43
cache_file = os .path .join (cache_dir , filename )
44
+ if replace_file and os .path .exists (cache_file ):
45
+ os .remove (cache_file )
43
46
if not os .path .exists (cache_file ) \
44
- or datetime .datetime .fromtimestamp (os .path .getmtime (cache_file )) < \
45
- datetime .datetime .now () - CACHE_AGE :
47
+ or replace_file \
48
+ or datetime .datetime .fromtimestamp (os .path .getmtime (cache_file )) < \
49
+ datetime .datetime .now () - CACHE_AGE :
46
50
try :
47
51
data = urllib2 .urlopen (url ).read ()
48
52
if sys .version_info .major > 2 :
@@ -58,35 +62,13 @@ def cache_load(url):
58
62
return open (cache_file )
59
63
60
64
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
-
84
65
class Terminologies (dict ):
85
66
"""
86
67
Terminologies facilitates synchronous and deferred loading, caching,
87
68
browsing and importing of full or partial odML terminologies.
88
69
"""
89
70
loading = {}
71
+ reload_cache = False
90
72
91
73
def load (self , url ):
92
74
"""
@@ -115,7 +97,7 @@ def _load(self, url):
115
97
It will silently return None, if any exceptions
116
98
occur to enable loading of nested odML files.
117
99
"""
118
- file_obj = cache_load (url )
100
+ file_obj = cache_load (url , self . reload_cache )
119
101
if file_obj is None :
120
102
print ("did not successfully load '%s'" % url )
121
103
return
@@ -140,11 +122,24 @@ def deferred_load(self, url):
140
122
self .loading [url ] = threading .Thread (target = self ._load , args = (url ,))
141
123
self .loading [url ].start ()
142
124
125
+ def refresh (self , url ):
126
+ """
127
+ Deletes and reloads all cached odML XML files given in the
128
+ terminology file from a URL.
129
+
130
+ :param url: location of an odML XML file.
131
+ """
132
+ self .reload_cache = True
133
+ self .clear ()
134
+ self .load (url )
135
+ self .reload_cache = False
136
+
143
137
144
138
terminologies = Terminologies ()
145
139
load = terminologies .load
146
140
deferred_load = terminologies .deferred_load
141
+ refresh = terminologies .refresh
147
142
148
143
149
144
if __name__ == "__main__" :
150
- FILE_OBJECT = cache_load (REPOSITORY )
145
+ FILE_OBJECT = cache_load (REPOSITORY )
0 commit comments