CybOXProject / python-cybox

A Python library for parsing, manipulating, and generating CybOX content.
http://cybox.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
77 stars 42 forks source link

AttributeError: 'MeasureSource' object has no attribute 'export' #225

Open kkuehl opened 9 years ago

kkuehl commented 9 years ago

I am attempting to add Observable_Package_Source.

            source = MeasureSource()
            source.name = "Automatically Generated SCI NAME"
            source.description = "Automatic Description"
            contributors = Personnel()
            contrib = Contributor()
            contrib.name = "Support"
            contributors.append(contrib)
            source.contributors = contributors
            tools = ToolInformationList()
            tool = ToolInformation()
            tool.name = "Test"
            tool.version = "2.2.0"
            tools.append(tool)
            source.tools = tools
            observables.observable_package_source = source
            observables.set_Observable_Package_Source(source)
            # debug print
            print "This: %s" % observables.get_Observable_Package_Source().to_xml()
            outfile = open(outfilename, 'w')
            outfile.write('<?xml version="1.0" encoding="utf-8"?>\n')
            nsparser = NamespaceParser(observables.get_Observable())
            ns_string = nsparser.build_namespaces_schemalocations_str()
            observables.export(outfile, 0, namespacedef_=ns_string)

File "/usr/lib/python2.7/site-packages/cybox-2.0.1.2-py2.7.egg/cybox/bindings/cybox_core.py", line 912, in exportChildren self.Observable_PackageSource.export(outfile, level, "cybox:", name='Observable_Package_Source', pretty_print=pretty_print) AttributeError: 'MeasureSource' object has no attribute 'export'

Am I adding the Observable_Package_Source incorrectly?

...

gtback commented 9 years ago

Hey, @kkuehl, sorry for the (woefully) late response.

It looks like you might be mixing "bindings" objects and "API" objects. Most bindings objects have class names ending in Type, and you manipulate them by calling get_X or set_X. API classes do not end in Type, and you manipulate them by directly getting or setting attributes. It looks like "observables" is a binding object (cybox.bindings.cybox_core.ObservablesType), while source is an API object cybox.common.MeasureSource.

You can get around this by changing:

observables.set_Observable_Package_Source(source)

to

observables.set_Observable_Package_Source(source.to_obj())

but you might be better off using the API version: cybox.core.Observables. API objects were designed to be easier to use than the bindings objects, which are defined in auto-generated code. to_obj converts an API object to a binding object, and you can do the reverse with Observables.from_obj(<binding_obj>)

For a larger discussion of API objects vs. Binding objects (in the context of STIX, but the concept is identical), see http://stix.readthedocs.org/en/latest/api_vs_bindings/index.html