diff --git a/poetry.lock b/poetry.lock index 762548ba1..2914019e0 100644 --- a/poetry.lock +++ b/poetry.lock @@ -981,7 +981,7 @@ files = [ name = "pyyaml" version = "6.0" description = "YAML parser and emitter for Python" -category = "dev" +category = "main" optional = false python-versions = ">=3.6" files = [ @@ -1505,4 +1505,4 @@ testing = ["big-O", "flake8 (<5)", "jaraco.functools", "jaraco.itertools", "more [metadata] lock-version = "2.0" python-versions = "^3.7.2" -content-hash = "0253742903de5ff1aacb5f82eaca030dc432b7f24b518a13f0be03a1ac65b3ea" +content-hash = "2b4661fdebb07e104cddd6f32776bb14d582c280cf2030fa61ae3fed0cc923e0" diff --git a/pyproject.toml b/pyproject.toml index 9d7cbcb74..d2e57bcb6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -57,6 +57,7 @@ binaryornot = "^0.4.4" license-expression = ">=1.0" python-debian = "^0.1.38,!=0.1.45,!=0.1.46,!=0.1.47" importlib-metadata = { version = ">=1.4", python = "<3.8" } +PyYAML = ">=3.13" [tool.poetry.dev-dependencies] Sphinx = ">=4.0.0" diff --git a/src/reuse/_config.py b/src/reuse/_config.py index f2e2e4f1f..cdbebff58 100644 --- a/src/reuse/_config.py +++ b/src/reuse/_config.py @@ -8,6 +8,8 @@ from gettext import gettext as _ from typing import Any, Dict, Optional +import yaml + @dataclass class AnnotateOptions: @@ -49,6 +51,23 @@ def from_dict(cls, value: Dict[str, Any]) -> "Config": ] = _annotate_options_from_dict(override) return config + @classmethod + def from_yaml(cls, text: str) -> "Config": + """Parse yaml to generate a Config object. + + An example of a yaml file:: + + annotate: + default_name: Jane Doe + default_contact: jane@example.com + default_license: GPL-3.0-or-later + + overrides: + - path: ~/Projects/FSFE + default_contact: jane@fsfe.example.com + """ + return cls.from_dict(yaml.load(text, Loader=yaml.Loader)) + def _annotate_options_from_dict(value: Dict[str, str]) -> AnnotateOptions: return AnnotateOptions( diff --git a/tests/test_config.py b/tests/test_config.py index 07c9db861..aeb57ac7c 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -4,6 +4,8 @@ """Tests for some _config.""" +from inspect import cleandoc + from reuse._config import Config # REUSE-IgnoreStart @@ -61,4 +63,28 @@ def test_config_from_dict_override(): assert result.override_annotate_options["bar"].license == "MIT" +def test_config_from_yaml_simple(): + """Load Config from yaml.""" + text = cleandoc( + """ + annotate: + default_name: Jane Doe + default_contact: jane@example.com + default_license: GPL-3.0-or-later + + overrides: + - path: ~/Projects/FSFE + default_contact: jane@fsfe.example.com + """ + ) + result = Config.from_yaml(text) + assert result.global_annotate_options.name == "Jane Doe" + assert result.global_annotate_options.contact == "jane@example.com" + assert result.global_annotate_options.license == "GPL-3.0-or-later" + assert ( + result.override_annotate_options["~/Projects/FSFE"].contact + == "jane@fsfe.example.com" + ) + + # REUSE-IgnoreEnd