Source code for vinstall.core.core

#-*- coding: utf-8 -*-


"""Utilities for declaring classes as views of certain model."""


from log import get_logger


# global registry
_REGISTRY = {}
_MAIN_WINDOW = None
LOG = get_logger(__name__)

[docs]class RegistryError(Exception): """Exception raised for registry errors. """ pass
[docs]def renders(model): """Returns a class decorator that can be used for registering a view against certain model. """ def register_view(cls): """Add a View class to the Views/Models mapping""" # check if the model is already in the registry. if model in _REGISTRY: LOG.error("View already registered. Raising RegistryError") raise RegistryError("A view is already registered for this "\ "model: %s" % _REGISTRY[model]) # this flag indicates if there are values entered by the user that # needs to be processed by controller classes. Defaults to True. _process = getattr(model, "process", True) cls._process = _process # add the mapping to the registry _REGISTRY[model] = cls LOG.debug("Registering view %s for %s" % (cls, model)) return cls return register_view
[docs]def get_view(model): """Answer with a view registered for the given model""" view = _REGISTRY.get(model) if not view: LOG.error("View not found. Raising RegistryError") raise RegistryError("There is no view registered for this model") LOG.debug("Found view %s for %s" % (view, model)) return view
[docs]def main_window(cls): """Class decorator for registering a main window""" LOG.debug("Registering main window: %s" % cls) global _MAIN_WINDOW _MAIN_WINDOW = cls return cls
[docs]def get_main_window(): """Returns the main window for a given view""" LOG.debug("Using main window: %s" % _MAIN_WINDOW) return _MAIN_WINDOW