File: factory.py

package info (click to toggle)
terminator 0.93-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,672 kB
  • ctags: 980
  • sloc: python: 7,551; sh: 22; makefile: 12
file content (108 lines) | stat: -rwxr-xr-x 3,327 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/python
# Terminator by Chris Jones <cmsj@tenshu.net>
# GPL v2 only
"""factory.py - Maker of objects

>>> maker = Factory()
>>> window = maker.make_window()
>>> maker.isinstance(window, 'Window')
True
>>> terminal = maker.make_terminal()
>>> maker.isinstance(terminal, 'Terminal')
True
>>> hpaned = maker.make_hpaned()
>>> maker.isinstance(hpaned, 'HPaned')
True
>>> vpaned = maker.make_vpaned()
>>> maker.isinstance(vpaned, 'VPaned')
True

"""

from borg import Borg
from util import dbg, err

# pylint: disable-msg=R0201
# pylint: disable-msg=W0613
class Factory(Borg):
    """Definition of a class that makes other classes"""
    types = {'Terminal': 'terminal',
             'VPaned': 'paned',
             'HPaned': 'paned',
             'Paned': 'paned',
             'Notebook': 'notebook',
             'Container': 'container',
             'Window': 'window'}

    def __init__(self):
        """Class initialiser"""
        Borg.__init__(self, self.__class__.__name__)
        self.prepare_attributes()

    def prepare_attributes(self):
        """Required by the borg, but a no-op here"""
        pass

    def isinstance(self, product, classtype):
        """Check if a given product is a particular type of object"""
        if classtype in self.types.keys():
            # This is quite ugly, but we're importing from the current
            # directory if that makes sense, otherwise falling back to
            # terminatorlib. Someone with real Python skills should fix
            # this to be less insane.
            try:
                module = __import__(self.types[classtype], None, None, [''])
            except ImportError:
                module = __import__('terminatorlib.%s' % self.types[classtype],
                    None, None, [''])
            return(isinstance(product, getattr(module, classtype)))
        else:
            err('Factory::isinstance: unknown class type: %s' % classtype)
            return(False)

    def type(self, product):
        """Determine the type of an object we've previously created"""
        for atype in self.types:
            # Skip over generic types
            if atype in ['Container', 'Paned']:
                continue
            if self.isinstance(product, atype):
                return(atype)
        return(None)

    def make(self, product, **kwargs):
        """Make the requested product"""
        try:
            func = getattr(self, 'make_%s' % product.lower())
        except AttributeError:
            err('Factory::make: requested object does not exist: %s' % product)
            return(None)

        dbg('Factory::make: created a %s' % product)
        return(func(**kwargs))

    def make_window(self, **kwargs):
        """Make a Window"""
        import window
        return(window.Window(**kwargs))

    def make_terminal(self, **kwargs):
        """Make a Terminal"""
        import terminal
        return(terminal.Terminal())

    def make_hpaned(self, **kwargs):
        """Make an HPaned"""
        import paned
        return(paned.HPaned())

    def make_vpaned(self, **kwargs):
        """Make a VPaned"""
        import paned
        return(paned.VPaned())

    def make_notebook(self, **kwargs):
        """Make a Notebook"""
        import notebook
        return(notebook.Notebook(kwargs['window']))