@@ -99,14 +99,52 @@ def remove_part(reference, part):
9999 remove_part (reference [refs ], part )
100100
101101
102- # trim out parts of a data source that don't match part = True
103- def where_parts (reference , part ):
102+ # does the part match the where clause?
103+ # This can be:
104+ # where: key
105+ # In this case, the key lookup value must be True
106+ # or:
107+ # where: key eq|ne|in|notin value(s)
108+ # The key lookup value must equal or not equal the value, or
109+ # must be (not) one of the space-separated values (respectively)
110+ def partMatches (actual , cond , expected ):
111+ if cond is None and expected is None :
112+ return actual # Is it True?
113+ if cond == 'eq' :
114+ return actual == expected
115+ if cond == 'ne' :
116+ return actual != expected
117+ if cond == 'in' :
118+ return actual in expected
119+ if cond == 'notin' :
120+ return actual not in expected
121+ raise Exception (f"Unexpected condition { cond } " ) # pylint: disable=broad-exception-raised
122+
123+ # trim out parts of a data source that don't match the part condition (default == True)
124+ def where_parts (reference , where_clause ):
125+ items = where_clause .split ()
126+ key = items .pop (0 )
127+ if len (items ) == 0 :
128+ cond = None
129+ expected = None
130+ elif len (items ) >= 2 :
131+ cond = items .pop (0 )
132+ if cond in ['ne' , 'eq' ] and len (items ) == 1 : # should only be one item left
133+ expected = items .pop (0 )
134+ elif cond in ['in' , 'notin' ]:
135+ expected = items
136+ else :
137+ # TODO: choose better exception
138+ raise Exception (f"Unexpected condition '{ cond } ' or extra param: { len (items )} " ) # pylint: disable=broad-exception-raised
139+ else :
140+ # TODO: choose better exception
141+ raise Exception ("'where' expects 1 parameter optionally followed by at least 2 parameters" ) # pylint: disable=broad-exception-raised
104142 # currently only works on True parts
105143 # if we trim as we go we invalidate the iterator. Instead create a deletion list.
106144 filtered = [ ]
107145 # first find the list that needs to be trimmed.
108146 for refs in reference :
109- if not reference [refs ][part ] :
147+ if not partMatches ( reference [refs ][key ], cond , expected ) :
110148 filtered .append (refs )
111149 # remove the parts to be trimmed.
112150 for refs in filtered :
0 commit comments