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

Improve target configuration object to behave like a magic dictionary #772

Open
Schamper opened this issue Jul 31, 2024 · 1 comment
Open
Labels
enhancement New feature or request epic:target-* improvements good first issue Good for newcomers

Comments

@Schamper
Copy link
Member

Schamper commented Jul 31, 2024

Currently it behaves/is a module type to allow for easy attribute access, but this is annoying when attributes may or may not exist yet. For example:

cache_dir = getattr(target._config, "CACHE_DIR", None) if target._config else None

Ideally it behaves like a magic dictionary with the nice methods of a dictionary, but the magic attribute access like a module.

https://github.com/fox-it/dissect.target/blob/main/dissect/target/helpers/config.py

@JSCU-CNI
Copy link
Contributor

Is it acceptable to invoke the config as follows?

foo = target._config.get("FOO")

Because in that case the following patch should suffice:

diff --git a/dissect/target/helpers/config.py b/dissect/target/helpers/config.py
index 7a1cc3f..3a1868a 100644
--- a/dissect/target/helpers/config.py
+++ b/dissect/target/helpers/config.py
@@ -6,6 +6,7 @@ import importlib.util
 import logging
 from pathlib import Path
 from types import ModuleType
+from typing import Any
 
 log = logging.getLogger(__name__)
 
@@ -26,6 +27,10 @@ def load(paths: list[Path | str] | Path | str | None) -> ModuleType:
         config_values = _parse_ast(config_file.read_bytes())
         config.__dict__.update(config_values)
 
+    def get(self, value: str) -> Any | None:
+        return getattr(self, value, None)
+    config.get = get.__get__(config)
+
     return config

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request epic:target-* improvements good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants