-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathconvert_py2to3.py
576 lines (494 loc) · 13.4 KB
/
convert_py2to3.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
"""Utility to convert py2 rhino-python files to py3 format"""
import os
import os.path as op
import shutil
from lib2to3.main import main # this is what 2to3 cli utility uses
# read sources
SOURCE_DIR = r"../Scripts/"
DEST_DIR = r"../python3/"
ITEMS = [
r"rhinoscript/",
r"rhinoscriptsyntax.py",
r"scriptcontext.py",
]
THIS_DIR = op.dirname(__file__)
ROOT_DIR = op.dirname(THIS_DIR)
print(f"{THIS_DIR=}")
print(f"{ROOT_DIR=}")
class SourceFile:
"""Context manager for a python source file"""
def __init__(self, filename) -> None:
self.filename = filename
self.source = ""
def __enter__(self):
with open(self.filename, "r") as rf:
self.source = rf.read()
return self
def __exit__(self, exc_type, exc_value, exc_traceback):
with open(self.filename, "w") as wf:
wf.write(self.source)
def replace(self, *args, **kwargs):
"""Replace string in source"""
self.source = self.source.replace(*args, **kwargs)
def prep_dest(source, dest):
"""Prepare a new copy of source files"""
if op.exists(dest):
print(f"Deleting: {dest}")
if op.isdir(dest):
shutil.rmtree(dest)
else:
os.remove(dest)
print(f"Updating: {dest}")
if op.isdir(source):
shutil.copytree(source, dest)
else:
shutil.copy(source, dest)
def convert_dest(dest):
"""Convert files to python3-compatible"""
print(f"Converting: {dest}")
main("lib2to3.fixes", args=["--write", "--nobackups", "--no-diffs", dest])
def apply_dest_fixes(dest):
"""Apply misc fixes to files"""
def apply_fixes(item):
item_name = op.splitext(op.basename(item))[0]
fixer_func_name = f"{item_name}_fixes"
if fixer_func := globals().get(fixer_func_name, None):
fixer_func(item)
print(f"Applying Fixes: {dest}")
if op.isdir(dest):
for dest_item in os.listdir(dest):
apply_fixes(op.join(dest, dest_item))
else:
apply_fixes(dest)
# Fixers ======================================================================
# function naming format is <file_name>_fixes
# e.g. scriptcontext_fixes applies fixes to scriptcontext.py
# -----------------------------------------------------------------------------
def scriptcontext_fixes(item):
"""Fix misc items in scriptcontext.py"""
with SourceFile(item) as sf:
sf.replace(
"import RhinoPython.Host as __host", "import Rhino"
)
sf.replace("doc = None", "doc = Rhino.RhinoDoc.ActiveDoc")
sf.replace(
"rc = __host.EscapePressed(reset)",
"rc = None # __host.EscapePressed(reset)",
)
def application_fixes(item):
"""Fix misc items in rhinoscript/application.py"""
with SourceFile(item) as sf:
sf.replace(
"""
import scriptcontext
import Rhino
import Rhino.ApplicationSettings.ModelAidSettings as modelaid
import Rhino.Commands.Command as rhcommand
import System.TimeSpan, System.Enum, System.Environment
import System.Windows.Forms.Screen
import datetime
from . import utility as rhutil
""".strip(),
"""
import datetime
import scriptcontext
from . import utility as rhutil
import System
from Rhino.ApplicationSettings import ModelAidSettings as modelaid
from Rhino.Commands import Command as rhcommand
from System import TimeSpan, Enum, Environment
from System.Windows.Forms import Screen
""".strip(),
)
sf.replace(
"Rhino.PlugIns.PlugInType.None", "getattr(Rhino.PlugIns.PlugInType, 'NONE')"
)
sf.replace("filter =", "search_filter =")
sf.replace("filter |=", "search_filter |=")
sf.replace(
"GetInstalledPlugInNames(filter,", "GetInstalledPlugInNames(search_filter,"
)
def block_fixes(item):
"""Fix misc items in rhinoscript/block.py"""
with SourceFile(item) as sf:
sf.replace(
"""
import Rhino
import scriptcontext
from . import utility as rhutil
import math
import System.Guid
""".strip(),
"""
import math
import scriptcontext
from . import utility as rhutil
import System
import Rhino
from System import Guid
""".strip(),
)
def curve_fixes(item):
"""Fix misc items in rhinoscript/curve.py"""
with SourceFile(item) as sf:
sf.replace(
"""
import scriptcontext
from . import utility as rhutil
import Rhino
import math
import System.Guid, System.Array, System.Enum
""".strip(),
"""
import math
import scriptcontext
from . import utility as rhutil
import System
import Rhino
from System import Guid, Array, Enum
""".strip(),
)
sf.replace(
"Rhino.DocObjects.ObjectDecoration.None",
"getattr(Rhino.DocObjects.ObjectDecoration, 'NONE')",
)
sf.replace("ValueException(", "ValueError(")
def dimension_fixes(item):
"""Fix misc items in rhinoscript/dimension.py"""
with SourceFile(item) as sf:
sf.replace(
"""
import scriptcontext
from . import utility as rhutil
import Rhino
import System.Guid
from .view import __viewhelper, ViewCPlane
""".strip(),
"""
import scriptcontext
from . import utility as rhutil
from .view import __viewhelper, ViewCPlane
import System
import Rhino
from System import Guid
""".strip(),
)
def document_fixes(item):
"""Fix misc items in rhinoscript/document.py"""
with SourceFile(item) as sf:
sf.replace(
"""
import scriptcontext
import Rhino
import System.Enum, System.Drawing.Size
import System.IO
from . import utility as rhutil
import math
""".strip(),
"""
import math
import scriptcontext
from . import utility as rhutil
import System
import Rhino
from System import IO
from System import Enum
from System.Drawing import Size
""".strip(),
)
def geometry_fixes(item):
"""Fix misc items in rhinoscript/geometry.py"""
with SourceFile(item) as sf:
sf.replace(
"""
import scriptcontext
from . import utility as rhutil
import Rhino
import System.Guid, System.Array
""".strip(),
"""
import scriptcontext
from . import utility as rhutil
import System
import Rhino
from System import Guid, Array
""".strip(),
)
def hatch_fixes(item):
"""Fix misc items in rhinoscript/hatch.py"""
with SourceFile(item) as sf:
sf.replace(
"""
import scriptcontext
from . import utility as rhutil
import Rhino
import System.Guid
""".strip(),
"""
import scriptcontext
from . import utility as rhutil
import System
import Rhino
from System import Guid
""".strip(),
)
def layer_fixes(item):
"""Fix misc items in rhinoscript/layer.py"""
with SourceFile(item) as sf:
sf.replace(
"""
import Rhino.DocObjects.Layer
import scriptcontext
from . import utility as rhutil
import System.Guid
from Rhino.RhinoMath import UnsetIntIndex
""".strip(),
"""
import scriptcontext
from . import utility as rhutil
import System
import Rhino
from System import Guid
UnsetIntIndex = Rhino.RhinoMath.UnsetIntIndex
Layer = Rhino.DocObjects.Layer
""".strip(),
)
sf.replace("if idx is 0:", "if idx == 0:")
def light_fixes(item):
"""Fix misc items in rhinoscript/light.py"""
with SourceFile(item) as sf:
sf.replace("Rhino.UnitSystem.None", "getattr(Rhino.UnitSystem, 'NONE')")
def line_fixes(item):
"""Fix misc items in rhinoscript/line.py"""
with SourceFile(item) as sf:
sf.replace(
"Rhino.Geometry.Intersect.LineCylinderIntersection.None",
"getattr(Rhino.Geometry.Intersect.LineCylinderIntersection, 'NONE')",
)
sf.replace(
"Rhino.Geometry.Intersect.LineSphereIntersection.None",
"getattr(Rhino.Geometry.Intersect.LineSphereIntersection, 'NONE')",
)
sf.replace("Execption(", "Exception(")
def mesh_fixes(item):
"""Fix misc items in rhinoscript/mesh.py"""
with SourceFile(item) as sf:
sf.replace(
"""
import scriptcontext
from . import utility as rhutil
import Rhino
import System.Guid, System.Array, System.Drawing.Color
from .view import __viewhelper
""".strip(),
"""
import scriptcontext
from . import utility as rhutil
from .view import __viewhelper
import System
import Rhino
from System import Guid, Array
from System.Drawing import Color
""".strip(),
)
def object_fixes(item):
"""Fix misc items in rhinoscript/object.py"""
with SourceFile(item) as sf:
sf.replace(
"""
import scriptcontext
import Rhino
from . import utility as rhutil
import System.Guid, System.Enum
from .layer import __getlayer
from .view import __viewhelper
import math
""".strip(),
"""
import math
import scriptcontext
from . import utility as rhutil
from .layer import __getlayer
from .view import __viewhelper
import System
import Rhino
from System import Guid, Enum
""".strip(),
)
def selection_fixes(item):
"""Fix misc items in rhinoscript/selection.py"""
with SourceFile(item) as sf:
sf.replace(
"Rhino.DocObjects.ObjectType.None",
"getattr(Rhino.DocObjects.ObjectType, 'NONE')",
)
sf.replace("def __FilterHelper(filter):", "def __FilterHelper(input_filter):")
sf.replace("if filter &", "if input_filter &")
sf.replace(
"""
class CustomGetObject(Rhino.Input.Custom.GetObject):
def __init__(self, filter_function):
self.m_filter_function = filter_function
""".strip(),
"""
class CustomGetObject(Rhino.Input.Custom.GetObject):
def __init__(self, filter_function):
super().__init__()
self.m_filter_function = filter_function
""".strip(),
)
sf.replace(
"""
class __CustomGetObjectEx(Rhino.Input.Custom.GetObject):
def __init__(self, allowable_geometry):
self.m_allowable = allowable_geometry
""".strip(),
"""
class __CustomGetObjectEx(Rhino.Input.Custom.GetObject):
def __init__(self, allowable_geometry):
super().__init__()
self.m_allowable = allowable_geometry
""".strip(),
)
def surface_fixes(item):
"""Fix misc items in rhinoscript/surface.py"""
with SourceFile(item) as sf:
sf.replace(
"""
import scriptcontext
import math
import Rhino
import System.Guid
from . import utility as rhutil
from . import object as rhobject
from System.Collections.Generic import List
""".strip(),
"""
import math
import scriptcontext
from . import utility as rhutil
from . import object as rhobject
import System
import Rhino
from System import Guid
from System.Collections.Generic import List
""".strip(),
)
def transformation_fixes(item):
"""Fix misc items in rhinoscript/transformation.py"""
with SourceFile(item) as sf:
sf.replace(
"""
import scriptcontext
from . import utility as rhutil
import Rhino
import System.Guid, System.Array
import math
from . import view as rhview
""".strip(),
"""
import math
import scriptcontext
from . import utility as rhutil
from . import view as rhview
import System
import Rhino
from System import Guid, Array
""".strip(),
)
def userinterface_fixes(item):
"""Fix misc items in rhinoscript/userinterface.py"""
with SourceFile(item) as sf:
sf.replace(
"""
import Rhino
import Rhino.UI
from . import utility as rhutil
import scriptcontext
import System.Drawing.Color
import System.Enum
import System.Array
import Eto.Forms
import System.Windows.Forms
import math
from .view import __viewhelper
""".strip(),
"""
import math
import scriptcontext
from . import utility as rhutil
from .view import __viewhelper
import System
import Rhino
import Rhino.UI
import Eto.Forms
import System.Windows.Forms
from System import Enum, Array
from System.Drawing import Color
""".strip(),
)
sf.replace(
"Rhino.UI.ShowMessageIcon.None",
"getattr(Rhino.UI.ShowMessageIcon, 'NONE')",
)
def utility_fixes(item):
"""Fix misc items in rhinoscript/utility.py"""
with SourceFile(item) as sf:
sf.replace(
"""
import Rhino
import System.Drawing.Color, System.Array, System.Guid
import time
import System.Windows.Forms.Clipboard
import scriptcontext
import math
import string
import numbers
import RhinoPython.Host as __host
""".strip(),
"""
import time
import math
import string
import numbers
import scriptcontext
import System
import Rhino
from System import Array, Guid
from System.Drawing import Color
from System.Windows.Forms import Clipboard
# import RhinoPython.Host as __host
""".strip(),
)
sf.replace(
"= __host.Coerce3dPointFromEnumerables(point)",
"= None # __host.Coerce3dPointFromEnumerables(point)",
)
def view_fixes(item):
"""Fix misc items in rhinoscript/view.py"""
with SourceFile(item) as sf:
sf.replace(
"""
import scriptcontext
from . import utility as rhutil
import Rhino
import System.Enum
import math
""".strip(),
"""
import math
import scriptcontext
from . import utility as rhutil
import System
import Rhino
from System import Enum
""".strip(),
)
# =============================================================================
if __name__ == "__main__":
for item in ITEMS:
source = op.normpath(op.join(THIS_DIR, SOURCE_DIR, item))
dest = op.normpath(op.join(THIS_DIR, DEST_DIR, item))
prep_dest(source, dest)
convert_dest(dest)
apply_dest_fixes(dest)