@@ -26,13 +26,23 @@ class Discoverer(object):
2626
2727 def __init__ (self , client , cache_file ):
2828 self .client = client
29- default_cache_id = self .client .configuration .host
30- if six .PY3 :
31- default_cache_id = default_cache_id .encode ('utf-8' )
32- default_cachefile_name = 'osrcp-{0}.json' .format (hashlib .md5 (default_cache_id ).hexdigest ())
29+ default_cachefile_name = 'osrcp-{0}.json' .format (hashlib .md5 (self .__get_default_cache_id ()).hexdigest ())
3330 self .__cache_file = cache_file or os .path .join (tempfile .gettempdir (), default_cachefile_name )
3431 self .__init_cache ()
3532
33+ def __get_default_cache_id (self ):
34+ user = ""
35+ for func in [os .getlogin , os .getuid ]:
36+ try :
37+ user = str (func ())
38+ except OSError :
39+ pass
40+
41+ cache_id = "{0}-{1}" .format (self .client .configuration .host , user )
42+ if six .PY3 :
43+ return cache_id .encode ('utf-8' )
44+ return cache_id
45+
3646 def __init_cache (self , refresh = False ):
3747 if refresh or not os .path .exists (self .__cache_file ):
3848 self ._cache = {'library_version' : __version__ }
@@ -52,8 +62,12 @@ def __init_cache(self, refresh=False):
5262 self ._write_cache ()
5363
5464 def _write_cache (self ):
55- with open (self .__cache_file , 'w' ) as f :
56- json .dump (self ._cache , f , cls = CacheEncoder )
65+ try :
66+ with open (self .__cache_file , 'w' ) as f :
67+ json .dump (self ._cache , f , cls = CacheEncoder )
68+ except Exception :
69+ # Failing to write the cache shouldn't crash the library
70+ pass
5771
5872 def invalidate_cache (self ):
5973 self .__init_cache (refresh = True )
0 commit comments