~ubuntu-branches/debian/stretch/electrum/stretch

« back to all changes in this revision

Viewing changes to gui/kivy/tools/.buildozer/android/platform/python-for-android/dist/kivy/python-install/lib/python2.7/site-packages/kivy/adapters/adapter.py

  • Committer: Package Import Robot
  • Author(s): Tristan Seligmann
  • Date: 2016-04-04 03:02:39 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20160404030239-0szgkio8yryjv7c9
Tags: 2.6.3-1
* New upstream release.
  - Drop backported install-wizard-connect.patch.
* Add Suggests: python-zbar and update the installation hint to suggest
  apt-get instead of pip (closes: #819517).
* Bump Standards-Version to 3.9.7 (no changes).
* Update Vcs-* links.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
'''
 
2
Adapter
 
3
=======
 
4
 
 
5
.. versionadded:: 1.5
 
6
 
 
7
.. warning::
 
8
 
 
9
    This code is still experimental, and its API is subject to change in a
 
10
    future version.
 
11
 
 
12
An :class:`~kivy.adapters.adapter.Adapter` is a bridge between data and
 
13
an :class:`~kivy.uix.abstractview.AbstractView` or one of its subclasses, such
 
14
as a :class:`~kivy.uix.listview.ListView`.
 
15
 
 
16
The following arguments can be passed to the contructor to initialise the
 
17
corresponding properties:
 
18
 
 
19
* :attr:`~Adapter.data`: for any sort of data to be used in a view. For an
 
20
  :class:`~kivy.adapters.adapter.Adapter`, data can be an object as well as a
 
21
  list, dict, etc. For a :class:`~kivy.adapters.listadapter.ListAdapter`, data
 
22
  should be a list. For a :class:`~kivy.adapters.dictadapter.DictAdapter`,
 
23
  data should be a dict.
 
24
 
 
25
* :attr:`~Adapter.cls`: the class used to instantiate each list item view
 
26
  instance (Use this or the template argument).
 
27
 
 
28
* :attr:`~Adapter.template`: a kv template to use to instantiate each list item
 
29
  view instance (Use this or the cls argument).
 
30
 
 
31
* :attr:`~Adapter.args_converter`: a function used to transform the data items
 
32
  in preparation for either a cls instantiation or a kv template
 
33
  invocation. If no args_converter is provided, the data items are assumed
 
34
  to be simple strings.
 
35
 
 
36
Please refer to the :mod:`~kivy.adapters` documentation for an overview of how
 
37
adapters are used.
 
38
 
 
39
'''
 
40
 
 
41
__all__ = ('Adapter', )
 
42
 
 
43
from kivy.event import EventDispatcher
 
44
from kivy.properties import ObjectProperty
 
45
from kivy.lang import Builder
 
46
from kivy.adapters.args_converters import list_item_args_converter
 
47
from kivy.factory import Factory
 
48
from kivy.compat import string_types
 
49
 
 
50
 
 
51
class Adapter(EventDispatcher):
 
52
    '''An :class:`~kivy.adapters.adapter.Adapter` is a bridge between data and
 
53
    an :class:`~kivy.uix.abstractview.AbstractView` or one of its subclasses,
 
54
    such as a :class:`~kivy.uix.listview.ListView`.
 
55
    '''
 
56
 
 
57
    data = ObjectProperty(None)
 
58
    '''
 
59
    The data for which a view is to be constructed using either the cls or
 
60
    template provided, together with the args_converter provided or the default
 
61
    args_converter.
 
62
 
 
63
    In this base class, data is an ObjectProperty, so it could be used for a
 
64
    wide variety of single-view needs.
 
65
 
 
66
    Subclasses may override it in order to use another data type, such as a
 
67
    :class:`~kivy.properties.ListProperty` or
 
68
    :class:`~kivy.properties.DictProperty` as appropriate. For example, in a
 
69
    :class:`~.kivy.adapters.listadapter.ListAdapter`, data is a
 
70
    :class:`~kivy.properties.ListProperty`.
 
71
 
 
72
    :attr:`data` is an :class:`~kivy.properties.ObjectProperty` and defaults
 
73
    to None.
 
74
    '''
 
75
 
 
76
    cls = ObjectProperty(None)
 
77
    '''
 
78
    A class for instantiating a given view item (Use this or template). If this
 
79
    is not set and neither is the template, a :class:`~kivy.uix.label.Label`
 
80
    is used for the view item.
 
81
 
 
82
    :attr:`cls` is an :class:`~kivy.properties.ObjectProperty` and defaults
 
83
    to None.
 
84
    '''
 
85
 
 
86
    template = ObjectProperty(None)
 
87
    '''
 
88
    A kv template for instantiating a given view item (Use this or cls).
 
89
 
 
90
    :attr:`template` is an :class:`~kivy.properties.ObjectProperty` and defaults
 
91
    to None.
 
92
    '''
 
93
 
 
94
    args_converter = ObjectProperty(None)
 
95
    '''
 
96
    A function that prepares an args dict for the cls or kv template to build
 
97
    a view from a data item.
 
98
 
 
99
    If an args_converter is not provided, a default one is set that assumes
 
100
    simple content in the form of a list of strings.
 
101
 
 
102
    :attr:`args_converter` is an :class:`~kivy.properties.ObjectProperty` and
 
103
    defaults to None.
 
104
    '''
 
105
 
 
106
    def __init__(self, **kwargs):
 
107
 
 
108
        if 'data' not in kwargs:
 
109
            raise Exception('adapter: input must include data argument')
 
110
 
 
111
        if 'cls' in kwargs:
 
112
            if 'template' in kwargs:
 
113
                msg = 'adapter: cannot use cls and template at the same time'
 
114
                raise Exception(msg)
 
115
            elif not kwargs['cls']:
 
116
                raise Exception('adapter: a cls or template must be defined')
 
117
        else:
 
118
            if 'template' in kwargs:
 
119
                if not kwargs['template']:
 
120
                    msg = 'adapter: a cls or template must be defined'
 
121
                    raise Exception(msg)
 
122
            else:
 
123
                raise Exception('adapter: a cls or template must be defined')
 
124
 
 
125
        if 'args_converter' in kwargs:
 
126
            self.args_converter = kwargs['args_converter']
 
127
        else:
 
128
            self.args_converter = list_item_args_converter
 
129
 
 
130
        super(Adapter, self).__init__(**kwargs)
 
131
 
 
132
    def bind_triggers_to_view(self, func):
 
133
        self.bind(data=func)
 
134
 
 
135
    def get_data_item(self):
 
136
        return self.data
 
137
 
 
138
    def get_cls(self):
 
139
        '''
 
140
        .. versionadded:: 1.9.0
 
141
 
 
142
        Returns the widget type specified by self.cls. If it is a
 
143
        string, the :class:`~kivy.factory.Factory` is queried to retrieve the
 
144
        widget class with the given name, otherwise it is returned directly.
 
145
        '''
 
146
        cls = self.cls
 
147
        if isinstance(cls, string_types):
 
148
            try:
 
149
                cls = getattr(Factory, cls)
 
150
            except AttributeError:
 
151
                raise AttributeError(
 
152
                    'Listadapter cls widget does not exist.')
 
153
        return cls
 
154
 
 
155
    def get_view(self, index):  # pragma: no cover
 
156
        item_args = self.args_converter(self.data)
 
157
 
 
158
        cls = self.get_cls()
 
159
        if cls:
 
160
            return cls(**item_args)
 
161
        else:
 
162
            return Builder.template(self.template, **item_args)