Skip to content

Commit

Permalink
fix FcntlStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
Am K committed Sep 28, 2024
1 parent be0372d commit 76800df
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions zcache/Storage/FcntlStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@


class FileLock:
def __init__(self, filename):
def __init__(self, filename, mode):
self.filename = filename
self.file_handle = None
self.mode = mode

def __enter__(self):
self.file_handle = open(self.filename, "a+")
self.file_handle = open(self.filename, self.mode)
fcntl.flock(self.file_handle, fcntl.LOCK_EX)
self.file_handle.seek(0)
return self.file_handle
Expand All @@ -53,9 +54,9 @@ class FcntlStorage(Storage):
def __init__(self, path):
if not isinstance(path, str):
raise TypeError
self.path = path
if not os.path.exists(path):
self.create(path)
self.path = path

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

def load(self):
with FileLock(self.path) as f:
with FileLock(self.path, mode="r") as f:
return json.loads(f.read())

def save(self, data):
data = json.dumps(data)
with FileLock(self.path) as f:
with FileLock(self.path, mode="w") as f:
f.write(data)

0 comments on commit 76800df

Please sign in to comment.