Skip to content
config.py 3.14 KiB
Newer Older
"""Command line configuration module."""

import typing

import click

from librehosters_cli.network import _get_json
decentral1se's avatar
decentral1se committed
from librehosters_cli.settings import DIRECTORY_URL, SCHEMA_URL, TIMEOUT
decentral1se's avatar
decentral1se committed
def normalise_librehoster_name(librehoster: typing.Text) -> typing.Text:
    """Normalise a single librehosters name.

    See `normalise_librehosters` docstring for further documentation.
    """
    return librehoster.lower().replace(' ', '-')


decentral1se's avatar
decentral1se committed
def normalise_librehosters_names() -> typing.List[typing.Text]:
    """Provide user friendly list of librehosters.

    This is necessary for the `--librehoster` command line option where the
    user is expected to supply the name of the librehoster. Since there are
    many variations within the naming, we aim here to normalize them.

    The convention shall be:

    * All lowercase naming
    * White space is replaced with hyphens.

    So, for example, 'LinuxPizza' becomes 'linuxpizza' and 'lain haus' becomes
    'lain-haus'.

decentral1se's avatar
decentral1se committed
    :raises click.UsageError
    :return A list of normalised librehoster names
    """
    normalised = []
decentral1se's avatar
decentral1se committed
    directory = _get_json(DIRECTORY_URL)
    for librehoster in directory.keys():
decentral1se's avatar
decentral1se committed
        normalised.append(normalise_librehoster_name(librehoster))
    return normalised


class Config:
    """The global configuration object."""

    def __init__(self):
        """The initial configuration.

        These settings are configured via the entrypoing context handling in
        librehosters_cli.__main__ as per the Click best practices
        documentation.
        """
        self.bare = False
        self.colour = True
        self.debug = False
        self.timeout = TIMEOUT

decentral1se's avatar
decentral1se committed
        self.directory_url = DIRECTORY_URL
        self.directory = self._get_directory_json()

        self.standard_schema_url = SCHEMA_URL
        self.standard_schema = self._get_standard_schema_json()

        self.target_schema = None
        self.target_schema_url = None
decentral1se's avatar
decentral1se committed
    def _get_schema_url(self, librehoster: typing.Text) -> typing.Text:
        """Retrieve the librehosters's hosted schema URL from the directory.
decentral1se's avatar
decentral1se committed
        :raises click.UsageError
        :return The hosted schema of a librehoster
        """
        try:
            return self.directory[librehoster]
        except KeyError:
            message = 'Could not lookup {}'.format(librehoster)
decentral1se's avatar
decentral1se committed
            raise click.UsageError(message)
decentral1se's avatar
decentral1se committed
    def _get_standard_schema_json(self) -> typing.Dict:
        """Get the current standardised schema.

decentral1se's avatar
decentral1se committed
        :raises click.UsageError
        :return A response object
        """
decentral1se's avatar
decentral1se committed
        return _get_json(self.standard_schema_url)
decentral1se's avatar
decentral1se committed
    def _get_directory_json(self) -> typing.Dict:
        """Get the current directory listing.

decentral1se's avatar
decentral1se committed
        :raises click.UsageError
        :return A response object
        """
decentral1se's avatar
decentral1se committed
        directory = _get_json(self.directory_url)
        normalised = {}
        for librehoster, hosted_schema in directory.items():
decentral1se's avatar
decentral1se committed
            normalised_name = normalise_librehoster_name(librehoster)
            normalised[normalised_name] = hosted_schema
        return normalised


pass_config = click.make_pass_decorator(Config, ensure=True)
decentral1se's avatar
decentral1se committed
librehosters_cli_choices = normalise_librehosters_names()