Skip to content
This repository was archived by the owner on Jan 6, 2021. It is now read-only.

Commit e3f4079

Browse files
committed
truly minor modifications to names in components.py
1 parent b1f80fc commit e3f4079

1 file changed

Lines changed: 19 additions & 20 deletions

File tree

package/components.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55

66
def __make_function__(helper):
77
def component_function(*args, **kwargs):
8-
obj = helper.proxy
9-
comp = obj.CreateInstance()
8+
comp = helper.comp
109
comp.ClearData()
1110
if args:
1211
for i, arg in enumerate(args):
1312
if arg is None: continue
1413
param = comp.Params.Input[i]
1514
param.PersistentData.Clear()
16-
if hasattr(arg, '__iter__'): #TODO deal with polyline
15+
if hasattr(arg, '__iter__'): #TODO deal with polyline, str
1716
[param.AddPersistentData(a) for a in arg]
1817
else:
1918
param.AddPersistentData(arg)
@@ -23,7 +22,7 @@ def component_function(*args, **kwargs):
2322
if name in kwargs:
2423
param.PersistentData.Clear()
2524
arg = kwargs[name]
26-
if hasattr(arg, '__iter__'): #TODO deal with polyline
25+
if hasattr(arg, '__iter__'): #TODO deal with polyline, str
2726
[param.AddPersistentData(a) for a in arg]
2827
else:
2928
param.AddPersistentData(arg)
@@ -43,6 +42,7 @@ def __init__(self):
4342
class function_helper(object):
4443
def __init__(self, proxy):
4544
self.proxy = proxy
45+
self.comp = proxy.CreateInstance()
4646
self.return_type = None
4747

4848
def create_output(self, params):
@@ -87,9 +87,11 @@ def function_description(description, params):
8787
rc.append(s.format(out.Name.lower(), out.TypeName, out.Description))
8888
return '\n'.join(rc)
8989

90-
import sys, types, string
90+
import sys, types, re
9191
core_module = sys.modules['ghpython.components']
92-
translation = string.maketrans("'()*|+&", "_______")
92+
translate_from = u"|+-*\u2070\u00B9\u00B2\u00B3\u2074\u2075\u2076\u2077\u2078\u2079"
93+
translate_to = "X__x0123456789"
94+
transl = dict(zip(translate_from, translate_to))
9395
for obj in GH.Instances.ComponentServer.ObjectProxies:
9496
if obj.Exposure == GH.Kernel.GH_Exposure.hidden or obj.Obsolete:
9597
continue
@@ -100,26 +102,23 @@ def function_description(description, params):
100102
library_id = obj.LibraryGuid
101103
assembly = GH.Instances.ComponentServer.FindAssembly(library_id)
102104
if not assembly.IsCoreLibrary:
103-
module_name = assembly.Assembly.GetName().Name
105+
module_name = assembly.Assembly.GetName().Name.split('.', 1)[0]
106+
if module_name.upper().startswith("GH_"): module_name = module_name[3:]
104107
if module_name in core_module.__dict__:
105108
m = core_module.__dict__[module_name]
106109
else:
107110
m = namespace_object()
108111
setattr(core_module, module_name, m)
109-
name = obj.Desc.Name.Replace(" ", "")
110-
if "LEGACY" in name or "#" in name:
111-
continue
112-
name = name.translate(translation)
113-
if not name[0].isalpha(): name = '_' + name
112+
name = obj.Desc.Name
113+
if "LEGACY" in name or "#" in name: continue
114+
name = re.sub("[^a-zA-Z0-9]", lambda match: transl[match.group()] if (match.group() in transl) else '', name)
115+
if not name[0].isalpha(): name = 'x' + name
114116
function = __make_function__(function_helper(obj))
115-
if m == core_module:
116-
setattr(m, name, function)
117-
comp = obj.CreateInstance()
118-
a = m.__dict__[name]
119-
a.__name__ = name
120-
a.__doc__ = function_description(obj.Desc.Description, comp.Params)
121-
else:
122-
setattr(m, name, types.MethodType(function, m, type(m)))
117+
setattr(m, name, function)
118+
comp = obj.CreateInstance()
119+
a = m.__dict__[name]
120+
a.__name__ = name
121+
a.__doc__ = function_description(obj.Desc.Description, comp.Params)
123122

124123

125124
__build_module()

0 commit comments

Comments
 (0)