Newer
Older
"""Command line configuration module."""
import typing
import click
from librehosters_cli.network import _get_json
from librehosters_cli.settings import DIRECTORY_URL, SCHEMA_URL, TIMEOUT
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(' ', '-')
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'.
:return A list of normalised librehoster names
"""
normalised = []
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.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
def _get_schema_url(self, librehoster: typing.Text) -> typing.Text:
"""Retrieve the librehosters's hosted schema URL from the directory.
:return The hosted schema of a librehoster
"""
try:
return self.directory[librehoster]
except KeyError:
message = 'Could not lookup {}'.format(librehoster)
def _get_standard_schema_json(self) -> typing.Dict:
"""Get the current standardised schema.
"""Get the current directory listing.
normalised = {}
for librehoster, hosted_schema in directory.items():
normalised_name = normalise_librehoster_name(librehoster)
normalised[normalised_name] = hosted_schema
return normalised
pass_config = click.make_pass_decorator(Config, ensure=True)
librehosters_cli_choices = normalise_librehosters_names()