Rename core.py -> parsing.py

master
Chris Lenk 2020-03-27 05:53:39 -04:00
parent 01ba190525
commit 50df6f1474
13 changed files with 42 additions and 42 deletions

View File

@ -23,7 +23,6 @@
DEFAULT_VERSION = '2.0' # Default version will always be the latest STIX 2.X version DEFAULT_VERSION = '2.0' # Default version will always be the latest STIX 2.X version
from .confidence import scales from .confidence import scales
from .core import _collect_stix2_mappings, parse, parse_observable
from .datastore import CompositeDataSource from .datastore import CompositeDataSource
from .datastore.filesystem import ( from .datastore.filesystem import (
FileSystemSink, FileSystemSource, FileSystemStore, FileSystemSink, FileSystemSource, FileSystemStore,
@ -38,6 +37,7 @@ from .markings import (
add_markings, clear_markings, get_markings, is_marked, remove_markings, add_markings, clear_markings, get_markings, is_marked, remove_markings,
set_markings, set_markings,
) )
from .parsing import _collect_stix2_mappings, parse, parse_observable
from .patterns import ( from .patterns import (
AndBooleanExpression, AndObservationExpression, BasicObjectPathComponent, AndBooleanExpression, AndObservationExpression, BasicObjectPathComponent,
BinaryConstant, BooleanConstant, EqualityComparisonExpression, BinaryConstant, BooleanConstant, EqualityComparisonExpression,

View File

@ -4,7 +4,7 @@ import re
import six import six
from .base import _cls_init from .base import _cls_init
from .core import ( from .parsing import (
_register_marking, _register_object, _register_observable, _register_marking, _register_object, _register_observable,
_register_observable_extension, _register_observable_extension,
) )

View File

@ -10,11 +10,11 @@ import six
from stix2 import v20, v21 from stix2 import v20, v21
from stix2.base import _STIXBase from stix2.base import _STIXBase
from stix2.core import parse
from stix2.datastore import ( from stix2.datastore import (
DataSink, DataSource, DataSourceError, DataStoreMixin, DataSink, DataSource, DataSourceError, DataStoreMixin,
) )
from stix2.datastore.filters import Filter, FilterSet, apply_common_filters from stix2.datastore.filters import Filter, FilterSet, apply_common_filters
from stix2.parsing import parse
from stix2.utils import format_datetime, get_type_from_id from stix2.utils import format_datetime, get_type_from_id

View File

@ -7,9 +7,9 @@ import os
from stix2 import v20, v21 from stix2 import v20, v21
from stix2.base import _STIXBase from stix2.base import _STIXBase
from stix2.core import parse
from stix2.datastore import DataSink, DataSource, DataStoreMixin from stix2.datastore import DataSink, DataSource, DataStoreMixin
from stix2.datastore.filters import FilterSet, apply_common_filters from stix2.datastore.filters import FilterSet, apply_common_filters
from stix2.parsing import parse
def _add(store, stix_data, allow_custom=True, version=None): def _add(store, stix_data, allow_custom=True, version=None):

View File

@ -4,11 +4,11 @@ from requests.exceptions import HTTPError
from stix2 import v20, v21 from stix2 import v20, v21
from stix2.base import _STIXBase from stix2.base import _STIXBase
from stix2.core import parse
from stix2.datastore import ( from stix2.datastore import (
DataSink, DataSource, DataSourceError, DataStoreMixin, DataSink, DataSource, DataSourceError, DataStoreMixin,
) )
from stix2.datastore.filters import Filter, FilterSet, apply_common_filters from stix2.datastore.filters import Filter, FilterSet, apply_common_filters
from stix2.parsing import parse
from stix2.utils import deduplicate from stix2.utils import deduplicate
try: try:

View File

@ -4,8 +4,8 @@ import copy
import logging import logging
import time import time
from .core import parse as _parse
from .datastore import CompositeDataSource, DataStoreMixin from .datastore import CompositeDataSource, DataStoreMixin
from .parsing import parse as _parse
from .utils import STIXdatetime, parse_into_datetime from .utils import STIXdatetime, parse_into_datetime
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

View File

@ -12,11 +12,11 @@ from six import string_types, text_type
import stix2 import stix2
from .base import _STIXBase from .base import _STIXBase
from .core import STIX2_OBJ_MAPS, parse, parse_observable
from .exceptions import ( from .exceptions import (
CustomContentError, DictionaryKeyError, MissingPropertiesError, CustomContentError, DictionaryKeyError, MissingPropertiesError,
MutuallyExclusivePropertiesError, MutuallyExclusivePropertiesError,
) )
from .parsing import STIX2_OBJ_MAPS, parse, parse_observable
from .utils import _get_dict, get_class_hierarchy_names, parse_into_datetime from .utils import _get_dict, get_class_hierarchy_names, parse_into_datetime
try: try:

View File

@ -2,7 +2,7 @@ from __future__ import unicode_literals
import pytest import pytest
from stix2.core import _detect_spec_version from stix2.parsing import _detect_spec_version
@pytest.mark.parametrize( @pytest.mark.parametrize(

View File

@ -1,7 +1,7 @@
import pytest import pytest
import stix2 import stix2
from stix2 import core, exceptions from stix2 import exceptions, parsing
from .constants import IDENTITY_ID from .constants import IDENTITY_ID
@ -46,14 +46,14 @@ BUNDLE = {
def test_dict_to_stix2_bundle_with_version(): def test_dict_to_stix2_bundle_with_version():
with pytest.raises(exceptions.ExtraPropertiesError) as excinfo: with pytest.raises(exceptions.ExtraPropertiesError) as excinfo:
core.dict_to_stix2(BUNDLE, version='2.1') parsing.dict_to_stix2(BUNDLE, version='2.1')
assert str(excinfo.value) == "Unexpected properties for Bundle: (spec_version)." assert str(excinfo.value) == "Unexpected properties for Bundle: (spec_version)."
def test_parse_observable_with_version(): def test_parse_observable_with_version():
observable = {"type": "file", "name": "foo.exe"} observable = {"type": "file", "name": "foo.exe"}
obs_obj = core.parse_observable(observable, version='2.0') obs_obj = parsing.parse_observable(observable, version='2.0')
v = 'v20' v = 'v20'
assert v in str(obs_obj.__class__) assert v in str(obs_obj.__class__)
@ -62,38 +62,38 @@ def test_parse_observable_with_version():
@pytest.mark.xfail(reason="The default version is no longer 2.0", condition=stix2.DEFAULT_VERSION != "2.0") @pytest.mark.xfail(reason="The default version is no longer 2.0", condition=stix2.DEFAULT_VERSION != "2.0")
def test_parse_observable_with_no_version(): def test_parse_observable_with_no_version():
observable = {"type": "file", "name": "foo.exe"} observable = {"type": "file", "name": "foo.exe"}
obs_obj = core.parse_observable(observable) obs_obj = parsing.parse_observable(observable)
v = 'v20' v = 'v20'
assert v in str(obs_obj.__class__) assert v in str(obs_obj.__class__)
def test_register_object_with_version(): def test_register_object_with_version():
bundle = core.dict_to_stix2(BUNDLE, version='2.0') bundle = parsing.dict_to_stix2(BUNDLE, version='2.0')
core._register_object(bundle.objects[0].__class__, version='2.0') parsing._register_object(bundle.objects[0].__class__, version='2.0')
v = 'v20' v = 'v20'
assert bundle.objects[0].type in core.STIX2_OBJ_MAPS[v]['objects'] assert bundle.objects[0].type in parsing.STIX2_OBJ_MAPS[v]['objects']
# spec_version is not in STIX 2.0, and is required in 2.1, so this # spec_version is not in STIX 2.0, and is required in 2.1, so this
# suffices as a test for a STIX 2.0 object. # suffices as a test for a STIX 2.0 object.
assert "spec_version" not in bundle.objects[0] assert "spec_version" not in bundle.objects[0]
def test_register_marking_with_version(): def test_register_marking_with_version():
core._register_marking(stix2.v20.TLP_WHITE.__class__, version='2.0') parsing._register_marking(stix2.v20.TLP_WHITE.__class__, version='2.0')
v = 'v20' v = 'v20'
assert stix2.v20.TLP_WHITE.definition._type in core.STIX2_OBJ_MAPS[v]['markings'] assert stix2.v20.TLP_WHITE.definition._type in parsing.STIX2_OBJ_MAPS[v]['markings']
assert v in str(stix2.v20.TLP_WHITE.__class__) assert v in str(stix2.v20.TLP_WHITE.__class__)
@pytest.mark.xfail(reason="The default version is no longer 2.0", condition=stix2.DEFAULT_VERSION != "2.0") @pytest.mark.xfail(reason="The default version is no longer 2.0", condition=stix2.DEFAULT_VERSION != "2.0")
def test_register_marking_with_no_version(): def test_register_marking_with_no_version():
# Uses default version (2.0 in this case) # Uses default version (2.0 in this case)
core._register_marking(stix2.v20.TLP_WHITE.__class__) parsing._register_marking(stix2.v20.TLP_WHITE.__class__)
v = 'v20' v = 'v20'
assert stix2.v20.TLP_WHITE.definition._type in core.STIX2_OBJ_MAPS[v]['markings'] assert stix2.v20.TLP_WHITE.definition._type in parsing.STIX2_OBJ_MAPS[v]['markings']
assert v in str(stix2.v20.TLP_WHITE.__class__) assert v in str(stix2.v20.TLP_WHITE.__class__)
@ -128,10 +128,10 @@ def test_register_observable_with_version():
}, },
}, },
) )
core._register_observable(observed_data.objects['0'].__class__, version='2.0') parsing._register_observable(observed_data.objects['0'].__class__, version='2.0')
v = 'v20' v = 'v20'
assert observed_data.objects['0'].type in core.STIX2_OBJ_MAPS[v]['observables'] assert observed_data.objects['0'].type in parsing.STIX2_OBJ_MAPS[v]['observables']
assert v in str(observed_data.objects['0'].__class__) assert v in str(observed_data.objects['0'].__class__)
@ -166,11 +166,11 @@ def test_register_observable_extension_with_version():
}, },
}, },
) )
core._register_observable_extension(observed_data.objects['0'], observed_data.objects['0'].extensions['ntfs-ext'].__class__, version='2.0') parsing._register_observable_extension(observed_data.objects['0'], observed_data.objects['0'].extensions['ntfs-ext'].__class__, version='2.0')
v = 'v20' v = 'v20'
assert observed_data.objects['0'].type in core.STIX2_OBJ_MAPS[v]['observables'] assert observed_data.objects['0'].type in parsing.STIX2_OBJ_MAPS[v]['observables']
assert v in str(observed_data.objects['0'].__class__) assert v in str(observed_data.objects['0'].__class__)
assert observed_data.objects['0'].extensions['ntfs-ext']._type in core.STIX2_OBJ_MAPS[v]['observable-extensions']['file'] assert observed_data.objects['0'].extensions['ntfs-ext']._type in parsing.STIX2_OBJ_MAPS[v]['observable-extensions']['file']
assert v in str(observed_data.objects['0'].extensions['ntfs-ext'].__class__) assert v in str(observed_data.objects['0'].extensions['ntfs-ext'].__class__)

View File

@ -967,7 +967,7 @@ def test_register_custom_object():
class CustomObject2(object): class CustomObject2(object):
_type = 'awesome-object' _type = 'awesome-object'
stix2.core._register_object(CustomObject2, version="2.0") stix2.parsing._register_object(CustomObject2, version="2.0")
# Note that we will always check against newest OBJ_MAP. # Note that we will always check against newest OBJ_MAP.
assert (CustomObject2._type, CustomObject2) in stix2.v20.OBJ_MAP.items() assert (CustomObject2._type, CustomObject2) in stix2.v20.OBJ_MAP.items()

View File

@ -1,7 +1,7 @@
import pytest import pytest
import stix2 import stix2
from stix2 import core, exceptions from stix2 import exceptions, parsing
from .constants import IDENTITY_ID, OBSERVED_DATA_ID from .constants import IDENTITY_ID, OBSERVED_DATA_ID
@ -50,7 +50,7 @@ BUNDLE = {
def test_dict_to_stix2_bundle_with_version(): def test_dict_to_stix2_bundle_with_version():
with pytest.raises(exceptions.InvalidValueError) as excinfo: with pytest.raises(exceptions.InvalidValueError) as excinfo:
core.dict_to_stix2(BUNDLE, version='2.0') parsing.dict_to_stix2(BUNDLE, version='2.0')
msg = "Invalid value for Bundle 'objects': Spec version 2.0 bundles don't yet support containing objects of a different spec version." msg = "Invalid value for Bundle 'objects': Spec version 2.0 bundles don't yet support containing objects of a different spec version."
assert str(excinfo.value) == msg assert str(excinfo.value) == msg
@ -58,7 +58,7 @@ def test_dict_to_stix2_bundle_with_version():
def test_parse_observable_with_version(): def test_parse_observable_with_version():
observable = {"type": "file", "name": "foo.exe"} observable = {"type": "file", "name": "foo.exe"}
obs_obj = core.parse_observable(observable, version='2.1') obs_obj = parsing.parse_observable(observable, version='2.1')
v = 'v21' v = 'v21'
assert v in str(obs_obj.__class__) assert v in str(obs_obj.__class__)
@ -67,36 +67,36 @@ def test_parse_observable_with_version():
@pytest.mark.xfail(reason="The default version is not 2.1", condition=stix2.DEFAULT_VERSION != "2.1") @pytest.mark.xfail(reason="The default version is not 2.1", condition=stix2.DEFAULT_VERSION != "2.1")
def test_parse_observable_with_no_version(): def test_parse_observable_with_no_version():
observable = {"type": "file", "name": "foo.exe"} observable = {"type": "file", "name": "foo.exe"}
obs_obj = core.parse_observable(observable) obs_obj = parsing.parse_observable(observable)
v = 'v21' v = 'v21'
assert v in str(obs_obj.__class__) assert v in str(obs_obj.__class__)
def test_register_object_with_version(): def test_register_object_with_version():
bundle = core.dict_to_stix2(BUNDLE, version='2.1') bundle = parsing.dict_to_stix2(BUNDLE, version='2.1')
core._register_object(bundle.objects[0].__class__) parsing._register_object(bundle.objects[0].__class__)
v = 'v21' v = 'v21'
assert bundle.objects[0].type in core.STIX2_OBJ_MAPS[v]['objects'] assert bundle.objects[0].type in parsing.STIX2_OBJ_MAPS[v]['objects']
assert bundle.objects[0].spec_version == "2.1" assert bundle.objects[0].spec_version == "2.1"
def test_register_marking_with_version(): def test_register_marking_with_version():
core._register_marking(stix2.v21.TLP_WHITE.__class__, version='2.1') parsing._register_marking(stix2.v21.TLP_WHITE.__class__, version='2.1')
v = 'v21' v = 'v21'
assert stix2.v21.TLP_WHITE.definition._type in core.STIX2_OBJ_MAPS[v]['markings'] assert stix2.v21.TLP_WHITE.definition._type in parsing.STIX2_OBJ_MAPS[v]['markings']
assert v in str(stix2.v21.TLP_WHITE.__class__) assert v in str(stix2.v21.TLP_WHITE.__class__)
@pytest.mark.xfail(reason="The default version is not 2.1", condition=stix2.DEFAULT_VERSION != "2.1") @pytest.mark.xfail(reason="The default version is not 2.1", condition=stix2.DEFAULT_VERSION != "2.1")
def test_register_marking_with_no_version(): def test_register_marking_with_no_version():
# Uses default version (2.0 in this case) # Uses default version (2.0 in this case)
core._register_marking(stix2.v21.TLP_WHITE.__class__) parsing._register_marking(stix2.v21.TLP_WHITE.__class__)
v = 'v21' v = 'v21'
assert stix2.v21.TLP_WHITE.definition._type in core.STIX2_OBJ_MAPS[v]['markings'] assert stix2.v21.TLP_WHITE.definition._type in parsing.STIX2_OBJ_MAPS[v]['markings']
assert v in str(stix2.v21.TLP_WHITE.__class__) assert v in str(stix2.v21.TLP_WHITE.__class__)
@ -131,10 +131,10 @@ def test_register_observable_with_default_version():
}, },
}, },
) )
core._register_observable(observed_data.objects['0'].__class__) parsing._register_observable(observed_data.objects['0'].__class__)
v = 'v21' v = 'v21'
assert observed_data.objects['0'].type in core.STIX2_OBJ_MAPS[v]['observables'] assert observed_data.objects['0'].type in parsing.STIX2_OBJ_MAPS[v]['observables']
assert v in str(observed_data.objects['0'].__class__) assert v in str(observed_data.objects['0'].__class__)
@ -169,11 +169,11 @@ def test_register_observable_extension_with_default_version():
}, },
}, },
) )
core._register_observable_extension(observed_data.objects['0'], observed_data.objects['0'].extensions['ntfs-ext'].__class__) parsing._register_observable_extension(observed_data.objects['0'], observed_data.objects['0'].extensions['ntfs-ext'].__class__)
v = 'v21' v = 'v21'
assert observed_data.objects['0'].type in core.STIX2_OBJ_MAPS[v]['observables'] assert observed_data.objects['0'].type in parsing.STIX2_OBJ_MAPS[v]['observables']
assert v in str(observed_data.objects['0'].__class__) assert v in str(observed_data.objects['0'].__class__)
assert observed_data.objects['0'].extensions['ntfs-ext']._type in core.STIX2_OBJ_MAPS[v]['observable-extensions']['file'] assert observed_data.objects['0'].extensions['ntfs-ext']._type in parsing.STIX2_OBJ_MAPS[v]['observable-extensions']['file']
assert v in str(observed_data.objects['0'].extensions['ntfs-ext'].__class__) assert v in str(observed_data.objects['0'].extensions['ntfs-ext'].__class__)

View File

@ -1186,7 +1186,7 @@ def test_register_custom_object():
class CustomObject2(object): class CustomObject2(object):
_type = 'awesome-object' _type = 'awesome-object'
stix2.core._register_object(CustomObject2, version="2.1") stix2.parsing._register_object(CustomObject2, version="2.1")
# Note that we will always check against newest OBJ_MAP. # Note that we will always check against newest OBJ_MAP.
assert (CustomObject2._type, CustomObject2) in stix2.v21.OBJ_MAP.items() assert (CustomObject2._type, CustomObject2) in stix2.v21.OBJ_MAP.items()