-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetEDA
executable file
·246 lines (225 loc) · 8.6 KB
/
netEDA
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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# -*- encoding: utf-8 -*-
# -*- tabstop: 4 -*-
#All greetings may go to LordBlick on gmail… ;)
from os import path as ph, rename as mv
from clIniFile import IniSections, _p, xlist
from uiNetEDA import gtk, wg
class fixPCB:
def __init__(mn):
mn.uiInit()
mn.pcb = None
mn.appStart()
bkFn = lambda mn, fileNameNET: "%s-bkp.%s" % tuple(fileNameNET.rsplit('.', 1))
uiFn = lambda mn, fileName: fileName.replace(ph.expanduser('~'), '~')
def uiInit(mn):
from uiNetEDA import edaNetUI
ui = mn.ui = edaNetUI()
gdk = gtk.gdk
ui.mainWindow.connect("destroy", lambda xargs: mn.Exit())
#### log set ####
mn.TARGET_TYPE_URI_LIST = 80
mn.lsDragAndDrop = [("text/uri-list", 0, mn.TARGET_TYPE_URI_LIST )]
ui.logView.connect("drag-data-received", mn.dragReceived)
ui.logView.drag_dest_set(gtk.DEST_DEFAULT_MOTION|gtk.DEST_DEFAULT_HIGHLIGHT|gtk.DEST_DEFAULT_DROP,
mn.lsDragAndDrop, gdk.ACTION_COPY)
ui.logView.connect("drag-motion", mn.dragMotion)
#### end of log set ####
ui.buttonSelectFile.connect("clicked", lambda xargs: mn.appFileName())
ui.buttonAllNetsGlobal.connect("clicked", lambda xargs: mn.appGlobNet())
ui.buttonRestore.connect("clicked", lambda xargs: mn.appRestore())
ui.buttonExit.connect("clicked", lambda xargs: mn.Exit())
mn.pr = ui.logView.insert_end
def appPostUI(mn):
ui = mn.ui
from uiNetEDA import pango
logBuff = ui.logView.get_buffer()
mn.tgFindErr = logBuff.create_tag('error', weight = pango.WEIGHT_BOLD, foreground = 'red')
mn.tgFileName = logBuff.create_tag('filename', weight = pango.WEIGHT_BOLD, foreground = 'yellow')
mn.tgLineNo = logBuff.create_tag('line_number', weight = pango.WEIGHT_BOLD)
mn.tgLineNo.set_property('foreground-gdk', gtk.gdk.Color('#0F0'))
mn.tgFindLine = logBuff.create_tag('phrase', weight = pango.WEIGHT_BOLD, foreground = 'orange')
mn.tgFindLine.set_property('background-gdk', gtk.gdk.Color('#002818'))
mn.tgFindPhrase = logBuff.create_tag('replace', weight = pango.WEIGHT_BOLD, foreground = 'orange')
mn.tgFindPhrase.set_property('background-gdk', gtk.gdk.Color('#580028'))
mn.tgReplacePhrase = logBuff.create_tag('int_', weight = pango.WEIGHT_BOLD, foreground = 'orange')
mn.tgReplacePhrase.set_property('background-gdk', gtk.gdk.Color('#005828'))
def errReport(mn, txt):
ui = mn.ui
ui.logView.insert_end(("Script Version: v.%0.2f. Error report:\n"+txt) % ui.version)
def appRestore(mn):
ui = mn.ui
uif = mn.uiFn
fileNameNET = mn.cfg['Main']['lastNetFile']
if fileNameNET:
fileNameBk = mn.bkFn(fileNameNET)
if ph.isfile(fileNameBk):
mv(fileNameBk, fileNameNET)
ui.logView.insert_end("Restored file:\n\t'%s'\nFrom file:\n\t'%s'\n" %\
(uif(fileNameNET), uif(fileNameBk)))
mn.appUpdateFilename()
def appUpdateFilename(mn, bUpEmptyTxt=True):
ui = mn.ui
fileNameNET = mn.cfg['Main']['lastNetFile']
if bUpEmptyTxt or(fileNameNET):
ui.txtFilename.set_text(fileNameNET.replace(ph.expanduser('~'), '~'))
bFn = bool(fileNameNET) and(ph.isfile(fileNameNET))
ui.buttonAllNetsGlobal.set_sensitive(bFn)
if bFn:
fileNameBk = mn.bkFn(fileNameNET)
bFn = ph.isfile(fileNameBk)
ui.buttonRestore.set_sensitive(bFn)
def appDropFilename(mn):
print("Filename dropped (%s)" % mn.cfg['Main']['lastNetFile'])
mn.cfg['Main'].pop('lastNetFile')
mn.appUpdateFilename()
def appGlobNet(mn):
ui = mn.ui
uif = mn.uiFn
fileNameNET = mn.cfg['Main']['lastNetFile']
if fileNameNET:
fileNameBk = mn.bkFn(fileNameNET)
uiFn = uif(fileNameNET)
if ph.isfile(fileNameNET):
hFile = open(fileNameNET, 'r')
if not(hFile):
for txtslice, cTag in( ("Can't open a file:", 2), ("'", 0), (uif(fileNameNET), 1),("'\n", 0) ):
mn.pr(txtslice, tag=(None, mn.tgFileName, mn.tgFindErr)[cTag])
return
data = dataCp = hFile.read()
hFile.close()
for txtslice, cTag in( ("Readed file:'", 0), (uif(fileNameNET), 1),("'\n", 0) ):
mn.pr(txtslice, tag=(None, mn.tgFileName)[cTag])
import re
search, replace = r"(?P<Head>\(net \(code \d+\) \(name \"?)(?P<Delete>/)(?P<Tail>[\(\)\-\w]+\"?\))", "\\g<Head>\\g<Tail>"
rexFindIt = re.compile(search, re.U)
matchIt = rexFindIt.search(data)
if not(matchIt):
mn.pr(" doesn't contain any hierarchical connections description.\n", tag=mn.tgFindErr)
return
#output = re.sub(rexFindIt, replace, data)
bFnd = False
output = ''
codepage = 'utf-8'
for n, line in enumerate(data.splitlines()):
slices = [[], []]
tmpline = line.rstrip('\n')
lmatch = rexFindIt.search(tmpline)
if not(lmatch):
output += tmpline+'\n'
continue
if not(bFnd):
bFnd = True
mn.pr("Replaced:\n")
while tmpline:
srchB, srchE = lmatch.span()
for idp in range(2):
slices[idp].append((tmpline[:srchB], 1))
oldpiece = lmatch.group('Head')+lmatch.group('Delete')
slices[0].append((oldpiece, 2))
newpiece = lmatch.group('Head')
slices[1].append((newpiece, 2))
len0, len1 = len(oldpiece), len(newpiece)
slices[len0>=len1].append((' '*abs(len1-len0), 1))
output += tmpline[:srchB]+newpiece
for Slice in slices:
Slice.append((lmatch.group('Tail'), 2))
output += lmatch.group('Tail')
tmpline = tmpline[srchE:]
lmatch = rexFindIt.search(tmpline)
if not(lmatch):
for idp in range(2):
slices[idp].append((tmpline, 1))
output += tmpline+'\n'
break
mn.pr("Line %3d:\n" % (n+1), tag=mn.tgLineNo)
for idp in range(2):
for txtslice, cTag in slices[idp]:
if txtslice:
mn.pr(txtslice.decode(codepage, 'ignore'), tag=(None, mn.tgFindLine, (mn.tgFindPhrase, mn.tgReplacePhrase)[idp], mn.tgFindErr)[cTag])
mn.pr('\n')
if output != dataCp and(not(ui.checkTest.get_active())):
mv(fileNameNET, fileNameBk)
hFile = open(fileNameNET, 'w')
if not(hFile):
for txtslice, cTag in( ("Can't write to file:'", 0), (fileNameNET, 1), ("'\n", 0) ):
mn.pr(txtslice, tag=(mn.tgFindErr, mn.tgFileName)[cTag])
return
hFile.write(output)
hFile.close()
for txtslice, cTag in(
("Saved in file:\n\t'", 0), (uif(fileNameNET), 1),
("'\nBackup in file:\n\t'", 0), (uif(fileNameBk), 1), ("'\n", 0) ):
mn.pr(txtslice, tag=(None, mn.tgFileName)[cTag])
mn.appUpdateFilename()
else:
for txtslice, cTag in( ("File:'", 0), (uif(fileNameNET), 1), ("' not touched\n", 0) ):
mn.pr(txtslice, tag=(None, mn.tgFileName)[cTag])
else:
for txtslice, cTag in( ("Seems that file was deleted:\t'", 0), (uif(fileNameNET), 1), ("'\n", 0) ):
mn.pr(txtslice, tag=(None, mn.tgFileName)[cTag])
def appFileName(mn):
lastNetFile = mn.cfg['Main']['lastNetFile']
lastDir = ph.dirname(lastNetFile) if lastNetFile else mn.ui.runpath
newFilename = wg.dialogChooseFile(parent=mn.ui.mainWindow,
startDir=lastDir, filters=(mn.ui.netFilter,), title="Select file to read", bShowHidden=True)
if newFilename:
mn.cfg['Main']['lastNetFile'] = newFilename
mn.appUpdateFilename()
def uriPath(mn, uri):
from urllib import url2pathname as u2p
"get the path to file"
path = ""
# Windows nautilus, rox xffm
for prefix in ('file:\\\\\\', 'file://', 'file:'):
if uri.startswith(prefix):
path = u2p(uri[len(prefix):]).strip('\r\n\x00')
break
return path
def dragReceived(mn, widget, context, x, y, selection, target_type, timestamp):
if target_type == mn.TARGET_TYPE_URI_LIST:
uri = selection.data.strip('\r\n\x00')
mn.cfg['Main']['lastNetFile'] = mn.uriPath(uri.split(None, 1)[0])
mn.appUpdateFilename()
return True
def dragMotion(mn, widget, context, x, y, timestamp):
ui = mn.ui
if not(ui.mainWindow.is_active()):
ui.mainWindow.present()
gdk = wg.gtk.gdk
context.drag_status(gdk.ACTION_COPY, timestamp)
return True
def appCfgLoad(mn):
ui = mn.ui
mn.cfg = IniSections()
cfg_fn = mn.ui.runpath+'/'+ ph.basename(__file__)+'.ini'
if ph.isfile(cfg_fn):
mn.cfg.load(cfg_fn)
_p("Config file: '%s'\n" % (cfg_fn))
if not(mn.cfg.filename):
mn.cfg.filename = cfg_fn
mn.lastcfg = mn.cfg['Main'].copy(), mn.cfg['UI'].copy()
from dlgEngine import DialogEngine
ui.cfg = mn.cfg['UI']
ui.dlgEngine = DialogEngine(ui)
ui.restoreGeometry()
def appCfgStore(mn):
mn.ui.storeGeometry()
if mn.lastcfg!=(mn.cfg['Main'], mn.cfg['UI']):
mn.cfg.section_place('Main', 0)
mn.cfg.store()
_p("Written config:%s...\n" % mn.cfg.filename)
def appStart(mn):
mn.appCfgLoad()
mn.appUpdateFilename(False)
mn.appPostUI()
mn.ui.uiEnter()
appStop = lambda mn: mn.appCfgStore()
def Exit(mn):
print("Exiting...\n")
mn.appStop()
mn.ui.uiExit()
# Entry point
if __name__ == "__main__":
fixPCB()