D'autre façons de fournir human_to_ldap pour les filtres de recherches
This commit is contained in:
parent
731ee0221b
commit
cb4ac3ca18
2 changed files with 115 additions and 0 deletions
67
filter2.py
Normal file
67
filter2.py
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
"""Plus rapide"""
|
||||||
|
|
||||||
|
import pyparsing
|
||||||
|
|
||||||
|
def simplify(l):
|
||||||
|
if not isinstance(l, list):
|
||||||
|
return l
|
||||||
|
if len(l) == 1:
|
||||||
|
return simplify(l[0])
|
||||||
|
else:
|
||||||
|
return [simplify(i) for i in l]
|
||||||
|
|
||||||
|
def toprefix(l):
|
||||||
|
if not isinstance(l, list):
|
||||||
|
return l
|
||||||
|
op=l[1]
|
||||||
|
args=[toprefix(i) for i in l if i!=op]
|
||||||
|
return [op]+args
|
||||||
|
|
||||||
|
def prioritize(l):
|
||||||
|
if not isinstance(l, list):
|
||||||
|
return l
|
||||||
|
l=simplify(l)
|
||||||
|
for c in ['!=', '=', '&', '|']:
|
||||||
|
i=0
|
||||||
|
while i<len(l):
|
||||||
|
if l[i] == c:
|
||||||
|
tmp=[prioritize(l[i-1]), l[i], prioritize(l[i+1])]
|
||||||
|
l[i-1]=tmp
|
||||||
|
del(l[i+1])
|
||||||
|
del(l[i])
|
||||||
|
i-=1
|
||||||
|
i+=1
|
||||||
|
return simplify(l)
|
||||||
|
|
||||||
|
def collapse(l):
|
||||||
|
"""retire les parentheses inutiles"""
|
||||||
|
if not isinstance(l, list):
|
||||||
|
return l
|
||||||
|
if not isinstance(l[1], list):
|
||||||
|
return [l[0], collapse(l[1]), collapse(l[2])]
|
||||||
|
if l[0] == l[1][0]:
|
||||||
|
return [l[0], collapse(l[1][1]), collapse(l[1][2]), collapse(l[2])]
|
||||||
|
else:
|
||||||
|
return [l[0], collapse(l[1]), collapse(l[2])]
|
||||||
|
|
||||||
|
|
||||||
|
def toldapfilter(l):
|
||||||
|
if not isinstance(l, list):
|
||||||
|
return l
|
||||||
|
op=l[0]
|
||||||
|
if op == "=":
|
||||||
|
return "%s=%s" % (l[1], l[2])
|
||||||
|
elif op == "!=":
|
||||||
|
return "!(%s=%s)" % (l[1], l[2])
|
||||||
|
return op + ''.join(['(%s)' % toldapfilter(i) for i in l[1:]])
|
||||||
|
|
||||||
|
|
||||||
|
txt = "".join(c for c in pyparsing.printables if c not in '()&|!=')
|
||||||
|
expr = pyparsing.nestedExpr("(", ")", pyparsing.Word(txt) | pyparsing.oneOf("& | != ="))
|
||||||
|
|
||||||
|
def human_to_ldap(data):
|
||||||
|
data='(%s)' % data
|
||||||
|
l=expr.parseString(data).asList()
|
||||||
|
return "(%s)" % toldapfilter(collapse(toprefix(prioritize(l))))
|
||||||
|
|
48
filter3.py
Normal file
48
filter3.py
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
"""Plus joli"""
|
||||||
|
|
||||||
|
import pyparsing
|
||||||
|
|
||||||
|
txt = pyparsing.Word("".join(c for c in pyparsing.printables if c not in '=()|&'),exact=1)
|
||||||
|
|
||||||
|
ne = pyparsing.Literal('!=')
|
||||||
|
eq = pyparsing.Literal('=')
|
||||||
|
andop = pyparsing.oneOf('&')
|
||||||
|
orop = pyparsing.oneOf('|')
|
||||||
|
|
||||||
|
expr = pyparsing.operatorPrecedence( txt,
|
||||||
|
[
|
||||||
|
(ne, 2, pyparsing.opAssoc.RIGHT),
|
||||||
|
(eq, 2, pyparsing.opAssoc.RIGHT),
|
||||||
|
(andop, 2, pyparsing.opAssoc.LEFT),
|
||||||
|
(orop, 2, pyparsing.opAssoc.LEFT),]
|
||||||
|
)
|
||||||
|
|
||||||
|
def simplify(l):
|
||||||
|
if not isinstance(l, list):
|
||||||
|
return l
|
||||||
|
if len(l) == 1:
|
||||||
|
return simplify(l[0])
|
||||||
|
else:
|
||||||
|
return [simplify(i) for i in l]
|
||||||
|
|
||||||
|
def toprefix(l):
|
||||||
|
if not isinstance(l, list):
|
||||||
|
return l
|
||||||
|
op=l[1]
|
||||||
|
args=[toprefix(i) for i in l if i!=op]
|
||||||
|
return [op]+args
|
||||||
|
|
||||||
|
def toldapfilter(l):
|
||||||
|
if not isinstance(l, list):
|
||||||
|
return l
|
||||||
|
op=l[0]
|
||||||
|
if op == "=":
|
||||||
|
return "%s=%s" % (l[1], l[2])
|
||||||
|
elif op == "!=":
|
||||||
|
return "!(%s=%s)" % (l[1], l[2])
|
||||||
|
return op + ''.join(['(%s)' % toldapfilter(i) for i in l[1:]])
|
||||||
|
|
||||||
|
def human_to_ldap(str):
|
||||||
|
return "(%s)" % toldapfilter(toprefix(simplify(expr.parseString(str).asList())))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue