diff options
| author | Alon Levy <alon@pobox.com> | 2015-02-05 16:45:35 +0200 |
|---|---|---|
| committer | Alon Levy <alon@pobox.com> | 2015-02-05 16:45:35 +0200 |
| commit | aaf92e18e5a378fbd6766717fc0f7013c3677314 (patch) | |
| tree | 4813686aa281c914a70fd40a77ce87ee7dc19ab5 /src | |
| parent | a881241230fb384a72dbe8f5ef8ef30eda18cf56 (diff) | |
neo4j-cypher: reuse existing infra badly
Diffstat (limited to 'src')
| -rwxr-xr-x | src/local/neo4j-cypher | 30 | ||||
| -rw-r--r-- | src/local/rhizi_tools_neo4j.py | 43 |
2 files changed, 23 insertions, 50 deletions
diff --git a/src/local/neo4j-cypher b/src/local/neo4j-cypher index c2aee707..0ef3065e 100755 --- a/src/local/neo4j-cypher +++ b/src/local/neo4j-cypher @@ -4,6 +4,27 @@ import json import sys import time import argparse +import os +import logging + +root = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..')) +sys.path.append(os.path.join(root, 'src/server')) +import db_controller as dbc +from db_op import DBO_cypher_query +from rz_server import Config + +rhizi_server_conf_filename = os.path.join(root, 'res', 'etc', 'rhizi-server.conf') + +class Server(object): + def __init__(self): + cfg = Config.init_from_file(rhizi_server_conf_filename) + self.db_ctl = dbc.DB_Controller(cfg) + self.log = logging.getLogger('rhizi') + self.log.addHandler(logging.StreamHandler()) + + def run(self, statements): + for statement in statements: + print(repr(list(DBO_cypher_query([statement])))) help = """Welcome to neo4j-cypher, a tool to submit cypher queries to neo4j This tool follows the neo4j REST API and allows submitting queries from command @@ -11,20 +32,15 @@ line or standard in without using neo4j-shell since that doesn't work on all mac It relies on curl right now although this could be replaced with straight python. """ -import rhizi_tools_neo4j def main(statements): - one_at_a_time = False + server = Server() if len(statements) == 0: statements = [l.strip() for l in sys.stdin.readlines() if l.strip()[:2] != '//' and l.strip() != ""] - one_at_a_time = True print("----------------------------------------------------------------------") print "\n".join(statements) print("----------------------------------------------------------------------") - results = rhizi_tools_neo4j.run(statements=statements, one_at_a_time=one_at_a_time) - for result in results: - json.dump(result, sys.stdout, sort_keys=True, indent=4, separators=(',', ': ')) - print() + results = server.run(statements) if __name__ == '__main__': parser = argparse.ArgumentParser(description=help, add_help=True) diff --git a/src/local/rhizi_tools_neo4j.py b/src/local/rhizi_tools_neo4j.py deleted file mode 100644 index be8b4a5d..00000000 --- a/src/local/rhizi_tools_neo4j.py +++ /dev/null @@ -1,43 +0,0 @@ -import subprocess -import re -import json - -verbose = False - -BASE="http://rhizi.local:7474/db/data" - -def c(cmd, incoming=None): - if verbose: - print("executing %r" % cmd) - print(incoming) - p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - if incoming: - p.stdin.write(incoming) - if p.stdin: - p.stdin.close() - p.wait() - ret = p.stdout.read() - return ret - -def curl(path, incoming): - return c(["curl", "-H", 'Accept: application/json', "-H", 'Content-Type: application/json', '-XPOST', '-d@-', path], - incoming=incoming) - -def transact(datum): - transaction_output = curl(BASE + "/transaction/", '{"statements": [ ] }') - commit = re.search("http://.*commit", transaction_output).group() - ret = curl(commit, json.dumps(datum)) - obj = json.loads(ret) - return obj - -def run(statements, one_at_a_time): - def single_request_data(statements): - return {'statements': [{'statement': s} for s in statements]} - results = [] - if one_at_a_time: - data = [single_request_data([statement]) for statement in statements] - else: - data = [single_request_data(statements)] - for datum in data: - results.append(transact(datum)) - return results |
