From 704da405f55e2366eb2232cf87d8a6820053ffec Mon Sep 17 00:00:00 2001 From: bookug Date: Mon, 24 Jul 2017 23:11:23 +0800 Subject: [PATCH] docs: add contribution and python3 api --- .../python3/example/PythonAPIExample.py | 27 ++++ api/socket/python3/lib/.gitignore | 3 + api/socket/python3/src/GstoreConnector.py | 148 ++++++++++++++++++ api/socket/python3/src/GstoreConnector.pyc | Bin 0 -> 4807 bytes .../GstoreConnector.cpython-34.pyc | Bin 0 -> 4602 bytes docs/THANK.md | 22 ++- 6 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 api/socket/python3/example/PythonAPIExample.py create mode 100644 api/socket/python3/lib/.gitignore create mode 100644 api/socket/python3/src/GstoreConnector.py create mode 100644 api/socket/python3/src/GstoreConnector.pyc create mode 100644 api/socket/python3/src/__pycache__/GstoreConnector.cpython-34.pyc diff --git a/api/socket/python3/example/PythonAPIExample.py b/api/socket/python3/example/PythonAPIExample.py new file mode 100644 index 0000000..5e22924 --- /dev/null +++ b/api/socket/python3/example/PythonAPIExample.py @@ -0,0 +1,27 @@ +import sys +sys.path.append('../src') +from GstoreConnector import GstoreConnector +# print(sys.path) + +if __name__ == '__main__': + gc = GstoreConnector('127.0.0.1', 3305) + gc.build('LUBM10', 'data/LUBM_10.n3') + gc.load('LUBM10') + + sparql = '''select ?x where { + ?x . + ?y . + ?x ?y. + ?z ?y. + ?z . + ?z ?w. + ?w . + }''' + answer = gc.query(sparql) + print(answer) + + gc.unload('LUBM10') + # gc.load('LUBM10') + # answer = gc.query(sparql) + # print(answer) + # gc.unload('LUBM10') diff --git a/api/socket/python3/lib/.gitignore b/api/socket/python3/lib/.gitignore new file mode 100644 index 0000000..a5baada --- /dev/null +++ b/api/socket/python3/lib/.gitignore @@ -0,0 +1,3 @@ +* +!.gitignore + diff --git a/api/socket/python3/src/GstoreConnector.py b/api/socket/python3/src/GstoreConnector.py new file mode 100644 index 0000000..308ca8d --- /dev/null +++ b/api/socket/python3/src/GstoreConnector.py @@ -0,0 +1,148 @@ +import socket +import traceback + + +class GstoreConnector: + def _connect(self): + try: + self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self._sock.connect((self.ip, self.port)) + return True + except Exception as e: + print('socket connection error. @GstoreConnector.connect') + traceback.print_exc() + return False + + def _disconnect(self): + try: + self._sock.close() + return True + except Exception as e: + print('socket disconnection error. @GstoreConnector.disconnect') + traceback.print_exc() + return False + + def _send(self, msg): + data = self._pack(msg) + self._sock.send(data) + return True + + def _recv(self): + head = self._sock.recv(4) + context_len = 0 + for i in range(4): + # context_len |= (ord(head[i].decode('utf-8')) & 0xFF) << i * 8 + context_len |= (head[i] & 0xFF) << i * 8 + + data = bytearray() + recv_len = 0 + while recv_len < context_len: + chunk = self._sock.recv(context_len - recv_len) + data.extend(chunk) + recv_len += len(chunk) + # .rstrip('\x00') + return data.decode('utf-8') + + def _pack(self, msg): + data_context = bytearray() + data_context.extend(msg.encode(encoding='utf-8')) + context_len = len(data_context) + 1 + data_len = context_len + 4 + + data = bytearray(data_len) + for i in range(4): + # data[i] = chr((context_len >> i * 8) & 0xFF).encode('utf-8') + data[i] = (context_len >> i * 8) & 0xFF + for i, _ in enumerate(data_context): + data[i + 4] = data_context[i] + data[data_len - 1] = 0 + return data + + def _communicate(f): + def wrapper(self, *args, **kwargs): + if not self._connect(): + print('connect to server error. @GstoreConnector.%s' % f.__name__) + return False + + if f.__name__ == 'build': + cmd = 'import' + elif f.__name__ == 'show': + if args[0]: + cmd = 'show all' + else: + cmd = 'show databases' + else: + cmd = f.__name__ + if f.__name__ == 'query': + # DEBUG:only sparql query should be used, output should not be included in parameters + print("the first argument:") + print(args[0]) + # print args[1] + params = ' '.join([args[0]]) + else: + params = ' '.join(map(lambda x: str(x), args)) + full_cmd = ' '.join([ + cmd, + params + ]).strip() + print("command to send to server:") + print(full_cmd) + + if not self._send(full_cmd): + print('send %s command error. @GstoreConnector.build' % cmd) + return False + + recv_msg = self._recv() + self._disconnect() + + succ = { + 'test': 'OK', + 'load': 'load database done.', + 'unload': 'unload database done.', + 'import': 'import RDF file to database done.', + 'drop': 'drop database done.', + 'stop': 'server stopped.', + 'query': None, + 'show all': None, + 'show databases': None, + } + if cmd in succ: + if succ[cmd] == recv_msg: + return True + else: + return recv_msg + return False + + return wrapper + + def __init__(self, ip='127.0.0.1', port=3305): + self.ip = ip + self.port = port + + @_communicate + def test(self): + pass + + @_communicate + def load(self, db_name): + pass + + @_communicate + def unload(self, db_name): + pass + + @_communicate + def build(self, db_name, rdf_file_path): + pass + + @_communicate + def drop(self, db_name): + pass + + @_communicate + def query(self, sparql, output='/'): + pass + + @_communicate + def show(self, _type=False): + pass diff --git a/api/socket/python3/src/GstoreConnector.pyc b/api/socket/python3/src/GstoreConnector.pyc new file mode 100644 index 0000000000000000000000000000000000000000..5615cfd97e2a0341e041566937d8b58d70cff720 GIT binary patch literal 4807 zcmcgw-*X&C5$?HP(w!xrV#~4>yUY>FRR<(84n`&^;W$XPOr?;iJtrap*VbxxC7pD) zciP#rB}$^G$ORM+Jn&Bb0p5AaKa}V2#0z|1&z?>(#6I(Y&pX@G+tWSWUr$fl#jmDo zU;HEeG?e^L5$}6=>`e?2{u=3sY@E_YHjG3CiAoX`<+;d-$fp~a8)-GmSkwL&{;Xwn zkgGQ9rSWnvN#YRH*KGWhHom|RLpwAIbvZpGinKvuXlA2KljKw+Dl0J|5onB)^1L7$ zlM;ij^3w@+S=DK6IIZdQ+;DnMr&Gh}luoCI(`lWam#8k!jcPkjPF$C0X2_Wluho15 z4ul3n0S~=j#$$Id9DX2~$gb#UbS%h8L5@WHBFEjL?3R=$>sXO32pBQhgtXR^x1^L! z%3e(nHggO%0=)V#tIBSTAvcMUUuNeJb6Q@rw`jK)?!{Rnbn$NYl17}Sy>y{*?*$+h za*^f~R%MmbrLZjW;RF9?Yxf^Rw@B;J@<)E_v3LK&pJ(*WrwNtHHDt8j?q?;u`n@z$ z;&D2Nu~Cux2VvaLuo0bQX%NPn0Tl3CKW!(OA0LFO_(9O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BA%rId`;OQ+FxK0GlgHNP&ZXvS2a^D;AiTEGN}2&Ra|04a{;rmN|aC(YlWhj zc;UxlIRh;dM)%>`xn*egRxmgolhz~e6}x1iN0IM#2T41`Meq|YVaU)AEn40Yx8A+8 z@B{pAwZDXL$k6d&x~YD`fbxYiFnbU&IDE=J_NJUrgJ5cv(o=!Ce|g5*ohsKeS7(cx%Q z6MAfuKg62f;`!f9daGDEwyoO`{_KpcA-FGfk3z0hpskFKrqPzq(twsd+Zo&Xcd-(8 z`Txb!AHmL_Mz*ws-#ks`@%+^aVo@*lJ4*HjS$~j??JjS^KaaMcZR%^}61!PHJM70} z8~i?2{AFZN|EIy@KQeJsLzmy}MFU{wd#t@$M9#Q>|62)pB*FTCcuWFO7ahQ!f_tTBpfJxZJ;KLA;(# RE$vIr^SjieB?MKu_-_jyWpn@l literal 0 HcmV?d00001 diff --git a/api/socket/python3/src/__pycache__/GstoreConnector.cpython-34.pyc b/api/socket/python3/src/__pycache__/GstoreConnector.cpython-34.pyc new file mode 100644 index 0000000000000000000000000000000000000000..a70141e42181d9fa72b996417419a95fee426bb1 GIT binary patch literal 4602 zcmbtXOK%(36+U-n_!RYi(vlrIg@H1N={N}FI#p!_vK16T=^#{~hN&?a%^gXU$RTxS zXiEb-1!AKuy6B>d?z-r*2(s;}zht&uX_Hk~-n8Gj!-q^sZH*2!cg~zS=RUsk)DMO6 z{NMknzWQ#8=pS_MGC^gQQ8Xz%}WmL3O+es+FxXC{fkr zv42@0HgE?&VDK=N;As;LOoKVWB-5G(ks+DYbed$2WM0!?j*%>AI!kh# zT||5r%3WzpEIF3 z%OqDwUZ+<=Pd=%cH%Puiveaizk-P~!-i;?e4Z^naHru}Mxu9GD6!rOh4oQzfj|OA=oX9qLJT&Rh(nLy+Y1)H;Ai>b@`1i>QGjxRiw!e7v$uHuR&7bR;oTwKLgl#LuHzoXInA-X6Sr){R^T=FV&VOj)HtAeU942@1j@ZLx~a}-oV9xkr5RX8 zn1m^o#fr#^8Ic#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%i#u<1K>SaL3~=0x)9r59>4u|+dk+i#;oPDZ4yQ}S)?1yfp0dMJB=HfCEMu8J zo?Gg|`M63|%hT#32*r8ZZnb3>HnHP^Z69}?W|9q)49RKlTo&X8OL6Ec&Rg2~wOcB7 z8pRe=46owjD&EMs&FO7&B2gR#9L_-rLqQbsg&5LrF9A%x|Q3pOIH~$R^3%ptY literal 0 HcmV?d00001 diff --git a/docs/THANK.md b/docs/THANK.md index f33cbca..ed9934e 100644 --- a/docs/THANK.md +++ b/docs/THANK.md @@ -1,4 +1,24 @@ **This chapter lists people who inspire us or contribute to this project.** -*nobody now* +--- + +fei123581321@qq.com 王定峰 python3 in socket API + +--- + +王力博 + +--- + +吕鑫 + +--- + +邓智源 + +--- + +崔昊 + +---