cpg2py 1.0.3__tar.gz → 1.0.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: cpg2py
3
- Version: 1.0.3
3
+ Version: 1.0.4
4
4
  Summary: A graph-based data structure designed for querying CSV files in Joern format in Python
5
5
  Home-page: https://github.com/YichaoXu/cpg2py
6
6
  Author: Yichao Xu
@@ -0,0 +1,40 @@
1
+ from csv import DictReader
2
+ from pathlib import Path
3
+ from .cpg import _Graph
4
+ from .abc import *
5
+
6
+ def cpg_graph(node_csv: Path, edge_csv: Path) -> "_Graph":
7
+ storage = Storage()
8
+ with open(node_csv, 'r') as n_file:
9
+ reader = DictReader(n_file, delimiter='\t')
10
+ for node_props in reader:
11
+ nid = node_props.get("id:int", None)
12
+ if nid is None: node_props.get("id")
13
+ if not storage.add_node(nid):
14
+ print(f"WARN: Node {nid} already exists in the graph")
15
+ if not storage.set_node_props(nid, node_props):
16
+ print(f"WARN: Failed to set properties for node {nid}")
17
+ with open(edge_csv, 'r') as f:
18
+ reader = DictReader(f, delimiter='\t')
19
+ for edge_props in reader:
20
+ f_nid = str(edge_props.get("start", None) )
21
+ if f_nid is None: f_nid = str(edge_props.get("start:str"))
22
+ t_nid = str(edge_props.get("end", None))
23
+ if t_nid is None: t_nid = str(edge_props.get("end:str"))
24
+ e_type = str(edge_props.get("type", None))
25
+ if e_type is None: e_type = str(edge_props.get("type:str"))
26
+ edge_id = (f_nid, t_nid, e_type)
27
+ if not storage.contains_node(edge_id[0]):
28
+ storage.add_node(edge_id[0])
29
+ print(f"WARN: node {edge_id[0]} does not exists")
30
+ if not storage.contains_node(edge_id[1]):
31
+ storage.add_node(edge_id[1])
32
+ print(f"WARN: node {edge_id[1]} does not exists")
33
+ if not storage.add_edge(edge_id):
34
+ print(f"WARN: Edge {f_nid} -> {t_nid} already exists in the graph")
35
+ if not storage.set_edge_props(edge_id, edge_props):
36
+ print(f"WARN: Failed to set properties for edge {edge_id}")
37
+ return _Graph(storage)
38
+
39
+
40
+ __all__ = ['cpg_graph', 'AbcGraphQuerier', 'AbcNodeQuerier', 'AbcEdgeQuerier', 'Storage']
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: cpg2py
3
- Version: 1.0.3
3
+ Version: 1.0.4
4
4
  Summary: A graph-based data structure designed for querying CSV files in Joern format in Python
5
5
  Home-page: https://github.com/YichaoXu/cpg2py
6
6
  Author: Yichao Xu
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "cpg2py"
7
- version = "1.0.3"
7
+ version = "1.0.4"
8
8
  description = "A graph-based data structure designed for querying CSV files in Joern format in Python"
9
9
  readme = "README.md"
10
10
  authors = [
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
2
2
 
3
3
  setup(
4
4
  name="cpg2py",
5
- version="1.0.3",
5
+ version="1.0.4",
6
6
  author="Yichao Xu",
7
7
  author_email="yxu166@jhu.edu",
8
8
  description="A graph-based data structure designed for querying CSV files in Joern format in Python",
@@ -1,50 +0,0 @@
1
- import os
2
- from pathlib import Path
3
- from csv import DictReader
4
- from .cpg import _Graph
5
- from .abc import *
6
-
7
- def __remove_null_bytes(file_path: Path) -> Path:
8
- """Create a new clean file without NULL bytes and return its path."""
9
- clean_path = file_path.with_suffix(".clean.csv") # Create a temp clean file
10
- if clean_path.exists(): os.remove(clean_path)
11
- with open(file_path, "rb") as f:
12
- content = f.read().replace(b"\x00", b"") # Remove NULL bytes
13
- with open(clean_path, "wb") as f:
14
- f.write(content)
15
- return clean_path
16
-
17
- def cpg_graph(node_csv: Path, edge_csv: Path) -> _Graph:
18
- # Clean files before reading
19
- tmp_node_csv = __remove_null_bytes(node_csv)
20
- storage = Storage()
21
- with open(tmp_node_csv, 'r') as n_file:
22
- reader = DictReader(n_file, delimiter='\t')
23
- for node_props in reader:
24
- nid = node_props.get("id:int", None)
25
- if nid is None: node_props.get("id")
26
- if not storage.add_node(nid):
27
- print(f"Node {nid} already exists in the graph")
28
- if not storage.set_node_props(nid, node_props):
29
- print(f"Failed to set properties for node {nid}")
30
- if tmp_node_csv.exists(): os.remove(tmp_node_csv)
31
- tmp_edge_csv = __remove_null_bytes(edge_csv)
32
- with open(tmp_edge_csv, 'r') as f:
33
- reader = DictReader(f, delimiter='\t')
34
- for edge_props in reader:
35
- f_nid = str(edge_props.get("start", None) )
36
- if f_nid is None: f_nid = str(edge_props.get("start:str"))
37
- t_nid = str(edge_props.get("end", None))
38
- if t_nid is None: t_nid = str(edge_props.get("end:str"))
39
- e_type = str(edge_props.get("type", None))
40
- if e_type is None: e_type = str(edge_props.get("type:str"))
41
- edge_id = (f_nid, t_nid, e_type)
42
- if not storage.add_edge(edge_id):
43
- print(f"Edge {f_nid} -> {t_nid} already exists in the graph")
44
- if not storage.set_edge_props(edge_id, edge_props):
45
- print(f"Failed to set properties for edge {edge_id}")
46
- if tmp_edge_csv.exists(): os.remove(tmp_edge_csv)
47
- return _Graph(storage)
48
-
49
-
50
- __all__ = ['cpg_graph', 'AbcGraphQuerier', 'AbcNodeQuerier', 'AbcEdgeQuerier', 'Storage']
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes