""" Define functions to create an abstract syntax tree (AST) graph model using the 'graphinate' library. The 'ast_graph_model' function parses the AST of a specified class and creates nodes and edges for the graph model. The nodes represent AST nodes with their type and label, while the edges represent relationships between AST nodes. """importastimporthashlibimportinspectimportoperatorimportpicklefrom_astimportASTfromcollections.abcimportIterableimportgraphinatedef_ast_nodes(parsed_asts:Iterable[AST]):foriteminparsed_asts:ifnotisinstance(item,ast.Load):yielditemyield from_ast_nodes(ast.iter_child_nodes(item))def_ast_edge(parsed_ast:AST):forchild_astinast.iter_child_nodes(parsed_ast):ifnotisinstance(child_ast,ast.Load):edge={'source':parsed_ast,'target':child_ast}edge_types=(field_nameforfield_name,valueinast.iter_fields(parsed_ast)ifchild_ast==valueor(child_astinvalueifisinstance(value,list)elseFalse))edge_type=next(edge_types,None)ifedge_type:edge['type']=edge_typeyieldedgeyield from_ast_edge(child_ast)defast_graph_model():""" Create an abstract syntax tree (AST) graph model. Returns: GraphModel: A graph model representing the AST nodes and their relationships. """graph_model=graphinate.model(name='AST Graph')root_ast_node=ast.parse(inspect.getsource(graphinate.builders.D3Builder))defnode_type(ast_node):returnast_node.__class__.__name__defnode_label(ast_node)->str:label=ast_node.__class__.__name__forfield_namein('name','id'):iffield_nameinast_node._fields:value=operator.attrgetter(field_name)(ast_node)label=f"{label}\n{field_name}: {value}"returnlabeldefkey(value):# noinspection InsecureHashreturnhashlib.shake_128(pickle.dumps(value)).hexdigest(20)defendpoint(value,endpoint_name):returnkey(value[endpoint_name])defsource(value):returnendpoint(value,'source')deftarget(value):returnendpoint(value,'target')@graph_model.node(_type=node_type,key=key,label=node_label,uniqueness=True)defast_node(**kwargs):yield from_ast_nodes([root_ast_node])@graph_model.edge(_type='edge',source=source,target=target,label=operator.itemgetter('type'))defast_edge(**kwargs):yield from_ast_edge(root_ast_node)returngraph_modelif__name__=='__main__':ast_model=ast_graph_model()graphinate.materialize(ast_model,builder=graphinate.builders.GraphQLBuilder,builder_output_handler=graphinate.graphql)
frompipdeptree._cliimportget_optionsfrompipdeptree._discoveryimportget_installed_distributionsfrompipdeptree._modelsimportPackageDAGimportgraphinatedefdependency_graph_model():""" Generate a dependency graph model. Returns: GraphModel: A graph model representing the dependency graph. """options=get_options(args=None)pkgs=get_installed_distributions(local_only=options.local_only,user_only=options.user_only)tree=PackageDAG.from_pkgs(pkgs)graph_model=graphinate.model(name="Dependency Graph")@graph_model.edge()defdependency():forp,dintree.items():forcind:yield{'source':p.project_name,'target':c.project_name}returngraph_modelif__name__=='__main__':dependency_model=dependency_graph_model()graphinate.materialize(dependency_model,builder=graphinate.builders.GraphQLBuilder,builder_output_handler=graphinate.graphql)