Add Backend.getDir(), Backend.getFile()

Replaces Backend.getPath()
This commit is contained in:
miruka 2019-05-06 21:57:44 -04:00
parent a9964ab0f9
commit 9023d28c43
2 changed files with 16 additions and 13 deletions

View File

@ -106,20 +106,25 @@ class Backend(QObject):
) )
break break
@staticmethod @staticmethod
def getPath(kind: QStandardPaths.StandardLocation, def getDir(standard_dir: QStandardPaths.StandardLocation) -> str:
file: str, path = QStandardPaths.writableLocation(standard_dir)
initial_content: Optional[str] = None) -> str: os.makedirs(path, exist_ok=True)
relative_path = file.replace("/", os.sep) return path
path = QStandardPaths.locate(kind, relative_path)
def getFile(self,
standard_dir: QStandardPaths.StandardLocation,
relative_file_path: str,
initial_content: Optional[str] = None) -> str:
relative_file_path = relative_file_path.replace("/", os.sep)
path = QStandardPaths.locate(standard_dir, relative_file_path)
if path: if path:
return path return path
base_dir = QStandardPaths.writableLocation(kind) path = os.path.join(self.getDir(standard_dir), relative_file_path)
path = f"{base_dir}{os.sep}{relative_path}"
os.makedirs(os.path.split(path)[0], exist_ok=True)
if initial_content is not None: if initial_content is not None:
with AtomicFile(path, "w") as new: with AtomicFile(path, "w") as new:

View File

@ -115,16 +115,14 @@ class ClientManager(QObject, Mapping, metaclass=_ClientManagerMeta):
return f"{__about__.__pretty_name__}{os}" return f"{__about__.__pretty_name__}{os}"
# Standard file paths # Config file operations
def getAccountConfigPath(self) -> str: def getAccountConfigPath(self) -> str:
return self.backend.getPath( return self.backend.getFile(
QStandardPaths.AppConfigLocation, "accounts.json", "[]" QStandardPaths.AppConfigLocation, "accounts.json", "[]"
) )
# Config file operations
def configAccounts(self) -> AccountConfig: def configAccounts(self) -> AccountConfig:
with open(self.getAccountConfigPath(), "r") as file: with open(self.getAccountConfigPath(), "r") as file:
return json.loads(file.read().strip()) or {} return json.loads(file.read().strip()) or {}