示例#1
0
class DeviceInterface(object):
    """
    Base class for tv cards.
    """
    suffix = 'unknown'

    def __init__(self, type, number):
        self.device = '%s%s' % (type, number)
        self.configured = True
        # create config group
        self.config = Group(desc='%s card %s' % (type.upper(), number), schema =[
            Var(name='priority', default=5,
                desc='priority of the card'),
            Var(name='activate', default=True,
                desc='Set activate to False if the card should not be used') ])

    def _cfg_set_default(self, key, value):
        """
        Set new default value.
        """
        return self.config._cfg_get(key)._cfg_set(value, default=True)

    def _cfg_add(self, var):
        """
        Add a new variable or group to the config. The parameter 'var' can also be
        a list of variables to add.
        """
        if isinstance(var, (list, tuple)):
            for v in var:
                self.config.add_variable(v._name, v)
            return
        return self.config.add_variable(var._name, var)

    def __getattr__(self, attr):
        """
        Get an attribute. If the attribute is in the config variable return
        this, it not use instance object.
        """
        return getattr(self.config, attr)

    def __setattr__(self, attr, value):
        """
        Set an attribute. If the attribute is in the config variable set
        this, it not use instance object.
        """
        if not attr.startswith('_') and hasattr(self, 'config') and \
               hasattr(self.config, attr):
            return setattr(self.config, attr, value)
        super(DeviceInterface, self).__setattr__(attr, value)

    def __str__(self):
        """
        Return a string with some basic information about the card.
        """
        s = 'Priority: %s\n' % self.priority
        if self.activate:
            return s + 'Status: ready to use\n'
        elif not self.configured:
            return s + 'Status: needs configuration\n'
        else:
            return s + 'Status: deactivated\n'
示例#2
0
    Var(name='x', desc='desc_x', default=7 ),
    Var(name='y', type=range(0,5), desc='desc_y', default=3 ) ])
    ])


# create extra group and add it to the schema
subgroup = Group(desc='this is a subgroup', schema=[
    Var(name='x', desc=u'desc_x with non ascii ü', default=7 ),
    # the next variable allows numbers from 0-4
    Var(name='y', type=range(0,5), desc='desc_y', default=3 ) ])
config.add_variable('subgroup', subgroup)

# create a group again deeper in the tree
subsubgroup = Group(desc='desrc of subsubgroup', schema=[
    Var(name='a', desc='desc a', default=3 ) ])
subgroup.add_variable('z', subsubgroup)

# create a list of a group
l = List(desc='desrc of list subsubgroup', schema=Group([
    Var(name='a', type=int, desc='desc a', default=3 ),
    # z is again a group
    Group(name='z', desc='this is a subgroup', schema=[
    Var(name='x', desc='desc_x', default=7 ),
    Var(name='y', type=range(0,5), desc='desc_y', default=3 ) ]) ]))
subgroup.add_variable('list', l)

# create a dict of strings
epg = Dict(desc='desrc of dict epg', schema=Var(default=''))
subgroup.add_variable('epg', epg)

# store the schema up to this point, we will need it later