Skip to content

Commit 8692df7

Browse files
author
stroeder
committed
PEP-8 for ldap.filter
1 parent 637daad commit 8692df7

File tree

1 file changed

+47
-44
lines changed

1 file changed

+47
-44
lines changed

Lib/ldap/filter.py

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,63 @@
11
"""
2-
filters.py - misc stuff for handling LDAP filter strings (see RFC2254)
2+
filters.py - misc stuff for handling LDAP filter strings (see RFC 4515)
33
4-
See https://www.python-ldap.org/ for details.
5-
6-
Compability:
7-
- Tested with Python 2.0+
4+
See https://www.python-ldap.org/ for details
85
"""
96

7+
import time
8+
109
from ldap import __version__
1110

1211
from ldap.functions import strf_secs
1312

14-
import time
15-
1613

17-
def escape_filter_chars(assertion_value,escape_mode=0):
18-
"""
19-
Replace all special characters found in assertion_value
20-
by quoted notation.
14+
def escape_filter_chars(assertion_value, escape_mode=0):
15+
"""
16+
Replace all special characters found in assertion_value
17+
by quoted notation.
2118
22-
escape_mode
23-
If 0 only special chars mentioned in RFC 4515 are escaped.
24-
If 1 all NON-ASCII chars are escaped.
25-
If 2 all chars are escaped.
26-
"""
27-
if escape_mode:
28-
r = []
29-
if escape_mode==1:
30-
for c in assertion_value:
31-
if c < '0' or c > 'z' or c in "\\*()":
32-
c = "\\%02x" % ord(c)
33-
r.append(c)
34-
elif escape_mode==2:
35-
for c in assertion_value:
36-
r.append("\\%02x" % ord(c))
19+
escape_mode
20+
If 0 only special chars mentioned in RFC 4515 are escaped.
21+
If 1 all NON-ASCII chars are escaped.
22+
If 2 all chars are escaped.
23+
"""
24+
if escape_mode:
25+
res = []
26+
if escape_mode == 1:
27+
for char in assertion_value:
28+
if char < '0' or char > 'z' or char in "\\*()":
29+
char = "\\%02x" % ord(char)
30+
res.append(char)
31+
elif escape_mode == 2:
32+
for char in assertion_value:
33+
res.append("\\%02x" % ord(char))
34+
else:
35+
raise ValueError('escape_mode must be 0, 1 or 2.')
36+
val = ''.join(res)
3737
else:
38-
raise ValueError('escape_mode must be 0, 1 or 2.')
39-
s = ''.join(r)
40-
else:
41-
s = assertion_value.replace('\\', r'\5c')
42-
s = s.replace(r'*', r'\2a')
43-
s = s.replace(r'(', r'\28')
44-
s = s.replace(r')', r'\29')
45-
s = s.replace('\x00', r'\00')
46-
return s
38+
val = assertion_value.replace(
39+
'\\', r'\5c'
40+
).replace(
41+
r'*', r'\2a'
42+
).replace(
43+
r'(', r'\28'
44+
).replace(
45+
r')', r'\29'
46+
).replace(
47+
'\x00', r'\00'
48+
)
49+
return val
4750

4851

49-
def filter_format(filter_template,assertion_values):
50-
"""
51-
filter_template
52-
String containing %s as placeholder for assertion values.
53-
assertion_values
54-
List or tuple of assertion values. Length must match
55-
count of %s in filter_template.
56-
"""
57-
return filter_template % (tuple(map(escape_filter_chars,assertion_values)))
52+
def filter_format(filter_template, assertion_values):
53+
"""
54+
filter_template
55+
String containing %s as placeholder for assertion values.
56+
assertion_values
57+
List or tuple of assertion values. Length must match
58+
count of %s in filter_template.
59+
"""
60+
return filter_template % (tuple(map(escape_filter_chars, assertion_values)))
5861

5962

6063
def time_span_filter(

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy