コード例 #1
0
ファイル: elseql.py プロジェクト: snowtigersoft/elseql
    def init_search(self):
        self.search = ElseSearch(self.port, self.debug, cmd=True)

        if self.search.host:
            print("connected to", self.search.host)
        else:
            print("not connected")
コード例 #2
0
ファイル: elseql.py プロジェクト: RomanShestakov/elseql
    def init_search(self):
        self.search = ElseSearch(self.port, self.debug)

        if self.search.host:
            print("connected to", self.search.host)
        else:
            print("not connected")
コード例 #3
0
ファイル: elseql.py プロジェクト: snowtigersoft/elseql
class ElseShell(Cmd):

    prompt = "elseql> "
    port = DEFAULT_PORT
    creds = None
    debug = False
    query = False

    settable = Cmd.settable + """prompt Set command prompt
                                 port Set service [host:]port
                                 creds Set credentials (user:password)
                                 debug Set debug mode
                                 query Display query before results
                              """

    def __init__(self, port, debug):
        Cmd.__init__(self)

        if readline:
            path = os.path.join(os.environ.get('HOME', ''), HISTORY_FILE)
            self.history_file = os.path.abspath(path)
        else:
            self.history_file = None

        self.debug = debug
        self.port = port
        self.init_search()

    def init_search(self):
        self.search = ElseSearch(self.port, self.debug, cmd=True)

        if self.search.host:
            print("connected to", self.search.host)
        else:
            print("not connected")

    def _onchange_port(self, old=None, new=None):
        self.port = new
        self.init_search()

    def _onchange_creds(self, old=None, new=None):
        print("change creds to", new)

        self.creds = new.split(":", 1)
        print("creds:", self.creds)
        self.init_search()

    def _onchange_debug(self, old=None, new=None):
        self.debug = new
        self.search.debug = self.debug

    def _onchange_query(self, old=None, new=None):
        self.query = new
        self.search.print_query = self.query

    def getargs(self, line):
        return shlex.split(str(line.decode('string-escape')))

    def get_boolean(self, arg):
        return arg and [v for v in ['t', 'y', 'on', '1'] if arg.startswith(v)
                        ] != []

    def do_version(self, line):
        print()
        print("elseql %s - you know, for query" % __version__)
        print()

    def do_keywords(self, line):
        print(self.search.get_keywords())

    def do_mapping(self, line):
        "mapping [index-name]"
        mapping = self.search.get_mapping()

        if line:
            pprint.pprint(mapping[line])
        else:
            pprint.pprint(mapping)

    def do_indices(self, line):
        indices = self.search.get_indices()
        if line:
            pprint.pprint(line in indices)
        else:
            pprint.pprint(indices)

    def do_select(self, line):
        self.search.search('select ' + line)

    def do_explain(self, line):
        self.search.search(line, explain=True)

    def do_validate(self, line):
        self.search.search(line, validate=True)

    def do_EOF(self, line):
        "Exit shell"
        return True

    def do_shell(self, line):
        "Shell"
        os.system(line)

    #
    # aliases
    #
    do_describe = do_mapping

    #
    # override cmd
    #

    def emptyline(self):
        pass

    def onecmd(self, s):
        try:
            return Cmd.onecmd(self, s)
        except NotImplementedError as e:
            print(e.message)
        except:
            traceback.print_exc()
        return False

    def default(self, line):
        line = line.strip()
        if line and line[0] in ['#', ';']:
            return False
        else:
            return Cmd.default(self, line)

    def completedefault(self, test, line, beginidx, endidx):
        list = []

        for k in self.search.get_keywords():
            if k.startswith(test):
                list.append(k)

        return list

    def preloop(self):
        if self.history_file and os.path.exists(self.history_file):
            try:
                readline.read_history_file(self.history_file)
            except:
                print("can't read history file")

    def postloop(self):
        if self.history_file:
            readline.set_history_length(100)
            readline.write_history_file(self.history_file)

        print("Goodbye!")
コード例 #4
0
ファイル: elseql.py プロジェクト: RomanShestakov/elseql
class ElseShell(Cmd):

    prompt = "elseql> "
    port = DEFAULT_PORT
    creds = None
    debug = False
    query = False

    settable = Cmd.settable + """prompt Set command prompt
                                 port Set service [host:]port
                                 creds Set credentials (user:password)
                                 debug Set debug mode
                                 query Display query before results
                              """

    def __init__(self, port, debug):
        Cmd.__init__(self)

        if readline:
            path = os.path.join(os.environ.get('HOME', ''), HISTORY_FILE)
            self.history_file = os.path.abspath(path)
        else:
            self.history_file = None

        self.debug = debug
        self.port = port
        self.init_search()

    def init_search(self):
        self.search = ElseSearch(self.port, self.debug)

        if self.search.host:
            print("connected to", self.search.host)
        else:
            print("not connected")

    def _onchange_port(self, old=None, new=None):
        self.port = new
        self.init_search()

    def _onchange_creds(self, old=None, new=None):
        print("change creds to", new)

        self.creds = new.split(":", 1)
        print("creds:", self.creds)
        self.init_search()

    def _onchange_debug(self, old=None, new=None):
        self.debug = new
        self.search.debug = self.debug

    def _onchange_query(self, old=None, new=None):
        self.query = new
        self.search.print_query = self.query

    def getargs(self, line):
        return shlex.split(str(line.decode('string-escape')))

    def get_boolean(self, arg):
        return arg and [v for v in ['t', 'y', 'on', '1'] if arg.startswith(v)] != []

    def do_version(self, line):
        print()
        print("elseql %s - you know, for query" % __version__)
        print()

    def do_keywords(self, line):
        print(self.search.get_keywords())

    def do_mapping(self, line):
        "mapping [index-name]"
        mapping = self.search.get_mapping()

        if line:
            pprint.pprint(mapping[line])
        else:
            pprint.pprint(mapping)

    def do_select(self, line):
        self.search.search('select ' + line)

    def do_explain(self, line):
        self.search.search(line, explain=True)

    def do_validate(self, line):
        self.search.search(line, validate=True)

    def do_EOF(self, line):
        "Exit shell"
        return True

    def do_shell(self, line):
        "Shell"
        os.system(line)

    #
    # aliases
    #
    do_describe = do_mapping

    #
    # override cmd
    #

    def emptyline(self):
        pass

    def onecmd(self, s):
        try:
            return Cmd.onecmd(self, s)
        except NotImplementedError as e:
            print(e.message)
        except:
            traceback.print_exc()
        return False

    def default(self, line):
        line = line.strip()
        if line and line[0] in ['#', ';']:
            return False
        else:
            return Cmd.default(self, line)

    def completedefault(self, test, line, beginidx, endidx):
        list = []

        for k in self.search.get_keywords():
            if k.startswith(test):
                list.append(k)

        return list

    def preloop(self):
        if self.history_file and os.path.exists(self.history_file):
            try:
                readline.read_history_file(self.history_file)
            except:
                print("can't read history file")

    def postloop(self):
        if self.history_file:
            readline.set_history_length(100)
            readline.write_history_file(self.history_file)

        print("Goodbye!")