示例#1
0
文件: main.py 项目: d33tah/pgcli
 def handle_editor_command(self, cli, document):
     """
     Editor command is any query that is prefixed or suffixed
     by a '\e'. The reason for a while loop is because a user
     might edit a query multiple times.
     For eg:
     "select * from \e"<enter> to edit it in vim, then come
     back to the prompt with the edited query "select * from
     blah where q = 'abc'\e" to edit it again.
     :param cli: CommandLineInterface
     :param document: Document
     :return: Document
     """
     while special.editor_command(document.text):
         filename = special.get_filename(document.text)
         sql, message = special.open_external_editor(filename,
                                                     sql=document.text)
         if message:
             # Something went wrong. Raise an exception and bail.
             raise RuntimeError(message)
         cli.current_buffer.document = Document(sql,
                                                cursor_position=len(sql))
         document = cli.run(False)
         continue
     return document
示例#2
0
文件: main.py 项目: viveksinha/pgcli
 def handle_editor_command(self, cli, document):
     r"""
     Editor command is any query that is prefixed or suffixed
     by a '\e'. The reason for a while loop is because a user
     might edit a query multiple times.
     For eg:
     "select * from \e"<enter> to edit it in vim, then come
     back to the prompt with the edited query "select * from
     blah where q = 'abc'\e" to edit it again.
     :param cli: CommandLineInterface
     :param document: Document
     :return: Document
     """
     # FIXME: using application.pre_run_callables like this here is not the best solution.
     # It's internal api of prompt_toolkit that may change. This was added to fix #668.
     # We may find a better way to do it in the future.
     saved_callables = cli.application.pre_run_callables
     while special.editor_command(document.text):
         filename = special.get_filename(document.text)
         query = (special.get_editor_query(document.text)
                  or self.get_last_query())
         sql, message = special.open_external_editor(filename, sql=query)
         if message:
             # Something went wrong. Raise an exception and bail.
             raise RuntimeError(message)
         cli.current_buffer.document = Document(sql,
                                                cursor_position=len(sql))
         cli.application.pre_run_callables = []
         document = cli.run()
         continue
     cli.application.pre_run_callables = saved_callables
     return document
示例#3
0
 def handle_editor_command(self, cli, document):
     r"""
     Editor command is any query that is prefixed or suffixed
     by a '\e'. The reason for a while loop is because a user
     might edit a query multiple times.
     For eg:
     "select * from \e"<enter> to edit it in vim, then come
     back to the prompt with the edited query "select * from
     blah where q = 'abc'\e" to edit it again.
     :param cli: CommandLineInterface
     :param document: Document
     :return: Document
     """
     # FIXME: using application.pre_run_callables like this here is not the best solution.
     # It's internal api of prompt_toolkit that may change. This was added to fix #668.
     # We may find a better way to do it in the future.
     saved_callables = cli.application.pre_run_callables
     while special.editor_command(document.text):
         filename = special.get_filename(document.text)
         query = (special.get_editor_query(document.text) or
                  self.get_last_query())
         sql, message = special.open_external_editor(filename, sql=query)
         if message:
             # Something went wrong. Raise an exception and bail.
             raise RuntimeError(message)
         cli.current_buffer.document = Document(sql, cursor_position=len(sql))
         cli.application.pre_run_callables = []
         document = cli.run()
         continue
     cli.application.pre_run_callables = saved_callables
     return document
示例#4
0
文件: main.py 项目: xingsu56/pgcli
    def handle_editor_command(self, text):
        r"""
        Editor command is any query that is prefixed or suffixed
        by a '\e'. The reason for a while loop is because a user
        might edit a query multiple times.
        For eg:
        "select * from \e"<enter> to edit it in vim, then come
        back to the prompt with the edited query "select * from
        blah where q = 'abc'\e" to edit it again.
        :param text: Document
        :return: Document
        """
        editor_command = special.editor_command(text)
        while editor_command:
            if editor_command == '\\e':
                filename = special.get_filename(text)
                query = special.get_editor_query(
                    text) or self.get_last_query()
            else:  # \ev or \ef
                filename = None
                spec = text.split()[1]
                if editor_command == '\\ev':
                    query = self.pgexecute.view_definition(spec)
                elif editor_command == '\\ef':
                    query = self.pgexecute.function_definition(spec)
            sql, message = special.open_external_editor(
                filename, sql=query)
            if message:
                # Something went wrong. Raise an exception and bail.
                raise RuntimeError(message)
            while True:
                try:
                    text = self.prompt_app.prompt(default=sql)
                    break
                except KeyboardInterrupt:
                    sql = ""

            editor_command = special.editor_command(text)
        return text
示例#5
0
文件: main.py 项目: dbcli/pgcli
    def handle_editor_command(self, text):
        r"""
        Editor command is any query that is prefixed or suffixed
        by a '\e'. The reason for a while loop is because a user
        might edit a query multiple times.
        For eg:
        "select * from \e"<enter> to edit it in vim, then come
        back to the prompt with the edited query "select * from
        blah where q = 'abc'\e" to edit it again.
        :param text: Document
        :return: Document
        """
        editor_command = special.editor_command(text)
        while editor_command:
            if editor_command == '\\e':
                filename = special.get_filename(text)
                query = special.get_editor_query(
                    text) or self.get_last_query()
            else:  # \ev or \ef
                filename = None
                spec = text.split()[1]
                if editor_command == '\\ev':
                    query = self.pgexecute.view_definition(spec)
                elif editor_command == '\\ef':
                    query = self.pgexecute.function_definition(spec)
            sql, message = special.open_external_editor(
                filename, sql=query)
            if message:
                # Something went wrong. Raise an exception and bail.
                raise RuntimeError(message)
            while True:
                try:
                    text = self.prompt_app.prompt(default=sql)
                    break
                except KeyboardInterrupt:
                    sql = ""

            editor_command = special.editor_command(text)
        return text
示例#6
0
文件: main.py 项目: jknutson/pgcli
 def handle_editor_command(self, cli, document):
     """
     Editor command is any query that is prefixed or suffixed
     by a '\e'. The reason for a while loop is because a user
     might edit a query multiple times.
     For eg:
     "select * from \e"<enter> to edit it in vim, then come
     back to the prompt with the edited query "select * from
     blah where q = 'abc'\e" to edit it again.
     :param cli: CommandLineInterface
     :param document: Document
     :return: Document
     """
     while special.editor_command(document.text):
         filename = special.get_filename(document.text)
         sql, message = special.open_external_editor(filename, sql=document.text)
         if message:
             # Something went wrong. Raise an exception and bail.
             raise RuntimeError(message)
         cli.current_buffer.document = Document(sql, cursor_position=len(sql))
         document = cli.run(False)
         continue
     return document