Skip to content

Commit

Permalink
Cleanup. Added supported_stix_version() method to stix/__init__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Worrell committed Mar 2, 2015
1 parent 2bf9616 commit ffb3425
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 35 deletions.
6 changes: 6 additions & 0 deletions stix/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

__version__ = "1.1.1.3"


def supported_stix_version():
lib_version = __version__
return lib_version[:-2]


from .base import Entity, EntityList, TypedList

# Make sure common gets imported before anything else.
Expand Down
1 change: 1 addition & 0 deletions stix/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

_CONFLICTING_NAMES = keyword.kwlist + ['id', 'type','range']


@contextlib.contextmanager
def ignored(*exceptions):
"""Allows you to ignore exceptions cleanly using context managers. This
Expand Down
91 changes: 57 additions & 34 deletions stix/utils/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,32 @@
# internal
import stix
import stix.xmlconst as xmlconst
from stix.utils import ignored

# relative
from . import ignored


class UnknownVersionError(Exception):
"""Raised when a parsed STIX document contains no version information."""
pass


class UnsupportedVersionError(Exception):
"""Raised when a parsed STIX document contains a version that is
not supported by this verison of python-stix.
"""
def __init__(self, message, expected=None, found=None):
super(UnsupportedVersionError, self).__init__(message)
self.expected = expected
self.found = found


class UnsupportedRootElementError(Exception):
"""Raised when an input STIX document does not contain a supported root-
level element.
"""
def __init__(self, message, expected=None, found=None):
super(UnsupportedRootElementError, self).__init__(message)
self.expected = expected
Expand Down Expand Up @@ -94,6 +105,28 @@ def get_schemaloc_pairs(node):
return zip(l[::2], l[1::2])


def get_document_version(doc):
root = get_etree_root(doc)

if 'version' in root.attrib:
return root.attrib['version']

raise UnknownVersionError(
"Unable to determine the version if the input STIX document: no "
"version attribute found on the root element."
)


def root_tag(doc):
root = get_etree_root(doc)
return root.tag


def is_stix(doc):
root = root_tag(doc)
return root == xmlconst.TAG_STIX_PACKAGE


class EntityParser(object):
def __init__(self):
pass
Expand All @@ -103,43 +136,33 @@ def _check_version(self, tree):
by python-stix.
"""
root = get_etree_root(tree)
document_version = get_document_version(tree)
supported = stix.supported_stix_version()

if 'version' not in root.attrib:
raise UnknownVersionError(
"No version attribute set on root STIX_Package element. "
"Unable to determine version compatibility with python-stix."
)

python_stix_version = stix.__version__ # ex: '1.1.0.0'
supported_stix_version = python_stix_version[:-2] # ex: '1.1.0'
document_version = root.attrib['version']

if StrictVersion(supported_stix_version) != StrictVersion(document_version):
error = (
"Your python-stix library supports STIX %s. Document "
"version was %s" % (supported_stix_version, document_version),
)
raise UnsupportedVersionError(
message=error,
expected=supported_stix_version,
found=document_version
)

return True
if StrictVersion(supported) == StrictVersion(document_version):
return

def _check_root(self, tree):
root = get_etree_root(tree)
expected_tag = "{http://stix.mitre.org/stix-1}STIX_Package"
error = (
"Your python-stix library supports STIX %s. Document version was "
"%s" % (supported, document_version)
)

if root.tag != expected_tag:
raise UnsupportedRootElement(
"Document root element must be an instance of STIX_Package",
expected=expected_tag,
found=root.tag
)
raise UnsupportedVersionError(
message=error,
expected=supported,
found=document_version
)

return True
def _check_root(self, tree):
if is_stix(tree):
return

error = "Document root element must be an instance of STIX_Package"
raise UnsupportedRootElement(
message=error,
expected=xmlconst.TAG_STIX_PACKAGE,
found=root_tag(tree),
)

def _apply_input_namespaces(self, tree, entity):
root = get_etree_root(tree)
Expand Down
8 changes: 7 additions & 1 deletion stix/xmlconst.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Copyright (c) 2015, The MITRE Corporation. All rights reserved.
# See LICENSE.txt for complete terms.

# XML NAMESPACES
NS_XSI = "http://www.w3.org/2001/XMLSchema-instance"

# XML TAGS
TAG_XSI_TYPE = "{%s}type" % NS_XSI
TAG_SCHEMALOCATION = "{%s}schemaLocation" % NS_XSI
TAG_SCHEMALOCATION = "{%s}schemaLocation" % NS_XSI

# STIX TAGS
TAG_STIX_PACKAGE = "{http://stix.mitre.org/stix-1}STIX_Package"

0 comments on commit ffb3425

Please sign in to comment.