<commit_before>from __future__ import unicode_literals, print_function

import sys

from prompt_toolkit import Application, CommandLineInterface, AbortAction
from prompt_toolkit.history import History
from prompt_toolkit.shortcuts import create_default_layout, create_eventloop
from prompt_toolkit.filters import Always

from pygments.token import Token
import click
from py2neo.error import Unauthorized
from py2neo.packages.httpstream import SocketError

from cycli import __version__
from cycli.lexer import CypherLexer
from cycli.style import CypherStyle
from cycli.completer import CypherCompleter
from cycli.buffer import CypherBuffer
from cycli.neo4j import Neo4j


def get_tokens(x):
        return [(Token.Prompt, "> ")]

class Cycli:

    def __init__(self, host, port, username, password):
        self.host = host
        self.port = port
        self.username = username
        self.password = password

    def run(self):
        neo4j = Neo4j(self.host, self.port, self.username, self.password)

        try:
            labels = neo4j.labels()
            relationship_types = neo4j.relationship_types()
            properties = neo4j.properties()

        except Unauthorized:
            print("Unauthorized. See cycli --help for authorization instructions.")
            return

        except SocketError:
            print("Connection refused. Is Neo4j turned on?")
            return

        completer = CypherCompleter(labels, relationship_types, properties)

        layout = create_default_layout(
            lexer=CypherLexer,
            get_prompt_tokens=get_tokens,
            reserve_space_for_menu=True
        )

        buff = CypherBuffer(
            history=History(),
            completer=completer,
            complete_while_typing=Always()
        )

        application = Application(
            style=CypherStyle,
            buffer=buff,
            layout=layout,
            on_exit=AbortAction.RAISE_EXCEPTION
        )

        cli = CommandLineInterface(application=application, eventloop=create_eventloop())

        try:
            while True:
                document = cli.run()
                query = document.text

                if query in ["quit", "exit"]:
                    raise Exception

                elif query == "help":
                    print(help_text())

                else:
                    results = neo4j.cypher(query)
                    print(results)

        except Exception:
            print("Goodbye!")


@click.command()
@click.option("-h", "--host", default="localhost", help="The host address of Neo4j.")
@click.option("-p", "--port", default="7474", help="The port number on which Neo4j is listening.")
@click.option("-u", "--username", default=False,
              help="Username for Neo4j authentication. If provided, you will be prompted for a password.")
@click.option("-v", "--version", is_flag=True, help="Show cycli version and exit.")
def run(host, port, username, version):
    if version:
        print("cycli {}".format(__version__))
        sys.exit(0)

    print("Version: {}".format(__version__))
    print("Bug reports: https://github.com/nicolewhite/cycli/issues\n")

    password = <PASSWORD>

    if username:
        password = click.prompt("Password", hide_input=True, show_default=False, type=str)

    cycli = Cycli(host, port, username, password)
    cycli.run()
<commit_msg>Add option for setting socket timeout<commit_after>from __future__ import unicode_literals, print_function

import sys
import click

from prompt_toolkit import Application, CommandLineInterface, AbortAction
from prompt_toolkit.history import History
from prompt_toolkit.shortcuts import create_default_layout, create_eventloop
from prompt_toolkit.filters import Always
from pygments.token import Token

from py2neo.error import Unauthorized
from py2neo.packages.httpstream import SocketError, http

from cycli import __version__
from cycli.lexer import CypherLexer
from cycli.style import CypherStyle
from cycli.completer import CypherCompleter
from cycli.buffer import CypherBuffer
from cycli.neo4j import Neo4j


def get_tokens(x):
        return [(Token.Prompt, "> ")]

class Cycli:

    def __init__(self, host, port, username, password):
        self.host = host
        self.port = port
        self.username = username
        self.password = password

    def run(self):
        neo4j = Neo4j(self.host, self.port, self.username, self.password)

        try:
            labels = neo4j.labels()
            relationship_types = neo4j.relationship_types()
            properties = neo4j.properties()

        except Unauthorized:
            print("Unauthorized. See cycli --help for authorization instructions.")
            return

        except SocketError:
            print("Connection refused. Is Neo4j turned on?")
            return

        completer = CypherCompleter(labels, relationship_types, properties)

        layout = create_default_layout(
            lexer=CypherLexer,
            get_prompt_tokens=get_tokens,
            reserve_space_for_menu=True
        )

        buff = CypherBuffer(
            history=History(),
            completer=completer,
            complete_while_typing=Always()
        )

        application = Application(
            style=CypherStyle,
            buffer=buff,
            layout=layout,
            on_exit=AbortAction.RAISE_EXCEPTION
        )

        cli = CommandLineInterface(application=application, eventloop=create_eventloop())

        try:
            while True:
                document = cli.run()
                query = document.text

                if query in ["quit", "exit"]:
                    raise Exception

                elif query == "help":
                    print(help_text())

                else:
                    results = neo4j.cypher(query)
                    print(results)

        except Exception:
            print("Goodbye!")


@click.command()
@click.option("-h", "--host", default="localhost", help="The host address of Neo4j.")
@click.option("-p", "--port", default="7474", help="The port number on which Neo4j is listening.")
@click.option("-u", "--username", default=False,
              help="Username for Neo4j authentication. If provided, you will be prompted for a password.")
@click.option("-v", "--version", is_flag=True, help="Show cycli version and exit.")
@click.option("-t", "--timeout", default=False, help="Set a global socket timeout for queries.", type=click.INT)
def run(host, port, username, version, timeout):
    if version:
        print("cycli {}".format(__version__))
        sys.exit(0)

    print("Version: {}".format(__version__))
    print("Bug reports: https://github.com/nicolewhite/cycli/issues\n")

    password = None

    if username:
        password = click.prompt("Password", hide_input=True, show_default=False, type=str)

    if timeout:
        http.socket_timeout = timeout

    cycli = Cycli(host, port, username, password)
    cycli.run()

{'fraction_non_alphanumeric': 0.09610167071255177, 'fraction_numerical': 0.005426245894616593, 'mean_word_length': 2.519597989949749, 'pattern_counts': {'":': 2, '<': 4, '<?xml version=': 0, '>': 6, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1916181', 'n_tokens_mistral': 1995, 'n_tokens_neox': 1858, 'n_words': 500}