root/gaphor/tags/gaphor-0.13.0/gaphor/core.py

Revision 2223, 1.0 kB (checked in by arj..@yirdis.nl, 11 months ago)

Do no longer cache service instances in inject() as it frustrates unit tests.

Line 
1 """
2 The Core module provides an entry point for Gaphor's core constructs.
3
4 An average module should only need to import this module.
5 """
6
7 from gaphor.application import Application
8 from gaphor.transaction import Transaction, transactional
9 from gaphor.action import action, toggle_action, radio_action, build_action_group
10 from gaphor.i18n import _
11
12
13 class inject(object):
14     """
15     Simple descriptor for dependency injection.
16     This is technically a wrapper around Application.get_service().
17
18     Usage::
19
20       class A(object):
21         gui_manager = inject('gui_manager')
22     """
23    
24     def __init__(self, name):
25         self._name = name
26         #self._s = None
27        
28     def __get__(self, obj, class_=None):
29         """
30         Resolve a dependency, but only if we're called from an object instance.
31         """
32         if not obj:
33             return self
34         return Application.get_service(self._name)
35         #if self._s is None:
36         #    self._s = _Application.get_service(self._name)
37         #return self._s
38
39
40 # vim:sw=4:et:ai
Note: See TracBrowser for help on using the browser.