configproxy.py 38.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
#!/usr/bin/env python3
import os
from common import auth

import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


def split_line(line, max_length=76):
    words = list(filter(None, map(str.strip, line.split(" "))))
    lines = []
    current_line = []
    current_line_length = 0
    for word in words:
        if (current_line and current_line_length + len(word) + 1 > max_length):
            lines.append(" ".join(current_line))
            current_line = []
            current_line_length = 0
        current_line.append(word)
        current_line_length += len(word) + 1
    if current_line:
        lines.append(" ".join(current_line))
    return lines


def prefix_lines(lines, prefix="#  "):
    return map(lambda line: "".join((prefix, line)), lines)


class ConfigEntry:
    def __init__(self, name, default, required=True, internal=True,
                 immutable=False, no_sane_default=False,
                 description=None, value=None):
        self.name = name
        self.default = default
        self.required = required
        self.internal = internal
        self.immutable = immutable
        self.no_sane_default = no_sane_default
        self.description = description
        self.value = value

    def extract(self, config):
        if self.immutable:
            self.value = self.default
            return
        if not hasattr(config, self.name) and self.required:
            raise ValueError(
                "Missing required config entry {}!".format(self.name))
        self.value = getattr(config, self.name, self.default)
        if self.no_sane_default and self.value == self.default:
            raise ValueError(
                "You have not configured {}, which is required!".format(
                    self.name))

    def get_example(self):
        if self.immutable:
            return []
        lines = []
        if self.description is not None:
            lines.extend(prefix_lines(split_line(self.description)))
        entry_line = "{} = {}".format(self.name, repr(self.default))
        if not self.required:
            entry_line = "# {}".format(entry_line)
        lines.append(entry_line)
        return lines


class ConfigSection:
    def __init__(self, name, entries, check, description=None,
                 deactivatable=False, active=None, imports=None):
        self.name = name
        self.entries = entries
        self.check = check
        self.description = description
        self.deactivatable = deactivatable
        self.active = active
        if isinstance(imports, str):
            imports = [imports]
        self.imports = imports

    def extract(self, config):
        if self.deactivatable:
            self.active = getattr(config, "{}_ACTIVE".format(self.name), False)
            if not self.active:
                return
        for entry in self.entries:
            entry.extract(config)

    def get_example(self):
        lines = []
        if self.imports:
            lines.extend(self.imports)
        header_line = (
            "# [{}]".format(self.name) +
            (" (optional)" if self.deactivatable else ""))
        lines.append(header_line)
        if self.description is not None:
            lines.extend(prefix_lines(split_line(self.description)))
            lines.append("")
        if self.deactivatable:
            lines.extend(prefix_lines(split_line("deactivate with")))
            lines.append("# {}_ACTIVE = False".format(self.name))
            lines.append("")
        for entry in self.entries:
            lines.extend(entry.get_example())
            lines.append("")
        lines.append("")
        return "\n".join(lines)

    def fill_modules(self, config, public_config):
        if self.deactivatable:
            setattr(config, "{}_ACTIVE".format(self.name), self.active)
            setattr(public_config, "{}_ACTIVE".format(self.name), self.active)
            if not self.active:
                return
        for entry in self.entries:
            setattr(config, entry.name, entry.value)
            if not entry.internal:
                setattr(public_config, entry.name, entry.value)


def check_choice(option, value, choices):
    if value not in choices:
        raise ValueError(
            "{} is not allowed choices! Should be one of {}, is {}".format(
                option, choices, value))


def check_database(
        SQLALCHEMY_DATABASE_URI,
        SQLALCHEMY_TRACK_MODIFICATIONS):
    from sqlalchemy_utils import database_exists
    from sqlalchemy import exc
    try:
        if not database_exists(SQLALCHEMY_DATABASE_URI):
            raise ValueError(
                "The database '{}' does not exist! "
                "Please configure it correctly or create it!".format(
                    SQLALCHEMY_DATABASE_URI))
    except (exc.NoSuchModuleError, exc.OperationalError) as error:
        raise ValueError(
            "The database uri '{}' does not state a valid "
            "database: {}".format(SQLALCHEMY_DATABASE_URI, error))


def check_security(SECRET_KEY, SECURITY_KEY, SESSION_PROTECTION):
    MIN_KEY_LENGTH = 20
    if len(SECRET_KEY) < MIN_KEY_LENGTH:
        raise ValueError(
            "Insufficient length of SECRET_KEY, should be at least {}!".format(
                MIN_KEY_LENGTH))
    if len(SECURITY_KEY) < MIN_KEY_LENGTH:
        raise ValueError(
            "Insufficient length of SECURITY_KEY, should be at "
            "least {}!".format(
                MIN_KEY_LENGTH))
    check_choice("SESSION_PROTECTION", SESSION_PROTECTION, ["strong"])


Robin Sonnabend's avatar
Robin Sonnabend committed
162
def check_server_name(SERVER_NAME, PREFERRED_URL_SCHEME, CDN_URL):
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    # todo: check ip address and server name
    check_choice(
        "PREFERRED_URL_SCHEME", PREFERRED_URL_SCHEME,
        ["http", "https"])


def check_debug(DEBUG):
    if DEBUG:
        logger.warning("DEBUG mode is activated!")


def check_celery(
        CELERY_BROKER_URL, CELERY_TASK_SERIALIZER, CELERY_ACCEPT_CONTENT):
    pass
    # todo: check broker url
    check_choice(
        "CELERY_TASK_SERIALIZER", CELERY_TASK_SERIALIZER,
        ["pickle"])
    check_choice(
        "CELERY_ACCEPT_CONTENT", CELERY_ACCEPT_CONTENT,
        [["pickle"]])


def check_sentry(SENTRY_DSN):
    pass


def check_authentication(AUTH_MAX_DURATION, AUTH_BACKENDS):
    if AUTH_MAX_DURATION <= 0:
        raise ValueError(
            "AUTH_MAX_DURATION should be positive, is {}!".format(
                AUTH_MAX_DURATION))
    if not AUTH_BACKENDS:
        raise ValueError("No authentication backends have been configured!")


def check_error_report_context(ERROR_CONTEXT_LINES):
    if ERROR_CONTEXT_LINES < 0:
        raise ValueError(
            "ERROR_CONTEXT_LINES should be positive, is {}!".format(
                ERROR_CONTEXT_LINES))


def check_pagination(PAGE_LENGTH, PAGE_DIFF):
    if PAGE_LENGTH <= 0:
        raise ValueError(
            "PAGE_LENGTH should be positive, is {}!".format(
                PAGE_LENGTH))
    if PAGE_DIFF < 0:
        raise ValueError(
            "PAGE_DIFF should be positive, is {}!".format(
                PAGE_DIFF))


def check_index_page(
        MAX_INDEX_DAYS, MAX_PAST_INDEX_DAYS,
        MAX_PAST_INDEX_DAYS_BEFORE_REMINDER):
    if MAX_INDEX_DAYS < 0:
        raise ValueError(
            "MAX_INDEX_DAYS should be positive, is {}!".format(
                MAX_INDEX_DAYS))
    if MAX_PAST_INDEX_DAYS < 0:
        raise ValueError(
            "MAX_PAST_INDEX_DAYS should be positive, is {}!".format(
                MAX_PAST_INDEX_DAYS))
    if MAX_PAST_INDEX_DAYS_BEFORE_REMINDER < 0:
        raise ValueError(
            "MAX_PAST_INDEX_DAYS_BEFORE_REMINDER should be "
            "positive, is {}!".format(
                MAX_PAST_INDEX_DAYS_BEFORE_REMINDER))


def check_admin_data(ADMIN_MAIL, ADMIN_GROUP):
    if not ADMIN_MAIL:
        raise ValueError("No admin mail address given!")
    if not ADMIN_GROUP:
        raise ValueError("No admin group given!")


def check_parser(PARSER_LAZY, FUZZY_MIN_SCORE, PRIVATE_KEYWORDS):
    if PARSER_LAZY:
        logger.warning(
            "Parser lazy mode is activated, this is not meant or useful "
            "for production operation!")
    if not 0 <= FUZZY_MIN_SCORE <= 100:
        raise ValueError(
            "FUZZY_MIN_SCORE should be a percentage from 0 to 100")
    for keyword in PRIVATE_KEYWORDS:
        if not keyword:
            raise ValueError("Invalid private keyword given: {}".format(
                keyword))


def check_rendering(
        FONTS, DOCUMENTS_PATH, LATEX_BULLETPOINTS, HTML_LEVEL_OFFSET,
        LATEX_LOCAL_TEMPLATES, LATEX_LOGO_TEMPLATE, LATEX_GEOMETRY,
259 260
        LATEX_PAGESTYLE, LATEX_HEADER_FOOTER, LATEX_ADDITIONAL_PACKAGES,
        LATEX_TEMPLATES):
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
    for key in ("main", "roman", "sans", "mono"):
        if key not in FONTS:
            raise ValueError("No font for type {} given!".format(key))
        try:
            path = FONTS[key]["path"]
            extension = FONTS[key]["extension"]
            for face in ("regular", "bold", "italic", "bolditalic"):
                if face not in FONTS[key]:
                    raise ValueError("Missing fontface {} for {} font!".format(
                        face, key))
                filepath = os.path.join(
                    path, "".join((FONTS[key][face], extension)))
                if not os.path.exists(filepath):
                    raise ValueError(
                        "Font {} does not exist in the filesystem! "
                        "Expected it at {}".format(
                            FONTS[key][face], filepath))
        except KeyError:
            raise ValueError(
                "Missing path information in font specification "
                "{key}: ".format(key=key))
    if not os.path.isdir(DOCUMENTS_PATH):
        raise ValueError(
            "Document savedir does not exist! Should be at {}.".format(
                DOCUMENTS_PATH))
    if not LATEX_BULLETPOINTS:
        raise ValueError("LATEX_BULLETPOINTS is empty.")
    if not 1 <= HTML_LEVEL_OFFSET <= 4:
        raise ValueError(
            "HTML_LEVEL_OFFSET should be from 1 to 4, but is {}".format(
                HTML_LEVEL_OFFSET))
    # todo: check templates stuff


def check_mail(MAIL_FROM, MAIL_HOST, MAIL_USER, MAIL_PASSWORD,
               MAIL_USE_TLS, MAIL_USE_STARTTLS):
    if MAIL_USE_TLS and MAIL_USE_STARTTLS:
        raise ValueError(
            "Both TLS and STARTTLS are set for Mail! Please set only one.")
    from utils import MailManager
    import socket
    import smtplib
    import ssl
    mail_config = Config()
    mail_config.MAIL_ACTIVE = True
    mail_config.MAIL_FROM = MAIL_FROM
    mail_config.MAIL_HOST = MAIL_HOST
    mail_config.MAIL_USER = MAIL_USER
    mail_config.MAIL_PASSWORD = MAIL_PASSWORD
    mail_config.MAIL_USE_TLS = MAIL_USE_TLS
    mail_config.MAIL_USE_STARTTLS = MAIL_USE_STARTTLS
    mail_manager = MailManager(mail_config)
    try:
        if not mail_manager.check():
            raise ValueError("The mail connection is improperly configured.")
    except (ConnectionRefusedError, socket.gaierror) as error:
        raise ValueError("Mail Cannot connect to the server: {}".format(error))
    except smtplib.SMTPAuthenticationError as error:
        raise ValueError("Mail Cannot authenticate: {}".format(error))
    except (ssl.SSLError, smtplib.SMTPNotSupportedError) as error:
        raise ValueError("TLS for Mail does not work: {}".format(error))
    except Exception as error:
        raise ValueError(
            "Testing the mail connection failed: {}".format(error))


def check_printing(PRINTING_SERVER, PRINTING_USER, PRINTING_PRINTERS):
    LPR_PATH = "/usr/bin/lpr"
    LPSTAT_PATH = "/usr/bin/lpstat"
    LPOPTIONS_PATH = "/usr/bin/lpoptions"
    for path in [LPR_PATH, LPSTAT_PATH, LPOPTIONS_PATH]:
        if not os.path.exists(path):
            raise ValueError(
                "{} is not installed! It is required for printing".format(
                    path))
    import subprocess as sp
    list_printers_command = [LPSTAT_PATH, "-h", PRINTING_SERVER, "-p"]
    raw_printers = sp.check_output(list_printers_command).decode("utf-8")
    printers = [
        line.split(" ")[1].lower()
        for line in raw_printers.splitlines()
        if not line.startswith(" ")
    ]
    configured_printers = [key.lower() for key in PRINTING_PRINTERS]
    missing_printers = set(configured_printers) - set(printers)
    if missing_printers:
        raise ValueError(
            "Some configured printers are not available: {}, "
            "available are: {}".format(
                missing_printers, printers))

    def _strip_star(value):
        if value.startswith("*"):
            return value[1:]
        return value
    for printer in configured_printers:
        options_command = [
            LPOPTIONS_PATH, "-h", PRINTING_SERVER, "-p", printer, "-l"]
        raw_options = sp.check_output(options_command).decode("utf-8")
        available_options = {}
        for line in raw_options.splitlines():
            name_part, value_part = line.split(":")
            values = list(map(_strip_star, value_part.strip().split(" ")))
            for name in name_part.split("/"):
                available_options[name] = values
        for option in PRINTING_PRINTERS[printer]:
            name, value = option.split("=")
            if name not in available_options:
                logger.warning(
                    "Printer {} has unknown option {} set!".format(
                        printer, name))
            elif value not in available_options[name]:
                logger.warning(
                    "Printer {} has the invalid value {} specified "
                    "for option {}! Valid values are {}.".format(
                        printer, value, name, available_options[name]))


def check_etherpad(ETHERPAD_URL, EMPTY_ETHERPAD):
    import requests
    key = "Server"
    try:
        answer = requests.get(ETHERPAD_URL)
        if answer.status_code != 200:
            raise ValueError(
                "The etherpad does not return 200 OK at {}".format(
                    ETHERPAD_URL))
        headers = answer.headers
        if key not in headers or "Etherpad" not in headers[key]:
            raise ValueError("{} does not look like an etherpad!".format(
                ETHERPAD_URL))
    except requests.exceptions.ConnectionError as error:
        raise ValueError("Cannot connect to the etherpad at {}: {}".format(
            ETHERPAD_URL, error))


def check_wiki(
        WIKI_TYPE, WIKI_API_URL, WIKI_ANONYMOUS,
        WIKI_USER, WIKI_PASSWORD, WIKI_DOMAIN):
    check_choice("WIKI_TYPE", WIKI_TYPE, ["MEDIAWIKI", "DOKUWIKI"])
    # todo: check the connection


def check_calendar(
405
        CALENDAR_URL, CALENDAR_DEFAULT_DURATION, CALENDAR_MAX_REQUESTS):
406 407 408 409 410 411 412 413 414
    from calendarpush import Client, CalendarException
    try:
        client = Client(url=CALENDAR_URL)
        client.get_calendars()
    except (KeyError, CalendarException) as error:
        raise ValueError("Cannot connect to the calendar at {}!".format(
            CALENDAR_URL))


415 416 417 418
def check_timezone(CALENDAR_TIMEZONE_MAP):
    pass


419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
CONFIG_SECTIONS = [
    ConfigSection(
        name="Database",
        entries=[
            ConfigEntry(
                name="SQLALCHEMY_DATABASE_URI",
                default="engine://user:password@host/database",
                required=True, internal=True, no_sane_default=True,
                description=(
                    "Database connection string. See "
                    "http://docs.sqlalchemy.org/en/latest/core/engines.html "
                    "for details. SQLite does not work with Alembic "
                    "migrations.")),
            ConfigEntry(
                name="SQLALCHEMY_TRACK_MODIFICATIONS",
                default=False,
                required=False, internal=True, immutable=True,
                description="Necessary option. Do not change."),
        ],
        check=check_database,
        description="Settings for SQLAlchemy"),
    ConfigSection(
        name="Security",
        entries=[
            ConfigEntry(
                name="SECRET_KEY",
                default=os.urandom(128),
                required=True, internal=True,
                description="Secret random key used for session security."),
            ConfigEntry(
                name="SECURITY_KEY",
                default=os.urandom(128),
                required=True, internal=True,
                description="Secret random key used for user sessions."),
            ConfigEntry(
                name="SESSION_PROTECTION",
                default="strong",
                required=False, internal=True, immutable=True,
                description="Flask setting for sessions. Do not change."),
        ],
        check=check_security,
        description="Secret keys and random strings"),
    ConfigSection(
        name="URL Root",
        entries=[
            ConfigEntry(
                name="SERVER_NAME",
                default="protokolle.example.com",
                required=True, internal=False, no_sane_default=True,
                description="Domain on which this website is hosted"),
            ConfigEntry(
                name="PREFERRED_URL_SCHEME",
                default="https",
                required=False, internal=False,
                description="Protocol used by this website. "
                            "Either 'http' or 'https'."),
475 476 477 478 479
            ConfigEntry(
                name="CDN_URL",
                default=None,
                required=False, internal=False,
                description="URL to get bootstrap and jQuery from.")
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
        ],
        check=check_server_name,
        description="Where is the website hosted"),
    ConfigSection(
        name="Debug",
        entries=[
            ConfigEntry(
                name="DEBUG",
                default=False,
                required=False, internal=True,
                description="Activate debug mode"),
        ],
        check=check_debug,
        description="Debug mode. Do not set in production."),
    ConfigSection(
        name="Celery",
        entries=[
            ConfigEntry(
                name="CELERY_BROKER_URL",
                default="redis://localhost:6379/0",
                required=True, internal=True,
                description=(
                    "Connection to the celery broker. See http://docs.celery"
                    "project.org/en/latest/userguide/configuration.html")),
            ConfigEntry(
                name="CELERY_TASK_SERIALIZER",
                default="pickle",
                required=False, internal=True, immutable=True,
                description="How celery serializes tasks. Do not change."),
            ConfigEntry(
                name="CELERY_ACCEPT_CONTENT",
                default=['pickle'],
                required=False, internal=True, immutable=True,
                description="How celery deserializes tasks. Do not change."),
        ],
        check=check_celery,
        description="Settings for the task scheduler."),
    ConfigSection(
        name="Sentry",
        entries=[
            ConfigEntry(
                name="SENTRY_DSN",
                default=None,
                required=False, internal=True,
                description=(
                    "Connection string for sentry. See "
                    "https://docs.sentry.io/quickstart/#configure-the-dsn")),
        ],
        check=check_sentry,
        description="Connection information for sentry exception reporting."),
    ConfigSection(
        name="Authentication",
        entries=[
            ConfigEntry(
                name="AUTH_MAX_DURATION",
                default=300,
                required=False, internal=False,
                description="Time in seconds for which non-permanent "
                            "sessions remain valid"),
            ConfigEntry(
                name="AUTH_BACKENDS",
                default=[
                    auth.StaticUserManager((("user", "password", ["group"]),)),
                ],
                required=True, internal=True, no_sane_default=True,
                description="Active Authentication backends"),
        ],
        check=check_authentication,
        description="User Authentication backend settings.",
        imports="from common import auth"),
    ConfigSection(
        name="Error Report Context",
        entries=[
            ConfigEntry(
                name="ERROR_CONTEXT_LINES",
                default=3,
                required=False, internal=False,
                description=(
                    "Number of lines before and after an error to include "
                    "in the error description")),
        ],
        check=check_error_report_context,
        description="Compiling error context settings"),
    ConfigSection(
        name="Pagination",
        entries=[
            ConfigEntry(
                name="PAGE_LENGTH",
                default=20,
                required=False, internal=False,
                description="Default number of entries per page"),
            ConfigEntry(
                name="PAGE_DIFF",
                default=3,
                required=False, internal=False,
                description=(
                    "Number of pages before and after the current one "
                    "with a direct link.")),
        ],
        check=check_pagination,
        description="Pagination settings, used for list pages"),
    ConfigSection(
        name="Index Page",
        entries=[
            ConfigEntry(
                name="MAX_INDEX_DAYS",
                default=14,
                required=False, internal=False,
                description="Next days to list upcoming meetings on"),
            ConfigEntry(
                name="MAX_PAST_INDEX_DAYS",
                default=2,
                required=False, internal=False,
                description="Past days to list unfinished meetings on"),
            ConfigEntry(
                name="MAX_PAST_INDEX_DAYS_BEFORE_REMINDER",
                default=14,
                required=False, internal=True,
                description=(
                    "If a meeting is unfinished this many days after its "
                    "date, a reminder is sent by mail")),
        ],
        check=check_index_page,
        description="Settings for what to show on the index page"),
    ConfigSection(
        name="Admin data",
        entries=[
            ConfigEntry(
                name="ADMIN_MAIL",
                default="admin@example.com",
                required=True, internal=False, no_sane_default=True,
                description="Mail address to tell users to "
                            "contact in case of errors"),
            ConfigEntry(
                name="ADMIN_GROUP",
                default="admin",
                required=True, internal=False,
                description="Users with this group are admins and are "
                            "allowed to do and see everything."),
        ],
        check=check_admin_data,
        description="Settings for who is an admin and how to contact them"),
    ConfigSection(
        name="Parser",
        entries=[
            ConfigEntry(
                name="PARSER_LAZY",
                default=False,
                required=False, internal=False,
                description="Do not enforce some parser policies."),
            ConfigEntry(
                name="FUZZY_MIN_SCORE",
                default=90,
                required=False, internal=False,
                description="Todos with at least this equality score are "
                            "considered equal whe importing old protocols."),
            ConfigEntry(
                name="PRIVATE_KEYWORDS",
                default=["private", "internal", "privat", "intern"],
                required=False, internal=False,
                description="Keywords indicating private protocol parts"),
        ],
        check=check_parser,
        description="Settings for the protocol syntax parser"),
    ConfigSection(
        name="Rendering",
        entries=[
            ConfigEntry(
                name="FONTS",
                default={
                    "main": {
                        "extension": ".otf",
                        "path": "/usr/share/fonts/gsfonts/",
                        "regular": "NimbusSans-Regular",
                        "bold": "NimbusSans-Bold",
                        "italic": "NimbusSans-Italic",
                        "bolditalic": "NimbusSans-BoldItalic"
                    },
                    "roman": {
                        "extension": ".otf",
                        "path": "/usr/share/fonts/gsfonts/",
                        "regular": "NimbusRoman-Regular",
                        "bold": "NimbusRoman-Bold",
                        "italic": "NimbusRoman-Italic",
                        "bolditalic": "NimbusRoman-BoldItalic"
                    },
                    "sans": {
                        "extension": ".otf",
                        "path": "/usr/share/fonts/gsfonts/",
                        "regular": "NimbusSans-Regular",
                        "bold": "NimbusSans-Bold",
                        "italic": "NimbusSans-Italic",
                        "bolditalic": "NimbusSans-BoldItalic"
                    },
                    "mono": {
                        "extension": ".otf",
                        "path": "/usr/share/fonts/gsfonts/",
                        "regular": "NimbusMonoPS-Regular",
                        "bold": "NimbusMonoPS-Bold",
                        "italic": "NimbusMonoPS-Italic",
                        "bolditalic": "NimbusMonoPS-BoldItalic"
                    }
                },
                required=True, internal=False,
                description="fonts for xelatex"),
            ConfigEntry(
                name="DOCUMENTS_PATH",
                default="documents",
                required=False, internal=True,
                description="Path to the directory to save protocols in. "
                            "Write access is necessary."),
            ConfigEntry(
                name="LATEX_BULLETPOINTS",
                default=[
                    r"\textbullet", r"\normalfont \bfseries \textendash",
                    r"\textasteriskcentered", r"\textperiodcentered"],
                required=False, internal=False,
                description="list of bulletpoints to use in latex"),
            ConfigEntry(
                name="HTML_LEVEL_OFFSET",
                default=3,
                required=False, internal=False,
                description="Header level at which to start with "
                            "HTML headlines"),
            ConfigEntry(
                name="LATEX_LOCAL_TEMPLATES",
                default=None,
                required=False, internal=False,
                description="path to additional jinja2 templates"),
            ConfigEntry(
                name="LATEX_LOGO_TEMPLATE",
                default=None,
                required=False, internal=False,
                description="template to include at the top of protocols"),
            ConfigEntry(
                name="LATEX_GEOMETRY",
Robin Sonnabend's avatar
Robin Sonnabend committed
716 717
                default="vmargin=1.5cm,hmargin={1.5cm,1.2cm},"
                        "bindingoffset=8mm",
718 719 720 721 722 723 724 725 726 727 728 729
                required=False, internal=False,
                description="custom latex page geometry"),
            ConfigEntry(
                name="LATEX_PAGESTYLE",
                default=None,
                required=False, internal=False,
                description="custom latex pagestyle, e.g. 'fancy'"),
            ConfigEntry(
                name="LATEX_HEADER_FOOTER",
                default=False,
                required=False, internal=False,
                description="Include a header and footer in protocols"),
730 731 732 733 734
            ConfigEntry(
                name="LATEX_ADDITIONAL_PACKAGES",
                default=None,
                required=False, internal=False,
                description="Include additional latex packages in protocols"),
735 736 737 738 739 740
            ConfigEntry(
                name="LATEX_TEMPLATES",
                default=None,
                required=False, internal=False,
                description=(
                    "define multiple LaTeX-templates to use with a each "
741
                    "protocol type individually overriding the general LATEX "
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
                    "options the LATEX_LOCAL_TEMPLATES parameter is need to "
                    "provide the path for the templates each template must "
                    "be placed in an individual folder named by its ID in "
                    "LATEX_TEMPLATES and must contain the provided template "
                    "files: e.g. the files for the template 'yourtemplate' "
                    "need to be in the folder named 'yourtemplate', and the "
                    "templates provides the files: 'protokoll2.cls' (class), "
                    "'protocol.tex' (protocol), 'decision.tex' (decision) and "
                    "'asta-logo.tex'")),
        ],
        check=check_rendering,
        description="Settings for rendering protocols to pdf, html, etc."),
    ConfigSection(
        name="MAIL",
        entries=[
            ConfigEntry(
                name="MAIL_FROM",
                default="protokolle@example.com",
                required=True, internal=False, no_sane_default=True,
                description="Mail sender address"),
            ConfigEntry(
                name="MAIL_HOST",
                default="mail.example.com:465",
                required=True, internal=True, no_sane_default=True,
                description="SMTP Mail server address with port"),
            ConfigEntry(
                name="MAIL_USER",
                default="",
                required=False, internal=True,
                description="Mail server login user. "
                            "Empty for no authentication."),
            ConfigEntry(
                name="MAIL_PASSWORD",
                default="",
                required=False, internal=True,
                description="Mail server login password. "
                            "Empty for no authentication."),
            ConfigEntry(
                name="MAIL_USE_TLS",
                default=True,
                required=False, internal=True,
                description="Use SMPTS (not STARTTLS). Should match port."),
            ConfigEntry(
                name="MAIL_USE_STARTTLS",
                default=False,
                required=False, internal=True,
                description="Use SMTP with STARTTLS. Should match port."),

        ],
        check=check_mail,
        deactivatable=True,
        description="Mail server connection"),
    ConfigSection(
        name="PRINTING",
        entries=[
            ConfigEntry(
                name="PRINTING_SERVER",
                default="printsrv.example.com:631",
                required=True, internal=True, no_sane_default=True,
                description="CUPS printserver connection"),
            ConfigEntry(
                name="PRINTING_USER",
                default="protocols",
                required=False, internal=True,
                description="CUPS user for print jobs"),
            ConfigEntry(
                name="PRINTING_PRINTERS",
                default={
                    "example_printer":
                        ["Duplex=DuplexNoTumble", "option2=value"],
                    "other_printer": ["list", "of", "options"],
                },
                required=True, internal=False, no_sane_default=True,
                description="printers with corresponding options"),
        ],
        check=check_printing,
        deactivatable=True,
        description="CUPS printing settings"),
    ConfigSection(
        name="ETHERPAD",
        entries=[
            ConfigEntry(
                name="ETHERPAD_URL",
                default="https://example.com/etherpad",
                required=True, internal=False, no_sane_default=True,
                description=(
                    "URL of the etherpad installation. "
                    "Do not include the '/p'!")),
            ConfigEntry(
                name="EMPTY_ETHERPAD",
                default="\n".join([
                    "Welcome to Etherpad!",
                    "",
                    "This pad text is synchronized as you type, so that "
                    "everyone viewing this page sees the same text. This "
                    "allows you to collaborate seamlessly on documents!",
                    "",
                    "Get involved with Etherpad at http://etherpad.org",
                    ""
                ]),
                required=False, internal=False,
                description="The content a new etherpad contains."),
        ],
        check=check_etherpad,
        deactivatable=True,
        description="Etherpad settings"),
    ConfigSection(
        name="WIKI",
        entries=[
            ConfigEntry(
                name="WIKI_TYPE",
                default="MEDIAWIKI",
                required=True, internal=False,
                description="'MEDIAWIKI' or 'DOKUWIKI'"),
            ConfigEntry(
                name="WIKI_API_URL",
                default="https://wiki.example.com/wiki/api.php",
                required=True, internal=True, no_sane_default=True,
                description=(
                    "API Endpoint for Mediawiki, "
                    "'https://user:password@wiki.example.com/lib/exe/"
                    "xmlrpc.php' for Dokuwiki")),
            ConfigEntry(
                name="WIKI_ANONYMOUS",
                default=False,
                required=False, internal=True,
                description="Skip login (only for Mediawiki)"),
            ConfigEntry(
                name="WIKI_USER",
                default=None,
                required=False, internal=True,
                description="Login user (only for Mediawiki)"),
            ConfigEntry(
                name="WIKI_PASSWORD",
                default=None,
                required=False, internal=True,
                description="Login password (only for Mediawiki)"),
            ConfigEntry(
                name="WIKI_DOMAIN",
                default=None,
                required=False, internal=True,
                description="Login domain (only for Mediawiki)"),
        ],
        check=check_wiki,
        deactivatable=True,
        description="Mediawiki or Dokuwiki settings"),
    ConfigSection(
        name="CALENDAR",
        entries=[
            ConfigEntry(
                name="CALENDAR_URL",
                default="https://user:password@calendar.example.com/dav/",
                required=True, internal=True, no_sane_default=True,
                description="CalDAV server URL"),
            ConfigEntry(
                name="CALENDAR_DEFAULT_DURATION",
                default=3,
                required=False, internal=False,
                description="Default meeting length in hours"),
            ConfigEntry(
                name="CALENDAR_MAX_REQUESTS",
                default=10,
                required=False, internal=False,
                description=(
                    "Number of retries before giving a connection attempt up. "
                    "Some CalDAV servers reply randomly with errors.")),
908 909 910 911 912 913 914
        ],
        check=check_calendar,
        deactivatable=True,
        description="CalDAV settings"),
    ConfigSection(
        name="TIMEZONE",
        entries=[
915 916 917 918 919 920 921 922 923
            ConfigEntry(
                name="CALENDAR_TIMEZONE_MAP",
                default={
                    "CET": "Europe/Berlin",
                    "CEST": "Europe/Berline",
                },
                required=False, internal=False,
                description="Timezone abbreviation map. Add as needed."),
        ],
924 925
        check=check_timezone,
        description="Settings for translating timezone information."),
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
]


class Config:
    def __init__(self, data=None):
        if data is None:
            data = {}
        object.__setattr__(self, "data", data)

    def __setattr__(self, key, value):
        self.data[key] = value

    def __getattr__(self, key):
        try:
            return self.data[key]
        except KeyError:
            raise AttributeError

    def __dir__(self):
        return self.data.keys()


def import_config(sections=CONFIG_SECTIONS):
    import config
    for section in sections:
        section.extract(config)
    internal_config, public_config = Config(), Config()
    for section in sections:
        section.fill_modules(internal_config, public_config)
    return internal_config, public_config


def check_config(sections=CONFIG_SECTIONS):
    import config
    successful = True
    for section in sections:
        section.extract(config)
        if not section.deactivatable or section.active:
            logger.info("Checking {}".format(section.name))
            try:
                section.check(
                    **{entry.name: entry.value for entry in section.entries})
            except ValueError as error:
                logger.error(error.args[0])
                successful = False
    return successful


def write_example_config(
        sections=CONFIG_SECTIONS, filename="config.py.example"):
    example_config = "\n".join([
        section.get_example()
        for section in sections
    ])
    with open(filename, "w") as example_config_file:
        example_config_file.write(example_config)


if __name__ == "__main__":
    import argparse
    import sys
    parser = argparse.ArgumentParser(
        description="Create and check the config")
    subparsers = parser.add_subparsers(dest="command")
    subparsers.required = True

    check_parser = subparsers.add_parser("check")
    check_parser.add_argument(
        "--log-level", choices=["error", "warning", "info", "debug"])

    create_parser = subparsers.add_parser("create")
    create_parser.add_argument("--filename", default="config.py.example")

    show_parser = subparsers.add_parser("show")

    arguments = parser.parse_args()

    if arguments.command == "check":
        if arguments.log_level:
            logger.setLevel(arguments.log_level.upper())
        sys.exit(not check_config())
    elif arguments.command == "create":
        filename = arguments.filename
        write_example_config(filename=filename)
        print("An example config has been generated at {}. "
              "Please change the requried entries.".format(filename))
    elif arguments.command == "show":
        import pprint
        internal_config, public_config = import_config()
        print("The complete config is:")
        pprint.pprint(internal_config.data)
        print("The public part is:")
        pprint.pprint(public_config.data)