~openerp-dev/openobject-client-web/6.0-opw-591397-xal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import cherrypy

_REGISTRY = {}

def register_object(cls, key, group):
    """Register and object with key in the given group.

    @param cls: the type to register
    @param key: key to access the registered type from the pool
    @param group: the group in which the type should be registered

    >>>
    >>> pooler.register_object(cls, "char", group="form_input_widgets")
    >>> pooler.register_object(cls, "/about", group="controllers")
    >>> pooler.register_object(cls, "calendar", group="view_type")
    >>>
    """
    assert isinstance(cls, type), 'You can only register classes to the pooler'
    module = None
    if hasattr(cls, '__module__'):
        module = str(cls.__module__).split('.')[0]

    registry = _REGISTRY.setdefault(group, {})
    group_types = registry.setdefault(module, {})

    group_types[key] = cls

class Pool(object):

    def __init__(self):
        self.types_pool = {}

    def get(self, key, group):
        return self.get_group(group).get(key, None)

    def get_group(self, group):
        return self.types_pool.setdefault(group, {})

    def get_controller(self, name):
        """ Fetches and initializes a controller instance
        """
        Controller = self.get(name, "controllers")
        if Controller: return Controller()
        return None

    def load(self, package):
        ''' Loads all the objects of the provided package in the current pooler
        '''
        for group, registry in _REGISTRY.iteritems():
            if package in registry:
                for key, typ in registry[package].iteritems():
                    types = self.types_pool.setdefault(group, {})
                    if key in types:
                        # new type is already a super class of current type
                        # for the key, don't build new type
                        if issubclass(types[key], typ):
                            continue
                        # already have an object there, build subtype
                        types[key] = type(typ.__name__,
                                          (typ, types[key]),
                                          {})
                    else:
                        types[key] = typ

pool_dict = {}

def restart_pool():

    db_name = cherrypy.session['db']
    
    if db_name in pool_dict:
        import addons
        
        del pool_dict[db_name]
        del addons._loaded[db_name]
    
    return get_pool()
        
def get_pool():

    config = cherrypy.request.app.config
    db_name = None

    try:
        db_name = cherrypy.session['db']
    except Exception, e:
        pass

    if db_name in pool_dict:
        pool = pool_dict[db_name]
    else:
        import addons

        pool = pool_dict[db_name] = Pool()
        
        try:
            addons.load_addons(db_name, config)
        except:
            del pool_dict[db_name]
            raise

    return pool