Skip to content

Commit

Permalink
Add from_yaml to Config
Browse files Browse the repository at this point in the history
Signed-off-by: Carmen Bianca BAKKER <[email protected]>
  • Loading branch information
carmenbianca committed Jun 16, 2023
1 parent a121a6f commit 206b68f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
4 changes: 2 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
19 changes: 19 additions & 0 deletions src/reuse/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from gettext import gettext as _
from typing import Any, Dict, Optional

import yaml


@dataclass
class AnnotateOptions:
Expand Down Expand Up @@ -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: [email protected]
default_license: GPL-3.0-or-later
overrides:
- path: ~/Projects/FSFE
default_contact: [email protected]
"""
return cls.from_dict(yaml.load(text, Loader=yaml.Loader))


def _annotate_options_from_dict(value: Dict[str, str]) -> AnnotateOptions:
return AnnotateOptions(
Expand Down
26 changes: 26 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

"""Tests for some _config."""

from inspect import cleandoc

from reuse._config import Config

# REUSE-IgnoreStart
Expand Down Expand Up @@ -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: [email protected]
default_license: GPL-3.0-or-later
overrides:
- path: ~/Projects/FSFE
default_contact: [email protected]
"""
)
result = Config.from_yaml(text)
assert result.global_annotate_options.name == "Jane Doe"
assert result.global_annotate_options.contact == "[email protected]"
assert result.global_annotate_options.license == "GPL-3.0-or-later"
assert (
result.override_annotate_options["~/Projects/FSFE"].contact
== "[email protected]"
)


# REUSE-IgnoreEnd

0 comments on commit 206b68f

Please sign in to comment.