-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwrite_pg.py
82 lines (73 loc) · 1.91 KB
/
write_pg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"Writes results to PG"
import argparse
import json
from networkx.readwrite.gml import read_gml
import psycopg2
parser = argparse.ArgumentParser(description="Push data to PG Database.")
parser.add_argument("uri", metavar="N", help="pass the PG URI")
args = parser.parse_args()
print(args.uri)
connstr = args.uri
try:
conn = psycopg2.connect(connstr, connect_timeout=2)
except psycopg2.Error as err:
conn = None
print(str(err))
cur = conn.cursor()
cur.execute(
"""
select exists(
select * from information_schema.tables where table_name=%s
)
""",
("metadata_parser_nodes",),
)
if cur.fetchone()[0]:
cur.execute("TRUNCATE TABLE metadata_parser_nodes cascade;")
conn.commit()
else:
cur.execute(open("src/pg_store_tbl.sql", "r").read())
G = read_gml("graph_data.gml")
for node in G.nodes():
json_rep = json.loads(
G.nodes[node]
.get("title")
.replace("<br>", "")
.replace("'", '"')
.replace("True", "true")
.replace("False", "false")
.replace("None", "[]")[1:-1]
)
print(json_rep["id"])
cur.execute(
"insert into metadata_parser_nodes values(%s, %s);",
(json_rep["id"], json.dumps(json_rep)),
)
conn.commit()
for edge in G.edges():
json_rep = json.loads(
G.edges[edge]["title"]
.replace("<br>", "")
.replace("'", '"')
.replace("True", "true")
.replace("False", "false")
.replace("None", "[]")[1:-1]
)
print(json.dumps(json_rep))
cur.execute(
"insert into metadata_parser_edges values(%s, %s, %s);",
(
json_rep["from"],
json_rep["to"],
json.dumps(json_rep),
),
)
cur.execute(
"insert into metadata_parser_edges values(%s, %s, %s);",
(
json_rep["to"],
json_rep["from"],
json.dumps(json_rep),
),
)
conn.commit()