Esempio n. 1
0
    def push(self, s, insert_into_history=True):
        """Push a line of code onto the buffer so it can process it all
        at once when a code block ends"""
        if s.lstrip(' ') and s.lstrip(' ')[0] == '!':
            self.buffer = []
            return

        s = s.rstrip('\n')
        self.buffer.append(s)

        if insert_into_history:
            if self.config.hist_length:
                histfilename = os.path.expanduser(self.config.hist_file)
                oldhistory = self.rl_history.entries
                self.rl_history.entries = []
                if os.path.exists(histfilename):
                    self.rl_history.load(histfilename, getpreferredencoding())
                self.rl_history.append(s)
                try:
                    self.rl_history.save(histfilename, getpreferredencoding(), self.config.hist_length)
                except EnvironmentError as e:
                    self.interact.notify("Error occured while writing to file %s (%s) " % (histfilename, e.strerror))
                    self.rl_history.entries = oldhistory
                    self.rl_history.append(s)
            else:
                self.rl_history.append(s)

        if len(self.buffer) == 1:
            line = self.buffer[0]
            if self.interp.is_commandline(line) and not self.is_assignment_statement:
                result = self.interp.runcommand(line)
                self.buffer = []
                return result

        more = self.interp.runsource('\n'.join(self.buffer))

        if not more:
            self.buffer = []

        return more
Esempio n. 2
0
 def get_command_spec(self, line):
     try:
         if not PY3 and isinstance(line, unicode):
             encoding = getpreferredencoding()
             words = map(lambda s: s.decode(encoding), command_tokenize(line.encode(encoding)))
         else:
             words = command_tokenize(line)
     except ValueError:
         pass
     else:
         if len(words) > 0:
             command_name = words[0]
             if command_name in self.command_table:
                 return [command_name, self.command_table[command_name]]
def key_wrap(obj):
    if PY3:
        if isinstance(obj, str):
            return '"' + obj + '"]'
        elif isinstance(obj, bytes):
            return 'b"' + obj.decode(getpreferredencoding()) + '"]'
        else:
            return obj
    else:
        if isinstance(obj, str):
            return '"' + obj + '"]'
        elif isinstance(obj, unicode):
            return 'u"' + str(obj) + '"]'
        else:
            return obj
Esempio n. 4
0
 def runcommand(self, line):
     try:
         if not PY3 and isinstance(line, unicode):
             encoding = getpreferredencoding()
             words = map(lambda s: s.decode(encoding), command_tokenize(line.encode(encoding)))
         else:
             words = command_tokenize(line)
     except ValueError:
         pass
     else:
         if len(words) > 0:
             command_name = words[0]
             if command_name in self.command_table:
                 source = "__command_table['%s'](%s)" % (command_name, ",".join(words[1:]))
                 self.runsource(source)
Esempio n. 5
0
 def is_commandline(self, line):
     try:
         if not PY3 and isinstance(line, unicode):
             encoding = getpreferredencoding()
             words = map(lambda s: s.decode(encoding), command_tokenize(line.encode(encoding)))
         else:
             words = command_tokenize(line)
     except ValueError:
         return False
     else:
         if len(words) > 0:
             command_name = words[0]
             return command_name in self.command_table
         else:
             return False
Esempio n. 6
0
    def __init__(self, interp, config):
        """Initialise the repl.

        interp is a Python code.InteractiveInterpreter instance

        config is a populated bpython.config.Struct.
        """
        self.config = config
        self.buffer = []
        self.interp = interp
        self.interp.syntaxerror_callback = self.clear_current_line
        self.match = False
        self.s = ""
        self.cpos = 0
        self.s_hist = []
        self.rl_history = History(allow_duplicates=self.config.hist_duplicates)
        self.stdin_history = History()
        self.stdout_history = History()
        self.evaluating = False
        self.completer = BPythonCompleter(self.interp.locals, config)
        self.parser = ReplParser(self)
        self.matches = []
        self.matches_iter = MatchesIterator()
        self.argspec = None
        self.list_win_visible = False
        self._C = {}
        self.interact = Interaction(self.config)
        self.ps1 = '>>> '
        self.ps2 = '... '

        # Necessary to fix mercurial.ui.ui expecting sys.stderr to have this
        # attribute
        self.closed = False

        pythonhist = os.path.expanduser(self.config.hist_file)
        if os.path.exists(pythonhist):
            self.rl_history.load(pythonhist,
                                 getpreferredencoding() or "ascii")