summaryrefslogtreecommitdiff
path: root/urwid/__init__.py
blob: f3a00cc1513515a1038bcd8aa3e9c0c749ea7fa7 (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/python
#
# Urwid __init__.py - all the stuff you're likely to care about
#
#    Copyright (C) 2004-2012  Ian Ward
#
#    This library is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Lesser General Public
#    License as published by the Free Software Foundation; either
#    version 2.1 of the License, or (at your option) any later version.
#
#    This library is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with this library; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Urwid web site: http://excess.org/urwid/

import sys

class _UrwidModule(object):
    """
    Import specific names from deeper in the urwid module hierarchy
    to the top level because urwid was once a small library with
    everything in one module, and life is easier for users this way.

    This class lazily imports so all users don't need to pay the
    price of importing almost all urwid sub-modules on startup.
    """
    from urwid.version import VERSION, __version__

    _names = {
        'widget': (
            'FLOW', 'BOX', 'FIXED', 'LEFT', 'RIGHT', 'CENTER', 'TOP', 'MIDDLE',
            'BOTTOM', 'SPACE', 'ANY', 'CLIP', 'PACK', 'GIVEN', 'RELATIVE',
            'RELATIVE_100', 'WEIGHT', 'WidgetMeta',
            'WidgetError', 'Widget', 'FlowWidget', 'BoxWidget', 'fixed_size',
            'FixedWidget', 'Divider', 'SolidFill', 'TextError', 'Text',
            'EditError', 'Edit', 'IntEdit', 'delegate_to_widget_mixin',
            'WidgetWrapError', 'WidgetWrap'),
        'decoration': (
            'WidgetDecoration', 'WidgetPlaceholder',
            'AttrMapError', 'AttrMap', 'AttrWrap', 'BoxAdapterError',
            'BoxAdapter', 'PaddingError',
            'Padding', 'FillerError', 'Filler', 'WidgetDisable'),
        'container': (
            'GridFlowError', 'GridFlow', 'OverlayError', 'Overlay',
            'FrameError', 'Frame', 'PileError', 'Pile', 'ColumnsError',
            'Columns', 'WidgetContainerMixin'),
        'wimp': (
            'SelectableIcon', 'CheckBoxError', 'CheckBox', 'RadioButton',
            'Button', 'PopUpLauncher', 'PopUpTarget'),
        'listbox': (
            'ListWalkerError', 'ListWalker', 'PollingListWalker',
            'SimpleListWalker', 'SimpleFocusListWalker', 'ListBoxError',
            'ListBox'),
        'graphics': (
            'BigText', 'LineBox', 'BarGraphMeta', 'BarGraphError',
            'BarGraph', 'GraphVScale', 'ProgressBar', 'scale_bar_values'),
        'canvas': (
            'CanvasCache', 'CanvasError', 'Canvas', 'TextCanvas',
            'BlankCanvas', 'SolidCanvas', 'CompositeCanvas', 'CanvasCombine',
            'CanvasOverlay', 'CanvasJoin'),
        'font': (
            'get_all_fonts', 'Font', 'Thin3x3Font', 'Thin4x3Font',
            'HalfBlock5x4Font', 'HalfBlock6x5Font', 'HalfBlockHeavy6x5Font',
            'Thin6x6Font', 'HalfBlock7x7Font'),
        'signals': (
            'MetaSignals', 'Signals', 'emit_signal', 'register_signal',
            'connect_signal', 'disconnect_signal'),
        'monitored_list': (
            'MonitoredList', 'MonitoredFocusList'),
        'command_map': (
            'CommandMap', 'command_map',
            'REDRAW_SCREEN', 'CURSOR_UP', 'CURSOR_DOWN', 'CURSOR_LEFT',
            'CURSOR_RIGHT', 'CURSOR_PAGE_UP', 'CURSOR_PAGE_DOWN',
            'CURSOR_MAX_LEFT', 'CURSOR_MAX_RIGHT', 'ACTIVATE'),
        'main_loop': (
            'ExitMainLoop', 'MainLoop', 'SelectEventLoop',
            'TwistedEventLoop', 'GLibEventLoop', 'TornadoEventLoop'),
        'text_layout': (
            'TextLayout', 'StandardTextLayout', 'default_layout',
            'LayoutSegment'),
        'display_common': (
            'UPDATE_PALETTE_ENTRY', 'DEFAULT', 'BLACK',
            'DARK_RED', 'DARK_GREEN', 'BROWN', 'DARK_BLUE', 'DARK_MAGENTA',
            'DARK_CYAN', 'LIGHT_GRAY', 'DARK_GRAY', 'LIGHT_RED', 'LIGHT_GREEN',
            'YELLOW', 'LIGHT_BLUE', 'LIGHT_MAGENTA', 'LIGHT_CYAN', 'WHITE',
            'AttrSpecError', 'AttrSpec', 'RealTerminal',
            'ScreenError', 'BaseScreen'),
        'util': (
            'calc_text_pos', 'calc_width', 'is_wide_char',
            'move_next_char', 'move_prev_char', 'within_double_byte',
            'detected_encoding', 'set_encoding', 'get_encoding_mode',
            'apply_target_encoding', 'supports_unicode',
            'calc_trim_text', 'TagMarkupException', 'decompose_tagmarkup',
            'MetaSuper', 'int_scale', 'is_mouse_event'),
        'treetools': (
            'TreeWidgetError', 'TreeWidget', 'TreeNode',
            'ParentNode', 'TreeWalker', 'TreeListBox'),
        'vterm': (
            'TermModes', 'TermCharset', 'TermScroller', 'TermCanvas',
            'Terminal'),
        }

    _reverse_names = dict((name, mod) for mod, names in _names.iteritems()
        for name in names)

    # allow imports of sub-modules to continue to work properly
    __path__ = __path__

    # 3.3 compat
    __name__ = __name__

    def __getattr__(self, name):
        """
        Lazy import machinery. Use normal import statements to make
        it easier for things like pyinstaller to tell what modules
        get imported.
        """
        if name == 'raw_display':
            # special case:
            # urwid.raw_display.Screen should work after import urwid
            import urwid.raw_display
            self.raw_display = urwid.raw_display
            return urwid.raw_display

        found = self._reverse_names.get(name)
        if not found:
            raise AttributeError("urwid has no attribute '%s'" % name)
        elif found == 'widget':
            import urwid.widget
        elif found == 'decoration':
            import urwid.decoration
        elif found == 'container':
            import urwid.container
        elif found == 'wimp':
            import urwid.wimp
        elif found == 'listbox':
            import urwid.listbox
        elif found == 'graphics':
            import urwid.graphics
        elif found == 'canvas':
            import urwid.canvas
        elif found == 'font':
            import urwid.font
        elif found == 'signals':
            import urwid.signals
        elif found == 'monitored_list':
            import urwid.monitored_list
        elif found == 'command_map':
            import urwid.command_map
        elif found == 'main_loop':
            import urwid.main_loop
        elif found == 'text_layout':
            import urwid.text_layout
        elif found == 'display_common':
            import urwid.display_common
        elif found == 'util':
            import urwid.util
        elif found == 'treetols':
            import urwid.treetols
        elif found == 'vterm':
            import urwid.vterm
        else:
            assert 0, 'missing import line for %s' % found

        if found:
            module = getattr(urwid, found)
            for n in self._names[found]:
                setattr(self, n, getattr(module, n))

        return getattr(self, name)


# https://mail.python.org/pipermail/python-ideas/2012-May/014969.html
sys.modules[__name__] = _UrwidModule()