Build filter function map

stix2.1
Greg Back 2017-08-31 18:03:12 +00:00
parent 71d42b0e51
commit 7b46283a5c
5 changed files with 17 additions and 37 deletions

View File

@ -20,8 +20,8 @@ import uuid
from six import iteritems
from filters import (FILTER_OPS, FILTER_VALUE_TYPES, STIX_COMMON_FIELDS,
STIX_COMMON_FILTERS_MAP)
from stix2.sources.filters import (FILTER_OPS, FILTER_VALUE_TYPES,
STIX_COMMON_FIELDS, STIX_COMMON_FILTERS_MAP)
def make_id():
@ -273,7 +273,7 @@ class DataSource(object):
clean = False
break
match = STIX_COMMON_FILTERS_MAP[filter_.field](filter_, stix_obj)
match = STIX_COMMON_FILTERS_MAP[filter_.field.split('.')[0]](filter_, stix_obj)
if not match:
clean = False
break

View File

@ -13,8 +13,6 @@ or if cleaner solution possible.
import collections
import types
import filters
# Currently, only STIX 2.0 common SDO fields (that are not complex objects)
# are supported for filtering on
STIX_COMMON_FIELDS = [
@ -180,11 +178,11 @@ def check_labels_filter(filter_, stix_obj):
def check_modified_filter(filter_, stix_obj):
return _timestamp_filter(filter_, stix_obj["created"])
return _timestamp_filter(filter_, stix_obj["modified"])
def check_object_markings_ref_filter(filter_, stix_obj):
for marking_id in stix_obj["object_market_refs"]:
def check_object_marking_refs_filter(filter_, stix_obj):
for marking_id in stix_obj["object_marking_refs"]:
r = _id_filter(filter_, marking_id)
if r:
return r
@ -199,29 +197,8 @@ def check_type_filter(filter_, stix_obj):
return _string_filter(filter_, stix_obj["type"])
# script to collect STIX common field filter
# functions and create mapping to them
"""
MK: I want to build the filter name -> filter function dictionary
dynamically whenever it is imported. By enumerating the functions
in this module, extracting the "check*" functions and making
pointers to them. But having issues getting an interable of the
modules entities. globals() works but returns an active dictionary
so iterating over it is a no go
"""
for entity in dir(filters):
if "check_" in str(entity) and isinstance(filters.__dict__.get(entity), types.FunctionType):
field_name = entity.split("_")[1].split("_")[0]
STIX_COMMON_FILTERS_MAP[field_name] = filters.__dict__.get(entity)
# Tried this to, didnt work ##############
"""
import sys
for entity in dir(sys.modules[__name__]):
print(entity)
if "check_" in str(entity) and type(entity) == "function":
print(sys.modules[__name__].__dict__.get(entity))
STIX_COMMON_FILTERS_MAP[str(entity)] = sys.modules[__name__].__dict__.get(entity)
"""
# Create mapping of field names to filter functions
for name, obj in dict(globals()).items():
if "check_" in name and isinstance(obj, types.FunctionType):
field_name = "_".join(name.split("_")[1:-1])
STIX_COMMON_FILTERS_MAP[field_name] = obj

View File

@ -24,7 +24,8 @@ import os
from stix2validator import validate_string
from stix2 import Bundle
from stix2.sources import DataSink, DataSource, DataStore, Filter
from stix2.sources import DataSink, DataSource, DataStore
from stix2.sources.filters import Filter
class MemoryStore(DataStore):

View File

@ -12,7 +12,8 @@ TODO: Test everything
import json
from stix2.sources import DataSink, DataSource, DataStore, Filter, make_id
from stix2.sources import DataSink, DataSource, DataStore, make_id
from stix2.sources.filters import Filter
TAXII_FILTERS = ['added_after', 'id', 'type', 'version']

View File

@ -2,7 +2,8 @@ import pytest
from taxii2client import Collection
from stix2.sources import (CompositeDataSource, DataSink, DataSource,
DataStore, Filter, make_id, taxii)
DataStore, make_id, taxii)
from stix2.sources.filters import Filter
from stix2.sources.memory import MemorySource
COLLECTION_URL = 'https://example.com/api1/collections/91a7b528-80eb-42ed-a74d-c6fbd5a26116/'