11import json
22
33from py2neo import Graph , Node , Relationship
4+ import re
45
56import stix2
67from stix2 .base import _STIXBase
1011from stix2 .parsing import parse
1112
1213
14+ def convert_camel_case_to_snake_case (name ):
15+ return re .sub (r'(?<!^)(?=[A-Z])' , '_' , name ).lower ()
1316def remove_sro_from_list (sro , sro_list ):
1417 for rel in sro_list :
1518 if (rel ["source_ref" ] == sro ["source_ref" ] and
@@ -237,7 +240,7 @@ def _insert_external_references(self, refs, parent_node):
237240 def _insert_extensions (self , extensions , parent_node ):
238241 for ext in extensions :
239242 node_contents = dict ()
240- type_name = ext .__class__ .__name__
243+ type_name = convert_camel_case_to_snake_case ( ext .__class__ .__name__ )
241244 node_contents ["type" ] = type_name
242245 for key , value in ext .items ():
243246 if not key .endswith ("ref" ) and not key .endswith ("refs" ):
@@ -275,7 +278,7 @@ def _insert_sro(self, obj, recheck=False):
275278 if self ._is_node_available (obj ["source_ref" ]) and self ._is_node_available (obj ["target_ref" ]):
276279 cypher_string = f'MATCH (a),(b) WHERE a.id="{ str (obj ["source_ref" ])} " AND b.id="{ str (obj ["target_ref" ])} " CREATE (a)-[r:{ reltype } ]->(b) RETURN a,b'
277280 self .sgraph .run (cypher_string )
278- print (f'Created { str (obj ["source_ref" ])} { reltype } { obj ["target_ref" ]} ' )
281+ # print(f'Created {str(obj["source_ref"])} {reltype} {obj["target_ref"]}')
279282 if recheck :
280283 remove_sro_from_list (obj , self .relationships_to_recheck )
281284 else :
@@ -299,7 +302,7 @@ def _insert_embedded_relationships(self, obj, id, recheck=False):
299302 # The "b to a" relationship is reversed in this cypher query to ensure the correct relationship direction in the graph
300303 cypher_string = f'MATCH (a),(b) WHERE a.id="{ str (ref )} " AND b.id="{ str (id )} " CREATE (b)-[r:{ k } ]->(a) RETURN a,b'
301304 self .sgraph .run (cypher_string )
302- print (f'Created * { str (id )} { k } { str (ref )} ' )
305+ # print(f'Created * {str(id)} {k} {str(ref)}')
303306 if recheck :
304307 remove_sro_from_list (obj , self .relationships_to_recheck )
305308 else :
0 commit comments