summaryrefslogtreecommitdiff
path: root/lib/taurus/core/taurusvalidator.py
blob: 33df99f8bde57872f68e45acdca9ffd7919b8cb9 (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/usr/bin/env python

#############################################################################
##
## This file is part of Taurus
## 
## http://taurus-scada.org
##
## Copyright 2011 CELLS / ALBA Synchrotron, Bellaterra, Spain
## 
## Taurus is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
## 
## Taurus is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU Lesser General Public License for more details.
## 
## You should have received a copy of the GNU Lesser General Public License
## along with Taurus.  If not, see <http://www.gnu.org/licenses/>.
##
#############################################################################

"""This module contains the base taurus name validator classes"""

__all__ = ["AbstractTangoValidator", "DatabaseNameValidator",
           "DeviceNameValidator", "AttributeNameValidator",
           "ConfigurationNameValidator"]

__docformat__ = "restructuredtext"

import re

from .taurusbasetypes import MatchLevel
from .util.singleton import Singleton

InvalidAlias = "nada"

class AbstractTangoValidator:
    
    complete_name = None
    normal_name = None
    short_name = None
    
    uri_gen_delims = "\:\/\?\#\[\]\@"
    # theoreticaly sub_delims should include '+' but we are more permissive here in tango
    #uri_sub_delims = "\!\$\&\'\(\)\*\+\,\;\="
    uri_sub_delims = "\!\$\&\'\(\)\*\,\;\="
    uri_reserved = uri_gen_delims + uri_sub_delims
    tango_word = '[^' + uri_reserved + ']+'
    protocol_prefix = 'tango://'

    def __init__(self):
        self.complete_re = re.compile("^%s$" % self.complete_name)
        self.normal_re = re.compile("^%s$" % self.normal_name)
        self.short_re = re.compile("^%s$" % self.short_name)

    def __getMatch(self,str):
        return self.complete_re.match(str) or self.normal_re.match(str) or self.short_re.match(str)

    def isValid(self,str, matchLevel = MatchLevel.ANY):
        if matchLevel == MatchLevel.ANY:
            return not self.__getMatch(str) is None
        elif matchLevel == MatchLevel.SHORT:
            return not self.short_re.match(str) is None
        elif matchLevel == MatchLevel.NORMAL:
            return not self.normal_re.match(str) is None
        elif matchLevel == MatchLevel.COMPLETE:
            return not self.complete_re.match(str) is None
        elif matchLevel == MatchLevel.SHORT_NORMAL:
            return self.isValid(str,MatchLevel.SHORT) or \
                   self.isValid(str,MatchLevel.NORMAL)
        elif matchLevel == MatchLevel.NORMAL_COMPLETE:
            return self.isValid(str,MatchLevel.NORMAL) or \
                   self.isValid(str,MatchLevel.COMPLETE)
        return False
    
    def getParams(self,str):
        m = self.__getMatch(str)
        if m is None:
            return None
        return m.groupdict()
    
    def getNames(self, str, factory=None):
        """Returns a tuple of three elements with (complete_name, normal_name, short_name)
        or None if no match is found"""
        return None


class DatabaseNameValidator(Singleton, AbstractTangoValidator):
    
    protocol_prefix = '((?P<scheme>tango)://)?'
    
    db = '(?P<host>([\w\-_]+\.)*[\w\-_]+):(?P<port>\d{1,5})'
    # for tango://host:port
    complete_name = '(' + protocol_prefix + ')?' + db
    # for a/b/c
    normal_name = db
    # for devalias
    short_name = db
    
    def __init__(self):
        pass
    
    def init(self, *args, **kwargs):
        """Singleton instance initialization."""
        AbstractTangoValidator.__init__(self)
        
    def getNames(self, str, factory=None):
        elems = self.getParams(str)
        if elems is None:
            return str, None, None
        
        host = elems.get('host')
        port = elems.get('port')
        
        if host is None or port is None or len(host) == 0 or len(port) == 0:
            return None
        
        return 3*('%s:%s' % (host,port),)


class DatabaseQueryValidator(Singleton, AbstractTangoValidator):
    """Deprecated"""
    
    query = '\?query=(?P<query>[\w\-_]+)(?P<params>(\?param=[\w\*\?\%\-_]+)*)'

    complete_name = DatabaseNameValidator.complete_name + query 
    normal_name = DatabaseNameValidator.normal_name + query
    short_name = query
    
    def __init__(self):
        pass
    
    def init(self, *args, **kwargs):
        """Singleton instance initialization."""
        AbstractTangoValidator.__init__(self)

    def getNames(self, str, factory=None):
        elems = self.getParams(str)
        if elems is None:
            return str,None
        
        query = elems.get('query')
        params = elems.get('params')
        
        short = normal = query
        if params:
            normal += str(params.split('?param=')[1:])
        return str, normal, short    
            

class DeviceNameValidator(Singleton, AbstractTangoValidator):
    
    w = AbstractTangoValidator.tango_word
    dev = '(?P<devicename>' + w + '/' + w + '/' + w + ')'
    # for tango://host:port/a/b/c/attrname or host:port/a/b/c
    complete_name = DatabaseNameValidator.complete_name + '/' + dev
    # for a/b/c
    normal_name = DatabaseNameValidator.protocol_prefix + dev
    # for devalias
    short_name = '(?P<devalias>'+ w + ')'

    def __init__(self):
        pass
    
    def init(self, *args, **kwargs):
        """Singleton instance initialization."""
        AbstractTangoValidator.__init__(self)
        
    def getNames(self, str, factory=None):
        elems = self.getParams(str)
        if elems is None:
            return str,None
        
        dev_name = elems.get('devicename')
        alias = elems.get('devalias')
        
        host = elems.get('host')
        port = elems.get('port')
        
        if factory is None:
            return dev_name or '', dev_name or '', alias or ''

        db = None
        try:
            if host and port:
                db = factory.getDatabase("%s:%s" % (host,port))
                #db = PyTango.Database(host,int(port))
            else:
                #db = PyTango.Database()
                db = factory.getDatabase()
        except:
            return dev_name or '', dev_name or '', alias or ''
        
        if dev_name:
            alias = db.getElementAlias(dev_name) or dev_name
        else:
            dev_name = db.getElementFullName(alias)
        
        complete = "%s:%s/%s" % (host,port,dev_name)
        return complete, dev_name, alias


class DeviceQueryValidator(Singleton, AbstractTangoValidator):
    """Deprecated"""
    query = DatabaseQueryValidator.query

    complete_name = DeviceNameValidator.complete_name + query 
    normal_name = DeviceNameValidator.normal_name + query
    short_name = DeviceNameValidator.short_name + query

    def __init__(self):
        pass
    
    def init(self, *args, **kwargs):
        """Singleton instance initialization."""
        AbstractTangoValidator.__init__(self)    

    def getNames(self,str, factory=None):
        elems = self.getParams(str)
        if elems is None:
            return str,None
        
        query = elems.get('query')
        params = elems.get('params')
        
        short = query
        if params:
            short += str(params.split('?param=')[1:])
        return str,short    

    
class AttributeNameValidator(Singleton, AbstractTangoValidator):
    
    w = AbstractTangoValidator.tango_word
    attr = '/(?P<attributename>' + w + ')'
    # for tango://host:port/a/b/c/attributename or host:port/a/b/c/attributename
    complete_name = DeviceNameValidator.complete_name + attr
    # for a/b/c/attributename
    normal_name = DeviceNameValidator.normal_name + attr
    # for devalias/attributename
    short_name = DeviceNameValidator.short_name + attr
    
    def __init__(self):
        """ Initialization. Nothing to be done here for now."""
        pass
    
    def init(self, *args, **kwargs):
        """Singleton instance initialization."""
        AbstractTangoValidator.__init__(self)

    def getNames(self, str, factory=None):
        """Returns the complete and short names"""
        
        elems = self.getParams(str)
        if elems is None:
            return None
        
        dev_name = elems.get('devicename')
        attr_name = elems.get('attributename')
        
        if dev_name:
            normal_name = dev_name + "/" + attr_name
        else:
            normal_name = attr_name
            
        return str, normal_name, attr_name
    
    
class ConfigurationNameValidator(Singleton, AbstractTangoValidator):
    
    w = AbstractTangoValidator.tango_word
    conf = "\?(?i)configuration(=(?P<configparam>" + w + "))*"
    # for tango://host:port/a/b/c/attrname or host:port/a/b/c/attrname
    complete_name = AttributeNameValidator.complete_name + conf
    # for a/b/c/attrname
    normal_name = AttributeNameValidator.normal_name + conf
    # for devalias/attrname
    short_name = AttributeNameValidator.short_name + conf

    def __init__(self):
        """ Initialization. Nothing to be done here for now."""

    def init(self, *args, **kwargs):
        """Singleton instance initialization."""
        AbstractTangoValidator.__init__(self)

    def getNames(self, str, factory=None):
        """Returns the complete and short names"""
        
        elems = self.getParams(str)
        if elems is None:
            return None
                
        dev_name = elems.get('devicename')
        attr_name = elems.get('attributename')
        simple = elems.get('configparam') or 'configuration'
        
        if dev_name:
            normal = dev_name + "/" + attr_name + "?configuration=" + simple
        elif attr_name:
            normal = attr_name + "?configuration=" + simple
        else:
            normal = simple
        
        ret  = str,normal,simple
        return ret