-
Notifications
You must be signed in to change notification settings - Fork 0
/
component.py
103 lines (78 loc) · 1.67 KB
/
component.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import components
import config
import os
import schema
import struct
import tuples
import vertex
def get_dir(gno, cno):
rdir = os.path.expanduser(config.GRDB_DIR)
gdir = rdir + '/' + str(gno)
cdir = gdir + '/' + str(cno)
return cdir
def dump_vertexes(sv, cdir):
# Print vertex set
vfile = cdir + '/' + config.VERTEX_FILE
try:
vfd = open(vfile, 'rb')
except:
print('{}', end='')
return
print('{', end='')
vb = vfd.read(8)
while True:
print(struct.unpack('<q', vb)[0], end='')
tuples.dump(sv, vfd)
vb = vfd.read(8)
if not vb:
break;
print(',', end='')
print('}', end='')
vfd.close()
def dump_edges(se, cdir):
efile = cdir + '/' + config.EDGE_FILE
try:
efd = open(efile, 'rb')
except:
print(',{}', end='')
return
print(',{', end='')
e1b = efd.read(8)
if not e1b:
efd.close()
return;
e2b = efd.read(8)
if not e2b:
efd.close()
return;
while True:
eid1 = struct.unpack('<q', e1b)[0]
eid2 = struct.unpack('<q', e2b)[0]
print('(' + str(eid1) + ',' + str(eid2) + ')', end='')
tuples.dump(se, efd)
e1b = efd.read(8)
if not e1b:
break;
e2b = efd.read(8)
if not e2b:
break;
print(',', end='')
print('}', end='')
efd.close()
def dump(gidx, cidx):
cdir = get_dir(gidx, cidx)
sv = schema.read(cdir, 'v')
se = schema.read(cdir, 'e')
dump_vertexes(sv, cdir)
dump_edges(se, cdir)
def new(gno):
# Find highest existing component number for specified graph and
# add one
clist = components.get_list(gno)
cnolist = list(map(int, clist))
cno = max(cnolist or (-1)) + 1
# Create directory for new component
cdir = get_dir(gno, cno)
os.mkdir(cdir)
# Create first vertex in the new component
vertex.new(cdir)