fix StateManager to save the state
This commit is contained in:
20
run.py
20
run.py
@@ -267,6 +267,14 @@ class StateManager:
|
|||||||
with open(STATE_PATH, "w", encoding="utf-8") as f:
|
with open(STATE_PATH, "w", encoding="utf-8") as f:
|
||||||
yaml.safe_dump(self._state, f)
|
yaml.safe_dump(self._state, f)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _auto_save(func):
|
||||||
|
def wrapper(self, *args, **kwargs):
|
||||||
|
result = func(self, *args, **kwargs)
|
||||||
|
self.save()
|
||||||
|
return result
|
||||||
|
return wrapper
|
||||||
|
|
||||||
def check_space(self, lang: Language) -> bool:
|
def check_space(self, lang: Language) -> bool:
|
||||||
state = self._load()
|
state = self._load()
|
||||||
return lang.value in state.get("space", {}) and bool(state["space"][lang.value])
|
return lang.value in state.get("space", {}) and bool(state["space"][lang.value])
|
||||||
@@ -277,6 +285,7 @@ class StateManager:
|
|||||||
raise ValueError(f"No space found for language: {lang}")
|
raise ValueError(f"No space found for language: {lang}")
|
||||||
return state["space"][lang.value]
|
return state["space"][lang.value]
|
||||||
|
|
||||||
|
@_auto_save
|
||||||
def add_space(
|
def add_space(
|
||||||
self, lang: Language, file: str, location: str, is_completed: bool
|
self, lang: Language, file: str, location: str, is_completed: bool
|
||||||
) -> None:
|
) -> None:
|
||||||
@@ -290,12 +299,14 @@ class StateManager:
|
|||||||
}
|
}
|
||||||
self._state = state
|
self._state = state
|
||||||
|
|
||||||
|
@_auto_save
|
||||||
def remove_space(self, lang: Language) -> None:
|
def remove_space(self, lang: Language) -> None:
|
||||||
state = self._load()
|
state = self._load()
|
||||||
if "space" in state and lang.value in state["space"]:
|
if "space" in state and lang.value in state["space"]:
|
||||||
state["space"][lang.value] = {}
|
state["space"][lang.value] = {}
|
||||||
self._state = state
|
self._state = state
|
||||||
|
|
||||||
|
@_auto_save
|
||||||
def clear_space(self, lang: Language) -> None:
|
def clear_space(self, lang: Language) -> None:
|
||||||
state = self._load()
|
state = self._load()
|
||||||
if "space" in state and lang.value in state["space"]:
|
if "space" in state and lang.value in state["space"]:
|
||||||
@@ -305,6 +316,7 @@ class StateManager:
|
|||||||
def reload(self) -> None:
|
def reload(self) -> None:
|
||||||
self._state = None
|
self._state = None
|
||||||
|
|
||||||
|
@_auto_save
|
||||||
def set_completed(self, lang: Language, is_completed: bool) -> None:
|
def set_completed(self, lang: Language, is_completed: bool) -> None:
|
||||||
state = self._load()
|
state = self._load()
|
||||||
if lang.value not in state.get("space", {}):
|
if lang.value not in state.get("space", {}):
|
||||||
@@ -391,9 +403,7 @@ class StorageManager:
|
|||||||
raise FileNotFoundError(
|
raise FileNotFoundError(
|
||||||
f"File not found in storage: {location}/{filename}.{lang.value}"
|
f"File not found in storage: {location}/{filename}.{lang.value}"
|
||||||
)
|
)
|
||||||
file_path = f"{path}/{filename}"
|
os.remove(path)
|
||||||
if os.path.isfile(file_path):
|
|
||||||
os.remove(file_path)
|
|
||||||
|
|
||||||
def mark_completed(self, location: str, lang: Language, filename: str) -> None:
|
def mark_completed(self, location: str, lang: Language, filename: str) -> None:
|
||||||
uncompleted_file = (
|
uncompleted_file = (
|
||||||
@@ -1062,9 +1072,7 @@ def status():
|
|||||||
click.secho(" not completed", fg="cyan", bold=False, nl=False)
|
click.secho(" not completed", fg="cyan", bold=False, nl=False)
|
||||||
click.secho(")", fg="white", bold=False)
|
click.secho(")", fg="white", bold=False)
|
||||||
for lang_name, counts in sorted(langs.items()):
|
for lang_name, counts in sorted(langs.items()):
|
||||||
click.echo(
|
click.echo(f" {lang_name}: {counts['completed']}/{counts['total']}")
|
||||||
f" {lang_name}: {counts['completed']}/{counts['total']}"
|
|
||||||
)
|
|
||||||
click.echo()
|
click.echo()
|
||||||
|
|
||||||
click.secho(
|
click.secho(
|
||||||
|
|||||||
Reference in New Issue
Block a user