-
Notifications
You must be signed in to change notification settings - Fork 12
/
stage_utils.py
56 lines (50 loc) · 1.89 KB
/
stage_utils.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
#reindex_stage_ids.py
import re
import os
import glob
import json
def reindex_json_stage_ids():
stage_meta = []
#gs = glob.glob(os.path.dirname(os.path.abspath(__file__))+'/stages/*.json')
gs = glob.glob('./stages/*.json')
for g in gs:
if not g.endswith('stage_wrapper.json'):
stage_meta += [g]
for i in range(0,len(stage_meta)):
with open(stage_meta[i],'rb') as stage:
lines = stage.readlines()
with open(stage_meta[i],'wb') as stage:
print('processing %s'%stage_meta[i])
for j in range(0,len(lines)):
m = re.search('stage_id',lines[j])
if m is not None and m.group()=='stage_id':
lines[j] = lines[j].replace(lines[j].split('"stage_id":')[1].split(',')[0],str(i))
#print(lines[j])
stage.writelines(lines)
def get_stage_meta():
stage_meta = {}
gs = glob.glob(os.path.dirname(os.path.abspath(__file__))+'/stages/*.json')
for stage in gs:
if not stage.endswith('stage_wrapper.json'):
with open(stage) as stage_json:
try:
data = json.load(stage_json)
k = data.pop('stage_id')
stage_meta[k] = data
except Exception:
print('error reading stage meta data from %s'%stage)
return stage_meta
def get_stage_name_id(stage_meta):
return {stage_meta[k]['name']:'_S'+str(k) for k in stage_meta}
#given a list of n strings
#return the longest common string to the left
def get_common_string_left(L):
S = ''
if len(L)>1:
j,n = 0,min([len(i) for i in L])
for i in range(n):
if not all([L[0][i]==m[i] for m in L[1:]]): break
else: j+=1
S = L[0][0:j]
if len(L)==1: S = L
return S