mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-20 04:58:32 +02:00
make format
This commit is contained in:
parent
99030c4623
commit
b92dd7edc7
6 changed files with 69 additions and 63 deletions
|
@ -213,15 +213,18 @@ def open_editor_and_enter(context, method, text=""):
|
||||||
context.exit_status = e.code
|
context.exit_status = e.code
|
||||||
|
|
||||||
# fmt: on
|
# fmt: on
|
||||||
|
|
||||||
|
|
||||||
@then("the runtime config should have {key_as_dots} set to {override_value}")
|
@then("the runtime config should have {key_as_dots} set to {override_value}")
|
||||||
def config_override(context, key_as_dots:str, override_value: str):
|
def config_override(context, key_as_dots: str, override_value: str):
|
||||||
with open(context.config_path) as f:
|
with open(context.config_path) as f:
|
||||||
loaded_cfg = yaml.load(f, Loader=yaml.FullLoader)
|
loaded_cfg = yaml.load(f, Loader=yaml.FullLoader)
|
||||||
loaded_cfg['journal']='features/journals/simple.journal'
|
loaded_cfg["journal"] = "features/journals/simple.journal"
|
||||||
base_cfg = loaded_cfg.copy()
|
base_cfg = loaded_cfg.copy()
|
||||||
def _mock_callback(**args):
|
|
||||||
|
def _mock_callback(**args):
|
||||||
print("callback executed")
|
print("callback executed")
|
||||||
|
|
||||||
# fmt: off
|
# fmt: off
|
||||||
try:
|
try:
|
||||||
with \
|
with \
|
||||||
|
@ -241,6 +244,8 @@ def config_override(context, key_as_dots:str, override_value: str):
|
||||||
except SystemExit as e :
|
except SystemExit as e :
|
||||||
context.exit_status = e.code
|
context.exit_status = e.code
|
||||||
# fmt: on
|
# fmt: on
|
||||||
|
|
||||||
|
|
||||||
@then("the editor {editor} should have been called")
|
@then("the editor {editor} should have been called")
|
||||||
def editor_override(context, editor):
|
def editor_override(context, editor):
|
||||||
def _mock_editor(command_and_journal_file):
|
def _mock_editor(command_and_journal_file):
|
||||||
|
|
|
@ -52,8 +52,9 @@ def run(args):
|
||||||
# Apply config overrides
|
# Apply config overrides
|
||||||
overrides = args.config_override
|
overrides = args.config_override
|
||||||
from .override import apply_overrides
|
from .override import apply_overrides
|
||||||
config = apply_overrides(overrides,config)
|
|
||||||
|
config = apply_overrides(overrides, config)
|
||||||
|
|
||||||
# --- All the standalone commands are now done --- #
|
# --- All the standalone commands are now done --- #
|
||||||
|
|
||||||
# Get the journal we're going to be working with
|
# Get the journal we're going to be working with
|
||||||
|
|
|
@ -1,25 +1,28 @@
|
||||||
# import logging
|
# import logging
|
||||||
def apply_overrides(overrides: dict, base_config: dict) -> dict:
|
def apply_overrides(overrides: dict, base_config: dict) -> dict:
|
||||||
config = base_config.copy()
|
config = base_config.copy()
|
||||||
for k in overrides:
|
for k in overrides:
|
||||||
nodes = k.split('.')
|
nodes = k.split(".")
|
||||||
config = recursively_apply(config, nodes, overrides[k])
|
config = recursively_apply(config, nodes, overrides[k])
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
def recursively_apply(config: dict, nodes: list, override_value) -> dict:
|
def recursively_apply(config: dict, nodes: list, override_value) -> dict:
|
||||||
"""Recurse through configuration and apply overrides at the leaf of the config tree
|
"""Recurse through configuration and apply overrides at the leaf of the config tree
|
||||||
|
|
||||||
See: https://stackoverflow.com/a/47276490 for algorithm
|
See: https://stackoverflow.com/a/47276490 for algorithm
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
config (dict): loaded configuration from YAML
|
config (dict): loaded configuration from YAML
|
||||||
nodes (list): vector of override keys; the length of the vector indicates tree depth
|
nodes (list): vector of override keys; the length of the vector indicates tree depth
|
||||||
override_value (str): runtime override passed from the command-line
|
override_value (str): runtime override passed from the command-line
|
||||||
"""
|
"""
|
||||||
key = nodes[0]
|
key = nodes[0]
|
||||||
config[key] = override_value \
|
config[key] = (
|
||||||
if len(nodes) == 1 \
|
override_value
|
||||||
else \
|
if len(nodes) == 1
|
||||||
recursively_apply(config[key] if key in config else {}, nodes[1:], override_value)
|
else recursively_apply(
|
||||||
return config
|
config[key] if key in config else {}, nodes[1:], override_value
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return config
|
||||||
|
|
|
@ -43,7 +43,8 @@ def test_override_configured_editor(
|
||||||
cli_args = ["--config-override", '{"editor": "nano"}']
|
cli_args = ["--config-override", '{"editor": "nano"}']
|
||||||
parser = parse_args(cli_args)
|
parser = parse_args(cli_args)
|
||||||
assert parser.config_override.__len__() == 1
|
assert parser.config_override.__len__() == 1
|
||||||
assert "editor" in parser.config_override.keys()
|
assert "editor" in parser.config_override.keys()
|
||||||
|
|
||||||
def mock_editor_launch(editor):
|
def mock_editor_launch(editor):
|
||||||
print("%s launched! Success!" % editor)
|
print("%s launched! Success!" % editor)
|
||||||
|
|
||||||
|
@ -56,21 +57,21 @@ def test_override_configured_editor(
|
||||||
run(parser)
|
run(parser)
|
||||||
mock_write_in_editor.assert_called_once_with(expected_override)
|
mock_write_in_editor.assert_called_once_with(expected_override)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
def expected_color_override(minimal_config):
|
def expected_color_override(minimal_config):
|
||||||
exp_out_cfg = minimal_config.copy()
|
exp_out_cfg = minimal_config.copy()
|
||||||
exp_out_cfg["colors"] = {"body": "blue"}
|
exp_out_cfg["colors"] = {"body": "blue"}
|
||||||
exp_out_cfg["journal"] = "features/journals/simple.journal"
|
exp_out_cfg["journal"] = "features/journals/simple.journal"
|
||||||
yield exp_out_cfg
|
yield exp_out_cfg
|
||||||
|
|
||||||
|
|
||||||
# @mock.patch.object(install,'load_or_install_jrnl')
|
# @mock.patch.object(install,'load_or_install_jrnl')
|
||||||
# @mock.patch('subprocess.call')
|
# @mock.patch('subprocess.call')
|
||||||
# def test_override_configured_colors(mock_load_or_install, mock_subprocess_call, minimal_config, expected_color_override, capsys):
|
# def test_override_configured_colors(mock_load_or_install, mock_subprocess_call, minimal_config, expected_color_override, capsys):
|
||||||
# mock_load_or_install.return_value = minimal_config
|
# mock_load_or_install.return_value = minimal_config
|
||||||
|
|
||||||
# cli_args=["--config-override",'{"colors.body": "blue"}']
|
# cli_args=["--config-override",'{"colors.body": "blue"}']
|
||||||
# parser = parse_args(cli_args)
|
# parser = parse_args(cli_args)
|
||||||
# assert "colors.body" in parser.config_override.keys()
|
# assert "colors.body" in parser.config_override.keys()
|
||||||
# run(parser)
|
# run(parser)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,41 +1,34 @@
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from jrnl.override import apply_overrides, recursively_apply
|
from jrnl.override import apply_overrides, recursively_apply
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
def minimal_config():
|
def minimal_config():
|
||||||
cfg = {
|
cfg = {
|
||||||
"colors":{
|
"colors": {"body": "red", "date": "green"},
|
||||||
"body":"red",
|
"default": "/tmp/journal.jrnl",
|
||||||
"date":"green"
|
"editor": "vim",
|
||||||
},
|
|
||||||
"default":"/tmp/journal.jrnl",
|
|
||||||
"editor":"vim"
|
|
||||||
}
|
}
|
||||||
yield cfg
|
yield cfg
|
||||||
|
|
||||||
def test_apply_override(minimal_config):
|
|
||||||
|
def test_apply_override(minimal_config):
|
||||||
config = minimal_config.copy()
|
config = minimal_config.copy()
|
||||||
overrides = {
|
overrides = {"editor": "nano"}
|
||||||
'editor':'nano'
|
|
||||||
}
|
|
||||||
config = apply_overrides(overrides, config)
|
config = apply_overrides(overrides, config)
|
||||||
assert config['editor']=='nano'
|
assert config["editor"] == "nano"
|
||||||
|
|
||||||
def test_override_dot_notation(minimal_config):
|
|
||||||
cfg = minimal_config.copy()
|
def test_override_dot_notation(minimal_config):
|
||||||
overrides = {
|
cfg = minimal_config.copy()
|
||||||
"colors.body": "blue"
|
overrides = {"colors.body": "blue"}
|
||||||
}
|
|
||||||
cfg = apply_overrides(overrides=overrides, base_config=cfg)
|
cfg = apply_overrides(overrides=overrides, base_config=cfg)
|
||||||
assert cfg["colors"] == {"body": "blue", "date":"green"}
|
assert cfg["colors"] == {"body": "blue", "date": "green"}
|
||||||
|
|
||||||
def test_recursive_override(minimal_config):
|
|
||||||
|
def test_recursive_override(minimal_config):
|
||||||
cfg = {
|
|
||||||
"colors": {
|
cfg = {"colors": {"body": "red", "title": "green"}}
|
||||||
"body": "red",
|
cfg = recursively_apply(cfg, ["colors", "body"], "blue")
|
||||||
"title": "green"
|
assert cfg["colors"]["body"] == "blue"
|
||||||
}
|
|
||||||
}
|
|
||||||
cfg = recursively_apply(cfg,["colors",'body'],"blue")
|
|
||||||
assert cfg["colors"]["body"] == "blue"
|
|
||||||
|
|
|
@ -211,10 +211,13 @@ def test_editor_override():
|
||||||
assert cli_as_dict('--config-override \'{"editor": "nano"}\'') == expected_args(
|
assert cli_as_dict('--config-override \'{"editor": "nano"}\'') == expected_args(
|
||||||
config_override={"editor": "nano"}
|
config_override={"editor": "nano"}
|
||||||
)
|
)
|
||||||
def test_color_override():
|
|
||||||
assert cli_as_dict('--config-override \'{"colors.body": "blue"}\'') == expected_args(
|
|
||||||
config_override={"colors.body":"blue"}
|
def test_color_override():
|
||||||
)
|
assert cli_as_dict(
|
||||||
|
'--config-override \'{"colors.body": "blue"}\''
|
||||||
|
) == expected_args(config_override={"colors.body": "blue"})
|
||||||
|
|
||||||
|
|
||||||
# @see https://github.com/jrnl-org/jrnl/issues/520
|
# @see https://github.com/jrnl-org/jrnl/issues/520
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
|
Loading…
Add table
Reference in a new issue