2016-11-25 01:29:55 +00:00
|
|
|
#!/usr/bin/env python2
|
2009-11-24 23:47:32 +00:00
|
|
|
# Terminator by Chris Jones <cmsj@tenshu.net>
|
|
|
|
# GPL v2 only
|
2010-01-14 23:40:02 +00:00
|
|
|
"""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
|
|
|
|
|
|
|
|
"""
|
2009-11-24 23:47:32 +00:00
|
|
|
|
2018-04-24 18:22:10 +00:00
|
|
|
from .borg import Borg
|
|
|
|
from .util import dbg, err, inject_uuid
|
2009-11-24 23:47:32 +00:00
|
|
|
|
2009-11-24 23:49:03 +00:00
|
|
|
# pylint: disable-msg=R0201
|
|
|
|
# pylint: disable-msg=W0613
|
2009-11-24 23:47:32 +00:00
|
|
|
class Factory(Borg):
|
|
|
|
"""Definition of a class that makes other classes"""
|
2010-02-01 12:11:44 +00:00
|
|
|
types = {'Terminal': 'terminal',
|
|
|
|
'VPaned': 'paned',
|
|
|
|
'HPaned': 'paned',
|
|
|
|
'Paned': 'paned',
|
|
|
|
'Notebook': 'notebook',
|
|
|
|
'Container': 'container',
|
|
|
|
'Window': 'window'}
|
2018-04-24 18:22:10 +00:00
|
|
|
types_keys = list(types.keys())
|
2011-02-17 01:28:38 +00:00
|
|
|
instance_types = {}
|
|
|
|
instance_types_keys = []
|
2010-02-01 12:11:44 +00:00
|
|
|
|
2009-11-24 23:47:32 +00:00
|
|
|
def __init__(self):
|
|
|
|
"""Class initialiser"""
|
2009-12-22 00:25:05 +00:00
|
|
|
Borg.__init__(self, self.__class__.__name__)
|
2009-11-24 23:47:32 +00:00
|
|
|
self.prepare_attributes()
|
|
|
|
|
|
|
|
def prepare_attributes(self):
|
|
|
|
"""Required by the borg, but a no-op here"""
|
|
|
|
pass
|
|
|
|
|
2009-11-25 00:37:29 +00:00
|
|
|
def isinstance(self, product, classtype):
|
|
|
|
"""Check if a given product is a particular type of object"""
|
2011-02-17 01:28:38 +00:00
|
|
|
if classtype in self.types_keys:
|
|
|
|
# This is now very ugly, but now it's fast :-)
|
|
|
|
# Someone with real Python skills should fix this to be less insane.
|
|
|
|
# Optimisations:
|
|
|
|
# - swap order of imports, otherwise we throw ImportError
|
|
|
|
# almost every time
|
|
|
|
# - cache everything we can
|
2010-01-04 13:46:55 +00:00
|
|
|
try:
|
2011-02-17 01:28:38 +00:00
|
|
|
type_key = 'terminatorlib.%s' % self.types[classtype]
|
|
|
|
if type_key not in self.instance_types_keys:
|
|
|
|
self.instance_types[type_key] = __import__(type_key, None, None, [''])
|
|
|
|
self.instance_types_keys.append(type_key)
|
|
|
|
module = self.instance_types[type_key]
|
2010-01-22 18:57:31 +00:00
|
|
|
except ImportError:
|
2011-02-17 01:28:38 +00:00
|
|
|
type_key = self.types[classtype]
|
|
|
|
if type_key not in self.instance_types_keys:
|
|
|
|
self.instance_types[type_key] = __import__(type_key, None, None, [''])
|
|
|
|
self.instance_types_keys.append(type_key)
|
|
|
|
module = self.instance_types[type_key]
|
2009-12-09 13:22:27 +00:00
|
|
|
return(isinstance(product, getattr(module, classtype)))
|
2009-11-25 00:37:29 +00:00
|
|
|
else:
|
|
|
|
err('Factory::isinstance: unknown class type: %s' % classtype)
|
|
|
|
return(False)
|
|
|
|
|
2010-02-01 12:11:44 +00:00
|
|
|
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)
|
|
|
|
|
2010-01-05 12:49:04 +00:00
|
|
|
def make(self, product, **kwargs):
|
2009-11-24 23:47:32 +00:00
|
|
|
"""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)
|
2012-10-18 18:39:28 +00:00
|
|
|
output = func(**kwargs)
|
|
|
|
inject_uuid(output)
|
|
|
|
return(output)
|
2009-11-24 23:47:32 +00:00
|
|
|
|
2010-01-05 12:49:04 +00:00
|
|
|
def make_window(self, **kwargs):
|
2010-01-04 13:04:46 +00:00
|
|
|
"""Make a Window"""
|
2018-04-24 18:22:10 +00:00
|
|
|
from . import window
|
2010-01-05 12:49:04 +00:00
|
|
|
return(window.Window(**kwargs))
|
2010-01-04 13:04:46 +00:00
|
|
|
|
2010-01-05 12:49:04 +00:00
|
|
|
def make_terminal(self, **kwargs):
|
2009-11-24 23:47:32 +00:00
|
|
|
"""Make a Terminal"""
|
2018-04-24 18:22:10 +00:00
|
|
|
from . import terminal
|
2010-01-04 13:11:16 +00:00
|
|
|
return(terminal.Terminal())
|
2009-11-24 23:47:32 +00:00
|
|
|
|
2010-01-05 12:49:04 +00:00
|
|
|
def make_hpaned(self, **kwargs):
|
2009-11-24 23:47:32 +00:00
|
|
|
"""Make an HPaned"""
|
2018-04-24 18:22:10 +00:00
|
|
|
from . import paned
|
2009-11-24 23:47:32 +00:00
|
|
|
return(paned.HPaned())
|
|
|
|
|
2010-01-05 12:49:04 +00:00
|
|
|
def make_vpaned(self, **kwargs):
|
2009-11-24 23:47:32 +00:00
|
|
|
"""Make a VPaned"""
|
2018-04-24 18:22:10 +00:00
|
|
|
from . import paned
|
2009-11-24 23:47:32 +00:00
|
|
|
return(paned.VPaned())
|
|
|
|
|
2010-01-05 12:49:04 +00:00
|
|
|
def make_notebook(self, **kwargs):
|
2009-11-24 23:47:32 +00:00
|
|
|
"""Make a Notebook"""
|
2018-04-24 18:22:10 +00:00
|
|
|
from . import notebook
|
2010-01-05 12:49:04 +00:00
|
|
|
return(notebook.Notebook(kwargs['window']))
|
2009-11-24 23:47:32 +00:00
|
|
|
|