<s> import json <EOL> import collections <EOL> import itertools <EOL> import StringIO <EOL> from mixbox import idgen <EOL> from mixbox . binding_utils import save_encoding <EOL> from mixbox . cache import Cached <EOL> from . import utils <EOL> def _override ( * args , ** kwargs ) : <EOL> raise NotImplementedError ( ) <EOL> class Entity ( object ) : <EOL> """<STR_LIT>""" <EOL> _namespace = None <EOL> _XSI_TYPE = None <EOL> def _collect_ns_info ( self , ns_info = None ) : <EOL> if not ns_info : <EOL> return <EOL> ns_info . collect ( self ) <EOL> def _set_var ( self , klass , try_cast = True , arg = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> name , item = kwargs . iteritems ( ) . next ( ) <EOL> attr = utils . private_name ( name ) <EOL> if item is None : <EOL> setattr ( self , attr , None ) <EOL> elif isinstance ( item , klass ) : <EOL> setattr ( self , attr , item ) <EOL> elif try_cast : <EOL> promoted = utils . cast_var ( item , klass , arg = arg ) <EOL> setattr ( self , attr , promoted ) <EOL> else : <EOL> error = "<STR_LIT>" <EOL> error = error . format ( name , klass , type ( item ) ) <EOL> raise TypeError ( error ) <EOL> def _set_vocab ( self , klass = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> from stix . common import VocabString <EOL> klass = klass or VocabString <EOL> item = kwargs . itervalues ( ) . next ( ) <EOL> if isinstance ( item , VocabString ) : <EOL> self . _set_var ( VocabString , ** kwargs ) <EOL> else : <EOL> self . _set_var ( klass , ** kwargs ) <EOL> def to_obj ( self , return_obj = None , ns_info = None ) : <EOL> """<STR_LIT>""" <EOL> self . _collect_ns_info ( ns_info ) <EOL> return return_obj <EOL> @ classmethod <EOL> def from_obj ( cls , obj , return_obj = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def to_xml ( self , include_namespaces = True , include_schemalocs = False , <EOL> ns_dict = None , schemaloc_dict = None , pretty = True , <EOL> auto_namespace = True , encoding = '<STR_LIT:utf-8>' ) : <EOL> """<STR_LIT>""" <EOL> from . utils import nsparser <EOL> parser = nsparser . NamespaceParser ( ) <EOL> if auto_namespace : <EOL> ns_info = nsparser . NamespaceInfo ( ) <EOL> else : <EOL> ns_info = None <EOL> obj = self . to_obj ( ns_info = ns_info ) <EOL> if ( not auto_namespace ) and ( not ns_dict ) : <EOL> raise Exception ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> if auto_namespace : <EOL> ns_info . finalize ( ns_dict = ns_dict , schemaloc_dict = schemaloc_dict ) <EOL> obj_ns_dict = ns_info . binding_namespaces <EOL> else : <EOL> ns_info = nsparser . NamespaceInfo ( ) <EOL> ns_info . finalized_namespaces = ns_dict or { } <EOL> ns_info . finalized_schemalocs = schemaloc_dict or { } <EOL> obj_ns_dict = dict ( <EOL> itertools . chain ( <EOL> ns_dict . iteritems ( ) , <EOL> nsparser . DEFAULT_STIX_NAMESPACES . iteritems ( ) <EOL> ) <EOL> ) <EOL> namespace_def = "<STR_LIT>" <EOL> if include_namespaces : <EOL> xmlns = parser . get_xmlns_str ( ns_info . finalized_namespaces ) <EOL> namespace_def += ( "<STR_LIT>" + xmlns ) <EOL> if include_schemalocs and include_namespaces : <EOL> schemaloc = parser . get_schemaloc_str ( ns_info . finalized_schemalocs ) <EOL> namespace_def += ( "<STR_LIT>" + schemaloc ) <EOL> if not pretty : <EOL> namespace_def = namespace_def . replace ( '<STR_LIT>' , '<STR_LIT:U+0020>' ) <EOL> with save_encoding ( encoding ) : <EOL> sio = StringIO . StringIO ( ) <EOL> obj . export ( <EOL> sio . write , <EOL> <NUM_LIT:0> , <EOL> obj_ns_dict , <EOL> pretty_print = pretty , <EOL> namespacedef_ = namespace_def <EOL> ) <EOL> s = unicode ( sio . getvalue ( ) ) <EOL> if encoding : <EOL> return s . encode ( encoding ) <EOL> return s <EOL> def to_json ( self ) : <EOL> return json . dumps ( self . to_dict ( ) ) <EOL> @ classmethod <EOL> def from_json ( cls , json_doc ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> d = json . load ( json_doc ) <EOL> except AttributeError : <EOL> d = json . loads ( json_doc ) <EOL> return cls . from_dict ( d ) <EOL> def to_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> return utils . to_dict ( self ) <EOL> @ classmethod <EOL> def from_dict ( cls , d , return_obj = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> @ classmethod <EOL> def object_from_dict ( cls , entity_dict ) : <EOL> """<STR_LIT>""" <EOL> return cls . from_dict ( entity_dict ) . to_obj ( ) <EOL> @ classmethod <EOL> def dict_from_object ( cls , entity_obj ) : <EOL> """<STR_LIT>""" <EOL> return cls . from_obj ( entity_obj ) . to_dict ( ) <EOL> def walk ( self ) : <EOL> return utils . walk . iterwalk ( self ) <EOL> def find ( self , id_ ) : <EOL> """<STR_LIT>""" <EOL> if not id_ : <EOL> return <EOL> for entity in self . walk ( ) : <EOL> if id_ == getattr ( entity , "<STR_LIT>" , None ) : <EOL> return entity <EOL> class EntityList ( collections . MutableSequence , Entity ) : <EOL> _binding_class = _override <EOL> _binding_var = None <EOL> _contained_type = _override <EOL> _inner_name = None <EOL> _dict_as_list = False <EOL> def __init__ ( self , * args ) : <EOL> super ( EntityList , self ) . __init__ ( ) <EOL> self . _inner = [ ] <EOL> if not any ( args ) : <EOL> return <EOL> for arg in args : <EOL> if utils . is_sequence ( arg ) : <EOL> self . extend ( arg ) <EOL> else : <EOL> self . append ( arg ) <EOL> def __nonzero__ ( self ) : <EOL> return bool ( self . _inner ) <EOL> def __getitem__ ( self , key ) : <EOL> return self . _inner . __getitem__ ( key ) <EOL> def __setitem__ ( self , key , value ) : <EOL> if not self . _is_valid ( value ) : <EOL> value = self . _fix_value ( value ) <EOL> self . _inner . __setitem__ ( key , value ) <EOL> def __delitem__ ( self , key ) : <EOL> self . _inner . __delitem__ ( key ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _inner ) <EOL> def insert ( self , idx , value ) : <EOL> if not value : <EOL> return <EOL> if not self . _is_valid ( value ) : <EOL> value = self . _fix_value ( value ) <EOL> self . _inner . insert ( idx , value ) <EOL> def _is_valid ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return isinstance ( value , self . _contained_type ) <EOL> def _fix_value ( self , value ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> new_value = self . _contained_type ( value ) <EOL> except : <EOL> error = "<STR_LIT>" <EOL> error = error . format ( <EOL> value , <EOL> type ( value ) , <EOL> type ( self ) , <EOL> self . _contained_type <EOL> ) <EOL> raise ValueError ( error ) <EOL> return new_value <EOL> def to_obj ( self , return_obj = None , ns_info = None ) : <EOL> super ( EntityList , self ) . to_obj ( return_obj = return_obj , ns_info = ns_info ) <EOL> if not return_obj : <EOL> return_obj = self . _binding_class ( ) <EOL> objlist = [ x . to_obj ( ns_info = ns_info ) for x in self ] <EOL> setattr ( return_obj , self . _binding_var , objlist ) <EOL> return return_obj <EOL> def to_list ( self ) : <EOL> return [ h . to_dict ( ) for h in self ] <EOL> def to_dict ( self ) : <EOL> if self . _dict_as_list : <EOL> return self . to_list ( ) <EOL> d = utils . to_dict ( self , skip = ( '<STR_LIT>' , ) ) <EOL> if self . _inner : <EOL> d [ self . _inner_name ] = [ h . to_dict ( ) for h in self ] <EOL> return d <EOL> @ classmethod <EOL> def from_obj ( cls , obj , return_obj = None , contained_type = None , <EOL> binding_var = None ) : <EOL> if not obj : <EOL> return None <EOL> if return_obj is None : <EOL> return_obj = cls ( ) <EOL> if not contained_type : <EOL> contained_type = cls . _contained_type <EOL> if not binding_var : <EOL> binding_var = cls . _binding_var <EOL> for item in getattr ( obj , binding_var ) : <EOL> return_obj . append ( contained_type . from_obj ( item ) ) <EOL> return return_obj <EOL> @ classmethod <EOL> def from_list ( cls , list_repr , return_obj = None , contained_type = None ) : <EOL> if not utils . is_sequence ( list_repr ) : <EOL> return None <EOL> if return_obj is None : <EOL> return_obj = cls ( ) <EOL> if not contained_type : <EOL> contained_type = cls . _contained_type <EOL> return_obj . extend ( contained_type . from_dict ( x ) for x in list_repr ) <EOL> return return_obj <EOL> @ classmethod <EOL> def from_dict ( cls , dict_repr , return_obj = None , contained_type = None , <EOL> inner_name = None ) : <EOL> if cls . _dict_as_list : <EOL> return cls . from_list ( <EOL> dict_repr , <EOL> return_obj = return_obj , <EOL> contained_type = contained_type , <EOL> ) <EOL> if not isinstance ( dict_repr , dict ) : <EOL> return None <EOL> if return_obj is None : <EOL> return_obj = cls ( ) <EOL> if not contained_type : <EOL> contained_type = cls . _contained_type <EOL> if not inner_name : <EOL> inner_name = cls . _inner_name <EOL> for item in dict_repr . get ( inner_name , [ ] ) : <EOL> return_obj . append ( contained_type . from_dict ( item ) ) <EOL> return return_obj <EOL> class TypedCollection ( object ) : <EOL> """<STR_LIT>""" <EOL> _contained_type = _override <EOL> def __init__ ( self , * args ) : <EOL> self . _inner = [ ] <EOL> self . _initialize_inner ( * args ) <EOL> def _initialize_inner ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _inner ) <EOL> def __nonzero__ ( self ) : <EOL> return bool ( self . _inner ) <EOL> def _is_valid ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return isinstance ( value , self . _contained_type ) <EOL> def _fix_value ( self , value ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> new_value = self . _contained_type ( value ) <EOL> except Exception : <EOL> error = "<STR_LIT>" <EOL> error = error . format ( <EOL> value , <EOL> type ( value ) , <EOL> type ( self ) , <EOL> self . _contained_type <EOL> ) <EOL> raise ValueError ( error ) <EOL> return new_value <EOL> def to_obj ( self , ns_info = None ) : <EOL> return [ x . to_obj ( ns_info = ns_info ) for x in self ] <EOL> def to_list ( self ) : <EOL> return [ h . to_dict ( ) for h in self ] <EOL> @ classmethod <EOL> def from_obj ( cls , obj_list , contained_type = None ) : <EOL> if not obj_list : <EOL> return None <EOL> if not contained_type : <EOL> contained_type = cls . _contained_type <EOL> if not utils . is_sequence ( obj_list ) : <EOL> obj_list = [ obj_list ] <EOL> items = ( contained_type . from_obj ( x ) for x in obj_list ) <EOL> return cls ( items ) <EOL> @ classmethod <EOL> def from_list ( cls , list_repr , contained_type = None ) : <EOL> if not list_repr : <EOL> return None <EOL> if isinstance ( list_repr , dict ) or not utils . is_sequence ( list_repr ) : <EOL> list_repr = [ list_repr ] <EOL> if not contained_type : <EOL> contained_type = cls . _contained_type <EOL> items = ( contained_type . from_dict ( x ) for x in list_repr ) <EOL> return cls ( items ) <EOL> from_dict = from_list <EOL> to_dict = to_list <EOL> @ classmethod <EOL> def object_from_dict ( cls , entity_dict ) : <EOL> """<STR_LIT>""" <EOL> return cls . from_dict ( entity_dict ) . to_obj ( ) <EOL> @ classmethod <EOL> def dict_from_object ( cls , entity_obj ) : <EOL> """<STR_LIT>""" <EOL> return cls . from_obj ( entity_obj ) . to_dict ( ) <EOL> class TypedList ( TypedCollection , collections . MutableSequence ) : <EOL> def __init__ ( self , * args ) : <EOL> TypedCollection . __init__ ( self , * args ) <EOL> def _initialize_inner ( self , * args ) : <EOL> if not any ( args ) : <EOL> return <EOL> for arg in args : <EOL> if utils . is_sequence ( arg ) : <EOL> self . extend ( arg ) <EOL> else : <EOL> self . append ( arg ) <EOL> def __getitem__ ( self , key ) : <EOL> return self . _inner . __getitem__ ( key ) <EOL> def __setitem__ ( self , key , value ) : <EOL> if not self . _is_valid ( value ) : <EOL> value = self . _fix_value ( value ) <EOL> self . _inner . __setitem__ ( key , value ) <EOL> def __delitem__ ( self , key ) : <EOL> self . _inner . __delitem__ ( key ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _inner ) <EOL> def insert ( self , idx , value ) : <EOL> if not value : <EOL> return <EOL> if not self . _is_valid ( value ) : <EOL> value = self . _fix_value ( value ) <EOL> self . _inner . insert ( idx , value ) <EOL> class BaseCoreComponent ( Cached , Entity ) : <EOL> _ALL_VERSIONS = ( ) <EOL> _ID_PREFIX = None <EOL> def __init__ ( self , id_ = None , idref = None , timestamp = None , title = None , <EOL> description = None , short_description = None ) : <EOL> self . id_ = id_ or idgen . create_id ( self . _ID_PREFIX ) <EOL> self . idref = idref <EOL> self . title = title <EOL> self . description = description <EOL> self . short_description = short_description <EOL> self . version = None <EOL> self . information_source = None <EOL> self . handling = None <EOL> if timestamp : <EOL> self . timestamp = timestamp <EOL> else : <EOL> self . timestamp = utils . dates . now ( ) if not idref else None <EOL> @ property <EOL> def id_ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _id <EOL> @ id_ . setter <EOL> def id_ ( self , value ) : <EOL> if not value : <EOL> self . _id = None <EOL> else : <EOL> self . _id = value <EOL> self . idref = None <EOL> @ property <EOL> def idref ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _idref <EOL> @ idref . setter <EOL> def idref ( self , value ) : <EOL> if not value : <EOL> self . _idref = None <EOL> else : <EOL> self . _idref = value <EOL> self . id_ = None <EOL> @ property <EOL> def version ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _version <EOL> @ version . setter <EOL> def version ( self , value ) : <EOL> if not value : <EOL> self . _version = None <EOL> else : <EOL> utils . check_version ( self . _ALL_VERSIONS , value ) <EOL> self . _version = value <EOL> @ property <EOL> def timestamp ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _timestamp <EOL> @ timestamp . setter <EOL> def timestamp ( self , value ) : <EOL> self . _timestamp = utils . dates . parse_value ( value ) <EOL> @ property <EOL> def title ( self ) : <EOL> return self . _title <EOL> @ title . setter <EOL> def title ( self , value ) : <EOL> self . _title = value <EOL> @ property <EOL> def description ( self ) : <EOL> """<STR_LIT>""" <EOL> return next ( iter ( self . descriptions ) , None ) <EOL> @ description . setter <EOL> def description ( self , value ) : <EOL> self . descriptions = value <EOL> @ property <EOL> def descriptions ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _description <EOL> @ descriptions . setter <EOL> def descriptions ( self , value ) : <EOL> from stix . common import StructuredTextList <EOL> self . _description = StructuredTextList ( value ) <EOL> def add_description ( self , description ) : <EOL> """<STR_LIT>""" <EOL> self . descriptions . add ( description ) <EOL> @ property <EOL> def short_description ( self ) : <EOL> """<STR_LIT>""" <EOL> return next ( iter ( self . short_descriptions ) , None ) <EOL> @ short_description . setter <EOL> def short_description ( self , value ) : <EOL> self . short_descriptions = value <EOL> @ property <EOL> def short_descriptions ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _short_description <EOL> @ short_descriptions . setter <EOL> def short_descriptions ( self , value ) : <EOL> from stix . common import StructuredTextList <EOL> self . _short_description = StructuredTextList ( value ) <EOL> def add_short_description ( self , description ) : <EOL> """<STR_LIT>""" <EOL> self . short_descriptions . add ( description ) <EOL> @ property <EOL> def information_source ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _information_source <EOL> @ information_source . setter <EOL> def information_source ( self , value ) : <EOL> from stix . common import InformationSource <EOL> self . _set_var ( InformationSource , try_cast = False , information_source = value ) <EOL> @ property <EOL> def handling ( self ) : <EOL> return self . _handling <EOL> @ handling . setter <EOL> def handling ( self , value ) : <EOL> """<STR_LIT>""" <EOL> from stix . data_marking import Marking <EOL> self . _set_var ( Marking , try_cast = False , handling = value ) <EOL> @ classmethod <EOL> def from_obj ( cls , obj , return_obj = None ) : <EOL> from stix . common import StructuredTextList , InformationSource <EOL> from stix . data_marking import Marking <EOL> if not return_obj : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not obj : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return_obj . id_ = obj . id <EOL> return_obj . idref = obj . idref <EOL> return_obj . timestamp = obj . timestamp <EOL> return_obj . version = getattr ( obj , '<STR_LIT:version>' , None ) <EOL> return_obj . title = getattr ( obj , '<STR_LIT>' , None ) <EOL> return_obj . descriptions = StructuredTextList . from_obj ( getattr ( obj , '<STR_LIT>' , None ) ) <EOL> return_obj . short_descriptions = StructuredTextList . from_obj ( getattr ( obj , '<STR_LIT>' , None ) ) <EOL> return_obj . information_source = InformationSource . from_obj ( getattr ( obj , '<STR_LIT>' , None ) ) <EOL> return_obj . handling = Marking . from_obj ( getattr ( obj , '<STR_LIT>' , None ) ) <EOL> return return_obj <EOL> def to_obj ( self , return_obj = None , ns_info = None ) : <EOL> if not return_obj : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> super ( BaseCoreComponent , self ) . to_obj ( <EOL> return_obj = return_obj , <EOL> ns_info = ns_info <EOL> ) <EOL> return_obj . id = self . id_ <EOL> return_obj . idref = self . idref <EOL> return_obj . version = self . version <EOL> return_obj . Title = self . title <EOL> if self . timestamp : <EOL> return_obj . timestamp = utils . dates . serialize_value ( self . timestamp ) <EOL> if self . descriptions : <EOL> return_obj . Description = self . descriptions . to_obj ( ns_info = ns_info ) <EOL> if self . short_descriptions : <EOL> return_obj . Short_Description = self . short_descriptions . to_obj ( ns_info = ns_info ) <EOL> if self . information_source : <EOL> return_obj . Information_Source = self . information_source . to_obj ( ns_info = ns_info ) <EOL> if self . handling : <EOL> return_obj . Handling = self . handling . to_obj ( ns_info = ns_info ) <EOL> return return_obj <EOL> @ classmethod <EOL> def from_dict ( cls , d , return_obj = None ) : <EOL> from stix . common import StructuredTextList , InformationSource <EOL> from stix . data_marking import Marking <EOL> if not return_obj : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> get = d . get <EOL> return_obj . id_ = get ( '<STR_LIT:id>' ) <EOL> return_obj . idref = get ( '<STR_LIT>' ) <EOL> return_obj . timestamp = get ( '<STR_LIT>' ) <EOL> return_obj . version = get ( '<STR_LIT:version>' ) <EOL> return_obj . title = get ( '<STR_LIT:title>' ) <EOL> return_obj . descriptions = StructuredTextList . from_dict ( get ( '<STR_LIT:description>' ) ) <EOL> return_obj . short_descriptions = StructuredTextList . from_dict ( get ( '<STR_LIT>' ) ) <EOL> return_obj . information_source = InformationSource . from_dict ( get ( '<STR_LIT>' ) ) <EOL> return_obj . handling = Marking . from_dict ( get ( '<STR_LIT>' ) ) <EOL> return return_obj <EOL> def to_dict ( self ) : <EOL> return super ( BaseCoreComponent , self ) . to_dict ( ) </s>
