Skip to content

Commit

Permalink
Be able to extends one config
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Aug 13, 2022
1 parent 477a618 commit a0afeda
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
10 changes: 10 additions & 0 deletions config.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

## Properties

- **`extends`** _(string)_: The configuration to extends.
- **`merge_strategies`** _(object)_: The merge strategy to use, see https://deepmerge.readthedocs.io/en/latest/strategies.html#builtin-strategies.
- **`list`** _(array)_: The merge strategy to use on list. Default: `['override']`.
- **Items** _(string)_
- **`dict`** _(array)_: The merge strategy to use on dict. Default: `['merge']`.
- **Items** _(string)_
- **`fallback`** _(array)_: The fallback merge strategy. Default: `['override']`.
- **Items** _(string)_
- **`type_conflict`** _(array)_: The type_conflict merge strategy. Default: `['override']`.
- **Items** _(string)_
- **`scan_folder`** _(string)_: This should be shared with the process container in 'source'.
- **`scanimage`** _(string)_: The scanimage command. Default: `scanimage`.
- **`scanimage_arguments`** _(array)_: The scanimage arguments. Default: `['--format=png', '--mode=color', '--resolution=300']`.
Expand Down
23 changes: 21 additions & 2 deletions scan_to_paperless/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
from typing import cast

from deepmerge import Merger
from ruamel.yaml.main import YAML

if sys.version_info.minor >= 8:
Expand All @@ -28,6 +29,24 @@ def get_config(config_filename: str) -> stp_config.Configuration:
yaml = YAML()
yaml.default_flow_style = False
with open(config_filename, encoding="utf-8") as config_file:
return cast(stp_config.Configuration, yaml.load(config_file.read()))
print("Missig config file: " + config_filename)
config = cast(stp_config.Configuration, yaml.load(config_file))
if "exteds" in config:

base_config = get_config(
os.path.normpath(
os.path.join(os.path.dirname(config_filename), os.path.expanduser(config["extends"]))
)
)
strategies_config = config.get("strategies", {})
merger = Merger(
[
(list, strategies_config.get("list", ["override"])),
(dict, strategies_config.get("dict", ["merge"])),
],
strategies_config.get("fallback", ["merge"]),
strategies_config.get("type_conflict", ["merge"]),
)
config = cast(stp_config.Configuration, merger.merge(base_config, config))
return config
print(f"Missing config file: {config_filename}")
return {}
34 changes: 34 additions & 0 deletions scan_to_paperless/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@
Configuration = TypedDict(
"Configuration",
{
# The configuration to extends
"extends": str,
# WARNING: The required are not correctly taken in account,
# See: https://github.com/camptocamp/jsonschema-gentypes/issues/6
"merge_strategies": "_ConfigurationMergeStrategies",
# This should be shared with the process container in 'source'.
"scan_folder": str,
# The scanimage command
Expand Down Expand Up @@ -262,6 +267,35 @@
)


# The merge strategy to use, see https://deepmerge.readthedocs.io/en/latest/strategies.html#builtin-strategies
_ConfigurationMergeStrategies = TypedDict(
"_ConfigurationMergeStrategies",
{
# The merge strategy to use on list
#
# default:
# - override
"list": List[str],
# The merge strategy to use on dict
#
# default:
# - merge
"dict": List[str],
# The fallback merge strategy
#
# default:
# - override
"fallback": List[str],
# The type_conflict merge strategy
#
# default:
# - override
"type_conflict": List[str],
},
total=False,
)


_ConfigurationModesAdditionalproperties = TypedDict(
"_ConfigurationModesAdditionalproperties",
{
Expand Down
34 changes: 34 additions & 0 deletions scan_to_paperless/config_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,40 @@
}
},
"properties": {
"extends": {
"type": "string",
"description": "The configuration to extends"
},
"merge_strategies": {
"type": "object",
"description": "The merge strategy to use, see https://deepmerge.readthedocs.io/en/latest/strategies.html#builtin-strategies",
"properties": {
"list": {
"type": "array",
"description": "The merge strategy to use on list",
"default": ["override"],
"items": { "type": "string" }
},
"dict": {
"type": "array",
"description": "The merge strategy to use on dict",
"default": ["merge"],
"items": { "type": "string" }
},
"fallback": {
"type": "array",
"description": "The fallback merge strategy",
"default": ["override"],
"items": { "type": "string" }
},
"type_conflict": {
"type": "array",
"description": "The type_conflict merge strategy",
"default": ["override"],
"items": { "type": "string" }
}
}
},
"scan_folder": {
"type": "string",
"description": "This should be shared with the process container in 'source'."
Expand Down

0 comments on commit a0afeda

Please sign in to comment.