magpie.cli.register_defaults ============================ .. py:module:: magpie.cli.register_defaults .. autoapi-nested-parse:: Magpie helpers for user and group registration. Attributes ---------- .. autoapisummary:: magpie.cli.register_defaults.LOGGER Functions --------- .. autoapisummary:: magpie.cli.register_defaults.make_logging_options magpie.cli.register_defaults.setup_logger_from_options magpie.cli.register_defaults.get_constant magpie.cli.register_defaults.pseudo_random_string magpie.cli.register_defaults.get_json magpie.cli.register_defaults.get_logger magpie.cli.register_defaults.print_log magpie.cli.register_defaults.raise_log magpie.cli.register_defaults.register_user_with_group magpie.cli.register_defaults.init_anonymous magpie.cli.register_defaults.init_admin magpie.cli.register_defaults.init_users_group magpie.cli.register_defaults.register_defaults magpie.cli.register_defaults.make_parser magpie.cli.register_defaults.main Module Contents --------------- .. py:function:: make_logging_options(parser: argparse.ArgumentParser) -> None Defines argument parser options for logging operations. .. py:function:: setup_logger_from_options(logger: logging.Logger, args: argparse.Namespace) -> None Uses argument parser options to setup logging level from specified flags. Setup both the specific CLI logger that is provided and the generic `magpie` logger. .. py:function:: get_constant(constant_name: magpie.typedefs.Str, settings_container: Optional[magpie.typedefs.AnySettingsContainer] = None, settings_name: Optional[magpie.typedefs.Str] = None, default_value: Optional[magpie.typedefs.SettingValue] = None, raise_not_set: bool = True, raise_missing: bool = True, print_missing: bool = False, empty_missing: bool = False) -> magpie.typedefs.SettingValue Search in order for matched value of :paramref:`constant_name`: 1. search in :py:data:`MAGPIE_CONSTANTS` 2. search in settings if specified 3. search alternative setting names (see below) 4. search in :mod:`magpie.constants` definitions 5. search in environment variables Parameter :paramref:`constant_name` is expected to have the format ``MAGPIE_[VARIABLE_NAME]`` although any value can be passed to retrieve generic settings from all above-mentioned search locations. If :paramref:`settings_name` is provided as alternative name, it is used as is to search for results if :paramref:`constant_name` was not found. Otherwise, ``magpie.[variable_name]`` is used for additional search when the format ``MAGPIE_[VARIABLE_NAME]`` was used for :paramref:`constant_name` (i.e.: ``MAGPIE_ADMIN_USER`` will also search for ``magpie.admin_user`` and so on for corresponding constants). :param constant_name: key to search for a value :param settings_container: WSGI application settings container (if not provided, uses found one in current thread) :param settings_name: alternative name for `settings` if specified :param default_value: default value to be returned if not found anywhere, and exception raises are disabled. :param raise_not_set: raise an exception if the found key is ``None``, search until last case if others are ``None`` :param raise_missing: raise exception if key is not found anywhere :param print_missing: print message if key is not found anywhere, return ``None`` :param empty_missing: consider an empty value for an existing key as if it was missing (i.e.: as if not set). :returns: found value or `default_value` :raises ValueError: if resulting value is invalid based on options (by default raise missing/empty/``None`` value) :raises LookupError: if no appropriate value could be found from all search locations (according to options) .. py:function:: pseudo_random_string(length: int = 8, allow_chars: magpie.typedefs.Str = string.ascii_letters + string.digits) -> magpie.typedefs.Str Generate a string made of random characters. .. py:function:: get_json(request_or_response: Union[magpie.typedefs.AnyRequestType, magpie.typedefs.AnyResponseType]) -> magpie.typedefs.JSON Retrieves the 'JSON' body of a response using the property/callable according to the response's implementation. .. py:function:: get_logger(name: magpie.typedefs.Str, level: Optional[int] = None, force_stdout: bool = None, message_format: Optional[magpie.typedefs.Str] = None, datetime_format: Optional[magpie.typedefs.Str] = None) -> logging.Logger Immediately sets the logger level to avoid duplicate log outputs from the `root logger` and `this logger` when `level` is ``logging.NOTSET``. .. py:function:: print_log(msg: magpie.typedefs.Str, logger: Optional[logging.Logger] = None, level: int = logging.INFO, **kwargs: Any) -> None Logs the requested message to the logger and optionally enforce printing to the console according to configuration value defined by ``MAGPIE_LOG_PRINT``. .. py:function:: raise_log(msg: magpie.typedefs.Str, exception: Type[Exception] = Exception, logger: Optional[logging.Logger] = None, level: int = logging.ERROR) -> NoReturn Logs the provided message to the logger and raises the corresponding exception afterwards. :raises exception: whichever exception provided is raised systematically after logging. .. py:data:: LOGGER .. py:function:: register_user_with_group(user_name: magpie.typedefs.Str, group_name: magpie.typedefs.Str, email: magpie.typedefs.Str, password: Optional[magpie.typedefs.Str], db_session: sqlalchemy.orm.session.Session) -> None Registers the user if missing and associate him to a group specified by name, also created if missing. :param user_name: name of the user to create (if missing) and to make part of the group (if specified) :param group_name: name of the group to create (if missing and specified) and to make the user join (if not already) :param email: email of the user to be created (if missing) :param password: password of the user to be created (if missing), auto-generate if not provided (None). :param db_session: database connexion to apply changes .. warning:: Should be employed only for **special** users/groups in this module as other expected API behaviour and operations will not be applied (ex: create additional permissions or user-group references). .. py:function:: init_anonymous(db_session: sqlalchemy.orm.session.Session, settings: Optional[magpie.typedefs.AnySettingsContainer] = None) -> None Registers into the database the user and group matching configuration values of. :py:data:`magpie.constants.MAGPIE_ANONYMOUS_USER` and :py:data:`magpie.constants.MAGPIE_ANONYMOUS_GROUP` respectively if not defined. Afterwards, updates the group's parameters to ensure integrity with `Magpie` settings. .. py:function:: init_admin(db_session: sqlalchemy.orm.session.Session, settings: Optional[magpie.typedefs.AnySettingsContainer] = None) -> None Registers into the database the user and group matching configuration values of. :py:data:`magpie.constants.MAGPIE_ADMIN_USER` and :py:data:`magpie.constants.MAGPIE_ADMIN_GROUP` respectively if not defined. Also associates the created admin user with the admin group and give it admin permissions. Finally, updates the group's parameters to ensure integrity with `Magpie` settings. .. py:function:: init_users_group(db_session: sqlalchemy.orm.session.Session, settings: Optional[magpie.typedefs.AnySettingsContainer] = None) -> None Registers into database the group matching :py:data:`magpie.constants.MAGPIE_USERS_GROUP` if not defined. .. py:function:: register_defaults(db_session: Optional[sqlalchemy.orm.session.Session] = None, settings: Optional[magpie.typedefs.AnySettingsContainer] = None, ini_file_path: Optional[magpie.typedefs.Str] = None) -> None Registers into database every undefined default users and groups matching following variables: - :py:data:`magpie.constants.MAGPIE_ANONYMOUS_USER` - :py:data:`magpie.constants.MAGPIE_USERS_GROUP` - :py:data:`magpie.constants.MAGPIE_ADMIN_GROUP` - :py:data:`magpie.constants.MAGPIE_ADMIN_USER` .. py:function:: make_parser() -> argparse.ArgumentParser .. py:function:: main(args: Optional[Sequence[magpie.typedefs.Str]] = None, parser: Optional[argparse.ArgumentParser] = None, namespace: Optional[argparse.Namespace] = None) -> Any