-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathGH_to_CPython.py
More file actions
109 lines (97 loc) · 3.42 KB
/
Copy pathGH_to_CPython.py
File metadata and controls
109 lines (97 loc) · 3.42 KB
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
import inspect
import logging
from os import path
import sys
from time import sleep
import scriptcontext
import ghpythonremote
from ghpythonremote.connectors import GrasshopperToPythonRemote
from Grasshopper.Kernel.GH_RuntimeMessageLevel import Error, Warning
local_log_level = getattr(logging, log_level, logging.WARNING)
logger = logging.getLogger("ghpythonremote")
logger.setLevel(local_log_level)
ch = logging.StreamHandler()
ch.setLevel(local_log_level)
formatter = logging.Formatter("%(levelname)s: %(name)s:\n%(message)s")
ch.setFormatter(formatter)
logger.handlers = []
logger.addHandler(ch)
logger = logging.getLogger("ghpythonremote.GH_to_python")
ROOT = path.abspath(path.dirname(inspect.getfile(ghpythonremote)))
rpyc_server_py = path.join(ROOT, "pythonservice.py")
cluster_comp = ghenv.Component.OnPingDocument().Owner
# Set connection to CLOSED if this is the first run
# and initialize set of linked modules
try:
remote_python_status
except NameError:
remote_python_status = "CLOSED"
lkd_modules = set()
# Wait for connection to connect or terminate
timer = 0
while remote_python_status == "CONNECTING" or remote_python_status == "CLOSING":
sleep(1)
timer += 1
if timer == 10:
try:
gh2py_manager.__exit__(*sys.exc_info())
except Exception:
pass
remote_python_status = "CLOSED"
lkd_modules = set()
message = (
"Connection left in an inconsistent state and not returning. Reset "
"everything."
)
if cluster_comp is not None:
cluster_comp.AddRuntimeMessage(Warning, message)
raise RuntimeError(message)
if run:
if not remote_python_status == "OPEN":
remote_python_status = "CONNECTING"
gh2py_manager = GrasshopperToPythonRemote(
rpyc_server_py,
location=location,
timeout=10,
port=None,
log_level=log_level,
working_dir=working_dir,
)
gh2py = gh2py_manager.__enter__()
remote_python_status = "OPEN"
# Stuff that we can reach
rpymod = gh2py.py_remote_modules # A getter function for a named python module
rpy = gh2py.connection # Represents the remote instance root object
scriptcontext.sticky["rpy"] = rpy
# Add modules
for mod in modules:
try:
scriptcontext.sticky[mod] = rpymod(mod)
lkd_modules.add(mod)
except ImportError:
gh2py_manager.__exit__(*sys.exc_info())
if cluster_comp is not None:
cluster_comp.AddRuntimeMessage(
Error,
'Could not import module "{}" in remote Python.'.format(mod)
)
raise
except EOFError:
if cluster_comp is not None:
cluster_comp.AddRuntimeMessage(
Error,
'Remote Python has been closed unexpectedly'
)
raise
elif not remote_python_status == "CLOSED":
remote_python_status = "CLOSING"
# Remove linked modules
for mod in lkd_modules:
del scriptcontext.sticky[mod]
del scriptcontext.sticky["rpy"]
gh2py_manager.__exit__(*sys.exc_info())
lkd_modules = set()
remote_python_status = "CLOSED"
# Change variable name because ghpython resets outputs to None before each run
linked_modules = lkd_modules
logger.info("GH to python connection is {}".format(remote_python_status))