Skip to content

Commit 76800df

Browse files
author
Am K
committed
fix FcntlStorage
1 parent be0372d commit 76800df

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

zcache/Storage/FcntlStorage.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@
3131

3232

3333
class FileLock:
34-
def __init__(self, filename):
34+
def __init__(self, filename, mode):
3535
self.filename = filename
3636
self.file_handle = None
37+
self.mode = mode
3738

3839
def __enter__(self):
39-
self.file_handle = open(self.filename, "a+")
40+
self.file_handle = open(self.filename, self.mode)
4041
fcntl.flock(self.file_handle, fcntl.LOCK_EX)
4142
self.file_handle.seek(0)
4243
return self.file_handle
@@ -53,9 +54,9 @@ class FcntlStorage(Storage):
5354
def __init__(self, path):
5455
if not isinstance(path, str):
5556
raise TypeError
57+
self.path = path
5658
if not os.path.exists(path):
5759
self.create(path)
58-
self.path = path
5960

6061
def create(self, path):
6162
data = {}
@@ -64,14 +65,13 @@ def create(self, path):
6465
data["url"] = "https://github.com/guangrei/zcache"
6566
data["data"] = {}
6667
data["limit"] = 0
67-
with FileLock(path) as f:
68-
f.write(json.dumps(data))
68+
self.save(data)
6969

7070
def load(self):
71-
with FileLock(self.path) as f:
71+
with FileLock(self.path, mode="r") as f:
7272
return json.loads(f.read())
7373

7474
def save(self, data):
7575
data = json.dumps(data)
76-
with FileLock(self.path) as f:
76+
with FileLock(self.path, mode="w") as f:
7777
f.write(data)

0 commit comments

Comments
 (0)