|
From: <sr...@ce...> - 2016-01-13 16:08:27
|
From: Sergio Rubio Manrique <sr...@ce...>
---
lib/taurus/core/tango/search.py | 35 +++++++++++++++++++++++------------
1 file changed, 23 insertions(+), 12 deletions(-)
diff --git a/lib/taurus/core/tango/search.py b/lib/taurus/core/tango/search.py
index 0cc8428..f0c2dec 100644
--- a/lib/taurus/core/tango/search.py
+++ b/lib/taurus/core/tango/search.py
@@ -35,25 +35,34 @@ import taurus
###############################################################################
# Utils
-def searchCl(regexp,target):
- return re.search(extend_regexp(regexp).lower(),target.lower())
+def searchCl(regexp,target,extend=False):
+ return re.search((extend_regexp if extend else toCl)(regexp),target.lower())
-def matchCl(regexp,target):
- return re.match(extend_regexp(regexp).lower(),target.lower())
+def matchCl(regexp,target,extend=False):
+ return re.match((extend_regexp if extend else toCl)(regexp),target.lower())
def is_regexp(s):
return any(c in s for c in '.*[]()+?')
-def extend_regexp(s):
- s = str(s).strip()
- if '.*' not in s:
- s = s.replace('*','.*')
- if '.*' not in s:
- if ' ' in s: s = s.replace(' ','.*')
+def toCl(exp,terminate=False,wildcards=('*',' '),lower=True):
+ """ Convertes exp into a Careless Expression.
+ Replaces * by .* and ? by . in the given expression.
+ """
+ exp = str(exp).strip()
+ if lower: exp = exp.lower()
+ if not any(s in exp for s in ('.*','\*')):
+ for w in wildcards:
+ exp = exp.replace(w,'.*')
+ if terminate and not exp.strip().endswith('$'): exp += '$'
+ exp = exp.replace('(?p<','(?P<') #Preventing missing P<name> clausses
+ return exp
+
+def extend_regexp(r):
+ s = toCl(r,terminate=True)
+ if '.*' not in r:
if '/' not in s: s = '.*'+s+'.*'
else:
if not s.startswith('^'): s = '^'+s
- if not s.endswith('$'): s = s+'$'
return s
def isString(s):
@@ -99,7 +108,7 @@ def get_matching_devices(expressions,limit=0,exported=False):
#all_devs.extend('%s/%s'%(host,d) for d in odb.get_device_name('*','*'))
result = [e for e in expressions if e.lower() in all_devs]
expressions = [extend_regexp(e) for e in expressions if e not in result]
- result.extend(filter(lambda d: any(matchCl(extend_regexp(e),d) for e in expressions),all_devs))
+ result.extend(filter(lambda d: any(matchCl(e,d,extend=False) for e in expressions),all_devs))
return result
def get_device_for_alias(alias):
@@ -113,11 +122,13 @@ def get_alias_for_device(dev):
db = taurus.Database()
try:
result = db.get_alias(dev) #.get_database_device().DbGetDeviceAlias(dev)
+ assert(result!='nada','no alias found')
return result
except Exception,e:
if 'no alias found' in str(e).lower(): return None
return None #raise e
def get_alias_dict(exp='*'):
+ #Returns an alias/device dict
tango = taurus.Database()
return dict((k,tango.get_device_alias(k)) for k in tango.get_device_alias_list(exp))
\ No newline at end of file
--
2.4.6
|