Module plex_posters.config

Expand source code
from plex_posters.lib import export
import toml
import os
from typing import Union

__all__ = []


@export
class plexPosterConfig:

    """Handles the config options for the module

    Attributes
    ----------
    config_file : dict
        Contains the config options. See `plexPosterConfig.read_config()`
        for the data structure.
    """

    def __init__(self, path: str) -> None:
        self.config_file = self.read_config(path)

    @staticmethod
    def read_config(path: str) -> Union[dict, None]:

        """Reads the toml config file from `path` if it exists.

        Parameters
        ----------
        path : str
            Path to config file.

        Returns
        -------
        Union[dict, None]
            Returns a dict if the file is found else returns nothing.

            The dict contains a key for each header. Each key contains a
            dictionary containing a key, value pair for each config under
            that header.

            Example:

            [plex_posters]
            foo = bar

            Returns a dict: {'plex_posters' : {'foo': 'bar'}}
        """

        path += 'config.toml' if path[-1] == '/' else '/config.toml'

        try:
            with open(path, 'r+') as config_file:
                config_file = toml.load(config_file)
            return config_file
        except FileNotFoundError:
            pass

    def get(self, key: str, default: str = None, cast: callable = None):
        pass


inst = plexPosterConfig(
    os.path.expanduser('~/.config/plex-posters/')
)

print(inst.config_file)

Classes

class plexPosterConfig (path)

Handles the config options for the module

Attributes

config_file : dict
Contains the config options. See plexPosterConfig.read_config() for the data structure.
Expand source code
class plexPosterConfig:

    """Handles the config options for the module

    Attributes
    ----------
    config_file : dict
        Contains the config options. See `plexPosterConfig.read_config()`
        for the data structure.
    """

    def __init__(self, path: str) -> None:
        self.config_file = self.read_config(path)

    @staticmethod
    def read_config(path: str) -> Union[dict, None]:

        """Reads the toml config file from `path` if it exists.

        Parameters
        ----------
        path : str
            Path to config file.

        Returns
        -------
        Union[dict, None]
            Returns a dict if the file is found else returns nothing.

            The dict contains a key for each header. Each key contains a
            dictionary containing a key, value pair for each config under
            that header.

            Example:

            [plex_posters]
            foo = bar

            Returns a dict: {'plex_posters' : {'foo': 'bar'}}
        """

        path += 'config.toml' if path[-1] == '/' else '/config.toml'

        try:
            with open(path, 'r+') as config_file:
                config_file = toml.load(config_file)
            return config_file
        except FileNotFoundError:
            pass

    def get(self, key: str, default: str = None, cast: callable = None):
        pass

Static methods

def read_config(path)

Reads the toml config file from path if it exists.

Parameters

path : str
Path to config file.

Returns

Union[dict, None]

Returns a dict if the file is found else returns nothing.

The dict contains a key for each header. Each key contains a dictionary containing a key, value pair for each config under that header.

Example:

[plex_posters] foo = bar

Returns a dict: {'plex_posters' : {'foo': 'bar'}}

Expand source code
@staticmethod
def read_config(path: str) -> Union[dict, None]:

    """Reads the toml config file from `path` if it exists.

    Parameters
    ----------
    path : str
        Path to config file.

    Returns
    -------
    Union[dict, None]
        Returns a dict if the file is found else returns nothing.

        The dict contains a key for each header. Each key contains a
        dictionary containing a key, value pair for each config under
        that header.

        Example:

        [plex_posters]
        foo = bar

        Returns a dict: {'plex_posters' : {'foo': 'bar'}}
    """

    path += 'config.toml' if path[-1] == '/' else '/config.toml'

    try:
        with open(path, 'r+') as config_file:
            config_file = toml.load(config_file)
        return config_file
    except FileNotFoundError:
        pass

Methods

def get(self, key, default=None, cast=None)
Expand source code
def get(self, key: str, default: str = None, cast: callable = None):
    pass