Source code for vinstall.core.view
#-*- coding: utf-8 -*-
"""User interface abstraction layer.
Deprecated in favor of the core module.
"""
__all__ = [ "View" ]
import model
import abc
from inspect import getmro
class MetaView(abc.ABCMeta):
"""Metaclass implementing a mapping for views and models.
We create a registry of views, by looking up View subclasses. A new view
could be, for example, GtkView. This view would implement one renderer for
each model class. We would add GtkView to the views registry and then every
renderer into the renderers registry.
We have to handle three cases:
a) The class is a View subclass: in this case we are in front of a new
view. This class will be a base class for all the renderer classes.
We add this class to the list implementing the views registry
(View.views). Also, we add a get_renderer classmethod for getting
the renderer for certain model.
b) The class is a subclass of a View subclass. This class is a renderer
class implementing the view for a model class. We add this class to the
renderers registry implemented in its parent class by looking up the
renders class attribute.
c) The class is View, our base class for views. We just ignore this
one.
>>> class GtkView(View):
... mapping = {}
>>> View.views
[<class GtkView>]
>>> class GtkTextField(GtkView):
... renders = model.TextField
>>> GtkView.get_renderer(model.TextField)
<class GtkTextField>
"""
def __init__(cls, name, bases, namespace):
# Ignore abc
if name != "View":
# Find our base class, any subclass of View is good
for base in bases:
if issubclass(base, View):
base_class = base
# Handle new view
if base_class.__name__ == "View":
View.views.add(cls)
@classmethod
def get_renderer(cls, model):
"""Return the renderer for model."""
return cls.mapping.get(model)
setattr(cls, "get_renderer", get_renderer)
# A renderer finally
else:
if "renders" not in namespace:
raise TypeError("Missing 'renders' class attribute")
model = namespace["renders"]
base_class.mapping[model] = cls
super(MetaView, cls).__init__(name, bases, namespace)
[docs]class View(object):
"""Base class for views"""
__metaclass__ = MetaView
views = set()
main_window = None
@abc.abstractproperty
[docs] def renders(cls):
"""The renders class attribute must reference the model class that this
class will render.
"""
@abc.abstractmethod
[docs] def show(self):
"""the show method makes all the widgets visible. It's not needed in
the Urwid implementation.
"""
@abc.abstractmethod
@abc.abstractmethod
[docs] def connect(self):
"""The connect method must provide a way for adding observers.
Sometimes this method can delegate its responsibility to another one,
for example the connect method in a gtk widget.
This methodisnot needed in the Urwid implementation.
"""
@abc.abstractmethod
[docs] def get_value(self):
"""Returns the value entered by the user."""
@abc.abstractmethod
[docs] def accept(self):
"""This method should return the main widget, usually a container
widget. The widget returned by this method will be added directly to
the main window.
"""