Skip to content

Commit

Permalink
Create Config class
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 d92db87 commit a121a6f
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/reuse/_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# SPDX-FileCopyrightText: 2023 Carmen Bianca BAKKER <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later

"""REUSE configuration."""

from dataclasses import dataclass, field
from gettext import gettext as _
from typing import Any, Dict, Optional


@dataclass
class AnnotateOptions:
"""An object to hold the default values for annotation."""

name: Optional[str] = None
contact: Optional[str] = None
license: Optional[str] = None


@dataclass
class Config:
"""Object to hold all configuration options."""

global_annotate_options: AnnotateOptions = field(
default_factory=AnnotateOptions
)
#: Only truthy attributes override the global options. The key is the path
#: to which the override options apply.
override_annotate_options: Dict[str, AnnotateOptions] = field(
default_factory=dict
)

@classmethod
def from_dict(cls, value: Dict[str, Any]) -> "Config":
"""Factory method to create a Config from a dictionary."""
config = cls()
if annotate := value.get("annotate"):
config.global_annotate_options = _annotate_options_from_dict(
annotate
)
for override in annotate.get("overrides", []):
if not (path := override.get("path")):
raise ValueError(
_("'path' key is missing from one of the overrides.")
)
config.override_annotate_options[
path
] = _annotate_options_from_dict(override)
return config


def _annotate_options_from_dict(value: Dict[str, str]) -> AnnotateOptions:
return AnnotateOptions(
name=value.get("default_name"),
contact=value.get("default_contact"),
license=value.get("default_license"),
)
64 changes: 64 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# SPDX-FileCopyrightText: 2023 Carmen Bianca BAKKER <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later

"""Tests for some _config."""

from reuse._config import Config

# REUSE-IgnoreStart


def test_config_from_dict_global_simple():
"""A simple test case for Config.from_dict."""
value = {
"annotate": {
"default_name": "Jane Doe",
"default_contact": "[email protected]",
"default_license": "MIT",
}
}
result = Config.from_dict(value)
assert result.global_annotate_options.name == "Jane Doe"
assert result.global_annotate_options.contact == "[email protected]"
assert result.global_annotate_options.license == "MIT"


def test_config_from_dict_global_missing():
"""Only one value is defined."""
value = {
"annotate": {
"default_name": "Jane Doe",
}
}
result = Config.from_dict(value)
assert result.global_annotate_options.name == "Jane Doe"
assert result.global_annotate_options.contact is None
assert result.global_annotate_options.license is None


def test_config_from_dict_override():
"""Overrides are correctly parsed."""
value = {
"annotate": {
"default_name": "Jane Doe",
"overrides": [
{
"path": "foo",
"default_name": "John Doe",
},
{
"path": "bar",
"default_license": "MIT",
},
],
}
}
result = Config.from_dict(value)
assert result.global_annotate_options.name == "Jane Doe"
assert result.override_annotate_options["foo"].name == "John Doe"
assert result.override_annotate_options["bar"].name is None
assert result.override_annotate_options["bar"].license == "MIT"


# REUSE-IgnoreEnd

0 comments on commit a121a6f

Please sign in to comment.