Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename config_dict and use deepcopy #34

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion duetector/collectors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions duetector/collectors/db.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
from typing import Any, Dict, Optional

from sqlalchemy import select # type: ignore
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions duetector/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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("."):
Expand Down
4 changes: 2 additions & 2 deletions duetector/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", "")
Expand Down Expand Up @@ -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__):
Expand Down
6 changes: 3 additions & 3 deletions duetector/filters/pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion duetector/managers/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion duetector/managers/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion duetector/managers/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions duetector/monitors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion duetector/tools/config_generator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
from pathlib import Path
from typing import Dict

Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_bcc_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down