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

Add TOML support to the config parser #580

Merged
merged 4 commits into from
Mar 18, 2024
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
25 changes: 24 additions & 1 deletion dissect/target/helpers/configutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 # novermin

HAS_TOML = True
except ImportError:
HAS_TOML = False


def _update_dictionary(current: dict[str, Any], key: str, value: Any) -> None:
if prev_value := current.get(key):
Expand Down Expand Up @@ -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 HAS_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.

Expand Down Expand Up @@ -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]] = {
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/plugins/general/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading