2424CACHE_AGE = datetime .timedelta (days = 1 )
2525
2626
27- def cache_load (url ):
27+ def cache_load (url , replace_file = False ):
2828 """
2929 Loads the url and store it in a temporary cache directory
3030 subsequent requests for this url will use the cached version.
3131
3232 :param url: URL from where to load an odML terminology file from.
33+ :param replace_file: True, if file should be reloaded
3334 """
3435 filename = '.' .join ([md5 (url .encode ()).hexdigest (), os .path .basename (url )])
3536 cache_dir = os .path .join (tempfile .gettempdir (), "odml.cache" )
@@ -40,9 +41,12 @@ def cache_load(url):
4041 if not os .path .exists (cache_dir ):
4142 raise
4243 cache_file = os .path .join (cache_dir , filename )
44+ if replace_file and os .path .exists (cache_file ):
45+ os .remove (cache_file )
4346 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 :
4650 try :
4751 data = urllib2 .urlopen (url ).read ()
4852 if sys .version_info .major > 2 :
@@ -58,35 +62,13 @@ def cache_load(url):
5862 return open (cache_file )
5963
6064
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-
8465class Terminologies (dict ):
8566 """
8667 Terminologies facilitates synchronous and deferred loading, caching,
8768 browsing and importing of full or partial odML terminologies.
8869 """
8970 loading = {}
71+ reload_cache = False
9072
9173 def load (self , url ):
9274 """
@@ -115,7 +97,7 @@ def _load(self, url):
11597 It will silently return None, if any exceptions
11698 occur to enable loading of nested odML files.
11799 """
118- file_obj = cache_load (url )
100+ file_obj = cache_load (url , self . reload_cache )
119101 if file_obj is None :
120102 print ("did not successfully load '%s'" % url )
121103 return
@@ -140,11 +122,24 @@ def deferred_load(self, url):
140122 self .loading [url ] = threading .Thread (target = self ._load , args = (url ,))
141123 self .loading [url ].start ()
142124
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+
143137
144138terminologies = Terminologies ()
145139load = terminologies .load
146140deferred_load = terminologies .deferred_load
141+ refresh = terminologies .refresh
147142
148143
149144if __name__ == "__main__" :
150- FILE_OBJECT = cache_load (REPOSITORY )
145+ FILE_OBJECT = cache_load (REPOSITORY )
0 commit comments