feat: ACF class

This commit is contained in:
aclist committed 2026-07-07 18:10:52 +09:00
1 parent 0ce3ab3915
commit 33e367182e
1 file changed
+39 -15
+39 -15
View File
@@ -1,12 +1,18 @@
import logging
import re import re
from collections.abc import Iterator from collections.abc import Iterator
from pathlib import Path
from typing import Any from typing import Any
from warnings import deprecated from warnings import deprecated
from dzgui.const.constants import APP_NAME
@deprecated("Use dzgui.api.steam.unsubscribe()") logger = logging.getLogger(APP_NAME)
class WorkshopACF:
def __init__(self, file: str) -> None:
class ACF:
def __init__(self, file: Path) -> None:
super().__init__() super().__init__()
self.dict: dict[str, Any] self.dict: dict[str, Any]
@@ -15,9 +21,10 @@ class WorkshopACF:
def as_dict(self) -> dict[str, Any]: def as_dict(self) -> dict[str, Any]:
return self.dict return self.dict
def load(self, file: str) -> None: def load(self, file: Path) -> None:
delimiter = r"\t\t" delimiter = r"\t\t"
lines = [] lines = []
# TODO: illegal file handling
with open(file, "r", encoding="utf-8") as f: with open(file, "r", encoding="utf-8") as f:
for line in f: for line in f:
line = line.strip() line = line.strip()
@@ -27,6 +34,14 @@ class WorkshopACF:
lines.append(els) lines.append(els)
self.dict = self.parse(iter(lines)) self.dict = self.parse(iter(lines))
@classmethod
def enquote(cls, s: str) -> str:
return f'"{s}"'
@classmethod
def dequote(cls, s: str) -> str:
return s.rstrip('"').lstrip('"')
def parse(self, lines: Iterator[list[str]]) -> dict[str, str]: def parse(self, lines: Iterator[list[str]]) -> dict[str, str]:
acf: dict[str, Any] = {} acf: dict[str, Any] = {}
try: try:
@@ -54,6 +69,24 @@ class WorkshopACF:
except StopIteration: except StopIteration:
return acf return acf
def get_allows_downloads(self) -> int | None:
try:
# TODO: consider coercing strs to ints on initial import
flag = int(self.dict["AppState"]["AllowOtherDownloadsWhileRunning"])
return flag
except Exception as e:
logger.critical(e)
return None
@deprecated(
"Serializing workshop files is no longer supported, use dzgui.api.steam.unsubscribe"
)
class WorkshopACF(ACF):
def __init__(self, file: Path) -> None:
super().__init__(file)
# TODO: break into workshop parser and generic acf parser
def unpack(self, d: dict, lines: list[Any] = []) -> str: def unpack(self, d: dict, lines: list[Any] = []) -> str:
t1 = "AppWorkshop" t1 = "AppWorkshop"
t2 = ("WorkshopItemsInstalled", "WorkshopItemDetails") t2 = ("WorkshopItemsInstalled", "WorkshopItemDetails")
@@ -78,18 +111,9 @@ class WorkshopACF:
s += line + "\n" s += line + "\n"
return s return s
def to_file(self, file: str) -> None: def to_file(self, file: Path) -> None:
s = self.unpack(self.dict) s = self.unpack(self.dict)
with open(file, "w") as f: file.write_text(s)
f.write(s)
@classmethod
def enquote(cls, s: str) -> str:
return f'"{s}"'
@classmethod
def dequote(cls, s: str) -> str:
return s.rstrip('"').lstrip('"')
def delete(self, modid: int) -> None: def delete(self, modid: int) -> None:
del self.dict["AppWorkshop"]["WorkshopItemsInstalled"][modid] del self.dict["AppWorkshop"]["WorkshopItemsInstalled"][modid]