Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

############################################################################## 

# 

# Copyright (c) 2012 Jens Vagelpohl and Contributors. All Rights Reserved. 

# 

# This software is subject to the provisions of the Zope Public License, 

# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. 

# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED 

# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 

# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS 

# FOR A PARTICULAR PURPOSE. 

# 

############################################################################## 

 

import six 

 

from dataflake.fakeldap.utils import from_utf8 

 

 

class Filter(object): 

""" A simple representation for search filter elements 

""" 

 

def __init__(self, attr, comp, value): 

self.attr = attr 

self.comp = comp 

self.value = value.strip() 

 

def __repr__(self): 

repr_template = "Filter('%s', '%s', '%s')" 

if six.PY2: 

return repr_template % (self.attr, self.comp, self.value) 

else: 

return repr_template % (from_utf8(self.attr), 

from_utf8(self.comp), 

from_utf8(self.value)) 

 

def __eq__(self, other): 

v1 = (self.attr.lower(), self.comp, self.value) 

v2 = (other.attr.lower(), other.comp, other.value) 

return v1 == v2 

 

def __lt__(self, other): 

v1 = (self.attr.lower(), self.comp, self.value) 

v2 = (other.attr.lower(), other.comp, other.value) 

return v1 < v2 

 

def __hash__(self): 

return id(self) 

 

def __call__(self, tree_pos, base): 

res = [] 

query_value = self.value[:] 

wildcard = False 

 

if six.PY2 and isinstance(base, six.text_type): 

base = base.encode('UTF-8') 

 

if query_value.startswith(b'*') or query_value.endswith(b'*'): 

if query_value != b'*': 

# Wildcard search 

if query_value.startswith(b'*') and query_value.endswith(b'*'): 

wildcard = 'both' 

query_value = query_value[1:-1] 

elif query_value.startswith(b'*'): 

wildcard = 'start' 

query_value = query_value[1:] 

elif query_value.endswith(b'*'): 

wildcard = 'end' 

query_value = query_value[:-1] 

 

for rdn, record in tree_pos.items(): 

found = True 

 

if self.attr in record: 

if query_value == b'*': 

# Always include if there's a value for it. 

pass 

elif wildcard: 

found = False 

for x in record[self.attr]: 

if wildcard == 'start': 

if x.endswith(query_value): 

found = True 

break 

elif wildcard == 'end': 

if x.startswith(query_value): 

found = True 

break 

else: 

if query_value in x: 

found = True 

break 

elif query_value not in record[self.attr]: 

found = False 

 

if found: 

if base.startswith(rdn): 

dn = base 

else: 

dn = b'%s,%s' % (rdn, base) 

res.append((dn, record)) 

 

return res