~ubuntu-branches/ubuntu/trusty/terminator/trusty

« back to all changes in this revision

Viewing changes to terminatorlib/factory.py

  • Committer: Bazaar Package Importer
  • Author(s): Nicolas Valcárcel Scerpella (Canonical)
  • Date: 2010-04-07 17:10:31 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100407171031-35nsuj0tmbub0bj5
Tags: 0.92-0ubuntu1
* New upstream release
* Remove python-xdg from Recommends. (Closes: #567967)
* Downgrade python-gnome2 to Recommends.
* Update python-gtk2 dependency to (>= 2.14.0)
* Add python-keybinder to Recommends

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# Terminator by Chris Jones <cmsj@tenshu.net>
 
3
# GPL v2 only
 
4
"""factory.py - Maker of objects
 
5
 
 
6
>>> maker = Factory()
 
7
>>> window = maker.make_window()
 
8
>>> maker.isinstance(window, 'Window')
 
9
True
 
10
>>> terminal = maker.make_terminal()
 
11
>>> maker.isinstance(terminal, 'Terminal')
 
12
True
 
13
>>> hpaned = maker.make_hpaned()
 
14
>>> maker.isinstance(hpaned, 'HPaned')
 
15
True
 
16
>>> vpaned = maker.make_vpaned()
 
17
>>> maker.isinstance(vpaned, 'VPaned')
 
18
True
 
19
 
 
20
"""
 
21
 
 
22
from borg import Borg
 
23
from util import dbg, err
 
24
 
 
25
# pylint: disable-msg=R0201
 
26
# pylint: disable-msg=W0613
 
27
class Factory(Borg):
 
28
    """Definition of a class that makes other classes"""
 
29
    types = {'Terminal': 'terminal',
 
30
             'VPaned': 'paned',
 
31
             'HPaned': 'paned',
 
32
             'Paned': 'paned',
 
33
             'Notebook': 'notebook',
 
34
             'Container': 'container',
 
35
             'Window': 'window'}
 
36
 
 
37
    def __init__(self):
 
38
        """Class initialiser"""
 
39
        Borg.__init__(self, self.__class__.__name__)
 
40
        self.prepare_attributes()
 
41
 
 
42
    def prepare_attributes(self):
 
43
        """Required by the borg, but a no-op here"""
 
44
        pass
 
45
 
 
46
    def isinstance(self, product, classtype):
 
47
        """Check if a given product is a particular type of object"""
 
48
        if classtype in self.types.keys():
 
49
            # This is quite ugly, but we're importing from the current
 
50
            # directory if that makes sense, otherwise falling back to
 
51
            # terminatorlib. Someone with real Python skills should fix
 
52
            # this to be less insane.
 
53
            try:
 
54
                module = __import__(self.types[classtype], None, None, [''])
 
55
            except ImportError:
 
56
                module = __import__('terminatorlib.%s' % self.types[classtype],
 
57
                    None, None, [''])
 
58
            return(isinstance(product, getattr(module, classtype)))
 
59
        else:
 
60
            err('Factory::isinstance: unknown class type: %s' % classtype)
 
61
            return(False)
 
62
 
 
63
    def type(self, product):
 
64
        """Determine the type of an object we've previously created"""
 
65
        for atype in self.types:
 
66
            # Skip over generic types
 
67
            if atype in ['Container', 'Paned']:
 
68
                continue
 
69
            if self.isinstance(product, atype):
 
70
                return(atype)
 
71
        return(None)
 
72
 
 
73
    def make(self, product, **kwargs):
 
74
        """Make the requested product"""
 
75
        try:
 
76
            func = getattr(self, 'make_%s' % product.lower())
 
77
        except AttributeError:
 
78
            err('Factory::make: requested object does not exist: %s' % product)
 
79
            return(None)
 
80
 
 
81
        dbg('Factory::make: created a %s' % product)
 
82
        return(func(**kwargs))
 
83
 
 
84
    def make_window(self, **kwargs):
 
85
        """Make a Window"""
 
86
        import window
 
87
        return(window.Window(**kwargs))
 
88
 
 
89
    def make_terminal(self, **kwargs):
 
90
        """Make a Terminal"""
 
91
        import terminal
 
92
        return(terminal.Terminal())
 
93
 
 
94
    def make_hpaned(self, **kwargs):
 
95
        """Make an HPaned"""
 
96
        import paned
 
97
        return(paned.HPaned())
 
98
 
 
99
    def make_vpaned(self, **kwargs):
 
100
        """Make a VPaned"""
 
101
        import paned
 
102
        return(paned.VPaned())
 
103
 
 
104
    def make_notebook(self, **kwargs):
 
105
        """Make a Notebook"""
 
106
        import notebook
 
107
        return(notebook.Notebook(kwargs['window']))
 
108