Skip to content

Commit

Permalink
remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-black-tea committed May 23, 2024
1 parent c3e8505 commit e3d9e14
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 137 deletions.
63 changes: 30 additions & 33 deletions src/linktools/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,25 +257,23 @@ class Config:
def __init__(
self,
environ: "BaseEnviron",
config: ConfigDict,
data: ConfigDict,
namespace: str = __missing__,
prefix: str = __missing__,
share: bool = False
):
"""
初始化配置对象
:param environ: 环境对象
:param config: 配置相关数据
:param data: 配置相关数据
:param namespace: 缓存对应的命名空间
:param prefix: 环境变量前缀
:param share: 是否共享配置
"""
self._environ = environ
self._config = config if share else pickle.loads(pickle.dumps(config))
self._data = data
self._namespace = namespace if namespace != __missing__ else "MAIN"
self._prefix = prefix.upper() if prefix != __missing__ else ""
self._reload = None
self._cache = dict()
self._cache_data = dict()
self._cache_path = self._environ.get_data_path(f"{self._environ.name}.cfg", create_parent=True)
self.load_cache()

Expand Down Expand Up @@ -324,25 +322,25 @@ def get(self, key: str, type: "Union[Type[T], ConfigType]" = None, default: Any
value = os.environ.get(env_key)
return self.cast(value, type=type)

if key in self._cache:
value = self._cache.get(key)
prop = self._config.get(key, None)
if key in self._cache_data:
value = self._cache_data.get(key)
prop = self._data.get(key, None)
if self.reload:
if isinstance(prop, ConfigProperty) and prop.reloadable:
with self.__lock__:
value = self._cache[key] = prop.load(self, key, type=type, cache=value)
value = self._cache_data[key] = prop.load(self, key, type=type, cache=value)
return value
if isinstance(default, ConfigProperty) and default.reloadable:
with self.__lock__:
value = self._cache[key] = default.load(self, key, type=type, cache=value)
value = self._cache_data[key] = default.load(self, key, type=type, cache=value)
return value
return self.cast(value, type=type)

if key in self._config:
value = self._config.get(key)
if key in self._data:
value = self._data.get(key)
if isinstance(value, ConfigProperty):
with self.__lock__:
value = self._cache[key] = value.load(self, key, type=type)
value = self._cache_data[key] = value.load(self, key, type=type)
return value
return self.cast(value, type=type)

Expand All @@ -356,7 +354,7 @@ def get(self, key: str, type: "Union[Type[T], ConfigType]" = None, default: Any

if isinstance(default, ConfigProperty):
with self.__lock__:
value = self._cache[key] = default.load(self, key, type=type)
value = self._cache_data[key] = default.load(self, key, type=type)
return value

return default
Expand All @@ -365,8 +363,8 @@ def keys(self) -> Generator[str, None, None]:
"""
遍历配置名,默认不遍历内置配置
"""
keys = set(self._config.keys())
keys.update(self._cache.keys())
keys = set(self._data.keys())
keys.update(self._cache_data.keys())
for key in os.environ.keys():
if key.startswith(self._prefix):
keys.add(key[len(self._prefix):])
Expand All @@ -384,37 +382,37 @@ def set(self, key: str, value: Any) -> None:
"""
更新配置
"""
self._config[key] = value
self._data[key] = value

def set_default(self, key: str, value: Any) -> Any:
"""
设置默认配置
"""
return self._config.setdefault(key, value)
return self._data.setdefault(key, value)

def update(self, **kwargs) -> None:
"""
更新配置
"""
self._config.update(**kwargs)
self._data.update(**kwargs)

def update_defaults(self, **kwargs) -> None:
"""
更新默认配置
"""
for key, value in kwargs.items():
self._config.setdefault(key, value)
self._data.setdefault(key, value)

def update_from_file(self, path: str, load: Callable[[IO[Any]], Mapping] = None) -> bool:
"""
加载配置文件,按照扩展名来匹配相应的加载规则
"""
if load is not None:
return self._config.update_from_file(path, load=load)
return self._data.update_from_file(path, load=load)
if path.endswith(".py"):
return self._config.update_from_pyfile(path)
return self._data.update_from_pyfile(path)
elif path.endswith(".json"):
return self._config.update_from_file(path, load=json.load)
return self._data.update_from_file(path, load=json.load)
self._environ.logger.debug(f"Unsupported config file: {path}")
return False

Expand Down Expand Up @@ -454,8 +452,8 @@ def load_cache(self) -> None:
"""
parser = ConfigCacheParser(self)
with self.__lock__:
self._cache.clear()
self._cache.update(parser.items())
self._cache_data.clear()
self._cache_data.update(parser.items())

def save_cache(self, **kwargs: Any) -> None:
"""
Expand All @@ -465,7 +463,7 @@ def save_cache(self, **kwargs: Any) -> None:
parser = ConfigCacheParser(self)
with self.__lock__:
for key, value in kwargs.items():
self._cache[key] = value
self._cache_data[key] = value
parser.set(key, self.cast(value, type=str))
parser.dump()

Expand All @@ -477,14 +475,14 @@ def remove_cache(self, *keys: str) -> None:
parser = ConfigCacheParser(self)
with self.__lock__:
for key in keys:
self._cache.pop(key, None)
self._cache_data.pop(key, None)
parser.remove(key)
parser.dump()

def __contains__(self, key) -> bool:
return f"{self._prefix}{key}" in os.environ or \
key in self._config or \
key in self._cache
key in self._data or \
key in self._cache_data

def __getitem__(self, key: str) -> Any:
return self.get(key)
Expand Down Expand Up @@ -658,8 +656,7 @@ def __init__(
):
super().__init__(
config._environ,
config._config,
config._data,
namespace=namespace if namespace != __missing__ else config._namespace,
prefix=prefix if prefix != __missing__ else config._prefix,
share=True
prefix=prefix if prefix != __missing__ else config._prefix
)
78 changes: 24 additions & 54 deletions src/linktools/_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@
import os
import pathlib
import shutil
import sys
import time
from typing import TYPE_CHECKING, TypeVar, Type, Any, Dict
from typing import TYPE_CHECKING, TypeVar, Type, Any

from . import utils, metadata
from .decorator import cached_property, cached_classproperty
Expand Down Expand Up @@ -97,7 +96,7 @@ def debug(self) -> bool:
"""
debug模式
"""
return self.get_config("DEBUG", type=bool, default=False)
return self.get_config("DEBUG", type=bool)

@debug.setter
def debug(self, value: bool) -> None:
Expand Down Expand Up @@ -248,31 +247,18 @@ def get_logger(self, name: str = None) -> logging.Logger:
name = f"{self.name}.{name}" if name else self.name
return self._log_manager.getLogger(name)

@cached_classproperty
def _default_config(self) -> "ConfigDict":
from ._config import ConfigDict

config = ConfigDict()

config.update({
"TOOL_SHELL": {
"absolute_path": utils.get_shell_path(),
},
"TOOL_PYTHON": {
"absolute_path": sys.executable,
}
})

return config

def _create_config(self) -> "Config":
from ._config import Config
from ._config import Config, ConfigDict

return Config(
self,
self._default_config,
ConfigDict(
DEBUG=False,
SHOW_LOG_LEVEL=True,
SHOW_LOG_TIME=False,
),
namespace="MAIN",
prefix=f"{self.name.upper()}_",
prefix=f"{self.name.upper()}_"
)

@cached_property
Expand Down Expand Up @@ -317,33 +303,23 @@ def _create_tools(self) -> "Tools":

config = ConfigDict()

yaml_path = environ.get_path("template", "tools.yml")
if metadata.__release__ or not os.path.exists(yaml_path):
config.update_from_file(
environ.get_asset_path("tools.json"),
json.load
)
template_path = environ.get_path("template", "tools.yml")
data_path = environ.get_data_path("tools", "tools.json")
asset_path = environ.get_asset_path("tools.json")

if os.path.exists(data_path):
config.update_from_file(data_path, json.load)
elif metadata.__release__ or not os.path.exists(template_path):
config.update_from_file(asset_path, json.load)
else:
import yaml
config.update_from_file(
yaml_path,
yaml.safe_load
)
config.update_from_file(template_path, yaml.safe_load)

tools = Tools(self, config)

# set environment variable
index = 0
dir_names = os.environ["PATH"].split(os.pathsep)
for tool in Tools(self, config):
if tool.executable:
# dirname(executable[0]) -> environ["PATH"]
dir_name = tool.dirname
if dir_name and dir_name not in dir_names:
# insert to head
dir_names.insert(index, tool.dirname)
index += 1
os.environ["PATH"] = os.pathsep.join(dir_names)
os.environ["PATH"] = os.pathsep.join(
tools.env_path + os.environ["PATH"].split(os.pathsep)
)

return tools

Expand Down Expand Up @@ -395,6 +371,9 @@ def description(self) -> str:
def root_path(self) -> str:
return os.path.dirname(__file__)

def get_asset_path(self, *paths: str) -> str:
return self.get_path("assets", *paths)

def _create_config(self):
config = super()._create_config()

Expand All @@ -407,16 +386,7 @@ def _create_config(self):
"Safari/537.36"
)

# 导入configs文件夹中所有配置文件
config.update_from_file(
self.get_asset_path("android-tools.json"),
load=json.load
)

return config

def get_asset_path(self, *paths: str) -> str:
return self.get_path("assets", *paths)


environ = Environ()
58 changes: 34 additions & 24 deletions src/linktools/_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,30 +479,15 @@ def __init__(self, environ: "BaseEnviron", config: Dict[str, Dict]):
self.config = environ.wrap_config(prefix="")
self.all = self._parse_items(config)

def _parse_items(self, config: Dict[str, Dict]) -> Dict[str, Tool]:
result = {
"shell": Tool(self, "shell", {
"cmdline": None,
"absolute_path": utils.get_shell_path(),
}),
"python": Tool(self, "python", {
"cmdline": None,
"absolute_path": sys.executable,
}),
}

for key, value in config.items():
if not isinstance(value, dict):
warnings.warn(f"dict was expected, got {type(value)}, ignored.")
continue
name = value.get("name", None)
if name is None:
if key.startswith("TOOL_"):
key = key[len("TOOL_"):]
name = value["name"] = key.lower()
result[name] = Tool(self, name, value)

return result
@property
def env_path(self) -> List[str]:
paths = []
for tool in self:
if tool.executable:
path = tool.dirname
if path and path not in paths:
paths.append(path)
return paths

def keys(self) -> Generator[str, None, None]:
for k, v in self.all.items():
Expand Down Expand Up @@ -537,3 +522,28 @@ def __getattr__(self, item: str) -> Tool:

def __setitem__(self, key: str, value: Tool):
self.all[key] = value

def _parse_items(self, config: Dict[str, Dict]) -> Dict[str, Tool]:
result = {
"shell": Tool(self, "shell", {
"cmdline": None,
"absolute_path": utils.get_shell_path(),
}),
"python": Tool(self, "python", {
"cmdline": None,
"absolute_path": sys.executable,
}),
}

for key, value in config.items():
if not isinstance(value, dict):
warnings.warn(f"dict was expected, got {type(value)}, ignored.")
continue
name = value.get("name", None)
if name is None:
if key.startswith("TOOL_"):
key = key[len("TOOL_"):]
name = value["name"] = key.lower()
result[name] = Tool(self, name, value)

return result
Loading

0 comments on commit e3d9e14

Please sign in to comment.