Module trappedbot.configuration

Implement utility functions for

  • reading in the YAML config file
  • performing the according initialization and set-up
Expand source code
#!/usr/bin/env python3

"""Implement utility functions for

- reading in the YAML config file
- performing the according initialization and set-up
"""

import json
import typing


class ConfigError(RuntimeError):
    """Error encountered during reading the config file.

    Arguments:
        msg (str): The message displayed to the user on error
    """

    def __init__(self, msg):
        """Set up."""
        super(ConfigError, self).__init__("%s" % (msg,))


class Configuration(typing.NamedTuple):
    """Application configuration"""

    configuration: typing.Dict = {}
    config_filepath: str = ""
    database_filepath: str = ""
    store_filepath: str = ""
    user_id: str = ""
    user_password: str = ""
    user_access_token: str = ""
    device_id: str = ""
    device_name: str = ""
    homeserver_url: str = ""
    trust_own_devices: bool = False
    change_device_name: bool = False
    command_prefix: str = ""
    trusted_users: typing.List[str] = []
    commands: typing.Dict[str, "Command"] = {}
    responses: typing.List["Response"] = []

    def extension(self, section: str, setting: str):
        """Retrieve an extension from the config

        Allows users to store any setting in our app config and reference it in custom tasks
        """
        value = (
            self.configuration.get("extension", {}).get(section, {}).get(setting, None)
        )
        if not value:
            raise ConfigError(
                f"Config file does not contain /extension/{section}/{setting}"
            )
        return value

    def __str__(self):
        return json.dumps(self._asdict())

Classes

class ConfigError (msg)

Error encountered during reading the config file.

Arguments

msg (str): The message displayed to the user on error

Set up.

Expand source code
class ConfigError(RuntimeError):
    """Error encountered during reading the config file.

    Arguments:
        msg (str): The message displayed to the user on error
    """

    def __init__(self, msg):
        """Set up."""
        super(ConfigError, self).__init__("%s" % (msg,))

Ancestors

  • builtins.RuntimeError
  • builtins.Exception
  • builtins.BaseException
class Configuration (configuration: Dict = {}, config_filepath: str = '', database_filepath: str = '', store_filepath: str = '', user_id: str = '', user_password: str = '', user_access_token: str = '', device_id: str = '', device_name: str = '', homeserver_url: str = '', trust_own_devices: bool = False, change_device_name: bool = False, command_prefix: str = '', trusted_users: List[str] = [], commands: Dict[str, ForwardRef('Command')] = {}, responses: List[ForwardRef('Response')] = [])

Application configuration

Expand source code
class Configuration(typing.NamedTuple):
    """Application configuration"""

    configuration: typing.Dict = {}
    config_filepath: str = ""
    database_filepath: str = ""
    store_filepath: str = ""
    user_id: str = ""
    user_password: str = ""
    user_access_token: str = ""
    device_id: str = ""
    device_name: str = ""
    homeserver_url: str = ""
    trust_own_devices: bool = False
    change_device_name: bool = False
    command_prefix: str = ""
    trusted_users: typing.List[str] = []
    commands: typing.Dict[str, "Command"] = {}
    responses: typing.List["Response"] = []

    def extension(self, section: str, setting: str):
        """Retrieve an extension from the config

        Allows users to store any setting in our app config and reference it in custom tasks
        """
        value = (
            self.configuration.get("extension", {}).get(section, {}).get(setting, None)
        )
        if not value:
            raise ConfigError(
                f"Config file does not contain /extension/{section}/{setting}"
            )
        return value

    def __str__(self):
        return json.dumps(self._asdict())

Ancestors

  • builtins.tuple

Instance variables

var change_device_name

Alias for field number 11

var command_prefix

Alias for field number 12

var commands

Alias for field number 14

var config_filepath

Alias for field number 1

var configuration

Alias for field number 0

var database_filepath

Alias for field number 2

var device_id

Alias for field number 7

var device_name

Alias for field number 8

var homeserver_url

Alias for field number 9

var responses

Alias for field number 15

var store_filepath

Alias for field number 3

var trust_own_devices

Alias for field number 10

var trusted_users

Alias for field number 13

var user_access_token

Alias for field number 6

var user_id

Alias for field number 4

var user_password

Alias for field number 5

Methods

def extension(self, section: str, setting: str)

Retrieve an extension from the config

Allows users to store any setting in our app config and reference it in custom tasks

Expand source code
def extension(self, section: str, setting: str):
    """Retrieve an extension from the config

    Allows users to store any setting in our app config and reference it in custom tasks
    """
    value = (
        self.configuration.get("extension", {}).get(section, {}).get(setting, None)
    )
    if not value:
        raise ConfigError(
            f"Config file does not contain /extension/{section}/{setting}"
        )
    return value