Beispiel #1
0
        def sumHelp(arg, column):
            keepstate = Statekeeper(self, (
                'stdout',
                'stderr',
            ))
            keepsys = Statekeeper(sys, (
                'stdout',
                'stderr',
            ))
            try:
                data = None
                stdout = StringIO.StringIO()

                # Replace stderr and stdout
                sys.stdout = self.stdout = stdout
                self.stderr = sys.stderr = StringIO.StringIO()

                callHelp(arg, False, stdout=stdout)
                data = stdout.getvalue()
                #if not data or len(data) == 0:
                #	data = str(self.nohelp % (arg,))

                data = data.split('\n\n', 1)[0].replace("\n", " ")
                return splitHelp(data, column)
            finally:
                # Restore
                keepstate.restore()
                keepsys.restore()
Beispiel #2
0
	def redirect_streams(self, statement):
		self.kept_state = Statekeeper(self, ('stdout','stdin','stderr',))			
		self.kept_sys = Statekeeper(sys, ('stdout','stdin','stderr',))
		if statement.parsed.pipeTo:
			self.redirect = subprocess.Popen(statement.parsed.pipeTo, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
			sys.stdout = self.stdout = self.redirect.stdin
		elif statement.parsed.output:
			if (not statement.parsed.outputTo) and (not can_clip):
				raise EnvironmentError('Cannot redirect to paste buffer; install ``xclip`` and re-run to enable')

			if statement.parsed.outputTo:
				mode = 'w'
				if statement.parsed.output == 2 * self.redirector:
					mode = 'a'
				sys.stdout = self.stdout = open(os.path.expanduser(statement.parsed.outputTo), mode)							
			else:
				sys.stdout = self.stdout = tempfile.TemporaryFile(mode="w+")
				if statement.parsed.output == '>>':
					self.stdout.write(get_paste_buffer())

		if statement.parsed.input:
			if (not statement.parsed.inputFrom) and (not can_clip):
				raise EnvironmentError('Cannot redirect from paste buffer; install ``xclip`` and re-run to enable')

			if statement.parsed.inputFrom:
				mode = 'r'
				sys.stdin = self.stdin = open(os.path.expanduser(statement.parsed.inputFrom), mode)					
			else:
				self.stdin.write(get_paste_buffer())
Beispiel #3
0
 def help_(self):
     kept_state = Statekeeper(obj, ('stdout', 'stdin', 'stderr'))
     try:
         obj.stdout = self.stdout
         obj.stdin = self.stdin
         obj.stderr = self.stderr
         return obj.onecmd('help')
     finally:
         kept_state.restore()
Beispiel #4
0
			def do_(self, args):
				kept_state = Statekeeper(obj, ('stdout', 'stdin', 'stderr',))
				try:
					obj.stdout = self.stdout
					obj.stdin = self.stdin
					obj.stderr = self.stderr
					if not args:
						self.printError('*** No command\n')
						return obj.do_help("", stdout = self.stderr)
					else:
						if isinstance(args, ParsedString):
							args = args.parsed.args
						return obj.onecmd_plus_hooks(args)
				finally:			
					kept_state.restore()
Beispiel #5
0
    def do_py(self, line):
        '''
        py <command>: Executes a Python command.
        py: Enters interactive Python mode.
        End with ``Ctrl-D`` (Unix) / ``Ctrl-Z`` (Windows), ``quit()``, '`exit()``.
        Non-python commands can be issued with ``cmd("your command")``.
        Run python code from external files with ``run("filename.py")``
        '''
        self.pystate['self'] = self
        line = self.rebuildline(line)
        arg = line.strip()
        localvars = (self.locals_in_py and self.pystate) or {}
        from code import InteractiveConsole, InteractiveInterpreter
        interp = InteractiveConsole(locals=localvars)
        interp.runcode('import sys, os;sys.path.insert(0, os.getcwd())')
        if arg.strip():
            interp.runcode(arg)
        else:

            def quit():
                raise EmbeddedConsoleExit

            def onecmd_plus_hooks(arg):
                return self.onecmd_plus_hooks(arg + '\n')

            def run(arg):
                try:
                    file = open(arg)
                    interp.runcode(file.read())
                    file.close()
                except IOError as e:
                    self.perror(e)

            self.pystate['quit'] = quit
            self.pystate['exit'] = quit
            self.pystate['cmd'] = onecmd_plus_hooks
            self.pystate['run'] = run
            try:
                cprt = 'Type "help", "copyright", "credits" or "license" for more information.'
                keepstate = Statekeeper(sys, ('stdin', 'stdout'))
                sys.stdout = self.stdout
                sys.stdin = self.stdin
                interp.interact(banner="Python %s on %s\n%s\n(%s)\n%s" %
                                (sys.version, sys.platform, cprt,
                                 self.__class__.__name__, self.do_py.__doc__))
            except EmbeddedConsoleExit:
                pass
            keepstate.restore()