added FilterSet class for internal use; modified certain parsing processes to make deepcopies or suppled values(dicts) so as to taint original user passed data; added Filter logic to handle datetime objects; added/adjusted tests accordingly

stix2.0
= 2018-04-13 12:25:12 -04:00
parent 1a1e5e1616
commit 61e091baf3
2 changed files with 31 additions and 7 deletions

View File

@ -192,25 +192,43 @@ def _check_filter(filter_, stix_obj):
class FilterSet(object): class FilterSet(object):
""" """ """Internal STIX2 class to facilitate the grouping of Filters
into sets. The primary motivation for this class came from the problem
that Filters that had a dict as a value could not be added to a Python
set as dicts are not hashable. Thus this class provides set functionality
but internally stores filters in a list.
"""
def __init__(self, filters=None): def __init__(self, filters=None):
""" """ """
Args:
filters: see FilterSet.add()
"""
self._filters = [] self._filters = []
if filters: if filters:
self.add(filters) self.add(filters)
def __iter__(self): def __iter__(self):
""" """ """provide iteration functionality of FilterSet"""
for f in self._filters: for f in self._filters:
yield f yield f
def __len__(self): def __len__(self):
""" """ """provide built-in len() utility of FilterSet"""
return len(self._filters) return len(self._filters)
def add(self, filters=None): def add(self, filters=None):
""" """ """add a Filter, FilterSet, or list of Filters to the FilterSet
Operates like set, only adding unique stix2.Filters to the FilterSet
NOTE: method designed to be very accomodating (i.e. even accepting filters=None)
as it allows for blind calls (very useful in DataStore)
Args:
filters: stix2.Filter OR list of stix2.Filter OR stix2.FilterSet
"""
if not filters: if not filters:
# so add() can be called blindly, useful for # so add() can be called blindly, useful for
# DataStore/Environment usage of filter operations # DataStore/Environment usage of filter operations
@ -226,7 +244,14 @@ class FilterSet(object):
return return
def remove(self, filters=None): def remove(self, filters=None):
""" """ """remove a Filter, list of Filters, or FilterSet from the FilterSet
NOTE: method designed to be very accomodating (i.e. even accepting filters=None)
as it allows for blind calls (very useful in DataStore)
Args:
filters: stix2.Filter OR list of stix2.Filter or stix2.FilterSet
"""
if not filters: if not filters:
# so remove() can be called blindly, useful for # so remove() can be called blindly, useful for
# DataStore/Environemnt usage of filter ops # DataStore/Environemnt usage of filter ops

View File

@ -569,7 +569,6 @@ def test_filters3():
# "Return any object modified before or on 2017-01-28T13:49:53.935Z" # "Return any object modified before or on 2017-01-28T13:49:53.935Z"
fv = Filter("modified", "<=", parse_into_datetime("2017-01-27T13:49:53.935Z")) fv = Filter("modified", "<=", parse_into_datetime("2017-01-27T13:49:53.935Z"))
print(fv)
resp = list(apply_common_filters(REAL_STIX_OBJS2, [fv])) resp = list(apply_common_filters(REAL_STIX_OBJS2, [fv]))
assert resp[0].id == REAL_STIX_OBJS2[1].id assert resp[0].id == REAL_STIX_OBJS2[1].id
assert len(resp) == 2 assert len(resp) == 2