config.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. # This file is part of Radicale - CalDAV and CardDAV server
  2. # Copyright © 2008-2017 Guillaume Ayoub
  3. # Copyright © 2008 Nicolas Kandel
  4. # Copyright © 2008 Pascal Halter
  5. # Copyright © 2017-2020 Unrud <unrud@outlook.com>
  6. # Copyright © 2024-2025 Peter Bieringer <pb@bieringer.de>
  7. #
  8. # This library is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This library is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  20. """
  21. Configuration module
  22. Use ``load()`` to obtain an instance of ``Configuration`` for use with
  23. ``radicale.app.Application``.
  24. """
  25. import contextlib
  26. import json
  27. import math
  28. import os
  29. import string
  30. import sys
  31. from collections import OrderedDict
  32. from configparser import RawConfigParser
  33. from typing import (Any, Callable, ClassVar, Iterable, List, Optional,
  34. Sequence, Tuple, TypeVar, Union)
  35. from radicale import auth, hook, rights, storage, types, web
  36. from radicale.hook import email
  37. from radicale.item import check_and_sanitize_props
  38. DEFAULT_CONFIG_PATH: str = os.pathsep.join([
  39. "?/etc/radicale/config",
  40. "?~/.config/radicale/config"])
  41. def positive_int(value: Any) -> int:
  42. value = int(value)
  43. if value < 0:
  44. raise ValueError("value is negative: %d" % value)
  45. return value
  46. def positive_float(value: Any) -> float:
  47. value = float(value)
  48. if not math.isfinite(value):
  49. raise ValueError("value is infinite")
  50. if math.isnan(value):
  51. raise ValueError("value is not a number")
  52. if value < 0:
  53. raise ValueError("value is negative: %f" % value)
  54. return value
  55. def logging_level(value: Any) -> str:
  56. if value not in ("debug", "info", "warning", "error", "critical"):
  57. raise ValueError("unsupported level: %r" % value)
  58. return value
  59. def filepath(value: Any) -> str:
  60. if not value:
  61. return ""
  62. value = os.path.expanduser(value)
  63. if sys.platform == "win32":
  64. value = os.path.expandvars(value)
  65. return os.path.abspath(value)
  66. def list_of_ip_address(value: Any) -> List[Tuple[str, int]]:
  67. def ip_address(value):
  68. try:
  69. address, port = value.rsplit(":", 1)
  70. return address.strip(string.whitespace + "[]"), int(port)
  71. except ValueError:
  72. raise ValueError("malformed IP address: %r" % value)
  73. return [ip_address(s) for s in value.split(",")]
  74. def str_or_callable(value: Any) -> Union[str, Callable]:
  75. if callable(value):
  76. return value
  77. return str(value)
  78. def unspecified_type(value: Any) -> Any:
  79. return value
  80. def _convert_to_bool(value: Any) -> bool:
  81. if value.lower() not in RawConfigParser.BOOLEAN_STATES:
  82. raise ValueError("not a boolean: %r" % value)
  83. return RawConfigParser.BOOLEAN_STATES[value.lower()]
  84. def imap_address(value):
  85. if "]" in value:
  86. pre_address, pre_address_port = value.rsplit("]", 1)
  87. else:
  88. pre_address, pre_address_port = "", value
  89. if ":" in pre_address_port:
  90. pre_address2, port = pre_address_port.rsplit(":", 1)
  91. address = pre_address + pre_address2
  92. else:
  93. address, port = pre_address + pre_address_port, None
  94. try:
  95. return (address.strip(string.whitespace + "[]"),
  96. None if port is None else int(port))
  97. except ValueError:
  98. raise ValueError("malformed IMAP address: %r" % value)
  99. def imap_security(value):
  100. if value not in ("tls", "starttls", "none"):
  101. raise ValueError("unsupported IMAP security: %r" % value)
  102. return value
  103. def json_str(value: Any) -> dict:
  104. if not value:
  105. return {}
  106. ret = json.loads(value)
  107. for (name_coll, props) in ret.items():
  108. checked_props = check_and_sanitize_props(props)
  109. ret[name_coll] = checked_props
  110. return ret
  111. INTERNAL_OPTIONS: Sequence[str] = ("_allow_extra",)
  112. # Default configuration
  113. DEFAULT_CONFIG_SCHEMA: types.CONFIG_SCHEMA = OrderedDict([
  114. ("server", OrderedDict([
  115. ("hosts", {
  116. "value": "localhost:5232",
  117. "help": "set server hostnames including ports",
  118. "aliases": ("-H", "--hosts",),
  119. "type": list_of_ip_address}),
  120. ("max_connections", {
  121. "value": "8",
  122. "help": "maximum number of parallel connections",
  123. "type": positive_int}),
  124. ("max_content_length", {
  125. "value": "100000000",
  126. "help": "maximum size of request body in bytes",
  127. "type": positive_int}),
  128. ("timeout", {
  129. "value": "30",
  130. "help": "socket timeout",
  131. "type": positive_float}),
  132. ("ssl", {
  133. "value": "False",
  134. "help": "use SSL connection",
  135. "aliases": ("-s", "--ssl",),
  136. "opposite_aliases": ("-S", "--no-ssl",),
  137. "type": bool}),
  138. ("protocol", {
  139. "value": "",
  140. "help": "SSL/TLS protocol (Apache SSLProtocol format)",
  141. "type": str}),
  142. ("ciphersuite", {
  143. "value": "",
  144. "help": "SSL/TLS Cipher Suite (OpenSSL cipher list format)",
  145. "type": str}),
  146. ("certificate", {
  147. "value": "/etc/ssl/radicale.cert.pem",
  148. "help": "set certificate file",
  149. "aliases": ("-c", "--certificate",),
  150. "type": filepath}),
  151. ("key", {
  152. "value": "/etc/ssl/radicale.key.pem",
  153. "help": "set private key file",
  154. "aliases": ("-k", "--key",),
  155. "type": filepath}),
  156. ("certificate_authority", {
  157. "value": "",
  158. "help": "set CA certificate for validating clients",
  159. "aliases": ("--certificate-authority",),
  160. "type": filepath}),
  161. ("script_name", {
  162. "value": "",
  163. "help": "script name to strip from URI if called by reverse proxy (default taken from HTTP_X_SCRIPT_NAME or SCRIPT_NAME)",
  164. "type": str}),
  165. ("_internal_server", {
  166. "value": "False",
  167. "help": "the internal server is used",
  168. "type": bool})])),
  169. ("encoding", OrderedDict([
  170. ("request", {
  171. "value": "utf-8",
  172. "help": "encoding for responding requests",
  173. "type": str}),
  174. ("stock", {
  175. "value": "utf-8",
  176. "help": "encoding for storing local collections",
  177. "type": str})])),
  178. ("auth", OrderedDict([
  179. ("type", {
  180. "value": "denyall",
  181. "help": "authentication method (" + "|".join(auth.INTERNAL_TYPES) + ")",
  182. "type": str_or_callable,
  183. "internal": auth.INTERNAL_TYPES}),
  184. ("cache_logins", {
  185. "value": "false",
  186. "help": "cache successful/failed logins for until expiration time",
  187. "type": bool}),
  188. ("cache_successful_logins_expiry", {
  189. "value": "15",
  190. "help": "expiration time for caching successful logins in seconds",
  191. "type": int}),
  192. ("cache_failed_logins_expiry", {
  193. "value": "90",
  194. "help": "expiration time for caching failed logins in seconds",
  195. "type": int}),
  196. ("htpasswd_filename", {
  197. "value": "/etc/radicale/users",
  198. "help": "htpasswd filename",
  199. "type": filepath}),
  200. ("htpasswd_encryption", {
  201. "value": "autodetect",
  202. "help": "htpasswd encryption method",
  203. "type": str}),
  204. ("htpasswd_cache", {
  205. "value": "False",
  206. "help": "enable caching of htpasswd file",
  207. "type": bool}),
  208. ("dovecot_connection_type", {
  209. "value": "AF_UNIX",
  210. "help": "Connection type for dovecot authentication",
  211. "type": str_or_callable,
  212. "internal": auth.AUTH_SOCKET_FAMILY}),
  213. ("dovecot_socket", {
  214. "value": "/var/run/dovecot/auth-client",
  215. "help": "dovecot auth AF_UNIX socket",
  216. "type": str}),
  217. ("dovecot_host", {
  218. "value": "localhost",
  219. "help": "dovecot auth AF_INET or AF_INET6 host",
  220. "type": str}),
  221. ("dovecot_port", {
  222. "value": "12345",
  223. "help": "dovecot auth port",
  224. "type": int}),
  225. ("remote_ip_source", {
  226. "value": "REMOTE_ADDR",
  227. "help": "remote address source for passing it to auth method",
  228. "type": str,
  229. "internal": auth.REMOTE_ADDR_SOURCE}),
  230. ("realm", {
  231. "value": "Radicale - Password Required",
  232. "help": "message displayed when a password is needed",
  233. "type": str}),
  234. ("delay", {
  235. "value": "1",
  236. "help": "incorrect authentication delay",
  237. "type": positive_float}),
  238. ("ldap_uri", {
  239. "value": "ldap://localhost",
  240. "help": "URI to the LDAP server",
  241. "type": str}),
  242. ("ldap_base", {
  243. "value": "",
  244. "help": "Base DN of the LDAP server",
  245. "type": str}),
  246. ("ldap_reader_dn", {
  247. "value": "",
  248. "help": "DN of an LDAP user with read access to users anmd - if defined - groups",
  249. "type": str}),
  250. ("ldap_secret", {
  251. "value": "",
  252. "help": "Password of ldap_reader_dn (better: use ldap_secret_file)",
  253. "type": str}),
  254. ("ldap_secret_file", {
  255. "value": "",
  256. "help": "Path to the file containing the password of ldap_reader_dn",
  257. "type": str}),
  258. ("ldap_filter", {
  259. "value": "(cn={0})",
  260. "help": "Filter to search for the LDAP entry of the user to authenticate",
  261. "type": str}),
  262. ("ldap_user_attribute", {
  263. "value": "",
  264. "help": "Attribute to be used as username after authentication",
  265. "type": str}),
  266. ("ldap_use_ssl", {
  267. "value": "False",
  268. "help": "Use ssl on the LDAP connection. Deprecated, use ldap_security instead!",
  269. "type": bool}),
  270. ("ldap_security", {
  271. "value": "none",
  272. "help": "Encryption mode to be used: *none*|tls|starttls",
  273. "type": str}),
  274. ("ldap_ssl_verify_mode", {
  275. "value": "REQUIRED",
  276. "help": "Certificate verification mode for tls and starttls. NONE, OPTIONAL, default is REQUIRED",
  277. "type": str}),
  278. ("ldap_ssl_ca_file", {
  279. "value": "",
  280. "help": "Path to the CA file in PEM format which is used to certify the server certificate",
  281. "type": str}),
  282. ("ldap_groups_attribute", {
  283. "value": "",
  284. "help": "Attribute in the user's LDAP entry to read the group memberships from",
  285. "type": str}),
  286. ("ldap_group_members_attribute", {
  287. "value": "",
  288. "help": "Attribute in the group entries to read the group's members from",
  289. "type": str}),
  290. ("ldap_group_base", {
  291. "value": "",
  292. "help": "Base DN to search for groups. Only if it differs from ldap_base and if ldap_group_members_attribute is set",
  293. "type": str}),
  294. ("ldap_group_filter", {
  295. "value": "",
  296. "help": "Search filter to search for groups having the user as member. Only if ldap_group_members_attribute is set",
  297. "type": str}),
  298. ("ldap_ignore_attribute_create_modify_timestamp", {
  299. "value": "false",
  300. "help": "Quirk for Authentik LDAP server: ignore modifyTimestamp and createTimestamp attributes.",
  301. "type": bool}),
  302. ("imap_host", {
  303. "value": "localhost",
  304. "help": "IMAP server hostname: address|address:port|[address]:port|*localhost*",
  305. "type": imap_address}),
  306. ("imap_security", {
  307. "value": "tls",
  308. "help": "Secure the IMAP connection: *tls*|starttls|none",
  309. "type": imap_security}),
  310. ("oauth2_token_endpoint", {
  311. "value": "",
  312. "help": "OAuth2 token endpoint URL",
  313. "type": str}),
  314. ("pam_group_membership", {
  315. "value": "",
  316. "help": "PAM group user should be member of",
  317. "type": str}),
  318. ("pam_service", {
  319. "value": "radicale",
  320. "help": "PAM service",
  321. "type": str}),
  322. ("strip_domain", {
  323. "value": "False",
  324. "help": "strip domain from username",
  325. "type": bool}),
  326. ("uc_username", {
  327. "value": "False",
  328. "help": "convert username to uppercase, must be true for case-insensitive auth providers",
  329. "type": bool}),
  330. ("lc_username", {
  331. "value": "False",
  332. "help": "convert username to lowercase, must be true for case-insensitive auth providers",
  333. "type": bool}),
  334. ("urldecode_username", {
  335. "value": "False",
  336. "help": "url-decode the username, set to True when clients send url-encoded email address as username",
  337. "type": bool})])),
  338. ("rights", OrderedDict([
  339. ("type", {
  340. "value": "owner_only",
  341. "help": "rights backend",
  342. "type": str_or_callable,
  343. "internal": rights.INTERNAL_TYPES}),
  344. ("permit_delete_collection", {
  345. "value": "True",
  346. "help": "permit delete of a collection",
  347. "type": bool}),
  348. ("permit_overwrite_collection", {
  349. "value": "True",
  350. "help": "permit overwrite of a collection",
  351. "type": bool}),
  352. ("file", {
  353. "value": "/etc/radicale/rights",
  354. "help": "file for rights management from_file",
  355. "type": filepath})])),
  356. ("storage", OrderedDict([
  357. ("type", {
  358. "value": "multifilesystem",
  359. "help": "storage backend",
  360. "type": str_or_callable,
  361. "internal": storage.INTERNAL_TYPES}),
  362. ("filesystem_folder", {
  363. "value": "/var/lib/radicale/collections",
  364. "help": "path where collections are stored",
  365. "type": filepath}),
  366. ("filesystem_cache_folder", {
  367. "value": "",
  368. "help": "path where cache of collections is stored in case of use_cache_subfolder_* options are active",
  369. "type": filepath}),
  370. ("use_cache_subfolder_for_item", {
  371. "value": "False",
  372. "help": "use subfolder 'collection-cache' for 'item' cache file structure instead of inside collection folder",
  373. "type": bool}),
  374. ("use_cache_subfolder_for_history", {
  375. "value": "False",
  376. "help": "use subfolder 'collection-cache' for 'history' cache file structure instead of inside collection folder",
  377. "type": bool}),
  378. ("use_cache_subfolder_for_synctoken", {
  379. "value": "False",
  380. "help": "use subfolder 'collection-cache' for 'sync-token' cache file structure instead of inside collection folder",
  381. "type": bool}),
  382. ("use_mtime_and_size_for_item_cache", {
  383. "value": "False",
  384. "help": "use mtime and file size instead of SHA256 for 'item' cache (improves speed)",
  385. "type": bool}),
  386. ("folder_umask", {
  387. "value": "",
  388. "help": "umask for folder creation (empty: system default)",
  389. "type": str}),
  390. ("max_sync_token_age", {
  391. "value": "2592000", # 30 days
  392. "help": "delete sync token that are older",
  393. "type": positive_int}),
  394. ("skip_broken_item", {
  395. "value": "True",
  396. "help": "skip broken item instead of triggering exception",
  397. "type": bool}),
  398. ("hook", {
  399. "value": "",
  400. "help": "command that is run after changes to storage",
  401. "type": str}),
  402. ("_filesystem_fsync", {
  403. "value": "True",
  404. "help": "sync all changes to filesystem during requests",
  405. "type": bool}),
  406. ("predefined_collections", {
  407. "value": "",
  408. "help": "predefined user collections",
  409. "type": json_str})])),
  410. ("hook", OrderedDict([
  411. ("type", {
  412. "value": "none",
  413. "help": "hook backend",
  414. "type": str,
  415. "internal": hook.INTERNAL_TYPES}),
  416. ("dryrun", {
  417. "value": "False",
  418. "help": "dry-run (do not really trigger hook action)",
  419. "type": bool}),
  420. ("rabbitmq_endpoint", {
  421. "value": "",
  422. "help": "endpoint where rabbitmq server is running",
  423. "type": str}),
  424. ("rabbitmq_topic", {
  425. "value": "",
  426. "help": "topic to declare queue",
  427. "type": str}),
  428. ("rabbitmq_queue_type", {
  429. "value": "",
  430. "help": "queue type for topic declaration",
  431. "type": str}),
  432. ("smtp_server", {
  433. "value": "",
  434. "help": "SMTP server to send emails",
  435. "type": str}),
  436. ("smtp_port", {
  437. "value": "",
  438. "help": "SMTP server port",
  439. "type": str}),
  440. ("smtp_security", {
  441. "value": "none",
  442. "help": "SMTP security mode: *none*|tls|starttls",
  443. "type": str,
  444. "internal": email.SMTP_SECURITY_TYPES}),
  445. ("smtp_ssl_verify_mode", {
  446. "value": "REQUIRED",
  447. "help": "The certificate verification mode. Works for tls and starttls: NONE, OPTIONAL, default is REQUIRED",
  448. "type": str,
  449. "internal": email.SMTP_SSL_VERIFY_MODES}),
  450. ("smtp_username", {
  451. "value": "",
  452. "help": "SMTP server username",
  453. "type": str}),
  454. ("smtp_password", {
  455. "value": "",
  456. "help": "SMTP server password",
  457. "type": str}),
  458. ("from_email", {
  459. "value": "",
  460. "help": "SMTP server password",
  461. "type": str}),
  462. ("mass_email", {
  463. "value": "False",
  464. "help": "Send one email to all attendees, versus one email per attendee",
  465. "type": bool}),
  466. ("new_or_added_to_event_template", {
  467. "value": """Hello $attendee_name,
  468. You have been added as an attendee to the following calendar event.
  469. $event_title
  470. $event_start_time - $event_end_time
  471. $event_location
  472. This is an automated message. Please do not reply.""",
  473. "help": "Template for the email sent when an event is created or attendee is added. Select placeholder words prefixed with $ will be replaced",
  474. "type": str}),
  475. ("deleted_or_removed_from_event_template", {
  476. "value": """Hello $attendee_name,
  477. The following event has been deleted.
  478. $event_title
  479. $event_start_time - $event_end_time
  480. $event_location
  481. This is an automated message. Please do not reply.""",
  482. "help": "Template for the email sent when an event is deleted or attendee is removed. Select placeholder words prefixed with $ will be replaced",
  483. "type": str}),
  484. ("updated_event_template", {
  485. "value": """Hello $attendee_name,
  486. The following event has been updated.
  487. $event_title
  488. $event_start_time - $event_end_time
  489. $event_location
  490. This is an automated message. Please do not reply.""",
  491. "help": "Template for the email sent when an event is updated. Select placeholder words prefixed with $ will be replaced",
  492. "type": str
  493. })
  494. ])),
  495. ("web", OrderedDict([
  496. ("type", {
  497. "value": "internal",
  498. "help": "web interface backend",
  499. "type": str_or_callable,
  500. "internal": web.INTERNAL_TYPES})])),
  501. ("logging", OrderedDict([
  502. ("level", {
  503. "value": "info",
  504. "help": "threshold for the logger",
  505. "type": logging_level}),
  506. ("trace_on_debug", {
  507. "value": "False",
  508. "help": "do not filter debug messages starting with 'TRACE'",
  509. "type": bool}),
  510. ("trace_filter", {
  511. "value": "",
  512. "help": "filter debug messages starting with 'TRACE/<TOKEN>'",
  513. "type": str}),
  514. ("bad_put_request_content", {
  515. "value": "False",
  516. "help": "log bad PUT request content",
  517. "type": bool}),
  518. ("backtrace_on_debug", {
  519. "value": "False",
  520. "help": "log backtrace on level=debug",
  521. "type": bool}),
  522. ("request_header_on_debug", {
  523. "value": "False",
  524. "help": "log request header on level=debug",
  525. "type": bool}),
  526. ("request_content_on_debug", {
  527. "value": "False",
  528. "help": "log request content on level=debug",
  529. "type": bool}),
  530. ("response_content_on_debug", {
  531. "value": "False",
  532. "help": "log response content on level=debug",
  533. "type": bool}),
  534. ("rights_rule_doesnt_match_on_debug", {
  535. "value": "False",
  536. "help": "log rights rules which doesn't match on level=debug",
  537. "type": bool}),
  538. ("storage_cache_actions_on_debug", {
  539. "value": "False",
  540. "help": "log storage cache action on level=debug",
  541. "type": bool}),
  542. ("mask_passwords", {
  543. "value": "True",
  544. "help": "mask passwords in logs",
  545. "type": bool})])),
  546. ("headers", OrderedDict([
  547. ("_allow_extra", str)])),
  548. ("reporting", OrderedDict([
  549. ("max_freebusy_occurrence", {
  550. "value": "10000",
  551. "help": "number of occurrences per event when reporting",
  552. "type": positive_int})]))
  553. ])
  554. def parse_compound_paths(*compound_paths: Optional[str]
  555. ) -> List[Tuple[str, bool]]:
  556. """Parse a compound path and return the individual paths.
  557. Paths in a compound path are joined by ``os.pathsep``. If a path starts
  558. with ``?`` the return value ``IGNORE_IF_MISSING`` is set.
  559. When multiple ``compound_paths`` are passed, the last argument that is
  560. not ``None`` is used.
  561. Returns a dict of the format ``[(PATH, IGNORE_IF_MISSING), ...]``
  562. """
  563. compound_path = ""
  564. for p in compound_paths:
  565. if p is not None:
  566. compound_path = p
  567. paths = []
  568. for path in compound_path.split(os.pathsep):
  569. ignore_if_missing = path.startswith("?")
  570. if ignore_if_missing:
  571. path = path[1:]
  572. path = filepath(path)
  573. if path:
  574. paths.append((path, ignore_if_missing))
  575. return paths
  576. def load(paths: Optional[Iterable[Tuple[str, bool]]] = None
  577. ) -> "Configuration":
  578. """
  579. Create instance of ``Configuration`` for use with
  580. ``radicale.app.Application``.
  581. ``paths`` a list of configuration files with the format
  582. ``[(PATH, IGNORE_IF_MISSING), ...]``.
  583. If a configuration file is missing and IGNORE_IF_MISSING is set, the
  584. config is set to ``Configuration.SOURCE_MISSING``.
  585. The configuration can later be changed with ``Configuration.update()``.
  586. """
  587. if paths is None:
  588. paths = []
  589. configuration = Configuration(DEFAULT_CONFIG_SCHEMA)
  590. for path, ignore_if_missing in paths:
  591. parser = RawConfigParser()
  592. config_source = "config file %r" % path
  593. config: types.CONFIG
  594. try:
  595. with open(path) as f:
  596. parser.read_file(f)
  597. config = {s: {o: parser[s][o] for o in parser.options(s)}
  598. for s in parser.sections()}
  599. except Exception as e:
  600. if not (ignore_if_missing and isinstance(e, (
  601. FileNotFoundError, NotADirectoryError, PermissionError))):
  602. raise RuntimeError("Failed to load %s: %s" % (config_source, e)
  603. ) from e
  604. config = Configuration.SOURCE_MISSING
  605. configuration.update(config, config_source)
  606. return configuration
  607. _Self = TypeVar("_Self", bound="Configuration")
  608. class Configuration:
  609. SOURCE_MISSING: ClassVar[types.CONFIG] = {}
  610. _schema: types.CONFIG_SCHEMA
  611. _values: types.MUTABLE_CONFIG
  612. _configs: List[Tuple[types.CONFIG, str, bool]]
  613. def __init__(self, schema: types.CONFIG_SCHEMA) -> None:
  614. """Initialize configuration.
  615. ``schema`` a dict that describes the configuration format.
  616. See ``DEFAULT_CONFIG_SCHEMA``.
  617. The content of ``schema`` must not change afterwards, it is kept
  618. as an internal reference.
  619. Use ``load()`` to create an instance for use with
  620. ``radicale.app.Application``.
  621. """
  622. self._schema = schema
  623. self._values = {}
  624. self._configs = []
  625. default = {section: {option: self._schema[section][option]["value"]
  626. for option in self._schema[section]
  627. if option not in INTERNAL_OPTIONS}
  628. for section in self._schema}
  629. self.update(default, "default config", privileged=True)
  630. def update(self, config: types.CONFIG, source: Optional[str] = None,
  631. privileged: bool = False) -> None:
  632. """Update the configuration.
  633. ``config`` a dict of the format {SECTION: {OPTION: VALUE, ...}, ...}.
  634. The configuration is checked for errors according to the config schema.
  635. The content of ``config`` must not change afterwards, it is kept
  636. as an internal reference.
  637. ``source`` a description of the configuration source (used in error
  638. messages).
  639. ``privileged`` allows updating sections and options starting with "_".
  640. """
  641. if source is None:
  642. source = "unspecified config"
  643. new_values: types.MUTABLE_CONFIG = {}
  644. for section in config:
  645. if (section not in self._schema or
  646. section.startswith("_") and not privileged):
  647. raise ValueError(
  648. "Invalid section %r in %s" % (section, source))
  649. new_values[section] = {}
  650. extra_type = None
  651. extra_type = self._schema[section].get("_allow_extra")
  652. if "type" in self._schema[section]:
  653. if "type" in config[section]:
  654. plugin = config[section]["type"]
  655. else:
  656. plugin = self.get(section, "type")
  657. if plugin not in self._schema[section]["type"]["internal"]:
  658. extra_type = unspecified_type
  659. for option in config[section]:
  660. type_ = extra_type
  661. if option in self._schema[section]:
  662. type_ = self._schema[section][option]["type"]
  663. if (not type_ or option in INTERNAL_OPTIONS or
  664. option.startswith("_") and not privileged):
  665. raise RuntimeError("Invalid option %r in section %r in "
  666. "%s" % (option, section, source))
  667. raw_value = config[section][option]
  668. try:
  669. if type_ == bool and not isinstance(raw_value, bool):
  670. raw_value = _convert_to_bool(raw_value)
  671. new_values[section][option] = type_(raw_value)
  672. except Exception as e:
  673. raise RuntimeError(
  674. "Invalid %s value for option %r in section %r in %s: "
  675. "%r" % (type_.__name__, option, section, source,
  676. raw_value)) from e
  677. self._configs.append((config, source, bool(privileged)))
  678. for section in new_values:
  679. self._values[section] = self._values.get(section, {})
  680. self._values[section].update(new_values[section])
  681. def get(self, section: str, option: str) -> Any:
  682. """Get the value of ``option`` in ``section``."""
  683. with contextlib.suppress(KeyError):
  684. return self._values[section][option]
  685. raise KeyError(section, option)
  686. def get_raw(self, section: str, option: str) -> Any:
  687. """Get the raw value of ``option`` in ``section``."""
  688. for config, _, _ in reversed(self._configs):
  689. if option in config.get(section, {}):
  690. return config[section][option]
  691. raise KeyError(section, option)
  692. def get_source(self, section: str, option: str) -> str:
  693. """Get the source that provides ``option`` in ``section``."""
  694. for config, source, _ in reversed(self._configs):
  695. if option in config.get(section, {}):
  696. return source
  697. raise KeyError(section, option)
  698. def sections(self) -> List[str]:
  699. """List all sections."""
  700. return list(self._values.keys())
  701. def options(self, section: str) -> List[str]:
  702. """List all options in ``section``"""
  703. return list(self._values[section].keys())
  704. def sources(self) -> List[Tuple[str, bool]]:
  705. """List all config sources."""
  706. return [(source, config is self.SOURCE_MISSING) for
  707. config, source, _ in self._configs]
  708. def copy(self: _Self, plugin_schema: Optional[types.CONFIG_SCHEMA] = None
  709. ) -> _Self:
  710. """Create a copy of the configuration
  711. ``plugin_schema`` is a optional dict that contains additional options
  712. for usage with a plugin. See ``DEFAULT_CONFIG_SCHEMA``.
  713. """
  714. if plugin_schema is None:
  715. schema = self._schema
  716. else:
  717. new_schema = dict(self._schema)
  718. for section, options in plugin_schema.items():
  719. if (section not in new_schema or
  720. "type" not in new_schema[section] or
  721. "internal" not in new_schema[section]["type"]):
  722. raise ValueError("not a plugin section: %r" % section)
  723. new_section = dict(new_schema[section])
  724. new_type = dict(new_section["type"])
  725. new_type["internal"] = (self.get(section, "type"),)
  726. new_section["type"] = new_type
  727. for option, value in options.items():
  728. if option in new_section:
  729. raise ValueError("option already exists in %r: %r" %
  730. (section, option))
  731. new_section[option] = value
  732. new_schema[section] = new_section
  733. schema = new_schema
  734. copy = type(self)(schema)
  735. for config, source, privileged in self._configs:
  736. copy.update(config, source, privileged)
  737. return copy