def to_dict(self,include_node_id=False,no_attributes=False,track_namespaces=False):
flat_result = []<EOL>def make_ns_slug(name_counter,slug='<STR_LIT:n>'):<EOL><INDENT>while "<STR_LIT>" % (slug,name_counter['<STR_LIT>']) in namespace_mapping.values():<EOL><INDENT>name_counter['<STR_LIT>']  = name_counter['<STR_LIT>']+<NUM_LIT:1><EOL><DEDENT>return "<STR_LIT>" % (slug,name_counter['<STR_LIT>'])<EOL><DEDENT>name_counter = {'<STR_LIT>':<NUM_LIT:0>}<EOL>if track_namespaces:<EOL><INDENT>fact_thrus = self.fact_thru.all().prefetch_related(<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>fact_thrus = self.fact_thru.all().prefetch_related(<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>',<EOL>'<STR_LIT>')<EOL><DEDENT>export_ns_dict = {}<EOL>namespace_mapping=  {"<STR_LIT>" % (self.iobject_type.namespace.uri,self.iobject_type_revision): '<STR_LIT>'}<EOL>for fact_thru in fact_thrus:<EOL><INDENT>value_list = []<EOL>first = True<EOL>fact_datatype_name = None<EOL>fact_datatype_ns = None<EOL>fact_dict = {'<STR_LIT>': fact_thru.node_id.name,<EOL>'<STR_LIT>': fact_thru.fact.fact_term.term,<EOL>'<STR_LIT>' : fact_thru.fact.fact_term.attribute,<EOL>'<STR_LIT>' : fact_thru.namespace_map,<EOL>}<EOL>for fact_value in fact_thru.fact.fact_values.all():<EOL><INDENT>if first:<EOL><INDENT>first=False<EOL>fact_datatype_name = fact_value.fact_data_type.name<EOL>fact_datatype_ns = fact_value.fact_data_type.namespace.uri<EOL>if (fact_datatype_name == DINGOS_DEFAULT_FACT_DATATYPE and<EOL>fact_datatype_ns == DINGOS_NAMESPACE_URI) or fact_thru.fact.value_iobject_id:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>if not fact_datatype_ns in namespace_mapping:<EOL><INDENT>namespace_slug = make_ns_slug(name_counter)<EOL>namespace_mapping[fact_datatype_ns] = namespace_slug<EOL><DEDENT>else:<EOL><INDENT>namespace_slug= namespace_mapping[fact_datatype_ns]<EOL><DEDENT>fact_dict['<STR_LIT>'] = '<STR_LIT>' % (namespace_slug,fact_datatype_name)<EOL><DEDENT><DEDENT>value_list.append(fact_value.value)<EOL><DEDENT>fact_dict['<STR_LIT>'] = value_list<EOL>if fact_thru.fact.value_iobject_id:<EOL><INDENT>value_iobject_id_ns = fact_thru.fact.value_iobject_id.namespace.uri<EOL>if not value_iobject_id_ns in namespace_mapping:<EOL><INDENT>namespace_slug = make_ns_slug(name_counter)<EOL>namespace_mapping[value_iobject_id_ns] = namespace_slug<EOL><DEDENT>else:<EOL><INDENT>namespace_slug= namespace_mapping[value_iobject_id_ns]<EOL><DEDENT>value_iobject_id  =fact_thru.fact.value_iobject_id.uid<EOL>if fact_dict['<STR_LIT>']:<EOL><INDENT>fact_dict['<STR_LIT>'] = ["<STR_LIT>" % (namespace_slug,value_iobject_id)]<EOL><DEDENT>else:<EOL><INDENT>fact_dict['<STR_LIT>'] = "<STR_LIT>" % (namespace_slug,value_iobject_id)<EOL><DEDENT><DEDENT>flat_result.append(fact_dict)<EOL><DEDENT>result = DingoObjDict()<EOL>result.from_flat_repr(flat_result,<EOL>include_node_id=include_node_id,<EOL>no_attributes=no_attributes,<EOL>track_namespaces=track_namespaces,<EOL>namespace_mapping=namespace_mapping<EOL>)<EOL>if not no_attributes:<EOL><INDENT>if not track_namespaces:<EOL><INDENT>result['<STR_LIT>'] = self.iobject_type.name<EOL>result['<STR_LIT>'] = self.iobject_type.namespace.uri<EOL>return result<EOL><DEDENT>else:<EOL><INDENT>result['<STR_LIT>'] = namespace_mapping["<STR_LIT>" % (self.iobject_type.namespace.uri,self.iobject_type_revision)]<EOL>return {'<STR_LIT>': dict(map(lambda x : (x[<NUM_LIT:1>],x[<NUM_LIT:0>]), namespace_mapping.items())),<EOL>'<STR_LIT>' : [result]<EOL>}<EOL><DEDENT><DEDENT>else:<EOL><INDENT>return result<EOL><DEDENT>
This function is currently geared very much towards writing
STIX/CybOX objects to a dictionary. That should not be the case -- the function
needs to be generic just as the from_dict function.
TODO: make function generic.