<s> from __future__ import unicode_literals , print_function <EOL> import sys <EOL> import re <EOL> import os <EOL> import csv <EOL> from datetime import datetime <EOL> import click <EOL> from prompt_toolkit import Application , CommandLineInterface , AbortAction <EOL> from prompt_toolkit . buffer import AcceptAction <EOL> from prompt_toolkit . history import FileHistory <EOL> from prompt_toolkit . shortcuts import create_prompt_layout , create_eventloop <EOL> from prompt_toolkit . styles import PygmentsStyle <EOL> from pygments . token import Token <EOL> from cycli import __version__ <EOL> from cycli . style import CypherLexer , CypherStyle <EOL> from cycli . completer import CypherCompleter <EOL> from cycli . buffer import CypherBuffer <EOL> from cycli . binder import CypherBinder <EOL> from cycli . driver import Neo4j , AuthError , ConnectionError <EOL> from cycli . table import pretty_table <EOL> from cycli . cypher import Cypher <EOL> def get_tokens ( x ) : <EOL> return [ ( Token . Prompt , "<STR_LIT>" ) ] <EOL> class Cycli : <EOL> def __init__ ( self , host , port , username , password , logfile , filename , ssl , read_only , timeout ) : <EOL> self . logfile = logfile <EOL> self . filename = filename <EOL> self . read_only = read_only <EOL> self . neo4j = Neo4j ( host , port , username , password , ssl , timeout ) <EOL> self . cypher = Cypher ( ) <EOL> def write_to_logfile ( self , query , response ) : <EOL> headers = response [ "<STR_LIT>" ] <EOL> rows = response [ "<STR_LIT>" ] <EOL> duration = response [ "<STR_LIT>" ] <EOL> error = response [ "<STR_LIT:error>" ] <EOL> self . logfile . write ( "<STR_LIT>" . format ( query ) ) <EOL> self . logfile . write ( "<STR_LIT>" . format ( pretty_table ( headers , rows ) ) ) <EOL> if error is False : <EOL> self . logfile . write ( "<STR_LIT>" . format ( duration ) ) <EOL> @ staticmethod <EOL> def write_to_csvfile ( headers , rows ) : <EOL> filename = "<STR_LIT>" . format ( datetime . now ( ) . strftime ( "<STR_LIT>" ) ) <EOL> with open ( filename , "<STR_LIT>" ) as csvfile : <EOL> csvwriter = csv . writer ( csvfile , quotechar = str ( '<STR_LIT:">' ) , quoting = csv . QUOTE_NONNUMERIC , delimiter = str ( "<STR_LIT:U+002C>" ) ) <EOL> csvwriter . writerow ( headers ) <EOL> for row in rows : <EOL> csvwriter . writerow ( row ) <EOL> csvfile . close ( ) <EOL> def run ( self ) : <EOL> labels = self . neo4j . get_labels ( ) <EOL> relationship_types = self . neo4j . get_relationship_types ( ) <EOL> properties = self . neo4j . get_property_keys ( ) <EOL> if self . filename : <EOL> queries = self . filename . read ( ) <EOL> queries = queries . split ( "<STR_LIT:;>" ) [ : - <NUM_LIT:1> ] <EOL> for query in queries : <EOL> query += "<STR_LIT:;>" <EOL> query = query . strip ( ) <EOL> print ( "<STR_LIT>" + query ) <EOL> self . handle_query ( query ) <EOL> print ( ) <EOL> return <EOL> click . secho ( "<STR_LIT>" , fg = "<STR_LIT>" ) <EOL> click . secho ( "<STR_LIT>" , fg = "<STR_LIT>" ) <EOL> click . secho ( "<STR_LIT>" , fg = "<STR_LIT>" ) <EOL> click . secho ( "<STR_LIT>" , fg = "<STR_LIT>" ) <EOL> click . secho ( "<STR_LIT>" , fg = "<STR_LIT>" ) <EOL> msg = "<STR_LIT>" if type ( self . neo4j . client ) . __name__ == "<STR_LIT>" else "<STR_LIT>" <EOL> print ( msg ) <EOL> print ( "<STR_LIT>" . format ( __version__ ) ) <EOL> print ( "<STR_LIT>" ) <EOL> completer = CypherCompleter ( labels , relationship_types , properties ) <EOL> layout = create_prompt_layout ( <EOL> lexer = CypherLexer , <EOL> get_prompt_tokens = get_tokens , <EOL> reserve_space_for_menu = <NUM_LIT:8> , <EOL> ) <EOL> buff = CypherBuffer ( <EOL> accept_action = AcceptAction . RETURN_DOCUMENT , <EOL> history = FileHistory ( filename = os . path . expanduser ( '<STR_LIT>' ) ) , <EOL> completer = completer , <EOL> complete_while_typing = True , <EOL> ) <EOL> application = Application ( <EOL> style = PygmentsStyle ( CypherStyle ) , <EOL> buffer = buff , <EOL> layout = layout , <EOL> on_exit = AbortAction . RAISE_EXCEPTION , <EOL> key_bindings_registry = CypherBinder . registry <EOL> ) <EOL> cli = CommandLineInterface ( application = application , eventloop = create_eventloop ( ) ) <EOL> try : <EOL> while True : <EOL> document = cli . run ( ) <EOL> query = document . text <EOL> self . handle_query ( query ) <EOL> except : <EOL> print ( "<STR_LIT>" ) <EOL> def handle_query ( self , query ) : <EOL> run_n = re . match ( '<STR_LIT>' , query , re . DOTALL ) <EOL> save_csv = query . startswith ( "<STR_LIT>" ) <EOL> if self . cypher . is_a_write_query ( query ) and self . read_only : <EOL> print ( "<STR_LIT>" ) <EOL> elif query in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> raise Exception <EOL> elif query == "<STR_LIT>" : <EOL> print_help ( ) <EOL> elif query == "<STR_LIT>" : <EOL> self . neo4j . refresh ( ) <EOL> elif query == "<STR_LIT>" : <EOL> self . neo4j . print_schema ( ) <EOL> elif query == "<STR_LIT>" : <EOL> self . neo4j . print_indexes ( ) <EOL> elif query == "<STR_LIT>" : <EOL> self . neo4j . print_constraints ( ) <EOL> elif query == "<STR_LIT>" : <EOL> self . neo4j . print_labels ( ) <EOL> elif query == "<STR_LIT>" : <EOL> self . neo4j . print_relationship_types ( ) <EOL> elif query . startswith ( "<STR_LIT>" ) : <EOL> if query == "<STR_LIT>" : <EOL> for key , value in self . neo4j . parameters . items ( ) : <EOL> print ( "<STR_LIT>" . format ( key , value ) ) <EOL> else : <EOL> key = query [ <NUM_LIT:3> : ] <EOL> key = key . strip ( "<STR_LIT>" ) <EOL> value = self . neo4j . parameters . get ( key ) <EOL> if value is not None : <EOL> print ( value ) <EOL> elif query . startswith ( "<STR_LIT>" ) : <EOL> if "<STR_LIT:=>" not in query : <EOL> print ( "<STR_LIT>" ) <EOL> else : <EOL> params = query . replace ( "<STR_LIT>" , "<STR_LIT>" ) . strip ( ) <EOL> key , value = params . split ( "<STR_LIT:=>" , <NUM_LIT:1> ) <EOL> key = key . strip ( ) <EOL> value = value . strip ( ) <EOL> try : <EOL> value = eval ( value ) <EOL> self . neo4j . update_parameters ( key , value ) <EOL> except Exception as e : <EOL> print ( e ) <EOL> else : <EOL> count = int ( run_n . group ( <NUM_LIT:1> ) ) if run_n else <NUM_LIT:1> <EOL> query = run_n . group ( <NUM_LIT:2> ) if run_n else query <EOL> query = query [ len ( "<STR_LIT>" ) : ] if save_csv else query <EOL> if count <= <NUM_LIT:0> or not query : <EOL> raise Exception <EOL> total_duration = <NUM_LIT:0> <EOL> index = <NUM_LIT:0> <EOL> while index < count : <EOL> response = self . neo4j . cypher ( query ) <EOL> headers = response [ "<STR_LIT>" ] <EOL> rows = response [ "<STR_LIT>" ] <EOL> duration = response [ "<STR_LIT>" ] <EOL> error = response [ "<STR_LIT:error>" ] <EOL> profile = response . get ( "<STR_LIT>" ) <EOL> if error is False : <EOL> print ( pretty_table ( headers , rows ) ) <EOL> ms = "<STR_LIT>" . format ( index + <NUM_LIT:1> , duration ) if run_n else "<STR_LIT>" . format ( duration ) <EOL> print ( ms ) <EOL> if profile : <EOL> self . neo4j . print_profile ( profile ) <EOL> if save_csv : <EOL> self . write_to_csvfile ( headers , rows ) <EOL> else : <EOL> print ( error ) <EOL> if self . logfile : <EOL> self . write_to_logfile ( query , response ) <EOL> total_duration += duration <EOL> index += <NUM_LIT:1> <EOL> if run_n and error is False : <EOL> print ( "<STR_LIT>" . format ( total_duration ) ) <EOL> def print_help ( ) : <EOL> headers = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> rows = [ <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> ] <EOL> print ( pretty_table ( headers , rows ) ) <EOL> @ click . command ( ) <EOL> @ click . option ( "<STR_LIT>" , "<STR_LIT>" , is_flag = True , help = "<STR_LIT>" ) <EOL> @ click . option ( "<STR_LIT>" , "<STR_LIT>" , default = "<STR_LIT:localhost>" , help = "<STR_LIT>" ) <EOL> @ click . option ( "<STR_LIT>" , "<STR_LIT>" , default = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> @ click . option ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> @ click . option ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> @ click . option ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" , type = click . INT ) <EOL> @ click . option ( '<STR_LIT>' , '<STR_LIT>' , type = click . File ( mode = "<STR_LIT:a>" , encoding = "<STR_LIT:utf-8>" ) , help = "<STR_LIT>" ) <EOL> @ click . option ( "<STR_LIT>" , "<STR_LIT>" , type = click . File ( mode = "<STR_LIT:rb>" ) , help = "<STR_LIT>" ) <EOL> @ click . option ( "<STR_LIT>" , "<STR_LIT>" , is_flag = True , help = "<STR_LIT>" ) <EOL> @ click . option ( "<STR_LIT>" , "<STR_LIT>" , is_flag = True , help = "<STR_LIT>" ) <EOL> def run ( host , port , username , version , timeout , password , logfile , filename , ssl , read_only ) : <EOL> if version : <EOL> print ( "<STR_LIT>" . format ( __version__ ) ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> if username and not password : <EOL> password = click . prompt ( "<STR_LIT>" , hide_input = True , show_default = False , type = str ) <EOL> try : <EOL> cycli = Cycli ( host , port , username , password , logfile , filename , ssl , read_only , timeout ) <EOL> except AuthError : <EOL> print ( "<STR_LIT>" ) <EOL> except ConnectionError : <EOL> print ( "<STR_LIT>" ) <EOL> else : <EOL> cycli . run ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> run ( ) </s>
