| 1 |
""" |
|---|
| 2 |
The Application object. One application should be available. |
|---|
| 3 |
|
|---|
| 4 |
All important services are present in the application object: |
|---|
| 5 |
- plugin manager |
|---|
| 6 |
- undo manager |
|---|
| 7 |
- main window |
|---|
| 8 |
- UML element factory |
|---|
| 9 |
- action sets |
|---|
| 10 |
""" |
|---|
| 11 |
|
|---|
| 12 |
import gtk |
|---|
| 13 |
import pkg_resources |
|---|
| 14 |
from zope import component |
|---|
| 15 |
from gaphor.interfaces import IService |
|---|
| 16 |
|
|---|
| 17 |
import gaphor.ui |
|---|
| 18 |
from gaphor.ui import mainwindow, load_accel_map, save_accel_map |
|---|
| 19 |
|
|---|
| 20 |
class _Application(object): |
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
def __init__(self): |
|---|
| 25 |
self.services = { |
|---|
| 26 |
'main_window': mainwindow.MainWindow(), |
|---|
| 27 |
'element_factory': gaphor.UML.ElementFactory() |
|---|
| 28 |
} |
|---|
| 29 |
|
|---|
| 30 |
def __getattr__(self, key): |
|---|
| 31 |
return self.services[key] |
|---|
| 32 |
|
|---|
| 33 |
def init(self): |
|---|
| 34 |
""" |
|---|
| 35 |
Initialize the application. |
|---|
| 36 |
""" |
|---|
| 37 |
load_accel_map() |
|---|
| 38 |
import gaphor.adapters |
|---|
| 39 |
import gaphor.actions |
|---|
| 40 |
self.load_services() |
|---|
| 41 |
self.main_window.construct() |
|---|
| 42 |
main_window.connect(lambda win: win.get_state() == MainWindow.STATE_CLOSED and gtk.main_quit()) |
|---|
| 43 |
|
|---|
| 44 |
def load_services(self): |
|---|
| 45 |
""" |
|---|
| 46 |
Load services from resources. |
|---|
| 47 |
|
|---|
| 48 |
Services are registered as utilities in zope.component. |
|---|
| 49 |
Service should provide an interface gaphor.interfaces.IService. |
|---|
| 50 |
""" |
|---|
| 51 |
for ep in pkg_resources.iter_entry_points('gaphor.services'): |
|---|
| 52 |
|
|---|
| 53 |
log.debug('found entry point service.%s' % ep.name) |
|---|
| 54 |
cls = ep.load() |
|---|
| 55 |
if not IService.implementedBy(cls): |
|---|
| 56 |
raise 'MisConfigurationException', 'Entry point %s doesn''t provide IService' % ep.name |
|---|
| 57 |
srv = cls() |
|---|
| 58 |
srv.init(self) |
|---|
| 59 |
print 'service', srv |
|---|
| 60 |
component.provideUtility(srv, IService, ep.name) |
|---|
| 61 |
|
|---|
| 62 |
def get_service(self, name): |
|---|
| 63 |
component.getUtility(IService, name) |
|---|
| 64 |
|
|---|
| 65 |
def run(self): |
|---|
| 66 |
gtk.main() |
|---|
| 67 |
|
|---|
| 68 |
def shutdown(self): |
|---|
| 69 |
save_accel_map() |
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
Application = _Application() |
|---|
| 73 |
|
|---|
| 74 |
def restart(): |
|---|
| 75 |
Application.shutdown() |
|---|
| 76 |
Application = _Application() |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|