2222
2323
2424class Base (object ):
25-
2625 def add (self , url , content ):
2726 raise NotImplemented ()
2827
@@ -32,6 +31,7 @@ def get(self, url):
3231
3332class InMemoryCache (Base ):
3433 """Simple in-memory caching using dict lookup with support for timeouts"""
34+
3535 _cache = {} # global cache, thread-safe by default
3636
3737 def __init__ (self , timeout = 3600 ):
@@ -41,7 +41,8 @@ def add(self, url, content):
4141 logger .debug ("Caching contents of %s" , url )
4242 if not isinstance (content , (str , bytes )):
4343 raise TypeError (
44- "a bytes-like object is required, not {}" .format (type (content ).__name__ ))
44+ "a bytes-like object is required, not {}" .format (type (content ).__name__ )
45+ )
4546 self ._cache [url ] = (datetime .datetime .utcnow (), content )
4647
4748 def get (self , url ):
@@ -59,18 +60,20 @@ def get(self, url):
5960
6061class SqliteCache (Base ):
6162 """Cache contents via an sqlite database on the filesystem"""
62- _version = '1'
63+
64+ _version = "1"
6365
6466 def __init__ (self , path = None , timeout = 3600 ):
6567
6668 if sqlite3 is None :
6769 raise RuntimeError ("sqlite3 module is required for the SqliteCache" )
6870
6971 # No way we can support this when we want to achieve thread safety
70- if path == ' :memory:' :
72+ if path == " :memory:" :
7173 raise ValueError (
72- "The SqliteCache doesn't support :memory: since it is not " +
73- "thread-safe. Please use zeep.cache.InMemoryCache()" )
74+ "The SqliteCache doesn't support :memory: since it is not "
75+ + "thread-safe. Please use zeep.cache.InMemoryCache()"
76+ )
7477
7578 self ._lock = threading .RLock ()
7679 self ._timeout = timeout
@@ -83,14 +86,16 @@ def __init__(self, path=None, timeout=3600):
8386 """
8487 CREATE TABLE IF NOT EXISTS request
8588 (created timestamp, url text, content text)
86- """ )
89+ """
90+ )
8791 conn .commit ()
8892
8993 @contextmanager
9094 def db_connection (self ):
9195 with self ._lock :
9296 connection = sqlite3 .connect (
93- self ._db_path , detect_types = sqlite3 .PARSE_DECLTYPES )
97+ self ._db_path , detect_types = sqlite3 .PARSE_DECLTYPES
98+ )
9499 yield connection
95100 connection .close ()
96101
@@ -103,14 +108,14 @@ def add(self, url, content):
103108 cursor .execute ("DELETE FROM request WHERE url = ?" , (url ,))
104109 cursor .execute (
105110 "INSERT INTO request (created, url, content) VALUES (?, ?, ?)" ,
106- (datetime .datetime .utcnow (), url , data ))
111+ (datetime .datetime .utcnow (), url , data ),
112+ )
107113 conn .commit ()
108114
109115 def get (self , url ):
110116 with self .db_connection () as conn :
111117 cursor = conn .cursor ()
112- cursor .execute (
113- "SELECT created, content FROM request WHERE url=?" , (url , ))
118+ cursor .execute ("SELECT created, content FROM request WHERE url=?" , (url ,))
114119 rows = cursor .fetchall ()
115120
116121 if rows :
@@ -130,12 +135,12 @@ def _decode_data(self, data):
130135 if six .PY2 :
131136 data = str (data )
132137 if data .startswith (self ._version_string ):
133- return base64 .b64decode (data [len (self ._version_string ):])
138+ return base64 .b64decode (data [len (self ._version_string ) :])
134139
135140 @property
136141 def _version_string (self ):
137- prefix = u' $ZEEP:%s$' % self ._version
138- return bytes (prefix .encode (' ascii' ))
142+ prefix = u" $ZEEP:%s$" % self ._version
143+ return bytes (prefix .encode (" ascii" ))
139144
140145
141146def _is_expired (value , timeout ):
@@ -150,12 +155,12 @@ def _is_expired(value, timeout):
150155
151156
152157def _get_default_cache_path ():
153- path = appdirs .user_cache_dir (' zeep' , False )
158+ path = appdirs .user_cache_dir (" zeep" , False )
154159 try :
155160 os .makedirs (path )
156161 except OSError as exc :
157162 if exc .errno == errno .EEXIST and os .path .isdir (path ):
158163 pass
159164 else :
160165 raise
161- return os .path .join (path , ' cache.db' )
166+ return os .path .join (path , " cache.db" )
0 commit comments