| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
""" |
|---|
| 4 |
This is the main package for Gaphor, a Python based UML editor. |
|---|
| 5 |
|
|---|
| 6 |
An extra function is installed in the builtin namespace: GaphorResource(). |
|---|
| 7 |
This function can be used to locate application wide resources, such as |
|---|
| 8 |
object factories. |
|---|
| 9 |
""" |
|---|
| 10 |
|
|---|
| 11 |
import misc.singleton |
|---|
| 12 |
import misc.logger |
|---|
| 13 |
import misc.conf |
|---|
| 14 |
import version |
|---|
| 15 |
import types |
|---|
| 16 |
|
|---|
| 17 |
class GaphorError(Exception): |
|---|
| 18 |
""" |
|---|
| 19 |
Gaphor specific exception class |
|---|
| 20 |
""" |
|---|
| 21 |
def __init__(self, args=None): |
|---|
| 22 |
self.args = args |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
class Gaphor(object): |
|---|
| 27 |
"""Gaphor main app. |
|---|
| 28 |
This is a Singleton object that is used as a access point |
|---|
| 29 |
to unique resources within Gaphor. The method main() is called once to |
|---|
| 30 |
start an interactive instance of Gaphor. If an application wants to use |
|---|
| 31 |
Gaphor's functionallity, but not the GUI, that application should not call |
|---|
| 32 |
main(). |
|---|
| 33 |
|
|---|
| 34 |
Resources can be accessed through the get() method. Resources can be |
|---|
| 35 |
Python classes or strings. In case of classes, get() will try to find an |
|---|
| 36 |
existing class, if none is found, it will create one. No parameters will |
|---|
| 37 |
be passed to that class. |
|---|
| 38 |
|
|---|
| 39 |
In case of a string resource, a lookup will be done in the GConf |
|---|
| 40 |
configuration tree. This is currently not implemented though... |
|---|
| 41 |
""" |
|---|
| 42 |
__metaclass__ = misc.singleton.Singleton |
|---|
| 43 |
|
|---|
| 44 |
NAME='gaphor' |
|---|
| 45 |
VERSION=version.VERSION |
|---|
| 46 |
TITLE='Gaphor v' + VERSION |
|---|
| 47 |
|
|---|
| 48 |
__resources = { } |
|---|
| 49 |
|
|---|
| 50 |
def __init__(self, install=1): |
|---|
| 51 |
self.__main_window = None |
|---|
| 52 |
self.__conf = None |
|---|
| 53 |
if install: |
|---|
| 54 |
self.install_gettext() |
|---|
| 55 |
|
|---|
| 56 |
def install_gettext(self): |
|---|
| 57 |
import gettext |
|---|
| 58 |
gettext.install(self.NAME, unicode=1) |
|---|
| 59 |
|
|---|
| 60 |
def main(self): |
|---|
| 61 |
import bonobo |
|---|
| 62 |
import gnome |
|---|
| 63 |
|
|---|
| 64 |
import gnome.ui |
|---|
| 65 |
from ui import MainWindow |
|---|
| 66 |
gnome.init(Gaphor.NAME, Gaphor.VERSION) |
|---|
| 67 |
|
|---|
| 68 |
self.__main_window = MainWindow() |
|---|
| 69 |
self.__main_window.construct() |
|---|
| 70 |
|
|---|
| 71 |
self.__main_window.connect(lambda win: win.get_state() == MainWindow.STATE_CLOSED and bonobo.main_quit()) |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
bonobo.main() |
|---|
| 75 |
log.info('Bye!') |
|---|
| 76 |
|
|---|
| 77 |
def get_main_window(self): |
|---|
| 78 |
return self.__main_window |
|---|
| 79 |
|
|---|
| 80 |
def get_conf(self, key): |
|---|
| 81 |
if not self.__conf: |
|---|
| 82 |
from gaphor.misc.conf import Conf |
|---|
| 83 |
self.__conf = Conf(self.NAME) |
|---|
| 84 |
return self.__conf[key] |
|---|
| 85 |
|
|---|
| 86 |
def get_datadir(self): |
|---|
| 87 |
import os |
|---|
| 88 |
if os.environ.has_key('GAPHOR_DATADIR'): |
|---|
| 89 |
return os.environ['GAPHOR_DATADIR'] |
|---|
| 90 |
return self.get_conf('datadir') |
|---|
| 91 |
|
|---|
| 92 |
def get_resource(resource): |
|---|
| 93 |
""" |
|---|
| 94 |
*Static method* |
|---|
| 95 |
Locate a resource. Resource should be the class of the resource to |
|---|
| 96 |
look for or a string. In case of a string the resource will be looked |
|---|
| 97 |
up in the GConf configuration. |
|---|
| 98 |
|
|---|
| 99 |
example: Get the element factory: |
|---|
| 100 |
elemfact = Gaphor.get(gaphor.UML.ElementFactory) |
|---|
| 101 |
|
|---|
| 102 |
or (with Gaphor installed in the builtin namespace): |
|---|
| 103 |
elemfact = GaphorResource(gaphor.UML.ElementFactory) |
|---|
| 104 |
""" |
|---|
| 105 |
if isinstance (resource, types.StringType): |
|---|
| 106 |
hash = Gaphor.__resources |
|---|
| 107 |
if hash.has_key(resource): |
|---|
| 108 |
return hash[resource] |
|---|
| 109 |
else: |
|---|
| 110 |
hash = Gaphor.__resources |
|---|
| 111 |
if hash.has_key(resource): |
|---|
| 112 |
return hash[resource] |
|---|
| 113 |
try: |
|---|
| 114 |
log.debug('Adding new resource: %s' % resource.__name__) |
|---|
| 115 |
r = resource() |
|---|
| 116 |
hash[resource] = r |
|---|
| 117 |
hash[resource.__name__] = r |
|---|
| 118 |
return r |
|---|
| 119 |
except Exception, e: |
|---|
| 120 |
raise GaphorError, 'Could not create resource %s (%s)' % (str(resource), str(e)) |
|---|
| 121 |
|
|---|
| 122 |
get_resource = staticmethod(get_resource) |
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
import __builtin__ |
|---|
| 126 |
__builtin__.__dict__['GaphorError'] = GaphorError |
|---|
| 127 |
__builtin__.__dict__['GaphorResource'] = Gaphor.get_resource |
|---|
| 128 |
__builtin__.__dict__['log'] = misc.logger.Logger() |
|---|
| 129 |
|
|---|