From 01f3fa159f2f65004bd6bbc41bbcb9807fe11f19 Mon Sep 17 00:00:00 2001 From: zhangxiaoyang Date: Wed, 4 May 2016 22:26:54 +0800 Subject: [PATCH] add python api --- api/python/.gitignore | 1 + api/python/example/PythonAPIExample.py | 27 ++++++ api/python/lib/.gitignore | 3 + api/python/src/GstoreConnector.py | 124 +++++++++++++++++++++++++ 4 files changed, 155 insertions(+) create mode 100644 api/python/.gitignore create mode 100644 api/python/example/PythonAPIExample.py create mode 100644 api/python/lib/.gitignore create mode 100644 api/python/src/GstoreConnector.py diff --git a/api/python/.gitignore b/api/python/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/api/python/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/api/python/example/PythonAPIExample.py b/api/python/example/PythonAPIExample.py new file mode 100644 index 0000000..1ec864d --- /dev/null +++ b/api/python/example/PythonAPIExample.py @@ -0,0 +1,27 @@ +# coding: utf-8 +# zhangxiaoyang.hit#gmail.com +# github.com/zhangxiaoyang + +import sys +sys.path.append('../src') +from GstoreConnector import GstoreConnector + +if __name__ == '__main__': + gc = GstoreConnector('127.0.0.1', 3305) + gc.build('LUBM10.db', 'data/LUBM_10.n3') + sparql = '''select ?x where { + ?x . + ?y . + ?x ?y. + ?z ?y. + ?z . + ?z ?w. + ?w . + }''' + answer = gc.query(sparql) + print answer + + gc.unload('LUBM10.db') + gc.load('LUBM10.db') + answer = gc.query(sparql) + print answer diff --git a/api/python/lib/.gitignore b/api/python/lib/.gitignore new file mode 100644 index 0000000..a5baada --- /dev/null +++ b/api/python/lib/.gitignore @@ -0,0 +1,3 @@ +* +!.gitignore + diff --git a/api/python/src/GstoreConnector.py b/api/python/src/GstoreConnector.py new file mode 100644 index 0000000..ad295d6 --- /dev/null +++ b/api/python/src/GstoreConnector.py @@ -0,0 +1,124 @@ +# coding: utf-8 +# zhangxiaoyang.hit#gmail.com +# github.com/zhangxiaoyang + +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, e: + print 'socket disconnection error. @GstoreConnector.disconnect' + traceback.print_exc() + return False + + def _disconnect(self): + try: + self._sock.close() + return True + except Exception, 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]) << i * 8) + + data = bytearray(context_len) + recv_len = 0 + while recv_len < context_len: + chunk = self._sock.recv(1024) + data.extend(chunk) + recv_len += len(chunk) + return str(data) + + def _pack(self, msg): + data_context = bytearray() + data_context.extend(msg) + 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) + 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': + cmd = 'show databases' + else: + cmd = f.__name__ + params = ' '.join(map(lambda x:str(x), args)) + full_cmd = ' '.join([ + cmd, + params + ]).strip() + + if not self._send(full_cmd): + print 'send %s command error. @GstoreConnector.build' % cmd + return False + + recv_msg = self._recv() + self._disconnect() + + succ = { + 'load': 'load database done.', + 'unload': 'unload database done.', + 'import': 'import RDF file to database done.', + 'query': 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 load(self, db_name): + pass + + @_communicate + def unload(self, db_name): + pass + + @_communicate + def build(self, db_name, rdf_file_path): + pass + + @_communicate + def query(self, sparql): + pass + + @_communicate + def show(self): + pass