From ac66fbddd7d74ed2264da6a766752a48baa7bdcb Mon Sep 17 00:00:00 2001 From: wunder957 Date: Fri, 1 Sep 2023 08:49:55 +0800 Subject: [PATCH] Rename config_dict and use deepcopy --- duetector/collectors/base.py | 2 +- duetector/collectors/db.py | 5 +++-- duetector/config.py | 14 +++++++------- duetector/db.py | 4 ++-- duetector/filters/pattern.py | 6 +++--- duetector/managers/collector.py | 2 +- duetector/managers/filter.py | 2 +- duetector/managers/tracer.py | 2 +- duetector/monitors/base.py | 4 ++-- duetector/tools/config_generator.py | 3 ++- tests/test_bcc_monitor.py | 2 +- tests/test_collector.py | 2 +- tests/test_filter.py | 2 +- 13 files changed, 26 insertions(+), 24 deletions(-) diff --git a/duetector/collectors/base.py b/duetector/collectors/base.py index a7ec5e8..de18b0f 100644 --- a/duetector/collectors/base.py +++ b/duetector/collectors/base.py @@ -25,7 +25,7 @@ class Collector(Configuable): def __init__(self, config: Optional[Dict[str, Any]] = None, *args, **kwargs): super().__init__(config, *args, **kwargs) - self._backend = self._backend_imp(**self.backend_args.config_dict) + self._backend = self._backend_imp(**self.backend_args._config_dict) @property def config_scope(self): diff --git a/duetector/collectors/db.py b/duetector/collectors/db.py index 30ed666..b0c966d 100644 --- a/duetector/collectors/db.py +++ b/duetector/collectors/db.py @@ -1,3 +1,4 @@ +import copy from typing import Any, Dict, Optional from sqlalchemy import select # type: ignore @@ -29,14 +30,14 @@ class DBCollector(Collector): } def __repr__(self): - config_without_db = self.config.config_dict.copy() + config_without_db = copy.deepcopy(self.config._config_dict) config_without_db.pop("db", None) return f"<[DBCollector {self.sm}] {config_without_db}>" def __init__(self, config: Optional[Dict[str, Any]] = None, *args, **kwargs): super().__init__(config, *args, **kwargs) # Init as a submodel - self.sm = SessionManager(self.config.config_dict) + self.sm = SessionManager(self.config._config_dict) def _emit(self, t: Tracking): m = self.sm.get_tracking_model(t.tracer, self.id) diff --git a/duetector/config.py b/duetector/config.py index 1263a9c..67fc020 100644 --- a/duetector/config.py +++ b/duetector/config.py @@ -23,21 +23,21 @@ class Config: def __init__(self, config_dict: Optional[Dict[str, Any]] = None): if not config_dict: config_dict = {} - self.config_dict: Dict[str, Any] = config_dict + self._config_dict: Dict[str, Any] = config_dict def __repr__(self) -> str: - return str(self.config_dict) + return str(self._config_dict) def __getattr__(self, name): # All config keys are lower case name = name.lower() - if isinstance(self.config_dict.get(name), dict): - return Config(self.config_dict[name]) + if isinstance(self._config_dict.get(name), dict): + return Config(self._config_dict[name]) - return self.config_dict.get(name, None) + return self._config_dict.get(name, None) def __bool__(self): - return bool(self.config_dict) + return bool(self._config_dict) class ConfigLoader: @@ -163,7 +163,7 @@ def __init__(self, config: Optional[Union[Config, Dict[str, Any]]] = None, *args if not config: config = {} elif isinstance(config, Config): - config = config.config_dict + config = config._config_dict if self.config_scope: for score in self.config_scope.split("."): diff --git a/duetector/db.py b/duetector/db.py index db0cdf5..7afd9bd 100644 --- a/duetector/db.py +++ b/duetector/db.py @@ -83,7 +83,7 @@ def table_prefix(self): @property def engine_config(self) -> Dict[str, Any]: - config = self.config.engine.config_dict + config = self.config.engine._config_dict if self.debug: config["echo"] = True db_url = config.get("url", "") @@ -145,7 +145,7 @@ def to_tracking(self) -> Tracking: return self._tracking_models[tracer] def get_all_model(self) -> Dict[str, type]: - return self._tracking_models.copy() + return self._tracking_models def _init_tracking_model(self, tracking_model: type) -> type: if not sqlalchemy.inspect(self.engine).has_table(tracking_model.__tablename__): diff --git a/duetector/filters/pattern.py b/duetector/filters/pattern.py index 3f04943..e2d835a 100644 --- a/duetector/filters/pattern.py +++ b/duetector/filters/pattern.py @@ -35,14 +35,14 @@ def enable_customize_exclude(self) -> bool: return bool(self.config.enable_customize_exclude) def customize_exclude(self, data: NamedTuple) -> bool: - for k in self.config.config_dict: + for k in self.config._config_dict: if k.startswith("exclude_"): field = k.replace("exclude_", "") - if getattr(data, field, None) in self.config.config_dict[k]: + if getattr(data, field, None) in self.config._config_dict[k]: return True if k.startswith("re_exclude_"): field = k.replace("re_exclude_", "") - if self.re_exclude(getattr(data, field, None), self.config.config_dict[k]): + if self.re_exclude(getattr(data, field, None), self.config._config_dict[k]): return True return False diff --git a/duetector/managers/collector.py b/duetector/managers/collector.py index 116d52c..0d9036f 100644 --- a/duetector/managers/collector.py +++ b/duetector/managers/collector.py @@ -38,7 +38,7 @@ def init(self, ignore_disabled=True) -> List[Collector]: return [] objs = [] - for f in self.pm.hook.init_collector(config=self.config.config_dict): + for f in self.pm.hook.init_collector(config=self.config): if not f: continue if f.disabled and ignore_disabled: diff --git a/duetector/managers/filter.py b/duetector/managers/filter.py index 73f35b2..8bbfb5c 100644 --- a/duetector/managers/filter.py +++ b/duetector/managers/filter.py @@ -37,7 +37,7 @@ def init(self, ignore_disabled=True) -> List[Filter]: logger.info("FilterManager disabled.") return [] objs = [] - for f in self.pm.hook.init_filter(config=self.config.config_dict): + for f in self.pm.hook.init_filter(config=self.config._config_dict): if not f: continue if f.disabled and ignore_disabled: diff --git a/duetector/managers/tracer.py b/duetector/managers/tracer.py index dabbf5a..bb3f31a 100644 --- a/duetector/managers/tracer.py +++ b/duetector/managers/tracer.py @@ -38,7 +38,7 @@ def init(self, tracer_type=Tracer, ignore_disabled=True) -> List[Tracer]: return [] objs = [] - for f in self.pm.hook.init_tracer(config=self.config.config_dict): + for f in self.pm.hook.init_tracer(config=self.config._config_dict): if not f or (f.disabled and ignore_disabled): logger.debug(f"Tracer {f.__class__.__name__} is not available (None or Disabled)") continue diff --git a/duetector/monitors/base.py b/duetector/monitors/base.py index 4437290..c80f20d 100644 --- a/duetector/monitors/base.py +++ b/duetector/monitors/base.py @@ -37,8 +37,8 @@ class Monitor(Configuable): def __init__(self, config: Optional[Dict[str, Any]] = None, *args, **kwargs): super().__init__(config=config) - self._backend = self._backend_imp(**self.backend_args.config_dict) - self.poller = Poller(self.config.config_dict) + self._backend = self._backend_imp(**self.backend_args._config_dict) + self.poller = Poller(self.config._config_dict) @property def disabled(self): diff --git a/duetector/tools/config_generator.py b/duetector/tools/config_generator.py index 7a0efe0..7abcf7a 100644 --- a/duetector/tools/config_generator.py +++ b/duetector/tools/config_generator.py @@ -1,3 +1,4 @@ +import copy from pathlib import Path from typing import Dict @@ -18,7 +19,7 @@ def _recursive_load(config_scope: str, config_dict: dict, default_config: dict): last = config_dict for p in prefix: last = last.setdefault(p, {}) - last[config_scope] = default_config.copy() + last[config_scope] = copy.deepcopy(default_config) class ConfigGenerator: diff --git a/tests/test_bcc_monitor.py b/tests/test_bcc_monitor.py index 8c0818d..e7c2922 100644 --- a/tests/test_bcc_monitor.py +++ b/tests/test_bcc_monitor.py @@ -69,7 +69,7 @@ def __init__( self.mock_cls = mock_cls super().__init__(config=config) - tracer_config = TracerManager(config).config.config_dict + tracer_config = TracerManager(config).config._config_dict self.tracers = [mock_tracer(tracer_config)] self.filters = FilterManager(config).init() diff --git a/tests/test_collector.py b/tests/test_collector.py index ee6e600..9b0424f 100644 --- a/tests/test_collector.py +++ b/tests/test_collector.py @@ -9,7 +9,7 @@ @pytest.fixture def config(full_config): - yield CollectorManager(full_config).config.config_dict + yield CollectorManager(full_config).config._config_dict @pytest.fixture diff --git a/tests/test_filter.py b/tests/test_filter.py index d5307f2..c8781c7 100644 --- a/tests/test_filter.py +++ b/tests/test_filter.py @@ -12,7 +12,7 @@ @pytest.fixture def config(full_config): - yield FilterManager(full_config).config.config_dict + yield FilterManager(full_config).config._config_dict @pytest.fixture