From 0f297d61777f3a261d9050d4413be3cdf1de5694 Mon Sep 17 00:00:00 2001 From: Miauwkeru Date: Fri, 15 Mar 2024 14:02:36 +0000 Subject: [PATCH 1/4] Add TOML support to the config parser --- dissect/target/helpers/configutil.py | 25 ++++++++++++++++++++++++- pyproject.toml | 1 + tests/plugins/general/test_config.py | 1 + 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/dissect/target/helpers/configutil.py b/dissect/target/helpers/configutil.py index 5eb0e91cf..43e14bb46 100644 --- a/dissect/target/helpers/configutil.py +++ b/dissect/target/helpers/configutil.py @@ -3,6 +3,7 @@ import io import json import re +import sys from collections import deque from configparser import ConfigParser, MissingSectionHeaderError from dataclasses import dataclass @@ -31,9 +32,20 @@ import yaml PY_YAML = True -except (AttributeError, ImportError): +except ImportError: PY_YAML = False +try: + if sys.version_info < (3, 11): + import tomli as toml + else: + # tomllib is included since python 3.11 + import tomllib as toml + + TOML = True +except ImportError: + TOML = False + def _update_dictionary(current: dict[str, Any], key: str, value: Any) -> None: if prev_value := current.get(key): @@ -408,6 +420,16 @@ def parse_file(self, fh: TextIO) -> None: raise ConfigurationParsingError("Failed to parse file, please install PyYAML.") +class Toml(ConfigurationParser): + """Parses a Toml file.""" + + def parse_file(self, fh: TextIO) -> None: + if TOML: + self.parsed_data = toml.loads(fh.read()) + else: + raise ConfigurationParsingError("Failed to parse file, please install tomli.") + + class ScopeManager: """A (context)manager for dictionary scoping. @@ -696,6 +718,7 @@ def create_parser(self, options: Optional[ParserOptions] = None) -> Configuratio "sample": ParserConfig(Txt), "systemd": ParserConfig(SystemD), "template": ParserConfig(Txt), + "toml": ParserConfig(Toml), } KNOWN_FILES: dict[str, type[ConfigurationParser]] = { diff --git a/pyproject.toml b/pyproject.toml index 8b59b0d44..a3a725e2f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -73,6 +73,7 @@ full = [ # # Until such time that this dependency can be installed through # flow.record, we define it as a dependency of dissect.target. + "tomli; python_version<'3.11'", "zstandard", ] yara = [ diff --git a/tests/plugins/general/test_config.py b/tests/plugins/general/test_config.py index 6d81cb731..316e5ac3c 100644 --- a/tests/plugins/general/test_config.py +++ b/tests/plugins/general/test_config.py @@ -75,6 +75,7 @@ def test_collapse_types( ("conf", b"key value"), ("sample", b"currently_just_text"), ("template", b"currently_just_text"), + ("toml", b"key = 'value'"), ], ) def test_as_dict( From 742f875b935a40cfb0319fc6933259451cb96e74 Mon Sep 17 00:00:00 2001 From: Miauwkeru Date: Mon, 18 Mar 2024 07:47:09 +0000 Subject: [PATCH 2/4] Resolve comments --- dissect/target/helpers/configutil.py | 6 +++--- pyproject.toml | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dissect/target/helpers/configutil.py b/dissect/target/helpers/configutil.py index 43e14bb46..7b435b56f 100644 --- a/dissect/target/helpers/configutil.py +++ b/dissect/target/helpers/configutil.py @@ -42,9 +42,9 @@ # tomllib is included since python 3.11 import tomllib as toml - TOML = True + HAS_TOML = True except ImportError: - TOML = False + HAS_TOML = False def _update_dictionary(current: dict[str, Any], key: str, value: Any) -> None: @@ -424,7 +424,7 @@ class Toml(ConfigurationParser): """Parses a Toml file.""" def parse_file(self, fh: TextIO) -> None: - if TOML: + if HAS_TOML: self.parsed_data = toml.loads(fh.read()) else: raise ConfigurationParsingError("Failed to parse file, please install tomli.") diff --git a/pyproject.toml b/pyproject.toml index a3a725e2f..a56018221 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -73,8 +73,8 @@ full = [ # # Until such time that this dependency can be installed through # flow.record, we define it as a dependency of dissect.target. - "tomli; python_version<'3.11'", "zstandard", + "tomli; python_version<'3.11'", ] yara = [ # Grab the dependencies for dissect.target From 8100007a30bf46ad41fe411df18bf95cf7db7417 Mon Sep 17 00:00:00 2001 From: Miauwkeru Date: Mon, 18 Mar 2024 08:06:33 +0000 Subject: [PATCH 3/4] Add an ignore for vermin when running python>=3.11 --- dissect/target/helpers/configutil.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dissect/target/helpers/configutil.py b/dissect/target/helpers/configutil.py index 7b435b56f..5272fcbe0 100644 --- a/dissect/target/helpers/configutil.py +++ b/dissect/target/helpers/configutil.py @@ -40,7 +40,7 @@ import tomli as toml else: # tomllib is included since python 3.11 - import tomllib as toml + import tomllib as toml # novermin HAS_TOML = True except ImportError: From 2e87b94b8564c5fa39b605193d5745994edf1bcd Mon Sep 17 00:00:00 2001 From: Miauwkeru Date: Mon, 18 Mar 2024 12:17:22 +0000 Subject: [PATCH 4/4] Order optional dependencies correctly --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index a56018221..6c66b146f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -66,6 +66,7 @@ full = [ "fusepy", "pycryptodome", "pyyaml", + "tomli; python_version<'3.11'", # dissect.target's caching uses flow.record functionlity which depends on the # zstandard module being available. However flow.record does not define # zstandard as a dependency, nor does it allow zstandard to be installed @@ -74,7 +75,6 @@ full = [ # Until such time that this dependency can be installed through # flow.record, we define it as a dependency of dissect.target. "zstandard", - "tomli; python_version<'3.11'", ] yara = [ # Grab the dependencies for dissect.target