config.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # This file is part of Radicale Server - Calendar Server
  2. # Copyright © 2008-2016 Guillaume Ayoub
  3. # Copyright © 2008 Nicolas Kandel
  4. # Copyright © 2008 Pascal Halter
  5. #
  6. # This library is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This library is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Radicale. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. Radicale configuration module.
  20. Give a configparser-like interface to read and write configuration.
  21. """
  22. import os
  23. from collections import OrderedDict
  24. from configparser import RawConfigParser as ConfigParser
  25. # Default configuration
  26. INITIAL_CONFIG = OrderedDict([
  27. ("server", OrderedDict([
  28. ("hosts", {
  29. "value": "0.0.0.0:5232",
  30. "help": "set server hostnames including ports",
  31. "aliases": ["-H", "--hosts"]}),
  32. ("daemon", {
  33. "value": "False",
  34. "help": "launch as daemon",
  35. "aliases": ["-d", "--daemon"],
  36. "opposite": ["-f", "--foreground"]}),
  37. ("pid", {
  38. "value": "",
  39. "help": "set PID filename for daemon mode",
  40. "aliases": ["-p", "--pid"]}),
  41. ("max_connections", {
  42. "value": "20",
  43. "help": "maximum number of parallel connections"}),
  44. ("max_content_length", {
  45. "value": "10000000",
  46. "help": "maximum size of request body in bytes"}),
  47. ("timeout", {
  48. "value": "10",
  49. "help": "socket timeout"}),
  50. ("ssl", {
  51. "value": "False",
  52. "help": "use SSL connection",
  53. "aliases": ["-s", "--ssl"],
  54. "opposite": ["-S", "--no-ssl"]}),
  55. ("certificate", {
  56. "value": "/etc/apache2/ssl/server.crt",
  57. "help": "set certificate file",
  58. "aliases": ["-c", "--certificate"]}),
  59. ("key", {
  60. "value": "/etc/apache2/ssl/server.key",
  61. "help": "set private key file",
  62. "aliases": ["-k", "--key"]}),
  63. ("protocol", {
  64. "value": "PROTOCOL_SSLv23",
  65. "help": "SSL protocol used"}),
  66. ("ciphers", {
  67. "value": "",
  68. "help": "available ciphers"}),
  69. ("dns_lookup", {
  70. "value": "True",
  71. "help": "use reverse DNS to resolve client address in logs"}),
  72. ("base_prefix", {
  73. "value": "/",
  74. "help": "root URL of Radicale, starting and ending with a slash"}),
  75. ("can_skip_base_prefix", {
  76. "value": "False",
  77. "help": "allow URLs cleaned by a HTTP server"}),
  78. ("realm", {
  79. "value": "Radicale - Password Required",
  80. "help": "message displayed when a password is needed"})])),
  81. ("encoding", OrderedDict([
  82. ("request", {
  83. "value": "utf-8",
  84. "help": "encoding for responding requests"}),
  85. ("stock", {
  86. "value": "utf-8",
  87. "help": "encoding for storing local collections"})])),
  88. ("auth", OrderedDict([
  89. ("type", {
  90. "value": "None",
  91. "help": "authentication method"}),
  92. ("htpasswd_filename", {
  93. "value": "/etc/radicale/users",
  94. "help": "htpasswd filename"}),
  95. ("htpasswd_encryption", {
  96. "value": "crypt",
  97. "help": "htpasswd encryption method"})])),
  98. ("rights", OrderedDict([
  99. ("type", {
  100. "value": "None",
  101. "help": "rights backend"}),
  102. ("file", {
  103. "value": "~/.config/radicale/rights",
  104. "help": "file for rights management from_file"})])),
  105. ("storage", OrderedDict([
  106. ("type", {
  107. "value": "multifilesystem",
  108. "help": "storage backend"}),
  109. ("filesystem_folder", {
  110. "value": os.path.expanduser(
  111. "~/.config/radicale/collections"),
  112. "help": "file for rights management from_file"}),
  113. ("filesystem_fsync", {
  114. "value": "True",
  115. "help": "sync all changes to filesystem during requests"}),
  116. ("filesystem_close_lock_file", {
  117. "value": "False",
  118. "help": "close the lock file when no more clients are waiting"}),
  119. ("hook", {
  120. "value": "",
  121. "help": "command that is run after changes to storage"})])),
  122. ("logging", OrderedDict([
  123. ("config", {
  124. "value": "/etc/radicale/logging",
  125. "help": "logging configuration file"}),
  126. ("debug", {
  127. "value": "False",
  128. "help": "print debug information",
  129. "aliases": ["-D", "--debug"]}),
  130. ("full_environment", {
  131. "value": "False",
  132. "help": "store all environment variables"}),
  133. ("mask_passwords", {
  134. "value": "True",
  135. "help": "mask passwords in logs"})]))])
  136. def load(paths=(), extra_config=None):
  137. config = ConfigParser()
  138. for section, values in INITIAL_CONFIG.items():
  139. config.add_section(section)
  140. for key, data in values.items():
  141. config.set(section, key, data["value"])
  142. if extra_config:
  143. for section, values in extra_config.items():
  144. for key, value in values.items():
  145. config.set(section, key, value)
  146. for path in paths:
  147. if path:
  148. config.read(path)
  149. return config