/usr/share/pyshared/kivy/factory.py is in python-kivy 1.7.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | '''
Factory object
==============
The factory can be used to automatically import any class from a module,
by specifying the module to import instead of the class instance.
The class list and available modules are automatically generated by setup.py.
Example for registering a class/module::
>>> from kivy.factory import Factory
>>> Factory.register('Widget', module='kivy.uix.widget')
>>> Factory.register('Vector', module='kivy.vector')
Example of using the Factory::
>>> from kivy.factory import Factory
>>> widget = Factory.Widget(pos=(456,456))
>>> vector = Factory.Vector(9, 2)
Example using a class name::
>>> from kivy.factory import Factory
>>> Factory.register('MyWidget', cls=MyWidget)
'''
__all__ = ('Factory', 'FactoryException')
from kivy.logger import Logger
class FactoryException(Exception):
pass
class FactoryBase(object):
def __init__(self):
super(FactoryBase, self).__init__()
self.classes = {}
def is_template(self, classname):
'''Return True is the classname is a template from
:class:`~kivy.lang.Builder`.
.. versionadded:: 1.0.5
'''
if classname in self.classes:
return self.classes[classname]['is_template']
else:
return False
def register(self, classname, cls=None, module=None, is_template=False,
baseclasses=None, filename=None):
'''Register a new classname refering to a real class or
class definition in a module.
.. versionchanged:: 1.7.0
:data:`baseclasses` and :data:`filename` added
.. versionchanged:: 1.0.5
:data:`is_template` have been added in 1.0.5.
'''
if cls is None and module is None and baseclasses is None:
raise ValueError(
'You must specify either cls= or module= or baseclasses =')
if classname in self.classes:
return
self.classes[classname] = {
'module': module,
'cls': cls,
'is_template': is_template,
'baseclasses': baseclasses,
'filename': filename}
def unregister_from_filename(self, filename):
'''Unregister all the factory object related to the filename passed in
the parameter.
.. versionadded:: 1.7.0
'''
to_remove = [x for x in self.classes
if self.classes[x]['filename'] == filename]
for name in to_remove:
del self.classes[name]
def __getattr__(self, name):
classes = self.classes
if name not in classes:
raise FactoryException('Unknown class <%s>' % name)
item = classes[name]
cls = item['cls']
# No class to return, import the module
if cls is None:
if item['module']:
module = __import__(name=item['module'], fromlist='.')
if not hasattr(module, name):
raise FactoryException(
'No class named <%s> in module <%s>' % (
name, item['module']))
cls = item['cls'] = getattr(module, name)
elif item['baseclasses']:
rootwidgets = []
for basecls in item['baseclasses'].split('+'):
rootwidgets.append(Factory.get(basecls))
cls = item['cls'] = type(name, tuple(rootwidgets), {})
else:
raise FactoryException('No information to create the class')
return cls
get = __getattr__
#: Factory instance to use for getting new classes
Factory = FactoryBase()
# Now import the file with all registers
# automatically generated by build_factory
import kivy.factory_registers
Logger.info('Factory: %d symbols loaded' % len(Factory.classes))
if __name__ == '__main__':
Factory.register('Vector', module='kivy.vector')
Factory.register('Widget', module='kivy.uix.widget')
|