From 4ed63c1e4618f5af136f62437babe300b73475c1 Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Wed, 22 Jun 2022 08:49:15 +0200 Subject: [PATCH] Update agnostic_paths.py --- modules/agnostic_paths.py | 75 ++++++++++++++++++++++++++++++++++----- 1 file changed, 66 insertions(+), 9 deletions(-) diff --git a/modules/agnostic_paths.py b/modules/agnostic_paths.py index cb4d518..1caa56e 100644 --- a/modules/agnostic_paths.py +++ b/modules/agnostic_paths.py @@ -7,14 +7,32 @@ import sys class AgnosticPaths: + """ + An agnostic path object, containing all methods to access the files and folders in a standard discord install + """ + def __init__(self, ptb, forced_path=None): + """ + :param ptb: Whether the discord is a public beta install (ptb="PTB") or not (ptb="") + :type ptb: str + :param forced_path: An alternative discord install path, usefull to test the script on a non standard install + :type forced_path: str + """ self.ptb = ptb self.os = self.get_os() - self.main_path = self.get_path() + self.main_path = self._get_path() self.version = self.get_version() - self.update_path() + self._update_path() def get_os(self): + """ + Get the os string in a short, spydey-detector way. + For now only windows and linux are supported, note that due to the nature of linux builds, strings will likely become + linux-arch-deb, linux-arch-snap etc... + :return: ("windows", "linux", "mac") + :rtype: str + """ + platform = sys.platform if platform.startswith('win'): self.os = 'windows' @@ -26,7 +44,13 @@ class AgnosticPaths: raise OSError("This script can only check for Spideys on Windows, Linux and Mac.") return self.os - def get_path(self): + def _get_path(self): + """ + Return the base path of the install, note that the self.main_path is not returned, + and this method is meant to be used at init only + :return: Base path of the install (minus the version on windows) + :rtype: str + """ if self.os == 'windows': return os.path.join(os.path.expanduser('~'), "AppData", "Local", f"Discord{self.ptb}") elif self.os == 'linux': @@ -38,11 +62,14 @@ class AgnosticPaths: def get_version(self): """ - Looks for the version file in the discord folder + Looks for the version file in the discord folder + and return the latest version of the discord build + :return: The version build + :rtype: str """ versions = [] if self.os == 'windows': - for element in os.listdir(self.main_path): + for element in os.listdir(self._get_path()): if re.match(r"app-[\d.]+", element): versions.append(element) if not versions: @@ -57,14 +84,20 @@ class AgnosticPaths: else: raise OSError("Can't find for version on your OS.") - def update_path(self): + def _update_path(self): + """ + Update the main path with the version, if needed (windows & mac only) + """ if self.os == 'windows': self.main_path = os.path.join(self.main_path, self.version) def list(self, *args): """ - Lists all files in the given path. - :param args: The path + Lists all files in the path /discord_path/args[0]/args[1].../args[n] + :param args: The path as a list of parameters list("folder1", "folder2", ..., "foldern") + :type args: str + :return: The list of all files and folders in the given path + :rtype: list[str] """ return os.listdir(self(*args)) @@ -85,15 +118,39 @@ class AgnosticPaths: return dirs, files def __call__(self, *args): + """ + Tries to open the path /discord_path/args[0]/args[1].../args[n] + :param args: The path of the file or directory as a list of parameters __call__("folder1", "folder2", ..., "foldern") + :type args: str + :return: The path is it exists + :rtype: str + :raises: FileNotFoundError if the path doesn't exists + """ _path = os.path.join(self.main_path, *args) if not os.path.exists(_path): raise FileNotFoundError(f"Could not find {_path}") return _path - + + def get_path(self): + """ + The main path of the discord install + :return: self.main_path + :rtype: str + """ + return self.main_path + def __repr__(self): + """ + :return: A representation of the AgnosticPath + :rtype: str + """ return f"AgnosticPaths(ptb={self.ptb}, os={self.os}, main_path={self.main_path}, version={self.version})" def __str__(self): + """ + :return: The main path of the discord install + :rtype: str + """ return self.main_path