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

Commit 13f6d2e

Browse files
hlgaothms
authored andcommitted
improve interface method lookup, Python 3 adjustment
* lookup methods only once on interface init * add doc string for methods * __dict__ keys are no list anymore in Python 3
1 parent 71c852a commit 13f6d2e

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

bimserver.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import types
23

34
try:
45
import urllib2
@@ -22,7 +23,18 @@ class Api:
2223
class Interface:
2324
def __init__(self, api, name):
2425
self.api, self.name = api, name
25-
26+
meta = self if self.name == "MetaInterface" else self.api.MetaInterface
27+
methods = meta.make_request("getServiceMethods", serviceInterfaceName="org.bimserver."+self.name)
28+
for method in methods:
29+
self.add_method(method)
30+
31+
def add_method(self, methodMeta):
32+
def method(self, **kwargs):
33+
return self.make_request(methodMeta["name"], **kwargs)
34+
method.__name__ = str(methodMeta["name"])
35+
method.__doc__ = methodMeta["doc"]
36+
setattr(self, methodMeta["name"], types.MethodType(method, self))
37+
2638
def make_request(self, method, **kwargs):
2739
request = urlopen(self.api.url, data=json.dumps(dict({
2840
"request": {
@@ -38,12 +50,14 @@ def make_request(self, method, **kwargs):
3850
else: return response["response"]["result"]
3951

4052
def __getattr__(self, method):
53+
print("this should not be called anymore")
4154
return lambda **kwargs: self.make_request(method, **kwargs)
4255

56+
def __repr__(self):
57+
return self.name
58+
4359
def __dir__(self):
44-
methods = self.api.MetaInterface.getServiceMethods(serviceInterfaceName="org.bimserver."+self.name) # TODO use long names
45-
method_names = set(method["name"] for method in methods)
46-
return sorted(set(Api.Interface.__dict__.keys() + self.__dict__.keys()).union(method_names))
60+
return sorted(set(Api.Interface.__dict__.keys() + self.__dict__.keys()))
4761

4862

4963

@@ -54,11 +68,11 @@ def __init__(self, hostname, username=None, password=None):
5468
self.url = "%s/json" % hostname.strip('/')
5569
if not hostname.startswith('http://') and not hostname.startswith('https://'):
5670
self.url = "http://%s" % self.url
57-
71+
5872
self.interfaces = set(si["simpleName"] for si in self.MetaInterface.getServiceInterfaces())
59-
73+
6074
self.version = "1.4" if "Bimsie1AuthInterface" in self.interfaces else "1.5"
61-
75+
6276
if username is not None and password is not None:
6377
auth_interface = getattr(self, "Bimsie1AuthInterface", getattr(self, "AuthInterface"))
6478
self.token = auth_interface.login(
@@ -79,4 +93,4 @@ def __getattr__(self, interface):
7993
raise AttributeError("'%s' is does not name a valid interface on this server" % interface)
8094

8195
def __dir__(self):
82-
return sorted(set(Api.__dict__.keys() + self.__dict__.keys()).union(self.interfaces))
96+
return sorted(set(Api.__dict__.keys()).union(self.__dict__.keys()).union(self.interfaces))

0 commit comments

Comments
 (0)