keep api at v3, see #10

This commit is contained in:
Christian Zangl 2023-12-22 21:45:36 +01:00
parent 360167c5ce
commit aab7a2bd8f
No known key found for this signature in database
GPG Key ID: 6D468AC36E2A4B3D
3 changed files with 19 additions and 16 deletions

View File

@ -9,7 +9,7 @@ VERSION = 2 # index version
class Index: class Index:
def __init__(self, context, path, files): def __init__(self, context, path, files, *, readonly=False):
self.context = context self.context = context
self.path = path self.path = path
self.files = files self.files = files
@ -18,7 +18,8 @@ class Index:
self.ignore = [] self.ignore = []
self.load_ignore() self.load_ignore()
self.updates = [] self.updates = []
self.modified = True self.modified = None
self.readonly = readonly
@property @property
def ignore_filepath(self): def ignore_filepath(self):
@ -34,14 +35,14 @@ class Index:
return True return True
return False return False
def _setmod(self): def _setmod(self, value=True):
self.modified = True self.modified = value
def _log(self, stat: Status, name: str): def _log(self, stat: Status, name: str):
self.context.log(stat, os.path.join(self.path, name)) self.context.log(stat, os.path.join(self.path, name))
# calc new hashes for this index # calc new hashes for this index
def update(self, update): def update(self):
for name in self.files: for name in self.files:
if self.should_ignore(name): if self.should_ignore(name):
self._log(Status.SKIP, name) self._log(Status.SKIP, name)
@ -59,10 +60,10 @@ class Index:
a = old["a"] a = old["a"]
self.new[name] = self._calc_file(name, a) self.new[name] = self._calc_file(name, a)
else: else:
if update: if self.readonly:
self.new[name] = self._calc_file(name, a)
else:
self.new[name] = self._list_file(name, a) self.new[name] = self._list_file(name, a)
else:
self.new[name] = self._calc_file(name, a)
# check/update the index (old vs new) # check/update the index (old vs new)
def check_fix(self, force): def check_fix(self, force):
@ -101,8 +102,7 @@ class Index:
self._setmod() self._setmod()
def _list_file(self, name, a): def _list_file(self, name, a):
path = os.path.join(self.path, name) # produce a dummy entry for new files when the index is not updated
self.context.hit(cfiles=1)
return { return {
"mod": None, "mod": None,
"a": a, "a": a,
@ -123,13 +123,16 @@ class Index:
def save(self): def save(self):
if self.modified: if self.modified:
if self.readonly:
raise Exception("Error trying to save a readonly index.")
data = {"v": VERSION, "idx": self.new} data = {"v": VERSION, "idx": self.new}
text = json.dumps(self.new, separators=(",", ":")) text = json.dumps(self.new, separators=(",", ":"))
data["idx_hash"] = hashtext(text) data["idx_hash"] = hashtext(text)
with open(self.index_filepath, "w", encoding="utf-8") as f: with open(self.index_filepath, "w", encoding="utf-8") as f:
json.dump(data, f, separators=(",", ":")) json.dump(data, f, separators=(",", ":"))
self.modified = False self._setmod(False)
return True return True
else: else:
return False return False
@ -137,7 +140,7 @@ class Index:
def load(self): def load(self):
if not os.path.exists(self.index_filepath): if not os.path.exists(self.index_filepath):
return False return False
self.modified = False self._setmod(False)
with open(self.index_filepath, "r", encoding="utf-8") as f: with open(self.index_filepath, "r", encoding="utf-8") as f:
data = json.load(f) data = json.load(f)
if "data" in data: if "data" in data:
@ -152,7 +155,7 @@ class Index:
self.old = data["idx"] self.old = data["idx"]
text = json.dumps(self.old, separators=(",", ":")) text = json.dumps(self.old, separators=(",", ":"))
if data.get("idx_hash") != hashtext(text): if data.get("idx_hash") != hashtext(text):
self.modified = True self._setmod()
self._log(Status.ERR_IDX, self.index_filepath) self._log(Status.ERR_IDX, self.index_filepath)
return True return True

View File

@ -33,11 +33,11 @@ class IndexThread:
files.append(name) files.append(name)
# load index # load index
index = Index(self.context, parent, files) index = Index(self.context, parent, files, readonly=not self.update)
index.load() index.load()
# calc the new hashes # calc the new hashes
index.update(self.update) index.update()
# compare # compare
index.check_fix(self.context.force) index.check_fix(self.context.force)

View File

@ -1,6 +1,6 @@
[project] [project]
name = "chkbit" name = "chkbit"
version = "3.0.1" version = "3.0.2"
description = "chkbit checks the data integrity of your files" description = "chkbit checks the data integrity of your files"
authors = [ authors = [
{name = "Christian Zangl", email = "laktak@cdak.net"}, {name = "Christian Zangl", email = "laktak@cdak.net"},