Skip to content

Commit 811712c

Browse files
authored
Merge pull request #88 from ActivityWatch/dev/config-toml
2 parents aa14c8b + 067bb69 commit 811712c

File tree

5 files changed

+172
-12
lines changed

5 files changed

+172
-12
lines changed

aw_core/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
__summary__ = "Core library for ActivityWatch"
1717
__uri__ = "https://github.com/ActivityWatch/aw-core"
1818

19-
__version__ = "0.4.1"
19+
__version__ = "0.4.2"
2020

2121
__author__ = "Erik Bjäreholt, Johan Bjäreholt"
2222
__email__ = "erik@bjareho.lt, johan@bjareho.lt"

aw_core/config.py

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,83 @@
11
import os
22
import logging
3+
from typing import Any, Dict, Union
34
from configparser import ConfigParser
45

6+
from deprecation import deprecated
7+
import tomlkit
8+
59
from aw_core import dirs
10+
from aw_core.__about__ import __version__
611

712
logger = logging.getLogger(__name__)
813

914

15+
def _merge(a: dict, b: dict, path=None):
16+
"""
17+
Recursively merges b into a, with b taking precedence.
18+
19+
From: https://stackoverflow.com/a/7205107/965332
20+
"""
21+
if path is None:
22+
path = []
23+
for key in b:
24+
if key in a:
25+
if isinstance(a[key], dict) and isinstance(b[key], dict):
26+
_merge(a[key], b[key], path + [str(key)])
27+
elif a[key] == b[key]:
28+
pass # same leaf value
29+
else:
30+
a[key] = b[key]
31+
else:
32+
a[key] = b[key]
33+
return a
34+
35+
36+
def _comment_out_toml(s: str):
37+
return "\n".join(["#" + line for line in s.split("\n")])
38+
39+
40+
def load_config_toml(
41+
appname: str, default_config: str
42+
) -> Union[dict, tomlkit.container.Container]:
43+
config_dir = dirs.get_config_dir(appname)
44+
config_file_path = os.path.join(config_dir, "{}.toml".format(appname))
45+
46+
# Run early to ensure input is valid toml before writing
47+
default_config_toml = tomlkit.parse(default_config)
48+
49+
# Override defaults from existing config file
50+
if os.path.isfile(config_file_path):
51+
with open(config_file_path, "r") as f:
52+
config = f.read()
53+
config_toml = tomlkit.parse(config)
54+
else:
55+
# If file doesn't exist, write with commented-out default config
56+
with open(config_file_path, "w") as f:
57+
f.write(_comment_out_toml(default_config))
58+
config_toml = dict()
59+
60+
config = _merge(default_config_toml, config_toml)
61+
62+
return config
63+
64+
65+
def save_config_toml(appname: str, config: str) -> None:
66+
# Check that passed config string is valid toml
67+
assert tomlkit.parse(config)
68+
69+
config_dir = dirs.get_config_dir(appname)
70+
config_file_path = os.path.join(config_dir, "{}.toml".format(appname))
71+
72+
with open(config_file_path, "w") as f:
73+
f.write(config)
74+
75+
76+
@deprecated(
77+
details="Use the load_config_toml function instead",
78+
deprecated_in="0.4.2",
79+
current_version=__version__,
80+
)
1081
def load_config(appname, default_config):
1182
"""
1283
Take the defaults, and if a config file exists, use the settings specified
@@ -15,7 +86,7 @@ def load_config(appname, default_config):
1586
config = default_config
1687

1788
config_dir = dirs.get_config_dir(appname)
18-
config_file_path = os.path.join(config_dir, "{}.ini".format(appname))
89+
config_file_path = os.path.join(config_dir, "{}.toml".format(appname))
1990

2091
# Override defaults from existing config file
2192
if os.path.isfile(config_file_path):
@@ -28,6 +99,11 @@ def load_config(appname, default_config):
2899
return config
29100

30101

102+
@deprecated(
103+
details="Use the save_config_toml function instead",
104+
deprecated_in="0.4.2",
105+
current_version=__version__,
106+
)
31107
def save_config(appname, config):
32108
config_dir = dirs.get_config_dir(appname)
33109
config_file_path = os.path.join(config_dir, "{}.ini".format(appname))

poetry.lock

Lines changed: 30 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ python-json-logger = "^0.1.11"
2525
TakeTheTime = "^0.3.1"
2626
pymongo = {version = "^3.10.0", optional = true}
2727
strict-rfc3339 = "^0.7"
28+
tomlkit = "^0.6.0"
29+
deprecation = "^2.0.7"
2830
timeslot = "*"
2931

3032
[tool.poetry.dev-dependencies]

tests/test_config.py

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,77 @@
1-
import unittest
21
import shutil
32
from configparser import ConfigParser
43

4+
import pytest
5+
import deprecation
6+
57
from aw_core import dirs
6-
from aw_core.config import load_config, save_config
8+
from aw_core.config import load_config, save_config, load_config_toml, save_config_toml
9+
10+
appname = "aw-core-test"
11+
section = "section"
12+
config_dir = dirs.get_config_dir(appname)
13+
14+
default_config_str = f"""# A default config file, with comments!
15+
[{section}]
16+
somestring = "Hello World!" # A comment
17+
somevalue = 12.3 # Another comment
18+
somearray = ["asd", 123]"""
19+
20+
21+
@pytest.fixture(autouse=True)
22+
def clean_config():
23+
# Remove test config file if it already exists
24+
shutil.rmtree(config_dir, ignore_errors=True)
25+
26+
# Rerun get_config dir to create config directory
27+
dirs.get_config_dir(appname)
28+
29+
yield
30+
31+
# Remove test config file if it already exists
32+
shutil.rmtree(config_dir)
733

834

935
def test_create():
1036
appname = "aw-core-test"
1137
section = "section"
1238
config_dir = dirs.get_config_dir(appname)
1339

14-
# Remove test config file if it already exists
15-
shutil.rmtree(config_dir)
1640

41+
def test_config_defaults():
42+
# Load non-existing config (will create a out-commented default config file)
43+
config = load_config_toml(appname, default_config_str)
44+
45+
# Check that load_config used defaults
46+
assert config[section]["somestring"] == "Hello World!"
47+
assert config[section]["somevalue"] == 12.3
48+
assert config[section]["somearray"] == ["asd", 123]
49+
50+
51+
def test_config_no_defaults():
52+
# Write defaults to file
53+
save_config_toml(appname, default_config_str)
54+
55+
# Load written defaults without defaults
56+
config = load_config_toml(appname, "")
57+
assert config[section]["somestring"] == "Hello World!"
58+
assert config[section]["somevalue"] == 12.3
59+
assert config[section]["somearray"] == ["asd", 123]
60+
61+
62+
def test_config_override():
63+
# Create a minimal config file with one overridden value
64+
config = """[section]
65+
somevalue = 1000.1"""
66+
save_config_toml(appname, config)
67+
68+
# Open non-default config file and verify that values are correct
69+
config = load_config_toml(appname, default_config_str)
70+
assert config[section]["somevalue"] == 1000.1
71+
72+
73+
@deprecation.fail_if_not_removed
74+
def test_config_ini():
1775
# Create default config
1876
default_config = ConfigParser()
1977
default_config[section] = {"somestring": "Hello World!", "somevalue": 12.3}
@@ -37,6 +95,3 @@ def test_create():
3795
assert new_config[section].getfloat("somevalue") == config[section].getfloat(
3896
"somevalue"
3997
)
40-
41-
# Remove test config file
42-
shutil.rmtree(config_dir)

0 commit comments

Comments
 (0)