forked from 18thCentury/CodeSys
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathload.py
291 lines (242 loc) · 9.76 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
#!/bin/env python
# encoding:utf-8
from __future__ import print_function
import os
import shutil
import clr
from System import Environment
import codecs
clr.AddReference('System.Windows.Forms')
from System.Windows.Forms import FolderBrowserDialog, DialogResult
declaration_intro = '%' + '-' * 75 + '%\r\n%->> Declaration\r\n' + '%' + '-' * 75 + '%\r\n'
implementation_intro = '%' + '-' * 75 + '%\r\n%->> Implementation\r\n' + '%' + '-' * 75 + '%\r\n'
def check(func):
def wrapper(proj, path, name): # 指定宇宙无敌参数
# call func
func(proj, path, name)
# check
found = proj.find(name, False)
assert (found is not None and len(found) == 1, 'No object with the name {0} found '.format(name))
item = found[0]
return item
return wrapper # 返回
def insert_text(proj, path, name):
with codecs.open(path, 'r', encoding='utf-8') as f:
text = f.read()
index = text.find(implementation_intro)
if index >= 0:
implementation = text[index:].replace(implementation_intro, '')
proj.textual_implementation.replace(implementation)
try:
declaration = text[:index].replace(declaration_intro, '')
proj.textual_declaration.replace(declaration)
except AttributeError as error:
print("AttributeError: {}".format(error))
except:
pass
else:
declaration = text.replace(implementation_intro, '')
proj.textual_declaration.replace(declaration)
def create_taskconfig(proj, path, name):
proj = proj.create_task_configuration()
return proj
def create_task(proj, path, name):
proj.import_native(path)
@check
def create_dev(proj, path, name):
with open(path, 'r') as f:
type = int(f.readline().split("=")[1].strip())
id = f.readline().split("=")[1].strip()
ver = f.readline().split("=")[1].strip()
devId = device_repository.create_device_identification(type, id, ver)
devDesc = device_repository.get_device(devId)
if devDesc is None:
raise Exception('No WinV3 PLC available in device repo')
proj.add(name, devId)
@check
def create_pou(proj, path, name):
proj.create_pou(name, PouType.Program)
@check
def create_gvl(proj, path, name):
proj.create_gvl(name)
@check
def create_property(proj, path, name):
proj.create_property(name)
def create_method(proj, path, name):
proj = proj.create_method(name)
return proj
def create_act(proj, path, name):
try:
proj = proj.create_action(name)
except:
pass
return proj
@check
def create_folder(proj, path, name):
proj.create_folder(name)
@check
def create_fb(proj, path, name):
proj.create_pou(name, PouType.FunctionBlock)
@check
def create_function(proj, path, name):
proj.create_pou(name, PouType.Function)
@check
def create_itf(proj, path, name):
proj.create_interface(name)
def create_dut(proj, path, name):
item = proj.create_dut(name, DutType.Union)
return item
def add_library(proj, path, name):
proj.import_native(path)
def add_textlist(proj, path, name):
try:
proj = proj.find(name, False)[0]
except ValueError:
proj = proj.create_textlist()
try:
proj.importfile(path)
# avoid error: This object is already in use.
except Exception as error:
print("An error occurred: {}".format(error))
pass
@check
def add_prop_method(proj, path, name):
pass
def walk_folder(proj, path, depth=0):
if depth == 0:
if not any(file.endswith('.dev') for file in os.listdir(path)):
system.ui.warning(" !!! Wrong path !!! ")
return False
## 0 层文件存在 dev 文件夹,则继续,否则退出
for f_name in os.listdir(path):
sub_path = os.path.join(path, f_name)
is_folder = os.path.isdir(sub_path)
try:
name, f_ext = f_name.split('.') # File extension
except ValueError:
name = f_name
f_ext = ''
if not name: continue
if depth == 0:
if is_folder and f_ext == 'dev':
print('main device: {}'.format(name))
try:
dev_proj = proj.find(name, False)[0]
except ValueError:
system.ui.warning(" !!! Device mismatch !!! ")
return False
logic_path = os.path.join(sub_path, 'Plc Logic')
sub_proj = dev_proj.find('Plc Logic', False)[0]
sub_path = os.path.join(logic_path, 'Application')
sub_proj = sub_proj.find('Application', False)[0]
walk_folder(sub_proj, sub_path, depth + 3) # 直接进入 Application
else:
walk_folder(proj, path, 9999) # 打破 dev 处理方式,以正常方式处理非 dev 扩展名的 0 层文件
break # 避免由于 0 层文件太多导致的 不必要循环
else:
if depth == 9999:
print('current project directory: outside of top-dev')
else:
print('current project directory: ', proj.get_name())
if is_folder:
print('input folder: {}{}'.format(' ' * 4, name))
if f_ext == 'tc':
try:
sub_proj = proj.find(name, False)[0]
except ValueError:
sub_proj = create_taskconfig(proj, sub_path, name)
walk_folder(sub_proj, sub_path, depth + 1)
elif f_ext == '':
try:
sub_proj = proj.find(name, False)[0]
except ValueError:
sub_proj = create_folder(proj, sub_path, name)
walk_folder(sub_proj, sub_path, depth + 1)
else:
pass
else:
print('input file: {}{}.{}'.format(' ' * 40, name, f_ext))
if f_ext == 'pou':
try:
sub_proj = proj.find(name, False)[0]
except ValueError:
sub_proj = create_pou(proj, sub_path, name)
insert_text(sub_proj, sub_path, name)
elif f_ext == 'itf':
try:
sub_proj = proj.find(name, False)[0]
except ValueError:
sub_proj = create_itf(proj, sub_path, name)
insert_text(sub_proj, sub_path, name)
elif f_ext == 'gvl':
try:
sub_proj = proj.find(name, False)[0]
except ValueError:
sub_proj = create_gvl(proj, sub_path, name)
insert_text(sub_proj, sub_path, name)
elif f_ext == 'prop':
try:
sub_proj = proj.find(name, False)[0]
except ValueError:
sub_proj = create_property(proj, sub_path, name)
insert_text(sub_proj, sub_path, name)
elif f_ext == 'pm':
try:
sub_proj = proj.find(name, False)[0]
except ValueError:
sub_proj = add_prop_method(proj, sub_path, name)
insert_text(sub_proj, sub_path, name)
elif f_ext == 'm':
try:
sub_proj = proj.find(name, False)[0]
except ValueError:
sub_proj = create_method(proj, sub_path, name)
insert_text(sub_proj, sub_path, name)
elif f_ext == 'act':
try:
sub_proj = proj.find(name, False)[0]
except ValueError:
sub_proj = create_act(proj, sub_path, name)
insert_text(sub_proj, sub_path, name)
elif f_ext == 'dut':
try:
sub_proj = proj.find(name, False)[0]
except ValueError:
sub_proj = create_dut(proj, sub_path, name)
insert_text(sub_proj, sub_path, name)
elif f_ext == 'task':
create_task(proj, sub_path, name)
elif f_ext == 'lib':
add_library(proj, sub_path, name)
elif f_ext == 'tl':
add_textlist(proj, sub_path, name)
elif f_ext == 'gtl':
add_textlist(proj, sub_path, name)
else:
print('!' * 30 + ' Unrecognized file! ')
return True
def search_folder():
# 获取 MyDocuments 路径
root = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
# 目标路径 CsysL
default_path = os.path.join(root, 'CsysL')
if not os.path.exists(default_path): default_path = os.path.join(root)
def browse_directory_dialog(description, selected_path):
dialog = FolderBrowserDialog()
dialog.Description = description
dialog.SelectedPath = selected_path # 设置默认路径
dialog.ShowNewFolderButton = True
if dialog.ShowDialog() == DialogResult.OK:
return dialog.SelectedPath
return None
has_repo = False # git
# 使用 browse_directory_dialog 函数
selected_path = browse_directory_dialog("Choose a directory? 简: 选择源代码位置。 ", default_path)
print("Nice, you choose: '%s'" % selected_path)
return selected_path
if __name__ == '__main__':
source_folder = search_folder()
if source_folder:
status = walk_folder(projects.primary, source_folder)
if status:
system.ui.info(" Source codes are loaded! ")