Compare commits
4 Commits
34eac82c7a
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
| b4dff1d344 | |||
| b3cd28471e | |||
| b15648a17c | |||
| bfb79e573d |
38
run.py
38
run.py
@@ -267,6 +267,15 @@ 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 +286,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 +300,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 +317,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", {}):
|
||||||
@@ -370,7 +383,7 @@ class StorageManager:
|
|||||||
) -> None:
|
) -> None:
|
||||||
path = STORAGE_DIR / location / lang.value / ("completed" if completed else "")
|
path = STORAGE_DIR / location / lang.value / ("completed" if completed else "")
|
||||||
os.makedirs(path, exist_ok=True)
|
os.makedirs(path, exist_ok=True)
|
||||||
with open(f"{path}/{filename}", "wb") as f:
|
with open(path / f"{filename}.{lang.value}", "wb") as f:
|
||||||
f.write(content)
|
f.write(content)
|
||||||
|
|
||||||
def read_file(self, location: str, lang: Language, filename: str) -> bytes:
|
def read_file(self, location: str, lang: Language, filename: str) -> bytes:
|
||||||
@@ -379,8 +392,8 @@ 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}"
|
file_path = path / f"{filename}.{lang.value}"
|
||||||
if not os.path.isfile(file_path):
|
if not file_path.is_file():
|
||||||
raise FileNotFoundError(f"File not found in storage: {file_path}")
|
raise FileNotFoundError(f"File not found in storage: {file_path}")
|
||||||
with open(file_path, "rb") as f:
|
with open(file_path, "rb") as f:
|
||||||
return f.read()
|
return f.read()
|
||||||
@@ -391,9 +404,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 = (
|
||||||
@@ -541,7 +552,7 @@ def register(location: str):
|
|||||||
@click.option(
|
@click.option(
|
||||||
"--force", "-f", is_flag=True, help="Overwrite existing file if it already exists"
|
"--force", "-f", is_flag=True, help="Overwrite existing file if it already exists"
|
||||||
)
|
)
|
||||||
def create(target: str, completed: bool, template: bool, force: bool):
|
def create(target: str, completed: bool, no_template: bool, force: bool):
|
||||||
"""
|
"""
|
||||||
지정된 location을 storage에 생성하는 명령어
|
지정된 location을 storage에 생성하는 명령어
|
||||||
"""
|
"""
|
||||||
@@ -563,10 +574,10 @@ def create(target: str, completed: bool, template: bool, force: bool):
|
|||||||
if not force:
|
if not force:
|
||||||
return
|
return
|
||||||
|
|
||||||
if not template:
|
if no_template:
|
||||||
content = b""
|
content = b""
|
||||||
else:
|
else:
|
||||||
template_path = TEMPLATES_DIR / f"{lang.value}.txt"
|
template_path = TEMPLATES_DIR / f"template.{lang.value}"
|
||||||
if template_path.is_file():
|
if template_path.is_file():
|
||||||
content = template_path.read_bytes()
|
content = template_path.read_bytes()
|
||||||
click.secho(f"Using template: {template_path}", fg="cyan")
|
click.secho(f"Using template: {template_path}", fg="cyan")
|
||||||
@@ -575,7 +586,7 @@ def create(target: str, completed: bool, template: bool, force: bool):
|
|||||||
click.secho(f"Template not found: {template_path}", fg="yellow")
|
click.secho(f"Template not found: {template_path}", fg="yellow")
|
||||||
|
|
||||||
StorageManager().save_file(
|
StorageManager().save_file(
|
||||||
loc, lang, f"{filename}.{lang.value}", content, completed
|
loc, lang, filename, content, completed
|
||||||
)
|
)
|
||||||
click.secho(
|
click.secho(
|
||||||
f"Created '{filename}.{lang.value}' in location '{loc}'",
|
f"Created '{filename}.{lang.value}' in location '{loc}'",
|
||||||
@@ -757,10 +768,11 @@ def export(from_: tuple[str, ...], all_: bool, force: bool):
|
|||||||
|
|
||||||
with open(src_path, "rb") as f_src:
|
with open(src_path, "rb") as f_src:
|
||||||
content = f_src.read()
|
content = f_src.read()
|
||||||
|
os.remove(src_path)
|
||||||
StorageManager().save_file(
|
StorageManager().save_file(
|
||||||
s_loc, from_language, s_file, content, s_is_completed
|
s_loc, from_language, s_file, content, s_is_completed
|
||||||
)
|
)
|
||||||
|
StateManager().clear_space(from_language)
|
||||||
click.secho(
|
click.secho(
|
||||||
f"Exported '{s_file}.{from_language.value}' to location '{s_loc}'",
|
f"Exported '{s_file}.{from_language.value}' to location '{s_loc}'",
|
||||||
fg="cyan",
|
fg="cyan",
|
||||||
@@ -1062,9 +1074,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