Skip to content

Commit 961936a

Browse files
committed
config files can be placed different places and have different names
1 parent e9d770c commit 961936a

4 files changed

Lines changed: 46 additions & 19 deletions

File tree

caldav/config.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import json
2+
import logging
3+
import os
24

35
"""
46
This configuration parsing code was just copied from my plann library (and will be removed from there at some point in the future). It's lacking tests, documentation and ... generally just lacking.
@@ -71,6 +73,21 @@ def config_section(config, section="default"):
7173

7274

7375
def read_config(fn, interactive_error=False):
76+
if not fn:
77+
cfgdir = f"{os.environ.get('HOME', '/')}/.config/"
78+
for config_file in (
79+
f"{cfgdir}/caldav/calendar.conf",
80+
f"{cfgdir}/caldav/calendar.yaml"
81+
f"{cfgdir}/caldav/calendar.json"
82+
f"{cfgdir}/calendar.conf",
83+
"/etc/calendar.conf",
84+
"/etc/caldav/calendar.conf",
85+
):
86+
cfg = read_config(config_file)
87+
if cfg:
88+
return cfg
89+
return None
90+
7491
## This can probably be refactored into fewer lines ...
7592
try:
7693
try:

caldav/davclient.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ def request(
892892

893893

894894
def auto_calendars(
895-
config_file: str = f"{os.environ.get('HOME')}/.config/calendar.conf",
895+
config_file: str = None,
896896
config_section="default",
897897
testconfig=False,
898898
environment: bool = True,
@@ -1002,23 +1002,7 @@ def get_davclient(
10021002
if not config_section:
10031003
config_section = "default"
10041004

1005-
if not config_file:
1006-
cfgdir = f"{os.environ.get('HOME', '/')}/.config/"
1007-
for config_file in (
1008-
f"{cfgdir}/caldav/calendar.conf",
1009-
f"{cfgdir}/caldav/calendar.yaml"
1010-
f"{cfgdir}/caldav/calendar.json"
1011-
f"{cfgdir}/calendar.conf",
1012-
"/etc/calendar.conf",
1013-
"/etc/caldav/calendar.conf",
1014-
):
1015-
try:
1016-
cfg = config.read_config(config_file)
1017-
break
1018-
except FileNotFoundError:
1019-
pass
1020-
else:
1021-
cfg = config.read_config(config_file)
1005+
cfg = config.read_config(config_file)
10221006
if cfg:
10231007
section = config.config_section(cfg, config_section)
10241008
conn_params = {}

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ test = [
5959
"dulwich==0.20.50;python_version<'3.9'",
6060
"xandikos;python_version>='3.9'",
6161
"radicale",
62+
"pyfakefs"
6263
]
6364

6465
[tool.setuptools_scm]

tests/test_caldav.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,9 @@ def testTestConfig(self):
454454

455455
def testEnvironment(self):
456456
os.environ["PYTHON_CALDAV_USE_TEST_SERVER"] = "1"
457-
with get_davclient(environment=True, check_config_file=False, name="-1") as conn:
457+
with get_davclient(
458+
environment=True, check_config_file=False, name="-1"
459+
) as conn:
458460
assert conn.principal()
459461
del os.environ["PYTHON_CALDAV_USE_TEST_SERVER"]
460462
for key in ("url", "username", "password", "proxy"):
@@ -486,6 +488,29 @@ def testConfigfile(self):
486488
) as conn2:
487489
assert conn2.principal()
488490

491+
def testNoConfigfile(self, fs):
492+
"""This is actually a unit test, not a functional test.
493+
Should move it to another file probably, and make more unit
494+
tests covering the config file parsing
495+
"""
496+
assert get_davclient(testconfig=False, environment=False) is None
497+
HOME = os.environ["HOME"]
498+
fs.create_dir(f"{HOME}/.config/caldav")
499+
fs.create_file(
500+
f"{HOME}/.config/caldav/calendar.conf",
501+
contents=json.dumps(
502+
{
503+
"default": {
504+
"caldav_url": "https://caldav.example.com/dav",
505+
"caldav_username": "karl",
506+
"caldav_password": "hunter2",
507+
}
508+
}
509+
),
510+
)
511+
client = get_davclient(testconfig=False, environment=False)
512+
assert client.url == "https://caldav.example.com/dav"
513+
489514

490515
@pytest.mark.skipif(
491516
not rfc6638_users, reason="need rfc6638_users to be set in order to run this test"

0 commit comments

Comments
 (0)