Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
23c1ea8
A minor change as a result of the mixbox namespaces code revamp.
chisholm Oct 5, 2015
6225a2b
Updated python-maec code to work with 'typedfields' branches in
Oct 14, 2015
978d82e
Updated comparator.py and comparator example to leverage typedfields …
Oct 14, 2015
96171b6
Broken commit. Began refactoring comparator module.
Oct 15, 2015
9ffb3fa
Added comments to deduplicator and changed some string formatting.
Oct 15, 2015
44ab426
Changes to the deduplication code. Mostly style related.
Oct 22, 2015
28f2de5
Removed unused import.
Oct 23, 2015
f711317
package_generation_example.py used old APIs and didn't work.
chisholm Nov 4, 2015
1834373
Change MalwareSubjectList to use TypedField
apsillers Dec 30, 2015
e8444f9
Update EntitiyLists to use mixbox's new single-multifield approach
apsillers Feb 9, 2016
fa837ff
Strong bindings for data markings.
emmanvg Mar 8, 2016
a4f4fc6
Fixed some vocab test issues: a fully defined vocab string in the
chisholm Apr 4, 2016
c72a608
Merge pull request #86 from MAECProject/typedfields-fixes
apsillers Apr 7, 2016
5f642bf
Merge branch 'typedfields' into signals-typefields-integration
chisholm Apr 7, 2016
4f3fef7
Merge branch 'signals-typefields-integration' into namespaces_integra…
chisholm Apr 12, 2016
837ea59
Fixed a typo in ObjectHash.get_hash()
chisholm Apr 14, 2016
6febe6f
Merge branch 'master' into namespaces_integration
clenk Jul 29, 2016
f5e5ced
Require latest versions of mixbox and cybox
clenk Jul 29, 2016
676486c
Add tests for adding collections to bundles, and fix the bugs this fi…
clenk Jul 29, 2016
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions examples/package_generation_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@
from cybox.core import AssociatedObjects, AssociatedObject, Object, AssociationType
from cybox.common import Hash, HashList, VocabString
from cybox.objects.file_object import File
from maec.bundle import Bundle, Collections, MalwareAction, Capability
from maec.bundle import Bundle, MalwareAction, Capability
from maec.package import Analysis, MalwareSubject, Package
from cybox.utils import Namespace
import maec.utils

# Instantiate the ID generator class (for automatic ID generation) with our example namespace
NS = Namespace("http://example.com/", "example")
maec.utils.set_id_namespace(NS)
# Instantiate the Bundle, Package, MalwareSubject, and Analysis classes
bundle = Bundle(defined_subject=False)
package = Package()
Expand Down Expand Up @@ -58,5 +53,5 @@
# Add the Malware Subject to the Package
package.add_malware_subject(subject)
# Export the Package Bindings Object to an XML file and use the namespaceparser for writing out the namespace definitions
package.to_xml_file('sample_maec_package.xml', {"http://example.com/":"example"})
package.to_xml_file('sample_maec_package.xml')
print "Wrote to sample_maec_package.xml"
42 changes: 15 additions & 27 deletions maec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
from __future__ import absolute_import
from mixbox.entities import Entity as cyboxEntity
from mixbox.entities import EntityList
from mixbox.namespaces import (Namespace, get_xmlns_string,
get_schemaloc_string, lookup_name, lookup_prefix)
from mixbox.namespaces import ( get_xmlns_string,
make_namespace_subset_from_uris, get_schemaloc_string, lookup_prefix)
from mixbox.vendor.six import iteritems, string_types

from .bindings import maec_bundle as bundle_binding
from .bindings import maec_package as package_binding
import maec
from maec.utils import flip_dict, EntityParser

Expand Down Expand Up @@ -44,7 +42,7 @@ def to_xml_file(self, file, namespace_dict=None, custom_header=None):
namespace_dict = {}
else:
# Make a copy so we don't pollute the source
namespace_dict = dict(iteritems(namespace_dict))
namespace_dict = namespace_dict.copy()

# Update the namespace dictionary with namespaces found upon import
input_namespaces = self._ns_to_prefix_input_namespaces()
Expand Down Expand Up @@ -84,32 +82,23 @@ def _get_namespace_def(self, additional_ns_dict=None):
# if there are any other namepaces, include xsi for "schemaLocation"
# also, include the MAEC default vocabularies schema by default
if namespaces:
namespaces.update([lookup_prefix('xsi')])
namespaces.update([lookup_prefix('maecVocabs')])
namespaces.add(lookup_prefix('xsi'))
namespaces.add(lookup_prefix('maecVocabs'))

if namespaces and additional_ns_dict:
namespace_list = [x.name for x in namespaces if x]
for ns, prefix in iteritems(additional_ns_dict):
if ns not in namespace_list:
namespaces.update([Namespace(ns, prefix, '')])

if not namespaces:
ns_set = make_namespace_subset_from_uris(namespaces)
if additional_ns_dict:
for ns, prefix in iteritems(additional_ns_dict):
ns_set.add_namespace_uri(ns, prefix)
else:
return ""

namespaces = sorted(namespaces, key=str)

return ('\n\t' + get_xmlns_string(namespaces) +
'\n\txsi:schemaLocation="' + get_schemaloc_string(namespaces) +
'"')
return ('\n\t' + ns_set.get_xmlns_string(sort=True, delim='\n\t') +
'\n\t' + ns_set.get_schemaloc_string(sort=True, delim='\n\t'))

def _get_namespaces(self, recurse=True):
nsset = set()

# Get all _namespaces for parent classes
namespaces = [x._namespace for x in self.__class__.__mro__
if hasattr(x, '_namespace')]

nsset.update([lookup_name(ns) for ns in namespaces])
nsset = set(x._namespace for x in self.__class__.__mro__
if hasattr(x, '_namespace'))

#In case of recursive relationships, don't process this item twice
self.touched = True
Expand All @@ -122,8 +111,7 @@ def _get_namespaces(self, recurse=True):
# Add any additional namespaces that may be included in the entity
input_ns = self._ns_to_prefix_input_namespaces()
for namespace, alias in iteritems(input_ns):
if not lookup_name(namespace):
nsset.add(Namespace(namespace, alias, ''))
nsset.update(namespace)

return nsset

Expand Down
Loading