Add Backend.getDir(), Backend.getFile()
Replaces Backend.getPath()
This commit is contained in:
parent
a9964ab0f9
commit
9023d28c43
|
@ -106,20 +106,25 @@ class Backend(QObject):
|
|||
)
|
||||
break
|
||||
|
||||
|
||||
@staticmethod
|
||||
def getPath(kind: QStandardPaths.StandardLocation,
|
||||
file: str,
|
||||
initial_content: Optional[str] = None) -> str:
|
||||
relative_path = file.replace("/", os.sep)
|
||||
def getDir(standard_dir: QStandardPaths.StandardLocation) -> str:
|
||||
path = QStandardPaths.writableLocation(standard_dir)
|
||||
os.makedirs(path, exist_ok=True)
|
||||
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:
|
||||
return path
|
||||
|
||||
base_dir = QStandardPaths.writableLocation(kind)
|
||||
path = f"{base_dir}{os.sep}{relative_path}"
|
||||
os.makedirs(os.path.split(path)[0], exist_ok=True)
|
||||
path = os.path.join(self.getDir(standard_dir), relative_file_path)
|
||||
|
||||
if initial_content is not None:
|
||||
with AtomicFile(path, "w") as new:
|
||||
|
|
|
@ -115,16 +115,14 @@ class ClientManager(QObject, Mapping, metaclass=_ClientManagerMeta):
|
|||
return f"{__about__.__pretty_name__}{os}"
|
||||
|
||||
|
||||
# Standard file paths
|
||||
# Config file operations
|
||||
|
||||
def getAccountConfigPath(self) -> str:
|
||||
return self.backend.getPath(
|
||||
return self.backend.getFile(
|
||||
QStandardPaths.AppConfigLocation, "accounts.json", "[]"
|
||||
)
|
||||
|
||||
|
||||
# Config file operations
|
||||
|
||||
def configAccounts(self) -> AccountConfig:
|
||||
with open(self.getAccountConfigPath(), "r") as file:
|
||||
return json.loads(file.read().strip()) or {}
|
||||
|
|
Loading…
Reference in New Issue
Block a user