-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathload.py
322 lines (248 loc) · 7.78 KB
/
load.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# encoding:utf-8
from __future__ import print_function
import os
import shutil
PROJECT = r"D:\project\CoDeSys\HMI\a.project"
from_folder=r'D:\Gitlab\codesys\Yao'
def check(func):
def wrapper(proj,path,name): # 指定宇宙无敌参数
#call func
func(proj,path,name)
#check
name=name.split(".")[0]
found = proj.find(name, False)
assert(found is not None and len(found) == 1, 'No object with the name {0} found '.format(name))
#assert(found[0].is_folder, 'Found object is not a folder')
item = found[0]
return item
return wrapper # 返回
def insert_text(proj,path,name):
with open(path,'r') as f:
text = f.read()
index = text.find('(*#-#-#-#-#-#-#-#-#-#---Implementation---#-#-#-#-#-#-#-#-#-#-#-#-#*)\r\n')
if index >=0:
t1= text[index:].replace('(*#-#-#-#-#-#-#-#-#-#---Implementation---#-#-#-#-#-#-#-#-#-#-#-#-#*)\r\n','')
proj.textual_implementation.replace(t1)
#print(name+"*** t1")
try:
t1= text[:index].replace('(*#-#-#-#-#-#-#-#-#-#---Declaration---#-#-#-#-#-#-#-#-#-#-#-#-#*)\r\n','')
proj.textual_declaration.replace(t1)
except:
pass
else:
t1= text.replace('(*#-#-#-#-#-#-#-#-#-#---Declaration---#-#-#-#-#-#-#-#-#-#-#-#-#*)\r\n','')
proj.textual_declaration.replace(t1)
def export_visu(projects):
proj = projects.primary
device = proj.find('_3S_Testspec_Device')[0]
device.export_xml(reporter, ExportFileName, recursive = True)
def create_taskconfig(proj,path,name):
proj = proj.create_task_configuration()
return proj
#projects.primary.import_native(path)
def create_task(proj,path,name):
name=name.split('.')[0]
proj.import_native(path)
def create_task_old(proj,path,name):
name=name.split('.')[0]
proj=proj.create_task(name)
pou_names=[]
interval=''
priority=''
kindOftask=''
try:
with open(path,'r') as f:
t=f.readline()
pou_names=t.split("=")[1].replace("\r","").replace("\n","").split(',')
t=f.readline()
interval=t.split("=")[1].replace("\r","").replace("\n","")
t=f.readline()
kindOftask=t.split("=")[1].replace("\r","").replace("\n","")
except:
system.ui.info('open file:\n{0} \nfailed!'.format(path))
return
try:
proj.interval=interval
proj.priority=priority
for i in pou_names:
if i:
try:
proj.pous.add(i)
except:
pass
proj.kind_of_task= kindOftask
except:
pass
@check
def create_dev(proj,path,name):
type=0
id=''
ver=''
with open(os.path.join(path,name),'r') as f:
type=f.readline()
type=int(type.split("=")[1].replace("\r\n",""))
id=f.readline()
id=id.split("=")[1].replace("\r","").replace("\n","")
ver=f.readline()
ver=ver.split("=")[1].replace("\r","").replace("\n","")
#system.ui.info("create device:\ntype:{0}\nid:{1}\nver:{2}".format(type,id,ver))
devId = device_repository.create_device_identification(type, id, ver)
devDesc = device_repository.get_device(devId)
if devDesc is None:
system.ui.info("create device ERR:\n{0}\n{1}\n{2}".format(type,id,ver))
raise Exception('No WinV3 PLC available in device repo')
name=name.split('.')[0]
# Add PLC to an empty project using a DeviceId instance
proj.add(name, devId)
def create_app(proj,path,name):
pass
@check
def create_pou(proj,path,name):
name=name.split('.')[0]
proj.create_pou(name, PouType.Program)
@check
def create_gvl(proj,path,name):
name=name.split(".")[0]
proj.create_gvl(name)
@check
def create_property(proj,path,name):
name=name.split(".")[0]
proj.create_property(name)
def create_method(proj,path,name):
name=name.split(".")[0]
proj = proj.create_method(name)
return proj
def create_act(proj,path,name):
name=name.split(".")[0]
try:
proj = proj.create_action(name)
except:
system.ui.info("create action:{0}\nfailed".format(name))
return proj
@check
def create_folder(proj,path,name):
proj.create_folder(name)
@check
def create_fb(proj,path,name):
name=name.split('.')[0]
proj.create_pou(name, PouType.FunctionBlock)
@check
def create_fuction(proj,path,name):
name=name.split(".")[0]
proj.create_pou(name,PouType.Function)
@check
def create_itf(proj,path,name):
name=name.split(".")[0]
proj.create_interface(name)
found = proj.find(name, False)
def create_dut(proj,path,name):
name=name.split(".")[0]
item=proj.create_dut(name,DutType.Union)
return item
def add_library(proj,path,name):
name=name.split(".")[0]
#system.ui.info("add library:{0}".format(name))
proj.import_native(path)
def add_textlist(proj,path,name):
name=name.split(".")[0]
try:
proj=proj.create_textlist(name)
proj.importfile(path)
except:
pass
@check
def add_prop_method(proj,path,name):
name = name.split(".")[0]
def walk_folder(proj,path,tp):
curpath=path
for fi in os.listdir(curpath):
sub_path = os.path.join(curpath,fi)
is_folder = os.path.isdir(sub_path)
stp= fi.split('.')
fn=stp[0]
try :
stp=stp[1]
except:
stp=''
if tp!='' and tp==stp and not is_folder:
insert_text(proj,sub_path,fi)
else:
if stp=='dev':
sub_proj= create_dev(proj,sub_path,fi)
sub_path=os.path.join(sub_path,'Plc Logic')
sub_proj=sub_proj.find('Plc Logic',False)[0]
if not sub_proj :
raise Exception('No PLC Logic in device')
for sfi in os.listdir(sub_path):
sub_sub_path= os.path.join(sub_path,sfi)
sub_sub_proj=sub_proj.find(sfi,False)[0]
if sub_sub_proj :
walk_folder(sub_sub_proj,sub_sub_path,'app')
else:
sub_sub_proj = create_app(sub_sub_proj,sub_sub_path,sfi)
walk_folder(sub_sub_proj,sub_sub_path,'app')
elif stp=='pou':
sub_proj= create_pou(proj,sub_path,fi)
if not is_folder:
insert_text(sub_proj,sub_path,fi)
else:
walk_folder(sub_proj,sub_path,'pou')
elif stp=='itf':
sub_proj= create_itf(proj,sub_path,fi)
if not is_folder:
insert_text(sub_proj,sub_path,fi)
else:
walk_folder(sub_proj,sub_path,'itf')
elif stp=='gvl' : #gvl resistant
sub_proj=create_gvl(proj,sub_path,fi)
#system.ui.info("GVL:"+sub_path)
insert_text(sub_proj,sub_path,fi)
elif stp=='prop':
sub_proj=create_property(proj,sub_path,fi)
if not is_folder:
insert_text(sub_proj,sub_path,fi)
else:
walk_folder(sub_proj,sub_path,'prop')
elif stp=='pm': #property method
sub_proj = add_prop_method(proj,sub_path,fi)
if not is_folder:
insert_text(sub_proj,sub_path,fi)
else:
walk_folder(sub_proj,sub_path,'pm')
elif stp=='m': #method
sub_proj=create_method(proj,sub_path,fi)
if not is_folder:
insert_text(sub_proj,sub_path,fi)
else:
walk_folder(sub_proj,sub_path,'m')
elif stp=='act': #action
sub_proj=create_act(proj,sub_path,fi)
if not is_folder:
insert_text(sub_proj,sub_path,fi)
else:
walk_folder(sub_proj,sub_path,'act')
elif stp=='dut':
sub_proj=create_dut(proj,sub_path,fi)
insert_text(sub_proj,sub_path,fi)
elif stp=='tc': #task configuration
sub_proj=create_taskconfig(proj,sub_path,fi)
if is_folder:
walk_folder(sub_proj,sub_path,'tc')
elif stp=='task':
create_task(proj,sub_path,fi)
elif stp=='': #folder
sub_proj=create_folder(proj,sub_path,fi)
walk_folder(sub_proj,sub_path,tp)
elif stp=='lib':
add_library(proj,sub_path,fi)
elif stp=='tl': # textlist
add_textlist(proj,sub_path,fi)
elif stp=='gtl': #global_textlist
add_textlist(proj,sub_path,fi)
else:
pass
if projects.primary:
projects.primary.close()
proj = projects.create(PROJECT)
walk_folder(proj,from_folder,'')
system.ui.info("ok")