pbf.utils
1import pathlib 2import os 3import pip 4from apscheduler.schedulers.background import BackgroundScheduler 5 6# Debug 7try: 8 from ..config import user_directory 9except ImportError: 10 from pbf.config import user_directory 11 12# Scheduler 13scheduler = BackgroundScheduler(timezone="Asia/Shanghai") 14 15 16class Utils: 17 def __init__(self) -> None: 18 pass 19 20 @staticmethod 21 def installPackage(package: str): 22 """ 23 Install package. (Blocked) 24 :param package: str package name 25 :return: None 26 """ 27 pip.main(["install", package]) 28 29 @staticmethod 30 def installPackages(packages: list): 31 """ 32 Install packages. (Blocked) 33 :param packages: list package names 34 :return: None 35 """ 36 packages.insert(0, "install") 37 pip.main(packages) 38 39 40class Path: 41 @staticmethod 42 def make_sure_path_exists(path_to_file: str, replace: bool = True) -> None: 43 """ 44 Make sure path exists. 45 :param path_to_file: str path to file 46 :param replace: bool (可选)是否替换path_to_file中的特殊路径 47 :return: None 48 """ 49 if replace: 50 path_to_file = Path.replace(path_to_file) 51 pathlib.Path(path_to_file).mkdir(parents=True, exist_ok=True) 52 53 @staticmethod 54 def replace(path: str) -> str: 55 """ 56 替换path中的特殊路径。 57 :param path: str 58 :return: str 59 """ 60 replace_list = { 61 "home": os.path.join(pathlib.Path().home(), user_directory), 62 "cwd": pathlib.Path().cwd(), 63 } 64 return path.format(**replace_list) 65 66 67class MetaData: 68 name: str = "PBFPlugin" 69 version: str = "1.0.0" 70 versionCode: int = 1 71 description: str = "PBF Plugin" 72 author: str = "author" 73 license: str = "MIT" 74 keywords: list = ["pbf", "plugin"] 75 readme: str = """ 76 # PBF Plugin 77 hi 78 """ 79 80 def __init__(self, **kwargs) -> None: 81 """ 82 Initialize metadata. 83 :param kwargs: **dict Metadata 84 """ 85 for key, value in kwargs.items(): 86 setattr(self, key, value) 87 88 def __str__(self) -> str: 89 return f"{self.name} v{self.version} by {self.author}" 90 91 def toDict(self): 92 """ 93 Convert metadata to dict. 94 :return: dict 95 """ 96 ret_dict: dict = {} 97 for i in dir(self): 98 if not i.startswith("__") and not callable(getattr(self, i)): 99 ret_dict[i] = getattr(self, i) 100 return ret_dict 101 102 103if __name__ == "__main__": 104 rms = """ 105uvicorn~=0.30.5 106fastapi~=0.112.0 107requests~=2.32.3 108click~=8.1.7 109""" 110 Utils.installPackages(rms.strip().split("\n"))
scheduler =
<apscheduler.schedulers.background.BackgroundScheduler object>
class
Utils:
17class Utils: 18 def __init__(self) -> None: 19 pass 20 21 @staticmethod 22 def installPackage(package: str): 23 """ 24 Install package. (Blocked) 25 :param package: str package name 26 :return: None 27 """ 28 pip.main(["install", package]) 29 30 @staticmethod 31 def installPackages(packages: list): 32 """ 33 Install packages. (Blocked) 34 :param packages: list package names 35 :return: None 36 """ 37 packages.insert(0, "install") 38 pip.main(packages)
@staticmethod
def
installPackage(package: str):
21 @staticmethod 22 def installPackage(package: str): 23 """ 24 Install package. (Blocked) 25 :param package: str package name 26 :return: None 27 """ 28 pip.main(["install", package])
Install package. (Blocked)
Parameters
- package: str package name
Returns
None
@staticmethod
def
installPackages(packages: list):
30 @staticmethod 31 def installPackages(packages: list): 32 """ 33 Install packages. (Blocked) 34 :param packages: list package names 35 :return: None 36 """ 37 packages.insert(0, "install") 38 pip.main(packages)
Install packages. (Blocked)
Parameters
- packages: list package names
Returns
None
class
Path:
41class Path: 42 @staticmethod 43 def make_sure_path_exists(path_to_file: str, replace: bool = True) -> None: 44 """ 45 Make sure path exists. 46 :param path_to_file: str path to file 47 :param replace: bool (可选)是否替换path_to_file中的特殊路径 48 :return: None 49 """ 50 if replace: 51 path_to_file = Path.replace(path_to_file) 52 pathlib.Path(path_to_file).mkdir(parents=True, exist_ok=True) 53 54 @staticmethod 55 def replace(path: str) -> str: 56 """ 57 替换path中的特殊路径。 58 :param path: str 59 :return: str 60 """ 61 replace_list = { 62 "home": os.path.join(pathlib.Path().home(), user_directory), 63 "cwd": pathlib.Path().cwd(), 64 } 65 return path.format(**replace_list)
@staticmethod
def
make_sure_path_exists(path_to_file: str, replace: bool = True) -> None:
42 @staticmethod 43 def make_sure_path_exists(path_to_file: str, replace: bool = True) -> None: 44 """ 45 Make sure path exists. 46 :param path_to_file: str path to file 47 :param replace: bool (可选)是否替换path_to_file中的特殊路径 48 :return: None 49 """ 50 if replace: 51 path_to_file = Path.replace(path_to_file) 52 pathlib.Path(path_to_file).mkdir(parents=True, exist_ok=True)
Make sure path exists.
Parameters
- path_to_file: str path to file
- replace: bool (可选)是否替换path_to_file中的特殊路径
Returns
None
@staticmethod
def
replace(path: str) -> str:
54 @staticmethod 55 def replace(path: str) -> str: 56 """ 57 替换path中的特殊路径。 58 :param path: str 59 :return: str 60 """ 61 replace_list = { 62 "home": os.path.join(pathlib.Path().home(), user_directory), 63 "cwd": pathlib.Path().cwd(), 64 } 65 return path.format(**replace_list)
替换path中的特殊路径。
Parameters
- path: str
Returns
str
class
MetaData:
68class MetaData: 69 name: str = "PBFPlugin" 70 version: str = "1.0.0" 71 versionCode: int = 1 72 description: str = "PBF Plugin" 73 author: str = "author" 74 license: str = "MIT" 75 keywords: list = ["pbf", "plugin"] 76 readme: str = """ 77 # PBF Plugin 78 hi 79 """ 80 81 def __init__(self, **kwargs) -> None: 82 """ 83 Initialize metadata. 84 :param kwargs: **dict Metadata 85 """ 86 for key, value in kwargs.items(): 87 setattr(self, key, value) 88 89 def __str__(self) -> str: 90 return f"{self.name} v{self.version} by {self.author}" 91 92 def toDict(self): 93 """ 94 Convert metadata to dict. 95 :return: dict 96 """ 97 ret_dict: dict = {} 98 for i in dir(self): 99 if not i.startswith("__") and not callable(getattr(self, i)): 100 ret_dict[i] = getattr(self, i) 101 return ret_dict
MetaData(**kwargs)
81 def __init__(self, **kwargs) -> None: 82 """ 83 Initialize metadata. 84 :param kwargs: **dict Metadata 85 """ 86 for key, value in kwargs.items(): 87 setattr(self, key, value)
Initialize metadata.
Parameters
- kwargs: **dict Metadata
def
toDict(self):
92 def toDict(self): 93 """ 94 Convert metadata to dict. 95 :return: dict 96 """ 97 ret_dict: dict = {} 98 for i in dir(self): 99 if not i.startswith("__") and not callable(getattr(self, i)): 100 ret_dict[i] = getattr(self, i) 101 return ret_dict
Convert metadata to dict.
Returns
dict