File tree 1 file changed +7
-7
lines changed
1 file changed +7
-7
lines changed Original file line number Diff line number Diff line change 31
31
32
32
33
33
class FileLock :
34
- def __init__ (self , filename ):
34
+ def __init__ (self , filename , mode ):
35
35
self .filename = filename
36
36
self .file_handle = None
37
+ self .mode = mode
37
38
38
39
def __enter__ (self ):
39
- self .file_handle = open (self .filename , "a+" )
40
+ self .file_handle = open (self .filename , self . mode )
40
41
fcntl .flock (self .file_handle , fcntl .LOCK_EX )
41
42
self .file_handle .seek (0 )
42
43
return self .file_handle
@@ -53,9 +54,9 @@ class FcntlStorage(Storage):
53
54
def __init__ (self , path ):
54
55
if not isinstance (path , str ):
55
56
raise TypeError
57
+ self .path = path
56
58
if not os .path .exists (path ):
57
59
self .create (path )
58
- self .path = path
59
60
60
61
def create (self , path ):
61
62
data = {}
@@ -64,14 +65,13 @@ def create(self, path):
64
65
data ["url" ] = "https://github.com/guangrei/zcache"
65
66
data ["data" ] = {}
66
67
data ["limit" ] = 0
67
- with FileLock (path ) as f :
68
- f .write (json .dumps (data ))
68
+ self .save (data )
69
69
70
70
def load (self ):
71
- with FileLock (self .path ) as f :
71
+ with FileLock (self .path , mode = "r" ) as f :
72
72
return json .loads (f .read ())
73
73
74
74
def save (self , data ):
75
75
data = json .dumps (data )
76
- with FileLock (self .path ) as f :
76
+ with FileLock (self .path , mode = "w" ) as f :
77
77
f .write (data )
You can’t perform that action at this time.
0 commit comments