File: Registry.py

package info (click to toggle)
zope-textindexng2 1%3A2.2.0-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,888 kB
  • ctags: 1,598
  • sloc: ansic: 6,836; python: 6,596; xml: 185; makefile: 137; sh: 41
file content (131 lines) | stat: -rw-r--r-- 3,771 bytes parent folder | download
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
###########################################################################
#
# TextIndexNG                The next generation TextIndex for Zope
#
# This software is governed by a license. See
# LICENSE.txt for the terms of this license.
#
###########################################################################

""" 
Registry  for all and everything 

$Id: Registry.py,v 1.24 2004/12/28 10:54:15 ajung Exp $
"""

import sys
from types import InstanceType

from zLOG import WARNING, LOG
from classVerify import verifyClass
from interfaces.IRegistry import RegistryInterface

class RegistryException(Exception): pass

class Registry:
    """registry for data"""   

    __implements__ = RegistryInterface
    
    def __init__(self, id, ifaces=None):
        self.id = id
        self.ifaces = [] 
        self.data = {}
        if ifaces: 
            if not isinstance(ifaces, tuple): ifaces = [ ifaces ]
            self.ifaces.extend(ifaces) 

 
    def __call__(self, *args, **kw):
        """ emulate a singleton behaviour """
        return self 

    def getRegisteredObject(self, id):

        if not self.data.has_key(id):
            raise RegistryException, \
            '"%s" not registered in registry "%s"' % (id, self.id)

        return self.data[id] 

    get = getRegisteredObject

    def is_registered(self, id):
        return self.data.has_key(id)

    def register(self, id, instance=None):
        """ map an id to an instance """

        if self.data.has_key(id):
            raise RegistryException, '"%s" already registered' % id
        
        for iface in self.ifaces:
            try:
                if isinstance(instance, InstanceType):
                    verifyClass(iface, instance.__class__)
                else:
                    verifyClass(iface, instance)

            except:
                LOG('TextIndexNG', WARNING, 
                    'interface broken for %s' % str(instance),
                    error=sys.exc_info())
                raise
    
        self.data[id] = instance


    def unregister(self, id):
        """ delete an item """
        del self.data[id]


    def clear(self):
        self.data = {}

    def allRegisteredObjects(self):
        return self.data.values()

    def allIds(self):
        return self.data.keys()


from interfaces.IConverter import ConverterInterface
from interfaces.ILexicon import LexiconInterface
from interfaces.INormalizer import NormalizerInterface
from interfaces.IParser import ParserInterface
from interfaces.IStopwords import StopwordsInterface
from interfaces.IStorage import StorageInterface
from interfaces.IThesaurus import IThesaurus

class ConverterRegistry(Registry):
    """ registry for converters """    
ConverterRegistry = ConverterRegistry('converters', (ConverterInterface,) )

class LexiconRegistry(Registry):
    """registry for Lexicons"""    
LexiconRegistry = LexiconRegistry('lexicons', (LexiconInterface,) )

class NormalizerRegistry(Registry):
    """registry for Normalizer """    
NormalizerRegistry = NormalizerRegistry('Normalizer', NormalizerInterface )

class ParserRegistry(Registry):
    """registry for parsers"""    
ParserRegistry = ParserRegistry('parsers', (ParserInterface,) )

class SplitterRegistry(Registry):
    """registry for Splitter"""    
SplitterRegistry = SplitterRegistry('Splitter' )

class StopwordsRegistry(Registry):
    """registry for Stopwords """    
StopwordsRegistry = StopwordsRegistry('Stopwords', StopwordsInterface )

class StorageRegistry(Registry):
    """registry for storages """    
StorageRegistry = StorageRegistry('storages', (StorageInterface,) )

class ThesaurusRegistry(Registry):
    """registry for thesauruses """
ThesaurusRegistry = ThesaurusRegistry('thesaurus', (IThesaurus,))