1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
__all__ = ['Condition'] |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class Condition(object): |
7
|
|
|
''' |
8
|
|
|
class Condition permits to reduce the size of the data to return |
9
|
|
|
by applying a rule of filtering |
10
|
|
|
''' |
11
|
|
|
|
12
|
|
|
def __init__(self, **kwargs): |
13
|
|
|
''' |
14
|
|
|
set the 2 filters type : match and does_not_match |
15
|
|
|
''' |
16
|
|
|
if 'does_not_match' in kwargs: |
17
|
|
|
self.does_not_match = kwargs['does_not_match'] |
18
|
|
|
|
19
|
|
|
if 'match' in kwargs: |
20
|
|
|
self.match = kwargs['match'] |
21
|
|
|
|
22
|
|
|
def check(self, datas, *filers): |
23
|
|
|
''' |
24
|
|
|
this method permits to reduce the quantity of information to read |
25
|
|
|
by applying some filtering |
26
|
|
|
here '*filers' can receive a list of properties to be filtered |
27
|
|
|
''' |
28
|
|
|
# special case : no filter : want to read all the feed |
29
|
|
|
if self.match == "" and self.does_not_match == '': |
30
|
|
|
yield datas |
31
|
|
|
# let's filtering : |
32
|
|
|
else: |
33
|
|
|
condition1 = False |
34
|
|
|
condition2 = False |
35
|
|
|
# arg contain the property from which we want to check the 'data' |
36
|
|
|
for prop in filers: |
37
|
|
|
# check if my datas contains my property |
38
|
|
|
if prop in datas: |
39
|
|
|
# filter to find only this data |
40
|
|
|
if self.match != '' and condition1 is False: |
41
|
|
|
condition1 = self.filter_that(self.match, |
42
|
|
|
datas[prop]) |
43
|
|
|
# filter to exclude this data, |
44
|
|
|
# when found, continue to the next entry |
45
|
|
|
if self.does_not_match != '' and condition2 is False: |
46
|
|
|
condition2 = self.filter_that(self.does_not_match, |
47
|
|
|
datas[prop]) |
48
|
|
|
if condition2: |
49
|
|
|
continue |
50
|
|
|
if condition1 and condition2 is False: |
51
|
|
|
yield datas |
52
|
|
|
|
53
|
|
|
def filter_that(self, criteria, data): |
54
|
|
|
''' |
55
|
|
|
this method just use the module 're' to check if the data contain |
56
|
|
|
the string to find |
57
|
|
|
''' |
58
|
|
|
import re |
59
|
|
|
prog = re.compile(criteria) |
60
|
|
|
|
61
|
|
|
return True if prog.match(data) else False |
62
|
|
|
|