Example #1
0
 def _SaveHistory(self):
   """Save readline history then clear history."""
   self._saved_history = []
   for idx in xrange(1, readline.get_current_history_length()+1):
     self._saved_history.append(readline.get_history_item(idx))
   for idx in xrange(readline.get_current_history_length()):
     readline.remove_history_item(0)
def setup_readline_history(histfile=None):
    """Handle readline / history."""
    if not histfile:
        histfile = "./.python_history"
        # histfile = os.path.join(os.path.expanduser("~"), ".python_history")

    if sys.version_info.major >= 3:
        # python3
        try:
            readline.read_history_file(histfile)
            h_len = readline.get_current_history_length()
        except os.FileNotFoundError:
            open(histfile, 'wb').close()
            h_len = 0
    elif sys.version_info.major == 2:
        # python2
        try:
            readline.read_history_file(histfile)
            h_len = readline.get_current_history_length()
        except IOError:
            open(histfile, 'wb').close()
            h_len = 0
    else:
        h_len = 0

    print("readline history length: {}".format(
        readline.get_current_history_length()
    ))

    atexit.register(save, h_len, histfile)
Example #3
0
    def multiline(self, firstline=''):
        full_input = []
        # keep a list of the entries that we've made in history
        old_hist = []
        if firstline:
            print '  ' + firstline
            full_input.append(firstline)
        while True:
            if hasReadline:
                # add the current readline position
                old_hist.append(readline.get_current_history_length())
            if self.use_rawinput:
                try:
                    line = raw_input(self.multiline_prompt)
                except EOFError:
                    line = 'EOF'
            else:
                self.stdout.write(self.multiline_prompt)
                self.stdout.flush()
                line = self.stdin.readline()
                if not len(line):
                    line = 'EOF'
                else:
                    line = line[:-1] # chop \n
            if line == 'EOF':
                break
            full_input.append(line)

        # add the final readline history position
        if hasReadline:
            old_hist.append(readline.get_current_history_length())

        cmd = '\n'.join(full_input) + '\n'

        if hasReadline:
            # remove the old, individual readline history entries.

            # first remove any duplicate entries
            old_hist = sorted(set(old_hist))

            # Make sure you do this in reversed order so you move from
            # the end of the history up.
            for pos in reversed(old_hist):
                # get_current_history_length returns pos + 1
                readline.remove_history_item(pos - 1)
            # now add the full line
            readline.add_history(cmd)

        locals = self.curframe.f_locals
        globals = self.curframe.f_globals
        print
        self.save_history()
        try:
            try:
                code = compile(cmd, '<stdin>', 'single')
                exec code in globals, locals
            except:
                print self._reprExc()
        finally:
            self.read_history()
Example #4
0
 def __str__(self):
     global h
     h = History()
     for x in range(0,readline.get_current_history_length()-1):
       #print "length %d %s" % (x,readline.get_history_item(x))
       h.append(readline.get_history_item(x))
     return self.str % readline.get_current_history_length()
Example #5
0
def h(recent=25):
    """show command history
    recent: number of recent command lines to show.
    """
    length = readline.get_current_history_length()
    start = 0 if length <= recent else (length - recent)
    for x in xrange(start, readline.get_current_history_length()):
        print(readline.get_history_item(x + 1))
Example #6
0
    def write_to_log(self):
        if not os.path.isfile(log_file_name):
             with open(log_file_name, 'w') as fp:
                  pass

        with open(log_file_name, 'a') as fp:
            if readline.get_current_history_length():
                fp.write(str(datetime.now()) + '\t' + readline.get_history_item(readline.get_current_history_length()) + '\n')
Example #7
0
def textInput(title, insert=None):
    if insert:
        title = "%s [%s]:" % (title, insert,)
    result = raw_input(title)
    if readline.get_current_history_length():
        readline.remove_history_item(readline.get_current_history_length() - 1)
    if not result:
        result = insert
    return result
Example #8
0
def last_history(line):
	if line == "":
		return False
	if (readline.get_current_history_length() == 0
			or readline.get_history_item(
				readline.get_current_history_length()) != line.encode("utf-8")):
		readline.add_history(line.encode("utf-8"))
		return True
	return False
Example #9
0
 def execute(limit=10):
     import readline
     if limit <= 0:
         limit = 1000 # Some big number
     if readline.get_current_history_length() < limit:
         limit = readline.get_current_history_length()
     for index in range(readline.get_current_history_length()-limit, readline.get_current_history_length()):
         s = readline.get_history_item(index+1)
         print " %d: %s" % (index+1, s)
Example #10
0
 def __repr__(self):
     """print out current history information"""
     command = get_history_item(get_current_history_length())
     if command == 'history':
         length = get_current_history_length()
         if length > 1:
             return reduce(lambda x, y: '%s\n%s' % (x, y),
                           imap(get_history_item, xrange(1, length)))
         else:
             return ''
     else:
         return '<%s instance>' % __name__
def compact_history():
    if hasattr(readline, "replace_history_item"):
        unique_history = utils.unique_list()
        for index in reversed(list(range(1, readline.get_current_history_length()))):
            hist_item = readline.get_history_item(index)
            if hist_item:  # some history items are None (usually at index 0)
                unique_history.append(readline.get_history_item(index))
        unique_history.reverse()
        for index in range(len(unique_history)):
            readline.replace_history_item(index + 1, unique_history[index])
        for index in reversed(list(range(len(unique_history) + 1, readline.get_current_history_length()))):
            readline.remove_history_item(index)
Example #12
0
    def multiline(self, firstline=''):
        full_input = []
        # keep a list of the entries that we've made in history
        old_hist = []
        if firstline:
            full_input.append(firstline)
        while True:
            if hasReadline:
                # add the current readline position
                old_hist.append(readline.get_current_history_length())
            if self.use_rawinput:
                try:
                    line = raw_input(self.multiline_prompt)
                except EOFError:
                    line = 'EOF'
            else:
                self.stdout.write(self.multiline_prompt)
                self.stdout.flush()
                line = self.stdin.readline()
                if not len(line):
                    line = 'EOF'
                else:
                    line = line[:-1] # chop \n
            if line == 'EOF':
                print
                break
            full_input.append(line)
            if ';' in line:
                break

        # add the final readline history position
        if hasReadline:
            old_hist.append(readline.get_current_history_length())

        cmd = ' '.join(full_input)
        if hasReadline:
            # remove the old, individual readline history entries.

            # first remove any duplicate entries
            old_hist = sorted(set(old_hist))

            # Make sure you do this in reversed order so you move from
            # the end of the history up.
            for pos in reversed(old_hist):
                # get_current_history_length returns pos + 1
                readline.remove_history_item(pos - 1)
            # now add the full line
            readline.add_history(cmd)

        return cmd
Example #13
0
def getblocks_from_history(object, lstrip=False):# gettype=False):
    """extract code blocks from a code object using stored history"""
    import readline, inspect, types
    lbuf = readline.get_current_history_length()
    code = [readline.get_history_item(i)+'\n' for i in range(1,lbuf)]
    lnum = 0
    codeblocks = []
   #objtypes = []
    while lnum < len(code):#-1:
       if code[lnum].lstrip().startswith('def '):
           block = inspect.getblock(code[lnum:])
           lnum += len(block)
           if block[0].lstrip().startswith('def %s(' % object.func_name):
               if lstrip: block[0] = block[0].lstrip()
               codeblocks.append(block)
   #           obtypes.append(types.FunctionType)
       elif 'lambda ' in code[lnum]:
           block = inspect.getblock(code[lnum:])
           lnum += len(block)
           lhs,rhs = block[0].split('lambda ',1)[-1].split(":", 1) #FIXME: bad
           try: #FIXME: unsafe
               _ = eval("lambda %s : %s" % (lhs, rhs), globals(), locals())
           except: pass
           if _.func_code.co_code == object.func_code.co_code:
               if lstrip: block[0] = block[0].lstrip()
               codeblocks.append(block)
   #           obtypes.append('<lambda>')
       else:
           lnum +=1
   #if gettype: return codeblocks, objtypes 
    return codeblocks #XXX: danger... gets methods and closures w/o containers
Example #14
0
    def print_history (self):
        
        length = readline.get_current_history_length()

        for i in xrange(1, length + 1):
            cmd = readline.get_history_item(i)
            print "{:<5}   {:}".format(i, cmd)
Example #15
0
    def save(self, filename, pos = None, end = None):
        """write history number from pos to end into filename file"""
        length = get_current_history_length()
        if length > 1:
            if not pos:
                pos = 1
            elif pos >= length - 1:
                pos = length - 1
            elif pos < 1:
                pos = length + pos - 1
            if not end:
                end = length
            elif end >= length:
                end = length
            if end < 0:
                end = length + end
            else:
                end = end + 1

            fp = open(filename, 'w')
            write = fp.write
            if pos < end:
                map(lambda x: write('%s\n' %  x),
                    imap(get_history_item, xrange(pos, end)))
            else:
                write('%s\n' % get_history_item(pos))
            fp.close()
Example #16
0
def silent_invariant_raw_input(prompt,comp=None,wipe=False, echo = True,color = Colors.NONE,completion=None):
    prompt = color+prompt+Colors.ENDC
    if not Control.silent:
        readline.set_startup_hook(lambda: readline.insert_text(comp))
    if completion=="Files":
        readline.set_completer_delims(' \t\n;')
        readline.parse_and_bind("tab: complete")
        readline.set_completer(fileCompleter)
    elif not completion is None:
        readline.set_completer_delims(' \t\n;')
        readline.parse_and_bind("tab: complete")
        readline.set_completer(lambda a,b:listCompleter(a,b,completion))
    try:
        if Control.silent:
            prompt = ""
        if echo:
            ret = raw_input(prompt)
        else:
            ret = getpass.getpass(prompt)
            wipe = False
        if wipe:
            l = readline.get_current_history_length()
            if l > 0:
                readline.remove_history_item(l-1)
        return ret
    finally:
        if not Control.silent:
            readline.set_startup_hook()
        if not completion is None:
            readline.parse_and_bind("set disable-completion On")
            readline.set_completer(None)
            #readline.parse_and_bind("tab: \t")
            readline.set_completer_delims("")
Example #17
0
    def get_history_item (self, index):
        length = readline.get_current_history_length()
        if index > length:
            print format_text("please select an index between {0} and {1}".format(0, length))
            return None

        return readline.get_history_item(index)
Example #18
0
File: tui.py Project: fabaff/yokadi
def editLine(line, prompt="edit> ", echo=True):
    """Edit a line using readline
    @param prompt: change prompt
    @param echo: whether to echo user text or not"""

    if line:
        reinjectInRawInput(line)

    if len(_answers) > 0:
        line = _answers.pop(0)
    else:
        try:
            if echo:
                line = input(prompt)
            else:
                line = getpass(prompt)
        except EOFError:
            line = ""

    # Remove edited line from history:
    #   oddly, get_history_item is 1-based,
    #   but remove_history_item is 0-based
    if sys.platform != 'win32':
        length = readline.get_current_history_length()
        if length > 0:
            readline.remove_history_item(length - 1)

    return line
Example #19
0
def remote_log_history():
    import readline
    from time import time
    import requests
    import os
    import json
    import base64
    cnt = readline.get_current_history_length()
    for i in xrange(cnt):
        cmd = readline.get_history_item(i + 1)
        data = {}
        data['ts'] = time()
        data['cmd'] = base64.b64encode(cmd.encode('ascii'))
        data['optout'] = None
        if 'STATSOPTOUT' in os.environ:
            data['optout'] = os.environ['STATSOPTOUT']
        data['src'] = {}
        data['src']['user'] = os.environ['USER']
        if 'SESSIP' in os.environ:
            data['src']['ip'] = os.environ['SESSIP']
        pid = os.getpid()
        ppid = os.getppid()
        sessid = 'SESSID'
        if 'SESSID' in os.environ:
            sessid = os.environ['SESSID']
        data['src']['session'] = '%s.%s.%s.py' % (pid, ppid, sessid)
        url = 'http://ctf.local:9999/cmd'
        # print('request: %s' % json.dumps(data))
        try:
            requests.post(url, data=json.dumps(data), timeout=2.0)
        except:  # ConnectionError, HTTPError, Timeout, TooManyRedirects
            pass
Example #20
0
def raw_input_no_history(prompt):
    """Removes user input from readline history."""
    import readline
    input = raw_input(prompt)
    if input != '':
        readline.remove_history_item(readline.get_current_history_length()-1)
    return input
Example #21
0
 def __str__(self):
     try:
         _ = readline.get_history_item(readline.get_current_history_length())
         if _ not in [h[-1], None, h]: h.append(_);
     except NameError:
        pass
     return self.str % len(h);
Example #22
0
    def __init__(self, start):
        """The function start is taken as an argument.  The function end is
        figured out from the current history length

        """
        self.start = start
        self.end = readline.get_current_history_length()
Example #23
0
 def raw_input_no_history(self, prompt):
     if self.filelines:
         return self.filelines.pop(0)
     else:
         input = raw_input(prompt)
         readline.remove_history_item(readline.get_current_history_length() - 1)
         return input
Example #24
0
    def _t_add(self, cmd, line):
        """Code shared by t_add, bug_add and n_add."""
        parser = self._parser_t_add(cmd)
        args = parser.parse_args(line)

        line = " ".join(args.cmd)
        if not line:
            raise BadUsageException("Missing parameters")
        projectName, title, keywordDict = parseutils.parseLine(line)
        projectName = self._realProjectName(projectName)
        if not title:
            raise BadUsageException("Missing title")

        if args.crypt:
            # Obfuscate line in history
            length = readline.get_current_history_length()
            if length > 0:  # Ensure history is positive to avoid crash with bad readline setup
                readline.replace_history_item(length - 1, "%s %s " % (cmd,
                                                                  line.replace(title, "<...encrypted...>")))
            # Encrypt title
            title = self.cryptoMgr.encrypt(title)

        task = dbutils.addTask(projectName, title, keywordDict)
        if not task:
            tui.reinjectInRawInput("%s %s" % (cmd, line))
            return None
        self.lastTaskId = task.id

        if args.describe:
            self.do_t_describe(self.lastTaskId)
        return task
Example #25
0
def rl_autoindent():
    """Auto-indent upon typing a new line according to the contents of the
    previous line.  This function will be used as Readline's
    pre-input-hook.

    """
    hist_len = readline.get_current_history_length()
    last_input = readline.get_history_item(hist_len)
    try:
        last_indent_index = last_input.rindex("    ")
    except:
        last_indent = 0
    else:
        last_indent = int(last_indent_index / 4) + 1
    if len(last_input.strip()) > 1:
        if last_input.count("(") > last_input.count(")"):
            indent = ''.join(["    " for n in range(last_indent + 2)])
        elif last_input.count(")") > last_input.count("("):
            indent = ''.join(["    " for n in range(last_indent - 1)])
        elif last_input.count("[") > last_input.count("]"):
            indent = ''.join(["    " for n in range(last_indent + 2)])
        elif last_input.count("]") > last_input.count("["):
            indent = ''.join(["    " for n in range(last_indent - 1)])
        elif last_input.count("{") > last_input.count("}"):
            indent = ''.join(["    " for n in range(last_indent + 2)])
        elif last_input.count("}") > last_input.count("{"):
            indent = ''.join(["    " for n in range(last_indent - 1)])
        elif last_input[-1] == ":":
            indent = ''.join(["    " for n in range(last_indent + 1)])
        else:
            indent = ''.join(["    " for n in range(last_indent)])
    readline.insert_text(indent)
Example #26
0
def hist():
    """Fetches command line history. Returns dict of all lines."""
    history_dict = {}
    # create history_list
    for i in range(readline.get_current_history_length()):
        history_dict[i+1] = (readline.get_history_item(i+1))
    return history_dict
Example #27
0
 def __history_try_search(cls, text):
     history_range = range(readline.get_current_history_length(), 1, -1)
     for i in history_range:
         item = readline.get_history_item(i)
         if item.startswith(text):
             return item
     return ''
Example #28
0
    def run(self):

        # Preserve existing history
        if self.preserve_history:
            old_history = [readline.get_history_item(index) for index in xrange(readline.get_current_history_length())]
            readline.clear_history()
            map(readline.add_history, self.history)

        readline.set_completer(self.complete)
        readline.parse_and_bind("bind ^I rl_complete")

        while True:
            cmdline = raw_input("%s > " % (self.prefix,))
            self.last_wd_complete = ("", ())
            if not cmdline:
                continue

            # Try to dispatch command
            try:
                self.execute(cmdline)
            except SystemExit, e:
                print "Exiting shell: %s" % (e.code,)
                break
            except UnknownCommand, e:
                print "Command '%s' unknown." % (e,)
Example #29
0
    def history(start=None, end=None, lines=30,
                   number=False):
        """
        Display the history starting from entry 'start' to
        entry 'end'. Only available on platforms where the
        readline module can be imported.

        History starts from 0 and goes to a large number.
        The history file is $ZENHOME/.pyhistory by default.

        @parameter start: Line number to start printing
        @type start: integer
        @parameter end: Line number to finish printing
        @type end: integer
        @parameter lines: number of lines to show if no end
        @type lines: integer
        @parameter number: show the line numbers?
        @type number: boolean
        """
        if readline is not None:
            maxHistLength = readline.get_current_history_length()
            if start is None:
                start = maxHistLength
            if end is None:
                end = maxHistLength - lines
            if start < end:
                end, start = start, end
            for i in range(end, start):
                if number:
                    print i, readline.get_history_item(i)
                else:
                    print readline.get_history_item(i)
Example #30
0
 def operate_and_get_next(count, char):
     current_line = readline.where_history()
     offset = readline.get_current_history_length() - current_line
     # Accept the current line and set the hook to rewind history
     result = readline.accept_line(1, char)
     readline.set_pre_input_hook(pre_input_hook_factory(offset, char))
     return result
Example #31
0
def save():
    """save the readline history since last call to mark()"""
    end = readline.get_current_history_length() - 1
    session = []
    item = readline.get_history_item(end)
    while item != _marker:
        session.insert(0, item+"\n")
        end -= 1
        item = readline.get_history_item(end)
        
    file(_session_file, 'w').writelines(session)
    print >> sys.stderr, "saved session to", _session_file
Example #32
0
    def get_history(self):
        """Get the line by history level."""
        if not self.history_level:
            return None

        try:
            history_index = readline.get_current_history_length() + 1 - self.history_level
            if not history_index:
                return None
            return readline.get_history_item(history_index)
        except NameError:
            return None
Example #33
0
 def save_history(filename):
     history = [
         readline.get_history_item(i)
         for i in range(1,
                        readline.get_current_history_length() + 1)
     ]
     readline.clear_history()
     for line in history:
         if line.startswith('hashpass'):
             line = 'hashpass(...)'
         readline.add_history(line)
     return readline.write_history_file(filename)
Example #34
0
 def recover_source(self):
     jupyter_cache = (stack.frame.f_locals['_ih'] for stack in inspect.stack() if '_ih' in stack.frame.f_locals)
     src = next(jupyter_cache, None)
     if src is not None:
         src = '\n'.join(src)
     # Check REPL history
     if src is None:
         src = [str(readline.get_history_item(i + 1)) for i in range(readline.get_current_history_length())]
     # Check Python file
     if src is None or not src:
         src = self.__get_caller_src()
     return src
Example #35
0
 def push(self, line):
     try:
         cls_, method = line[:line.index("(")].split(".")
         method = getattr(self.locals[cls_], method)
         if hasattr(method, "_private"):
             readline.replace_history_item(
                 readline.get_current_history_length() - 1,
                 line[:line.index("(")] + "()",
             )
     except (ValueError, AttributeError, KeyError):
         pass
     return super().push(line)
Example #36
0
    def show(self):

        historyLength = readline.get_current_history_length()
        historyLengthDigitNr = len(str(historyLength))

        for idx in range(1, historyLength):
            idxLen = len(str(idx))
            print "%(space)s%(idx)s  '%(historyItem)s'" % {
                'space': ' ' * (1 + historyLengthDigitNr - idxLen),
                'idx': idx,
                'historyItem': readline.get_history_item(idx)
            }
Example #37
0
def get_history(max_entries):
    """Return the history in the readline buffer.

    Args:
      max_entries: An integer, the maximum number of entries to return.
    Returns:
      A list of string, the previous history of commands.
    """
    num_entries = readline.get_current_history_length()
    assert num_entries >= 0
    return [readline.get_history_item(index+1)
            for index in range(min(num_entries, max_entries))]
Example #38
0
    def execute(self, cmdline):

        # Check for history recall
        if cmdline == "!!" and self.history:
            cmdline = self.history[-1]
            print cmdline
            if readline.get_current_history_length():
                readline.replace_history_item(
                    readline.get_current_history_length() - 1, cmdline)
        elif cmdline.startswith("!"):
            try:
                index = int(cmdline[1:])
                if index > 0 and index <= len(self.history):
                    cmdline = self.history[index - 1]
                    print cmdline
                    if readline.get_current_history_length():
                        readline.replace_history_item(
                            readline.get_current_history_length() - 1, cmdline)
                else:
                    raise ValueError()
            except ValueError:
                print "%s: event not found" % (cmdline, )
                return

        # split the command line into command and options
        splits = cmdline.split(" ", 1)
        cmd = splits[0]
        options = splits[1] if len(splits) == 2 else ""

        # Find matching command
        try:
            if cmd not in self.commands:
                self.history.append(cmdline)
                raise UnknownCommand(cmd)
            else:
                self.commands[cmd].execute(cmd, options)
        finally:
            # Store in history
            self.history.append(cmdline)
Example #39
0
    def execute(self, index):

        command = readline.get_history_item(index)
        if command is not None:
            print "executing command: '%s'" % command

            exec(command, self.globals)

            # add the  executed command in the history by replacing maestro.history.execute()
            pos = readline.get_current_history_length() - 1
            readline.replace_history_item(pos, command)
        else:
            print "history: Index '%i' is out of range" % index
Example #40
0
 def write(self,path = None ,filename = 'script.py'):
     historylines = []
     if self.started == True:
         i = 0
         looking_for_begining = True
         history_length = readline.get_current_history_length()
         while looking_for_begining:
             line = readline.get_history_item(history_length - i)
             if line == self.start_signal:
                 looking_for_begining = False
             else:
                 historylines.append(line)
                 i+=1
Example #41
0
def history(last=10, printout=True):
	""" Display last commands in history """
	global HISTFILE

	lines = ''
	length = readline.get_current_history_length()
	start = length - last
	for i in range(start, length):
		line = readline.get_history_item(i)
		if printout:
			print('{} {}'.format(i, line))
		lines += line + '\n'
	return lines.encode().split(b'\n')
Example #42
0
    def testHistoryUpdates(self):
        readline.clear_history()

        readline.add_history("first line")
        readline.add_history("second line")

        self.assertEqual(readline.get_history_item(0), None)
        self.assertEqual(readline.get_history_item(1), "first line")
        self.assertEqual(readline.get_history_item(2), "second line")

        readline.replace_history_item(0, "replaced line")
        self.assertEqual(readline.get_history_item(0), None)
        self.assertEqual(readline.get_history_item(1), "replaced line")
        self.assertEqual(readline.get_history_item(2), "second line")

        self.assertEqual(readline.get_current_history_length(), 2)

        readline.remove_history_item(0)
        self.assertEqual(readline.get_history_item(0), None)
        self.assertEqual(readline.get_history_item(1), "second line")

        self.assertEqual(readline.get_current_history_length(), 1)
Example #43
0
    def history(self, arguments):
        """Display the current readline history buffer.

        Usage:
            history [<index>]
        """
        index = arguments["<index>"]
        if index:
            idx = int(index)
            self._ui.print(readline.get_history_item(idx))
        else:
            for i in range(readline.get_current_history_length()):
                self._ui.print(readline.get_history_item(i))
Example #44
0
    def execute(self, cmdname, options):

        opts, args = getopt.getopt(shlex.split(options), '')

        for name, _ignore_value in opts:

            print "Unknown option: %s" % (name, )
            print self.usage(cmdname)
            raise WrongOptions

        paths = []
        if len(args) == 0:
            print "Wrong number of arguments: %d" % (len(args), )
            print self.usage(cmdname)
            raise WrongOptions

        while True:
            result = raw_input("Really delete %d resource(s) [y/n]: " %
                               (len(args), ))
            if readline.get_current_history_length():
                readline.remove_history_item(
                    readline.get_current_history_length() - 1)
            if not result:
                continue
            if result[0] == "n":
                return True
            elif result[0] == "y":
                break

        for arg in args:
            path = arg
            if not path.startswith("/"):
                path = os.path.join(self.shell.wd, path)
            paths.append(path)

            resource = URL(url=path)
            self.shell.account.session.deleteResource(resource)

        return True
Example #45
0
    def get_history(self):
        """
            Gets history from hacky.hist file.

            Returns:
                list
        """
        history = []

        for i in xrange(1, readline.get_current_history_length() + 1):
            history.append(readline.get_history_item(i))

        return history
Example #46
0
def _pop_readline_history(clear_history: bool = True) -> List[str]:
    """Returns a copy of readline's history and optionally clears it (default)"""
    # noinspection PyArgumentList
    if rl_type == RlType.NONE:
        return []

    history = [
        readline.get_history_item(i)
        for i in range(1, 1 + readline.get_current_history_length())
    ]
    if clear_history:
        readline.clear_history()
    return history
Example #47
0
def save(prev_h_len, f):
    import readline

    new_h_len = readline.get_current_history_length()
    readline.set_history_length(1000)

    try:
        readline.append_history_file(new_h_len - prev_h_len, f)
    except:
        # Python2 doesn't support append_history_file
        readline.write_history_file(f)

    del readline
Example #48
0
    def register_readline():
        import atexit
        try:
            import readline
            import rlcompleter
        except ImportError:
            return

        # Reading the initialization (config) file may not be enough to set a
        # completion key, so we set one first and then read the file.
        readline_doc = getattr(readline, '__doc__', '')
        if readline_doc is not None and 'libedit' in readline_doc:
            readline.parse_and_bind('bind ^I rl_complete')
        else:
            readline.parse_and_bind('tab: complete')

        try:
            readline.read_init_file()
        except OSError:
            # An OSError here could have many causes, but the most likely one
            # is that there's no .inputrc file (or .editrc file in the case of
            # Mac OS X + libedit) in the expected location.  In that case, we
            # want to ignore the exception.
            pass

        if readline.get_current_history_length() == 0:
            # If no history was loaded, default to .python_history.
            # The guard is necessary to avoid doubling history size at
            # each interpreter exit when readline was already configured
            # through a PYTHONSTARTUP hook, see:
            # http://bugs.python.org/issue5845#msg198636
            history = os.path.join(os.path.expanduser('~'), '.python_history')
            try:
                readline.read_history_file(history)
            except OSError:
                pass

            def write_history():
                try:
                    readline.write_history_file(history)
                except OSError as e:
                    if isinstance(e, (FileNotFoundError, PermissionError)):
                        # home directory does not exist or is not writable
                        # https://bugs.python.org/issue19891
                        pass
                    elif isinstance(e, (OSError)) and e.errno == -1:
                        print("Warning: unable to write into .python_history")
                    else:
                        raise

            atexit.register(write_history)
Example #49
0
def register_readline():
    import atexit
    try:
        import readline
    except ImportError:
        return

    # Reading the initialization (config) file may not be enough to set a
    # completion key, so we set one first and then read the file.
    readline_doc = getattr(readline, '__doc__', '')
    if readline_doc is not None and 'libedit' in readline_doc:
        readline.parse_and_bind('bind ^I rl_complete')
    else:
        readline.parse_and_bind('tab: complete')

    try:
        readline.read_init_file()
    except OSError:
        # An OSError here could have many causes, but the most likely one
        # is that there's no .inputrc file (or .editrc file in the case of
        # Mac OS X + libedit) in the expected location.  In that case, we
        # want to ignore the exception.
        pass

    if readline.get_current_history_length() == 0:
        # If no history was loaded, default to .python_history.
        # The guard is necessary to avoid doubling history size at
        # each interpreter exit when readline was already configured
        # through a PYTHONSTARTUP hook, see:
        # http://bugs.python.org/issue5845#msg198636
        history = ""
        if "XDG_CACHE_HOME" in os.environ:
            history = Path(os.environ["XDG_CACHE_HOME"]) / "python" / "history"
            history.parent.mkdir(parents=True, exist_ok=True)
        else:
            history = os.path.join(os.path.expanduser('~'), '.python_history')
        try:
            readline.read_history_file(history)
        except OSError:
            pass

        def write_history():
            try:
                readline.write_history_file(history)
            except OSError:
                # bpo-19891, bpo-41193: Home directory does not exist
                # or is not writable, or the filesystem is read-only.
                pass

        atexit.register(write_history)
Example #50
0
 def do_history(self, arg):
     import readline
     usable, filename, append = self._redirect_split(arg)
     if usable == "":
         for i in range(1, readline.get_current_history_length()+1):
             print(("{}: {}".format(i, readline.get_history_item(i))))
     elif usable == "clear":
         readline.clear_history()
     elif "limit" in usable:
         try:
             length = int(usable.split()[1])
             readline.set_history_length(length)
         except ValueError:
             msg.err("The maximum history length you entered is not a valid integer.")
Example #51
0
    def delete_python_history(self, command: str) -> None:  # pylint: disable=no-self-use
        readline_history = [
            readline.get_history_item(i + 1)
            for i in range(readline.get_current_history_length())
        ]

        cmd_indexes = [
            i for i, cmd in enumerate(readline_history) if cmd == command
        ]

        for cmd_idx in reversed(cmd_indexes):
            readline.remove_history_item(cmd_idx)

        readline.write_history_file(str(SHELLS[Shell.STANDARD]["hist"]))
Example #52
0
def _closeHistory():
    # Ensure that our current history is written, and, if possible,
    # clear the history afterward.  Also, if possible, don't write
    # out any history content that might have come from a different
    # history.

    global _lastsize

    curlines = readline.get_history_length()
    currentsize = get_current_history_length()

    if currentsize is not None:
        lines_added = currentsize - _lastsize
        if lines_added<0:
            lines_added = currentsize
        if curlines==-1 or lines_added<curlines:
            readline.set_history_length(lines_added)

    readline.write_history_file(curhist)
    readline.set_history_length(curlines)

    clear_history()
    _lastsize = get_current_history_length()
Example #53
0
    def do_history(self, arg):
        """
        Print the history buffer to the screen, oldest to most recent.
        IF argument n is present print the most recent N items.

        Usage: history [n]
        """
        if readline_present:
            nall = readline.get_current_history_length()
            firstprint = 0
            if arg.strip():
                firstprint = max(nall - int(arg), 0)
            for index in range(firstprint, nall):
                print(index, readline.get_history_item(index))
Example #54
0
def _history_print(arg):
    global LAST_PRINTED_ENTRY, PID
    history_length = readline.get_current_history_length()
    for i in range(LAST_PRINTED_ENTRY, history_length + 1):
        # TODO: this isn't quite accurate since we're using the same
        # timestamp for all the history entries we're printing on this
        # round, but it's the best we can do for now :/
        print >> LOGFILE, to_compact_json(
            dict(timestamp=get_ms_since_epoch(),
                 command=readline.get_history_item(i),
                 pid=PID))
        LOGFILE.flush()
    print arg
    LAST_PRINTED_ENTRY = history_length + 1
Example #55
0
def auto_complete_manager(key, callback, history_file=None):  # pragma: no cover
    """Context manager for enabling command line auto-completion

    This context manager can be used to ensure that command completion for
    a Friendly Shell runner can have it's own self-defined auto-completion
    and command history while not affecting the global completion sub-system
    that may already be configured prior to running the Friendly Shell.
    Through the use of a context manager, we ensure the state of the
    command completion subsystem will be restored regardless of how the
    context manager was terminated.

    :param str key:
        descriptor for keyboard key to use for auto completion trigger
    :param callback:
        method point for the callback to run when completion key is pressed
    :param str history_file:
        optional path to the history file to use for storing previous commands
        run by this shell. If not provided, history will not be saved.
    """
    # If auto-completion isn't supported, do nothing
    if not AUTOCOMPLETE_ENABLED:
        yield
        return

    # If auto-complet enabled but no existing history exists, we can assume
    # we aren't working inside a nested sub-shell, so just return our
    # conventional context manager
    if not readline.get_current_history_length():
        with _autocomplete_helper(key, callback, history_file):
            yield
            return

    # If we get here we know we're inside a nest sub-shell, so we need
    # to take care to preserve the state of the parent shell so the sub-shell
    # can initialize it's own unique history
    with tempfile.NamedTemporaryFile() as temp_file:

        # Save the current shell's history to a temporary file
        readline.write_history_file(temp_file.name)
        readline.clear_history()

        # Then launch our typical auto-complete context manager
        with _autocomplete_helper(key, callback, history_file):
            yield

        # restore the state of our command history for the parent shell
        # when the sub-shell terminates
        readline.clear_history()
        readline.read_history_file(temp_file.name)
Example #56
0
def comp_disp_matches(substitution, matches, longest_match_length):
    """Display completion matches, colorized according to their syntactic
    role.

    """
    hist_len = readline.get_current_history_length()
    last_input = readline.get_history_item(hist_len)
    for n, m in enumerate(matches):
        (mod, d, w) = m.rpartition('.')
        w_strip = w.strip('(')
        # Parent modules: bold yellow, with the children highlighted
        # according to their syntactic role
        if mod != "":
            mod_str = "".join([ansi_colorize(mod, "yellow", "bold"), '.'])
        else:
            mod_str = ""
        # Function: bright cyan
        if w[-1] == "(":
            w_str = ansi_colorize(w, "cyan", "bright")
        # Keywords: bright yellow
        elif iskeyword(w_strip):
            w_str = ansi_colorize(w, "yellow", "bright")
        # Modules: bold yellow
        elif w_strip in sys.modules:
            w_str = ansi_colorize(w, "yellow", "bold")
        # Hidden: red
        elif w_strip[:2] == "__" and w_strip[-2:] == "__":
            w_str = ansi_colorize(w, "red", "regular")
        # Private: bright purple
        elif w_strip[0] == "_":
            w_str = ansi_colorize(w, "purple", "bright")
        # Otherwise: normal
        else:
            w_str = w
        # 3 columns of matches per line
        if n % 3 == 0:
            sys.stdout.write("\n")
        # Get the total length of escape sequences
        col_len = (len(mod_str) + len(w_str)) - (len(mod) + len(w))
        # Write columns with widths set by the longest match length
        sys.stdout.write("{0:{1}} ".format("{0}{1}".format(mod_str, w_str),
                                           longest_match_length + col_len))
    # Redisplay the prompt
    if last_input.strip()[-1] in ["(", "[", "{", ":"]:
        sys.stdout.write("\n{0}{1}".format(sys.ps2.__str__(prompt=False),
                                           readline.get_line_buffer()))
    else:
        sys.stdout.write("\n{0}{1}".format(sys.ps1.__str__(prompt=False),
                                           readline.get_line_buffer()))
    def do_history(self, line):
        """
        history

        Show previously executed commands.
        """

        try:
            import readline

            h = readline.get_current_history_length()
            for n in range(1, h + 1):
                print(readline.get_history_item(n))
        except ImportError:
            pass
Example #58
0
 def singleline(self, store_in_history=True, **kwargs):
     """Reads a single line of input. The store_in_history kwarg
     flags whether the input should be stored in readline's in-memory
     history.
     """
     if not store_in_history:  # store current position to remove it later
         try:
             import readline
         except ImportError:
             store_in_history = True
         pos = readline.get_current_history_length() - 1
     rtn = input(self.prompt)
     if not store_in_history and pos >= 0:
         readline.remove_history_item(pos)
     return rtn
Example #59
0
    def save(self):
        """Save the current context's history."""
        if self.current is None:
            raise ValueError("No context set.")

        if self.current.obj:
            lines = []
            for i in range(readline.get_current_history_length()+1):
                line = readline.get_history_item(i)
                if line:
                    lines.append(str(line))
            with open(self.current.filename, 'w') as f:
                pickle.dump(lines, f)
        else:
            readline.write_history_file(self.current.filename)
Example #60
0
    def test_do_history(self):
        self.exeCmd("history")
        self.assertFalse(self.capturedStdout.gotPsyqlException())

        if READLINE:
            # This will match the "help" command position
            position = int(readline.get_current_history_length())
            readline.replace_history_item(position - 1, "help")
            self.exeCmd("history")
            self.assertFalse(self.capturedStdout.gotPsyqlException())

        for cmd in ("history a", "history -1", "history 10.5", "history 10,5",
                    "history 2 4"):
            self.exeCmd(cmd)
            self.assertTrue(self.capturedStdout.gotPsyqlException())