-
Notifications
You must be signed in to change notification settings - Fork 0
/
invoke.py
80 lines (65 loc) · 1.96 KB
/
invoke.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
"""
SHACL-based validation BIM Bot.
Written by Thomas Krijnen <[email protected]>
"""
import os
import sys
import base64
import logging
# logging.basicConfig(level=logging.DEBUG)
import requests
import json
import pprint as pp
PORT=80
try:
PORT = int(sys.argv[1])
except: pass
class P(pp.PrettyPrinter):
def _format(self, object, *args, **kwargs):
if isinstance(object, str):
if len(object) > 50:
object = object[:47] + '...'
return pp.PrettyPrinter._format(self, object, *args, **kwargs)
pprint = P()
def request(url, body):
print("> POST", url, "HTTP/1.1")
r = requests.post(url, json=body)
for kv in r.request.headers.items():
print("> %s: %s" % kv)
print(">")
for ln in pprint.pformat(body).split("\n"):
print(">", ln)
print("\n")
for kv in r.headers.items():
print("< %s: %s" % kv)
print("<")
J = json.loads(r.content)
for ln in pprint.pformat(J).split("\n"):
print("<", ln)
return r
def invoke_bimbot(url, files):
def file_desc(f):
nm = os.path.basename(f.name)
return {
"identifier": nm.split('.')[0],
"type": "RDF",
"schema": "B4R",
"location": "data:text/turtle;base64," + base64.b64encode(f.read().encode('utf-8')).decode('ascii')
}
return request(url, {
"inputs": list(map(file_desc, files)),
"outputs": [{
"type": "FILE",
"schema": "BCF_ZIP_2_0",
"location": "embedded"
}]
})
if __name__ == "__main__":
resp = invoke_bimbot("http://localhost:%d" % PORT, [open('BIM4Ren_DUNANT_cleaned_IFC2x3.ifc_LBD.ttl', 'r', encoding='utf-8')])
J = json.loads(resp.content)
import io, zipfile
zfb = io.BytesIO(base64.b64decode(J['location'].split(',')[1].encode('ascii')))
zf = zipfile.ZipFile(zfb, "r")
print("\n\n\nBCF contents:")
for p in zf.namelist():
print("./" + p)