-
Notifications
You must be signed in to change notification settings - Fork 4
/
klive_server.py
217 lines (182 loc) · 7.93 KB
/
klive_server.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
import json
from dataclasses import dataclass
from pathlib import Path
from time import sleep
from typing import Optional
import pya
_path = Path(__file__).parent.parent
off = str(_path / "Koff.png")
live = str(_path / "Klive.png")
recv = str(_path / "Krecv.png")
poff = str(_path / "PortOff.png")
pon = str(_path / "PortOn.png")
polygon_dict = {}
dpolygon_dict = {}
shapes_shown = {}
class ServerInstance(pya.QTcpServer):
"""
Implements a TCP server listening on port 8082.
You can use it to instantly load a GDS or lyrdb (Results Database) file, programmatically, from Python.
Just send a JSON-formatted command to localhost:8082.
See README for more details.
"""
def new_connection(self):
"""
Handler for a new connection
"""
try:
self.action.icon = recv
self.app.process_events()
url = None
# Get a new connection object
connection = self.nextPendingConnection()
# Read in the request
data = None
while (
connection.isOpen()
and connection.state() == pya.QTcpSocket.ConnectedState
):
if connection.canReadLine():
line = connection.readLine()
data = json.loads(line)
# Interpret the data
gds_path = data["gds"]
# Store the current view
window = pya.Application.instance().main_window()
current_view = window.current_view()
previous_view = current_view.box() if current_view else None
send_data = {"version": "0.3.3", "klayout_version": pya.__version__}
libs = data.get("libraries", {})
for lib_dict in libs:
lib = pya.Library()
lib.register(lib_dict["name"])
lib.layout().read(lib_dict["file"])
def load_existing_layout():
for i in range(window.views()):
view = window.view(i)
for j in range(view.cellviews()):
if view.active_cellview().filename() == gds_path:
print(
"File {} already openend".format(
view.active_cellview().filename()
)
)
window.current_view_index = i
view.active_setview_index = j
view.reload_layout(j)
if view.active_cellview().cell is None:
view.active_cellview().cell = (
view.active_cellview()
.layout()
.top_cells()[0]
)
send_data["type"] = "reload"
send_data["file"] = gds_path
connection.write(
json.dumps(send_data).encode("utf-8")
)
connection.flush()
return view
else:
# Load the new layout
new_cview = window.load_layout(gds_path, 1)
new_view = new_cview.view()
new_view.max_hier()
window.current_view_index = window.index_of(new_view)
send_data["type"] = "open"
send_data["file"] = gds_path
connection.write(json.dumps(send_data).encode("utf-8"))
connection.flush()
return new_view
if window.views() > 0:
view = load_existing_layout()
else:
# Load the new layout
window.load_layout(gds_path, 1)
# Restore the previous position
view = window.current_view()
view.max_hier()
if previous_view and data["keep_position"] == True:
view.zoom_box(previous_view)
# Report progress
print("Loaded {}".format(gds_path))
send_data["type"] = "open"
send_data["file"] = gds_path
connection.write(json.dumps(send_data).encode("utf-8"))
connection.flush()
if "lyrdb" in data:
lyrdb_path = data["lyrdb"]
rdb = pya.ReportDatabase().load(lyrdb_path)
rdb_i = view.add_rdb(rdb)
view.show_rdb(rdb_i, view.active_cellview().cell_index)
if "l2n" in data:
l2n_path = data["l2n"]
l2n = pya.LayoutToNetlist()
l2n.read(l2n_path)
l2n_i = view.add_l2ndb(l2n)
view.show_l2ndb(l2n_i, view.active_cellview().cell_index)
else:
connection.waitForReadyRead(100)
# Disconnect
connection.disconnectFromHost()
signal = pya.qt_signal("disconnected()")
slot = pya.qt_slot("deleteLater()")
pya.QObject.connect(connection, signal, connection, slot)
self.action.icon = live
except Exception as ex:
print("ERROR " + str(ex))
def __init__(self, server, parent=None, action=None):
"""
Initialize the server and put into listen mode (port is tcp/8082)
"""
super(ServerInstance, self).__init__(parent)
ha = pya.QHostAddress.new_special(pya.QHostAddress.LocalHost)
self.listen(ha, 8082)
ha6 = pya.QHostAddress("::1")
self.listen(ha6, 8082)
self.newConnection(self.new_connection)
self.action = action
self.server = server
if self.action is not None and self.isListening():
self.action.on_triggered = self.on_action_click
print("klive 0.3.3 is running")
self.action.icon = live
else:
print("klive didn't start correctly. Most likely port tcp/8082")
self.app = app = pya.Application.instance()
def on_action_click(self):
self.server.reset(self.action)
def close(self):
super().close()
print("klive 0.3.3 stopped")
if self.action is not None and not self.action._destroyed():
self.action.icon = off
def __del__(self):
self.close()
super(ServerInstance, self).__del__()
def update_icon(action):
acv = pya.CellView.active()
idx = acv.index()
cell = acv.cell
if cell is not None:
cidx = cell.cell_index()
if idx in shapes_shown and cidx in shapes_shown[idx]:
action.icon = pon
action.icon_text = "Hide Ports"
action.tool_tip = "Current Status: Shown"
else:
action.icon = poff
action.icon_text = "Show Ports"
action.tool_tip = "Current Status: Hidden"
@dataclass
class KliveServer:
instance: Optional[ServerInstance] = None
def reset(self, action):
app = pya.Application.instance()
mw = app.main_window()
if self.instance is not None and self.instance.isListening():
self.instance.close()
app.process_events()
sleep(0.1)
self.instance = ServerInstance(self, parent=mw, action=action)
server = KliveServer()