Example #1
0
def test_unquote_filename():
    for win32 in (True, False):
        nt.assert_equal(path.unquote_filename('foo.py', win32=win32), 'foo.py')
        nt.assert_equal(path.unquote_filename('foo bar.py', win32=win32), 'foo bar.py')
    nt.assert_equal(path.unquote_filename('"foo.py"', win32=True), 'foo.py')
    nt.assert_equal(path.unquote_filename('"foo bar.py"', win32=True), 'foo bar.py')
    nt.assert_equal(path.unquote_filename("'foo.py'", win32=True), 'foo.py')
    nt.assert_equal(path.unquote_filename("'foo bar.py'", win32=True), 'foo bar.py')
    nt.assert_equal(path.unquote_filename('"foo.py"', win32=False), '"foo.py"')
    nt.assert_equal(path.unquote_filename('"foo bar.py"', win32=False), '"foo bar.py"')
    nt.assert_equal(path.unquote_filename("'foo.py'", win32=False), "'foo.py'")
    nt.assert_equal(path.unquote_filename("'foo bar.py'", win32=False), "'foo bar.py'")
    def save(self, parameter_s=''):
        """Save a set of lines or a macro to a given filename.

        Usage:\\
          %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...

        Options:

          -r: use 'raw' input.  By default, the 'processed' history is used,
          so that magics are loaded in their transformed version to valid
          Python.  If this option is given, the raw input as typed as the
          command line is used instead.
          
          -f: force overwrite.  If file exists, %save will prompt for overwrite
          unless -f is given.

        This function uses the same syntax as %history for input ranges,
        then saves the lines to the filename you specify.

        It adds a '.py' extension to the file if you don't do so yourself, and
        it asks for confirmation before overwriting existing files.

        If `-r` option is used, the default extension is `.ipy`.
        """

        opts, args = self.parse_options(parameter_s, 'fr', mode='list')
        raw = 'r' in opts
        force = 'f' in opts
        ext = '.ipy' if raw else '.py'
        fname, codefrom = unquote_filename(args[0]), " ".join(args[1:])
        if not fname.endswith(('.py', '.ipy')):
            fname += ext
        if os.path.isfile(fname) and not force:
            try:
                overwrite = self.shell.ask_yes_no(
                    'File `%s` exists. Overwrite (y/[N])? ' % fname,
                    default='n')
            except StdinNotImplementedError:
                print(
                    "File `%s` exists. Use `%%save -f %s` to force overwrite" %
                    (fname, parameter_s))
                return
            if not overwrite:
                print('Operation cancelled.')
                return
        try:
            cmds = self.shell.find_user_code(codefrom, raw)
        except (TypeError, ValueError) as e:
            print(e.args[0])
            return
        with io.open(fname, 'w', encoding="utf-8") as f:
            f.write("# coding: utf-8\n")
            f.write(py3compat.cast_unicode(cmds))
        print('The following commands were written to file `%s`:' % fname)
        print(cmds)
Example #3
0
 def make_filename(arg):
     "Make a filename from the given args"
     arg = unquote_filename(arg)
     try:
         filename = get_py_filename(arg)
     except IOError:
         # If it ends with .py but doesn't already exist, assume we want
         # a new file.
         if arg.endswith(".py"):
             filename = arg
         else:
             filename = None
     return filename
Example #4
0
 def make_filename(arg):
     "Make a filename from the given args"
     arg = unquote_filename(arg)
     try:
         filename = get_py_filename(arg)
     except IOError:
         # If it ends with .py but doesn't already exist, assume we want
         # a new file.
         if arg.endswith('.py'):
             filename = arg
         else:
             filename = None
     return filename
Example #5
0
    def save(self, parameter_s=''):
        """Save a set of lines or a macro to a given filename.

        Usage:\\
          %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...

        Options:

          -r: use 'raw' input.  By default, the 'processed' history is used,
          so that magics are loaded in their transformed version to valid
          Python.  If this option is given, the raw input as typed as the
          command line is used instead.
          
          -f: force overwrite.  If file exists, %save will prompt for overwrite
          unless -f is given.

        This function uses the same syntax as %history for input ranges,
        then saves the lines to the filename you specify.

        It adds a '.py' extension to the file if you don't do so yourself, and
        it asks for confirmation before overwriting existing files.

        If `-r` option is used, the default extension is `.ipy`.
        """

        opts,args = self.parse_options(parameter_s,'fr',mode='list')
        raw = 'r' in opts
        force = 'f' in opts
        ext = '.ipy' if raw else '.py'
        fname, codefrom = unquote_filename(args[0]), " ".join(args[1:])
        if not fname.endswith(('.py','.ipy')):
            fname += ext
        if os.path.isfile(fname) and not force:
            try:
                overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n')
            except StdinNotImplementedError:
                print("File `%s` exists. Use `%%save -f %s` to force overwrite" % (fname, parameter_s))
                return
            if not overwrite :
                print('Operation cancelled.')
                return
        try:
            cmds = self.shell.find_user_code(codefrom,raw)
        except (TypeError, ValueError) as e:
            print(e.args[0])
            return
        with io.open(fname,'w', encoding="utf-8") as f:
            f.write("# coding: utf-8\n")
            f.write(py3compat.cast_unicode(cmds))
        print('The following commands were written to file `%s`:' % fname)
        print(cmds)
Example #6
0
 def __call__(self, parameter_s=''):
     """ verbatim from core.magic.osm.pushd, except it calls mycd
         instead.  without this patch, function is noisy because
         it is using the ipython cd function and "-q" is
         not appended to parameter_s
     """
     dir_s = self.shell.dir_stack
     tgt = os.path.expanduser(unquote_filename(parameter_s))
     cwd = py3compat.getcwd().replace(
         self.shell.home_dir, '~')
     if tgt:
         self.mycd(parameter_s)
     dir_s.insert(0, cwd)
     return self.shell.magic('dirs')
Example #7
0
 def writefileref(self, line, cell):
     args = magic_arguments.parse_argstring(self.writefileref, line)
     filename = os.path.expanduser(unquote_filename(args.filename))
     if os.path.exists(filename):
         print("Overwriting %s" % filename)
     else:
         print("Writing %s" % filename)
     mode = 'a' if args.append else 'w'
     with io.open(filename, mode, encoding='utf-8') as f:
         f.write(cell)
     username = os.path.expanduser(args.username)
     if username in ecoopuser.keys():
         #ecoopuser[username]['attibution']=filename
         display('added references for user %s' % username, metadata={'references': ecoopuser[username]})
Example #8
0
    def pushd(self, parameter_s=''):
        """Place the current dir on stack and change directory.

        Usage:\\
          %pushd ['dirname']
        """

        dir_s = self.shell.dir_stack
        tgt = os.path.expanduser(unquote_filename(parameter_s))
        cwd = os.getcwd().replace(self.shell.home_dir,'~')
        if tgt:
            self.cd(parameter_s)
        dir_s.insert(0,cwd)
        return self.shell.magic('dirs')
Example #9
0
    def pushd(self, parameter_s=''):
        """Place the current dir on stack and change directory.

        Usage:\\
          %pushd ['dirname']
        """

        dir_s = self.shell.dir_stack
        tgt = os.path.expanduser(unquote_filename(parameter_s))
        cwd = os.getcwdu().replace(self.shell.home_dir,'~')
        if tgt:
            self.cd(parameter_s)
        dir_s.insert(0,cwd)
        return self.shell.magic('dirs')
Example #10
0
    def file(self, line, cell):
        """Write the contents of the cell to a file.
        
        For frontends that do not support stdin (Notebook), -f is implied.
        """
        args = magic_arguments.parse_argstring(self.file, line)
        filename = os.path.expanduser(unquote_filename(args.filename))

        if os.path.exists(filename):
            if args.amend:
                print "Amending to %s" % filename
            else:
                print "Overwriting %s" % filename
        else:
            print "Writing %s" % filename

        mode = "a" if args.amend else "w"
        with io.open(filename, mode, encoding="utf-8") as f:
            f.write(cell)
Example #11
0
 def file(self, line, cell):
     """Write the contents of the cell to a file.
     
     For frontends that do not support stdin (Notebook), -f is implied.
     """
     args = magic_arguments.parse_argstring(self.file, line)
     filename = unquote_filename(args.filename)
     
     if os.path.exists(filename):
         if args.amend:
             print("Amending to %s" % filename)
         else:
             print("Overwriting %s" % filename)
     else:
         print("Writing %s" % filename)
     
     mode = 'a' if args.amend else 'w'
     with io.open(filename, mode, encoding='utf-8') as f:
         f.write(cell)
Example #12
0
    def notebook(self, s):
        """Export and convert IPython notebooks.

        This function can export the current IPython history to a notebook file
        or can convert an existing notebook file into a different format. For
        example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb".
        To export the history to "foo.py" do "%notebook -e foo.py". To convert
        "foo.ipynb" to "foo.json" do "%notebook -f json foo.ipynb". Possible
        formats include (json/ipynb, py).
        """
        args = magic_arguments.parse_argstring(self.notebook, s)

        from IPython.nbformat import current
        args.filename = unquote_filename(args.filename)
        if args.export:
            fname, name, format = current.parse_filename(args.filename)
            cells = []
            hist = list(self.shell.history_manager.get_range())
            for session, prompt_number, input in hist[:-1]:
                cells.append(
                    current.new_code_cell(prompt_number=prompt_number,
                                          input=input))
            worksheet = current.new_worksheet(cells=cells)
            nb = current.new_notebook(name=name, worksheets=[worksheet])
            with io.open(fname, 'w', encoding='utf-8') as f:
                current.write(nb, f, format)
        elif args.format is not None:
            old_fname, old_name, old_format = current.parse_filename(
                args.filename)
            new_format = args.format
            if new_format == u'xml':
                raise ValueError('Notebooks cannot be written as xml.')
            elif new_format == u'ipynb' or new_format == u'json':
                new_fname = old_name + u'.ipynb'
                new_format = u'json'
            elif new_format == u'py':
                new_fname = old_name + u'.py'
            else:
                raise ValueError('Invalid notebook format: %s' % new_format)
            with io.open(old_fname, 'r', encoding='utf-8') as f:
                nb = current.read(f, old_format)
            with io.open(new_fname, 'w', encoding='utf-8') as f:
                current.write(nb, f, new_format)
Example #13
0
    def writefile(self, line, cell):
        """Write the contents of the cell to a file.
        
        The file will be overwritten unless the -a (--append) flag is specified.
        """
        args = magic_arguments.parse_argstring(self.writefile, line)
        filename = os.path.expanduser(unquote_filename(args.filename))

        if os.path.exists(filename):
            if args.append:
                print("Appending to %s" % filename)
            else:
                print("Overwriting %s" % filename)
        else:
            print("Writing %s" % filename)

        mode = "a" if args.append else "w"
        with io.open(filename, mode, encoding="utf-8") as f:
            f.write(cell)
Example #14
0
 def file(self, line, cell):
     """Write the contents of the cell to a file.
     
     For frontends that do not support stdin (Notebook), -f is implied.
     """
     args = magic_arguments.parse_argstring(self.file, line)
     filename = unquote_filename(args.filename)
     
     if os.path.exists(filename):
         if args.amend:
             print "Amending to %s" % filename
         else:
             print "Overwriting %s" % filename
     else:
         print "Writing %s" % filename
     
     mode = 'a' if args.amend else 'w'
     with io.open(filename, mode, encoding='utf-8') as f:
         f.write(cell)
Example #15
0
    def writefile(self, line, cell):
        """Write the contents of the cell to a file.
        
        The file will be overwritten unless the -a (--append) flag is specified.
        """
        args = magic_arguments.parse_argstring(self.writefile, line)
        filename = os.path.expanduser(unquote_filename(args.filename))

        if os.path.exists(filename):
            if args.append:
                print("Appending to %s" % filename)
            else:
                print("Overwriting %s" % filename)
        else:
            print("Writing %s" % filename)

        mode = 'a' if args.append else 'w'
        with io.open(filename, mode, encoding='utf-8') as f:
            f.write(cell)
Example #16
0
    def notebook(self, s):
        """Export and convert IPython notebooks.

        This function can export the current IPython history to a notebook file
        or can convert an existing notebook file into a different format. For
        example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb".
        To export the history to "foo.py" do "%notebook -e foo.py". To convert
        "foo.ipynb" to "foo.json" do "%notebook -f json foo.ipynb". Possible
        formats include (json/ipynb, py).
        """
        args = magic_arguments.parse_argstring(self.notebook, s)

        from IPython.nbformat import current
        args.filename = unquote_filename(args.filename)
        if args.export:
            fname, name, format = current.parse_filename(args.filename)
            cells = []
            hist = list(self.shell.history_manager.get_range())
            for session, prompt_number, input in hist[:-1]:
                cells.append(current.new_code_cell(prompt_number=prompt_number,
                                                   input=input))
            worksheet = current.new_worksheet(cells=cells)
            nb = current.new_notebook(name=name,worksheets=[worksheet])
            with io.open(fname, 'w', encoding='utf-8') as f:
                current.write(nb, f, format)
        elif args.format is not None:
            old_fname, old_name, old_format = current.parse_filename(args.filename)
            new_format = args.format
            if new_format == u'xml':
                raise ValueError('Notebooks cannot be written as xml.')
            elif new_format == u'ipynb' or new_format == u'json':
                new_fname = old_name + u'.ipynb'
                new_format = u'json'
            elif new_format == u'py':
                new_fname = old_name + u'.py'
            else:
                raise ValueError('Invalid notebook format: %s' % new_format)
            with io.open(old_fname, 'r', encoding='utf-8') as f:
                nb = current.read(f, old_format)
            with io.open(new_fname, 'w', encoding='utf-8') as f:
                current.write(nb, f, new_format)
Example #17
0
    def notebook(self, s):
        """Export and convert IPython notebooks.

        This function can export the current IPython history to a notebook file.
        For example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb".
        To export the history to "foo.py" do "%notebook -e foo.py".
        """
        args = magic_arguments.parse_argstring(self.notebook, s)

        from IPython.nbformat import write, v4
        args.filename = unquote_filename(args.filename)
        if args.export:
            cells = []
            hist = list(self.shell.history_manager.get_range())
            for session, execution_count, input in hist[:-1]:
                cells.append(
                    v4.new_code_cell(execution_count=execution_count,
                                     source=source))
            nb = v4.new_notebook(cells=cells)
            with io.open(args.filename, 'w', encoding='utf-8') as f:
                write(nb, f, version=4)
Example #18
0
    def notebook(self, s):
        """Export and convert IPython notebooks.

        This function can export the current IPython history to a notebook file.
        For example, to export the history to "foo.ipynb" do "%notebook -e foo.ipynb".
        To export the history to "foo.py" do "%notebook -e foo.py".
        """
        args = magic_arguments.parse_argstring(self.notebook, s)

        from IPython.nbformat import write, v4
        args.filename = unquote_filename(args.filename)
        if args.export:
            cells = []
            hist = list(self.shell.history_manager.get_range())
            for session, execution_count, input in hist[:-1]:
                cells.append(v4.new_code_cell(
                    execution_count=execution_count,
                    source=source
                ))
            nb = v4.new_notebook(cells=cells)
            with io.open(args.filename, 'w', encoding='utf-8') as f:
                write(nb, f, version=4)
Example #19
0
    def save(self, parameter_s=''):
        """Save a set of lines or a macro to a given filename.

        Usage:\\
          %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...

        Options:

          -r: use 'raw' input.  By default, the 'processed' history is used,
          so that magics are loaded in their transformed version to valid
          Python.  If this option is given, the raw input as typed as the
          command line is used instead.

        This function uses the same syntax as %history for input ranges,
        then saves the lines to the filename you specify.

        It adds a '.py' extension to the file if you don't do so yourself, and
        it asks for confirmation before overwriting existing files."""

        opts,args = self.parse_options(parameter_s,'r',mode='list')
        fname, codefrom = unquote_filename(args[0]), " ".join(args[1:])
        if not fname.endswith('.py'):
            fname += '.py'
        if os.path.isfile(fname):
            overwrite = self.shell.ask_yes_no('File `%s` exists. Overwrite (y/[N])? ' % fname, default='n')
            if not overwrite :
                print 'Operation cancelled.'
                return
        try:
            cmds = self.shell.find_user_code(codefrom, 'r' in opts)
        except (TypeError, ValueError) as e:
            print e.args[0]
            return
        with io.open(fname,'w', encoding="utf-8") as f:
            f.write(u"# coding: utf-8\n")
            f.write(py3compat.cast_unicode(cmds))
        print 'The following commands were written to file `%s`:' % fname
        print cmds
Example #20
0
    def loadpy(self, arg_s):
        """Load a .py python script into the GUI console.

        This magic command can either take a local filename or a url::

        %loadpy myscript.py
        %loadpy http://www.example.com/myscript.py
        """
        arg_s = unquote_filename(arg_s)
        remote_url = arg_s.startswith(('http://', 'https://'))
        local_url = not remote_url
        if local_url and not arg_s.endswith('.py'):
            # Local files must be .py; for remote URLs it's possible that the
            # fetch URL doesn't have a .py in it (many servers have an opaque
            # URL, such as scipy-central.org).
            raise ValueError('%%loadpy only works with .py files: %s' % arg_s)

        # openpy takes care of finding the source encoding (per PEP 263)
        if remote_url:
            contents = openpy.read_py_url(arg_s, skip_encoding_cookie=True)
        else:
            contents = openpy.read_py_file(arg_s, skip_encoding_cookie=True)

        self.shell.set_next_input(contents)
Example #21
0
    def _run_with_profiler(self, code, opts, namespace):
        """
        Run `code` with profiler.  Used by ``%prun`` and ``%run -p``.

        Parameters
        ----------
        code : str
            Code to be executed.
        opts : Struct
            Options parsed by `self.parse_options`.
        namespace : dict
            A dictionary for Python namespace (e.g., `self.shell.user_ns`).

        """

        # Fill default values for unspecified options:
        opts.merge(Struct(D=[''], l=[], s=['time'], T=['']))

        prof = profile.Profile()
        try:
            prof = prof.runctx(code, namespace, namespace)
            sys_exit = ''
        except SystemExit:
            sys_exit = """*** SystemExit exception caught in code being profiled."""

        stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)

        lims = opts.l
        if lims:
            lims = []  # rebuild lims with ints/floats/strings
            for lim in opts.l:
                try:
                    lims.append(int(lim))
                except ValueError:
                    try:
                        lims.append(float(lim))
                    except ValueError:
                        lims.append(lim)

        # Trap output.
        stdout_trap = StringIO()
        stats_stream = stats.stream
        try:
            stats.stream = stdout_trap
            stats.print_stats(*lims)
        finally:
            stats.stream = stats_stream

        output = stdout_trap.getvalue()
        output = output.rstrip()

        if 'q' not in opts:
            page.page(output)
        print sys_exit,

        dump_file = opts.D[0]
        text_file = opts.T[0]
        if dump_file:
            dump_file = unquote_filename(dump_file)
            prof.dump_stats(dump_file)
            print '\n*** Profile stats marshalled to file',\
                  repr(dump_file)+'.',sys_exit
        if text_file:
            text_file = unquote_filename(text_file)
            pfile = open(text_file,'w')
            pfile.write(output)
            pfile.close()
            print '\n*** Profile printout saved to text file',\
                  repr(text_file)+'.',sys_exit

        if 'r' in opts:
            return stats
        else:
            return None
Example #22
0
    def sync_to_file(self, line, cell):
        """
        assume the target file(s) is encoded in 'utf-8' and only use '\n' as line end
        run the code in cell and sync it with target file(s), encoding using 'utf-8'

        the programme will first determine the search region based on the -a and -b option
        once the region is figured out, what to do next depends on option -m
        if the mode of synchronization is update, then the programme will try to detect the
        write region use the content of the code cell
        :type line: unicode
        :type cell: unicode
        :param line: the arguments passed to the magic command
        :param cell: the lines of the cell below the magic command
        """
        # set the framework of necessary parameters
        log_message_l = []
        par_d = {
            'args_d': None,
            'file_path_l': None,
            'target_str_l': None,
            'n_target_str': None,
            'search_start_index_l': None,
            'search_end_index_l': None,
            'cell': cell,
            'cell_line_l': None,
            'n_cell_line_l': None,
            'modified_target_str_l': None
        }
        # get args
        args_d = vars(parse_argstring(self.sync_to_file, line))
        par_d['args_d'] = args_d
        log_message_l.append('# Parsing arguments...')
        log_message_l.append('-wrap2 ' + str(args_d))
        # run the code
        log_message_l.append('# Running the cell...')
        if not args_d['pass']:
            self.shell.run_cell(cell)
        # read content of files as str
        # if file does not exists, set the str as empty string
        file_path_l = [
            unquote_filename(_file_path) for _file_path in args_d['file']
        ]
        target_str_l = []
        log_message_l.append('# Reading files...')
        for file_path in file_path_l:
            if os.path.exists(file_path):
                with codecs.open(file_path, 'r', encoding='utf-8') as f:
                    # convert \r\n to \n
                    _raw_str = f.read()
                    target_str_l.append(convert_to_unix_line_feed(_raw_str))
            else:
                target_str_l.append('')
        n_target_str = len(target_str_l)
        par_d['file_path_l'], par_d['target_str_l'], par_d['n_target_str'] = \
            file_path_l, target_str_l, n_target_str
        # set core variables
        self.set_search_region(log_message_l, par_d)
        # modify target str
        self.modify_target_str(log_message_l, par_d)
        # write the modified target str to target file
        if not args_d['test']:
            log_message_l.append('# Write target file(s)..')
            for target_index, write_str in enumerate(
                    par_d['modified_target_str_l']):
                file_path = file_path_l[target_index]
                # log_message_l.append('++ Deal with file ' + file_path)
                with codecs.open(file_path, 'w', encoding='utf-8') as f:
                    f.write(write_str)
                # log_message_l.append('-- Finished. File: ' + file_path)
        else:
            log_message_l.append('#Test mode. No file is changed.')
        # output programme log if required
        if args_d['log']:
            print format_log(log_message_l)
Example #23
0
    def save(self, parameter_s=""):
        """Save a set of lines or a macro to a given filename.

        Usage:\\
          %save [options] filename n1-n2 n3-n4 ... n5 .. n6 ...

        Options:

          -r: use 'raw' input.  By default, the 'processed' history is used,
          so that magics are loaded in their transformed version to valid
          Python.  If this option is given, the raw input as typed as the
          command line is used instead.
          
          -f: force overwrite.  If file exists, %save will prompt for overwrite
          unless -f is given.

          -a: append to the file instead of overwriting it.

        This function uses the same syntax as %history for input ranges,
        then saves the lines to the filename you specify.

        It adds a '.py' extension to the file if you don't do so yourself, and
        it asks for confirmation before overwriting existing files.

        If `-r` option is used, the default extension is `.ipy`.
        """

        opts, args = self.parse_options(parameter_s, "fra", mode="list")
        if not args:
            raise UsageError("Missing filename.")
        raw = "r" in opts
        force = "f" in opts
        append = "a" in opts
        mode = "a" if append else "w"
        ext = u".ipy" if raw else u".py"
        fname, codefrom = unquote_filename(args[0]), " ".join(args[1:])
        if not fname.endswith((u".py", u".ipy")):
            fname += ext
        file_exists = os.path.isfile(fname)
        if file_exists and not force and not append:
            try:
                overwrite = self.shell.ask_yes_no("File `%s` exists. Overwrite (y/[N])? " % fname, default="n")
            except StdinNotImplementedError:
                print("File `%s` exists. Use `%%save -f %s` to force overwrite" % (fname, parameter_s))
                return
            if not overwrite:
                print("Operation cancelled.")
                return
        try:
            cmds = self.shell.find_user_code(codefrom, raw)
        except (TypeError, ValueError) as e:
            print(e.args[0])
            return
        out = py3compat.cast_unicode(cmds)
        with io.open(fname, mode, encoding="utf-8") as f:
            if not file_exists or not append:
                f.write(u"# coding: utf-8\n")
            f.write(out)
            # make sure we end on a newline
            if not out.endswith(u"\n"):
                f.write(u"\n")
        print("The following commands were written to file `%s`:" % fname)
        print(cmds)
Example #24
0
    def cd(self, parameter_s=''):
        """Change the current working directory.

        This command automatically maintains an internal list of directories
        you visit during your IPython session, in the variable _dh. The
        command %dhist shows this history nicely formatted. You can also
        do 'cd -<tab>' to see directory history conveniently.

        Usage:

          cd 'dir': changes to directory 'dir'.

          cd -: changes to the last visited directory.

          cd -<n>: changes to the n-th directory in the directory history.

          cd --foo: change to directory that matches 'foo' in history

          cd -b <bookmark_name>: jump to a bookmark set by %bookmark
             (note: cd <bookmark_name> is enough if there is no
              directory <bookmark_name>, but a bookmark with the name exists.)
              'cd -b <tab>' allows you to tab-complete bookmark names.

        Options:

        -q: quiet.  Do not print the working directory after the cd command is
        executed.  By default IPython's cd command does print this directory,
        since the default prompts do not display path information.

        Note that !cd doesn't work for this purpose because the shell where
        !command runs is immediately discarded after executing 'command'.

        Examples
        --------
        ::

          In [10]: cd parent/child
          /home/tsuser/parent/child
        """

        oldcwd = os.getcwdu()
        numcd = re.match(r'(-)(\d+)$',parameter_s)
        # jump in directory history by number
        if numcd:
            nn = int(numcd.group(2))
            try:
                ps = self.shell.user_ns['_dh'][nn]
            except IndexError:
                print 'The requested directory does not exist in history.'
                return
            else:
                opts = {}
        elif parameter_s.startswith('--'):
            ps = None
            fallback = None
            pat = parameter_s[2:]
            dh = self.shell.user_ns['_dh']
            # first search only by basename (last component)
            for ent in reversed(dh):
                if pat in os.path.basename(ent) and os.path.isdir(ent):
                    ps = ent
                    break

                if fallback is None and pat in ent and os.path.isdir(ent):
                    fallback = ent

            # if we have no last part match, pick the first full path match
            if ps is None:
                ps = fallback

            if ps is None:
                print "No matching entry in directory history"
                return
            else:
                opts = {}


        else:
            #turn all non-space-escaping backslashes to slashes,
            # for c:\windows\directory\names\
            parameter_s = re.sub(r'\\(?! )','/', parameter_s)
            opts,ps = self.parse_options(parameter_s,'qb',mode='string')
        # jump to previous
        if ps == '-':
            try:
                ps = self.shell.user_ns['_dh'][-2]
            except IndexError:
                raise UsageError('%cd -: No previous directory to change to.')
        # jump to bookmark if needed
        else:
            if not os.path.isdir(ps) or 'b' in opts:
                bkms = self.shell.db.get('bookmarks', {})

                if ps in bkms:
                    target = bkms[ps]
                    print '(bookmark:%s) -> %s' % (ps, target)
                    ps = target
                else:
                    if 'b' in opts:
                        raise UsageError("Bookmark '%s' not found.  "
                              "Use '%%bookmark -l' to see your bookmarks." % ps)

        # strip extra quotes on Windows, because os.chdir doesn't like them
        ps = unquote_filename(ps)
        # at this point ps should point to the target dir
        if ps:
            try:
                os.chdir(os.path.expanduser(ps))
                if hasattr(self.shell, 'term_title') and self.shell.term_title:
                    set_term_title('IPython: ' + abbrev_cwd())
            except OSError:
                print sys.exc_info()[1]
            else:
                cwd = os.getcwdu()
                dhist = self.shell.user_ns['_dh']
                if oldcwd != cwd:
                    dhist.append(cwd)
                    self.shell.db['dhist'] = compress_dhist(dhist)[-100:]

        else:
            os.chdir(self.shell.home_dir)
            if hasattr(self.shell, 'term_title') and self.shell.term_title:
                set_term_title('IPython: ' + '~')
            cwd = os.getcwdu()
            dhist = self.shell.user_ns['_dh']

            if oldcwd != cwd:
                dhist.append(cwd)
                self.shell.db['dhist'] = compress_dhist(dhist)[-100:]
        if not 'q' in opts and self.shell.user_ns['_dh']:
            print self.shell.user_ns['_dh'][-1]
Example #25
0
    def prun(self, parameter_s='', cell=None, user_mode=True,
                   opts=None,arg_lst=None,prog_ns=None):

        """Run a statement through the python code profiler.

        Usage, in line mode:
          %prun [options] statement

        Usage, in cell mode:
          %%prun [options] [statement]
          code...
          code...

        In cell mode, the additional code lines are appended to the (possibly
        empty) statement in the first line.  Cell mode allows you to easily
        profile multiline blocks without having to put them in a separate
        function.
        
        The given statement (which doesn't require quote marks) is run via the
        python profiler in a manner similar to the profile.run() function.
        Namespaces are internally managed to work correctly; profile.run
        cannot be used in IPython because it makes certain assumptions about
        namespaces which do not hold under IPython.

        Options:

        -l <limit>: you can place restrictions on what or how much of the
        profile gets printed. The limit value can be:

          * A string: only information for function names containing this string
          is printed.

          * An integer: only these many lines are printed.

          * A float (between 0 and 1): this fraction of the report is printed
          (for example, use a limit of 0.4 to see the topmost 40% only).

        You can combine several limits with repeated use of the option. For
        example, '-l __init__ -l 5' will print only the topmost 5 lines of
        information about class constructors.

        -r: return the pstats.Stats object generated by the profiling. This
        object has all the information about the profile in it, and you can
        later use it for further analysis or in other functions.

       -s <key>: sort profile by given key. You can provide more than one key
        by using the option several times: '-s key1 -s key2 -s key3...'. The
        default sorting key is 'time'.

        The following is copied verbatim from the profile documentation
        referenced below:

        When more than one key is provided, additional keys are used as
        secondary criteria when the there is equality in all keys selected
        before them.

        Abbreviations can be used for any key names, as long as the
        abbreviation is unambiguous.  The following are the keys currently
        defined:

                Valid Arg       Meaning
                  "calls"      call count
                  "cumulative" cumulative time
                  "file"       file name
                  "module"     file name
                  "pcalls"     primitive call count
                  "line"       line number
                  "name"       function name
                  "nfl"        name/file/line
                  "stdname"    standard name
                  "time"       internal time

        Note that all sorts on statistics are in descending order (placing
        most time consuming items first), where as name, file, and line number
        searches are in ascending order (i.e., alphabetical). The subtle
        distinction between "nfl" and "stdname" is that the standard name is a
        sort of the name as printed, which means that the embedded line
        numbers get compared in an odd way.  For example, lines 3, 20, and 40
        would (if the file names were the same) appear in the string order
        "20" "3" and "40".  In contrast, "nfl" does a numeric compare of the
        line numbers.  In fact, sort_stats("nfl") is the same as
        sort_stats("name", "file", "line").

        -T <filename>: save profile results as shown on screen to a text
        file. The profile is still shown on screen.

        -D <filename>: save (via dump_stats) profile statistics to given
        filename. This data is in a format understood by the pstats module, and
        is generated by a call to the dump_stats() method of profile
        objects. The profile is still shown on screen.

        -q: suppress output to the pager.  Best used with -T and/or -D above.

        If you want to run complete programs under the profiler's control, use
        '%run -p [prof_opts] filename.py [args to program]' where prof_opts
        contains profiler specific options as described here.

        You can read the complete documentation for the profile module with::

          In [1]: import profile; profile.help()
        """

        opts_def = Struct(D=[''],l=[],s=['time'],T=[''])

        if user_mode:  # regular user call
            opts,arg_str = self.parse_options(parameter_s,'D:l:rs:T:q',
                                              list_all=True, posix=False)
            namespace = self.shell.user_ns
            if cell is not None:
                arg_str += '\n' + cell
        else:  # called to run a program by %run -p
            try:
                filename = get_py_filename(arg_lst[0])
            except IOError as e:
                try:
                    msg = str(e)
                except UnicodeError:
                    msg = e.message
                error(msg)
                return

            arg_str = 'execfile(filename,prog_ns)'
            namespace = {
                'execfile': self.shell.safe_execfile,
                'prog_ns': prog_ns,
                'filename': filename
                }

        opts.merge(opts_def)

        prof = profile.Profile()
        try:
            prof = prof.runctx(arg_str,namespace,namespace)
            sys_exit = ''
        except SystemExit:
            sys_exit = """*** SystemExit exception caught in code being profiled."""

        stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)

        lims = opts.l
        if lims:
            lims = []  # rebuild lims with ints/floats/strings
            for lim in opts.l:
                try:
                    lims.append(int(lim))
                except ValueError:
                    try:
                        lims.append(float(lim))
                    except ValueError:
                        lims.append(lim)

        # Trap output.
        stdout_trap = StringIO()
        stats_stream = stats.stream
        try:
            stats.stream = stdout_trap
            stats.print_stats(*lims)
        finally:
            stats.stream = stats_stream

        output = stdout_trap.getvalue()
        output = output.rstrip()

        if 'q' not in opts:
            page.page(output)
        print sys_exit,

        dump_file = opts.D[0]
        text_file = opts.T[0]
        if dump_file:
            dump_file = unquote_filename(dump_file)
            prof.dump_stats(dump_file)
            print '\n*** Profile stats marshalled to file',\
                  repr(dump_file)+'.',sys_exit
        if text_file:
            text_file = unquote_filename(text_file)
            pfile = open(text_file,'w')
            pfile.write(output)
            pfile.close()
            print '\n*** Profile printout saved to text file',\
                  repr(text_file)+'.',sys_exit

        if 'r' in opts:
            return stats
        else:
            return None
Example #26
0
  def savef(self, parameter_s=''):
        """Save last line to a file specified , builds python script as a stack of commands
        i.e. pushes statements to the file in append mode

        Usage:\\
          %savef [options]

        Options:

            -s: SET new file to write , otherwise would write to temp.py

            -f: force create new file ( overwrite original to be used with -s )

            -r: use 'raw' input.  By default, the 'processed' history is used,
            so that magics are loaded in their transformed version to valid
            Python.  If this option is given, the raw input as typed as the
            command line is used instead.

        It works in modes either you can start a file or write commands,not both simultaneously
        This function serves as an extension to %save magic function.It writes to
        a file line by line instead of block for rapid development.
        Possible Enhancements (1) merge with save

        """

        opts, args = self.parse_options(parameter_s, 'srf', mode='list')
        set_f= 's' in opts
        raw = 'r' in opts
        force = 'f' in opts
        ext = u'.ipy' if raw else u'.py'
        n = 1
        global filename

        if set_f:
            filename = unquote_filename(args[0])
            if not filename.endswith((u'.py',u'.ipy')):
                filename += ext

        elif args:
            n = int(args[0])

        file_exists = os.path.isfile(filename)

        new_file = (force and file_exists) or not file_exists
        mode = 'w' if new_file else 'a'

        try:
            hist = self.shell.history_manager.get_tail(n, raw)
            cmds = "\n".join([x[2] for x in hist])

        except (TypeError, ValueError) as e:
            print(e.args[0])
            return
        out = py3compat.cast_unicode(cmds)
        with io.open(filename, mode, encoding="utf-8") as f:
            if new_file:
                print('Starting python file: `%s`' % filename)
                f.write(u"# coding: utf-8\n")
            elif not set_f:
                # Only allow setting of file in one command individual files can be pushed later
                f.write(out)
                print(out)
            if not out.endswith(u'\n'):
                f.write(u'\n')
Example #27
0
    def cd(self, parameter_s=''):
        """Change the current working directory.

        This command automatically maintains an internal list of directories
        you visit during your IPython session, in the variable _dh. The
        command %dhist shows this history nicely formatted. You can also
        do 'cd -<tab>' to see directory history conveniently.

        Usage:

          cd 'dir': changes to directory 'dir'.

          cd -: changes to the last visited directory.

          cd -<n>: changes to the n-th directory in the directory history.

          cd --foo: change to directory that matches 'foo' in history

          cd -b <bookmark_name>: jump to a bookmark set by %bookmark
             (note: cd <bookmark_name> is enough if there is no
              directory <bookmark_name>, but a bookmark with the name exists.)
              'cd -b <tab>' allows you to tab-complete bookmark names.

        Options:

        -q: quiet.  Do not print the working directory after the cd command is
        executed.  By default IPython's cd command does print this directory,
        since the default prompts do not display path information.

        Note that !cd doesn't work for this purpose because the shell where
        !command runs is immediately discarded after executing 'command'.

        Examples
        --------
        ::

          In [10]: cd parent/child
          /home/tsuser/parent/child
        """

        oldcwd = os.getcwd()
        numcd = re.match(r'(-)(\d+)$',parameter_s)
        # jump in directory history by number
        if numcd:
            nn = int(numcd.group(2))
            try:
                ps = self.shell.user_ns['_dh'][nn]
            except IndexError:
                print('The requested directory does not exist in history.')
                return
            else:
                opts = {}
        elif parameter_s.startswith('--'):
            ps = None
            fallback = None
            pat = parameter_s[2:]
            dh = self.shell.user_ns['_dh']
            # first search only by basename (last component)
            for ent in reversed(dh):
                if pat in os.path.basename(ent) and os.path.isdir(ent):
                    ps = ent
                    break

                if fallback is None and pat in ent and os.path.isdir(ent):
                    fallback = ent

            # if we have no last part match, pick the first full path match
            if ps is None:
                ps = fallback

            if ps is None:
                print("No matching entry in directory history")
                return
            else:
                opts = {}


        else:
            #turn all non-space-escaping backslashes to slashes,
            # for c:\windows\directory\names\
            parameter_s = re.sub(r'\\(?! )','/', parameter_s)
            opts,ps = self.parse_options(parameter_s,'qb',mode='string')
        # jump to previous
        if ps == '-':
            try:
                ps = self.shell.user_ns['_dh'][-2]
            except IndexError:
                raise UsageError('%cd -: No previous directory to change to.')
        # jump to bookmark if needed
        else:
            if not os.path.isdir(ps) or 'b' in opts:
                bkms = self.shell.db.get('bookmarks', {})

                if ps in bkms:
                    target = bkms[ps]
                    print('(bookmark:%s) -> %s' % (ps, target))
                    ps = target
                else:
                    if 'b' in opts:
                        raise UsageError("Bookmark '%s' not found.  "
                              "Use '%%bookmark -l' to see your bookmarks." % ps)

        # strip extra quotes on Windows, because os.chdir doesn't like them
        ps = unquote_filename(ps)
        # at this point ps should point to the target dir
        if ps:
            try:
                os.chdir(os.path.expanduser(ps))
                if hasattr(self.shell, 'term_title') and self.shell.term_title:
                    set_term_title('IPython: ' + abbrev_cwd())
            except OSError:
                print(sys.exc_info()[1])
            else:
                cwd = os.getcwd()
                dhist = self.shell.user_ns['_dh']
                if oldcwd != cwd:
                    dhist.append(cwd)
                    self.shell.db['dhist'] = compress_dhist(dhist)[-100:]

        else:
            os.chdir(self.shell.home_dir)
            if hasattr(self.shell, 'term_title') and self.shell.term_title:
                set_term_title('IPython: ' + '~')
            cwd = os.getcwd()
            dhist = self.shell.user_ns['_dh']

            if oldcwd != cwd:
                dhist.append(cwd)
                self.shell.db['dhist'] = compress_dhist(dhist)[-100:]
        if not 'q' in opts and self.shell.user_ns['_dh']:
            print(self.shell.user_ns['_dh'][-1])
Example #28
0
    def _run_with_profiler(self, code, opts, namespace):
        """
        Run `code` with profiler.  Used by ``%prun`` and ``%run -p``.

        Parameters
        ----------
        code : str
            Code to be executed.
        opts : Struct
            Options parsed by `self.parse_options`.
        namespace : dict
            A dictionary for Python namespace (e.g., `self.shell.user_ns`).

        """

        # Fill default values for unspecified options:
        opts.merge(Struct(D=[''], l=[], s=['time'], T=['']))

        prof = profile.Profile()
        try:
            prof = prof.runctx(code, namespace, namespace)
            sys_exit = ''
        except SystemExit:
            sys_exit = """*** SystemExit exception caught in code being profiled."""

        stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)

        lims = opts.l
        if lims:
            lims = []  # rebuild lims with ints/floats/strings
            for lim in opts.l:
                try:
                    lims.append(int(lim))
                except ValueError:
                    try:
                        lims.append(float(lim))
                    except ValueError:
                        lims.append(lim)

        # Trap output.
        stdout_trap = StringIO()
        stats_stream = stats.stream
        try:
            stats.stream = stdout_trap
            stats.print_stats(*lims)
        finally:
            stats.stream = stats_stream

        output = stdout_trap.getvalue()
        output = output.rstrip()

        if 'q' not in opts:
            page.page(output)
        print sys_exit,

        dump_file = opts.D[0]
        text_file = opts.T[0]
        if dump_file:
            dump_file = unquote_filename(dump_file)
            prof.dump_stats(dump_file)
            print '\n*** Profile stats marshalled to file',\
                  repr(dump_file)+'.',sys_exit
        if text_file:
            text_file = unquote_filename(text_file)
            pfile = open(text_file, 'w')
            pfile.write(output)
            pfile.close()
            print '\n*** Profile printout saved to text file',\
                  repr(text_file)+'.',sys_exit

        if 'r' in opts:
            return stats
        else:
            return None
    def sync_to_file(self, line, cell):
        """
        assume the target file(s) is encoded in 'utf-8' and only use '\n' as line end
        run the code in cell and sync it with target file(s), encoding using 'utf-8'

        the programme will first determine the search region based on the -a and -b option
        once the region is figured out, what to do next depends on option -m
        if the mode of synchronization is update, then the programme will try to detect the
        write region use the content of the code cell
        :type line: unicode
        :type cell: unicode
        :param line: the arguments passed to the magic command
        :param cell: the lines of the cell below the magic command
        """
        # set the framework of necessary parameters
        log_message_l = []
        par_d = {'args_d': None,
                 'file_path_l': None,
                 'target_str_l': None,
                 'n_target_str': None,
                 'search_start_index_l': None,
                 'search_end_index_l': None,
                 'cell': cell,
                 'cell_line_l': None,
                 'n_cell_line_l': None,
                 'modified_target_str_l': None}
        # get args
        args_d = vars(parse_argstring(self.sync_to_file, line))
        par_d['args_d'] = args_d
        log_message_l.append('# Parsing arguments...')
        log_message_l.append('-wrap2 ' + str(args_d))
        # run the code
        log_message_l.append('# Running the cell...')
        if not args_d['pass']:
            self.shell.run_cell(cell)
        # read content of files as str
        # if file does not exists, set the str as empty string
        file_path_l = [unquote_filename(_file_path) for _file_path in args_d['file']]
        target_str_l = []
        log_message_l.append('# Reading files...')
        for file_path in file_path_l:
            if os.path.exists(file_path):
                with codecs.open(file_path, 'r', encoding='utf-8') as f:
                    # convert \r\n to \n
                    _raw_str = f.read()
                    target_str_l.append(convert_to_unix_line_feed(_raw_str))
            else:
                target_str_l.append('')
        n_target_str = len(target_str_l)
        par_d['file_path_l'], par_d['target_str_l'], par_d['n_target_str'] = \
            file_path_l, target_str_l, n_target_str
        # set core variables
        self.set_search_region(log_message_l, par_d)
        # modify target str
        self.modify_target_str(log_message_l, par_d)
        # write the modified target str to target file
        if not args_d['test']:
            log_message_l.append('# Write target file(s)..')
            for target_index, write_str in enumerate(par_d['modified_target_str_l']):
                file_path = file_path_l[target_index]
                # log_message_l.append('++ Deal with file ' + file_path)
                with codecs.open(file_path, 'w', encoding='utf-8') as f:
                    f.write(write_str)
                # log_message_l.append('-- Finished. File: ' + file_path)
        else:
            log_message_l.append('#Test mode. No file is changed.')
        # output programme log if required
        if args_d['log']:
            print format_log(log_message_l)
Example #30
0
    def prun(self,
             parameter_s='',
             cell=None,
             user_mode=True,
             opts=None,
             arg_lst=None,
             prog_ns=None):
        """Run a statement through the python code profiler.

        Usage, in line mode:
          %prun [options] statement

        Usage, in cell mode:
          %%prun [options] [statement]
          code...
          code...

        In cell mode, the additional code lines are appended to the (possibly
        empty) statement in the first line.  Cell mode allows you to easily
        profile multiline blocks without having to put them in a separate
        function.
        
        The given statement (which doesn't require quote marks) is run via the
        python profiler in a manner similar to the profile.run() function.
        Namespaces are internally managed to work correctly; profile.run
        cannot be used in IPython because it makes certain assumptions about
        namespaces which do not hold under IPython.

        Options:

        -l <limit>: you can place restrictions on what or how much of the
        profile gets printed. The limit value can be:

          * A string: only information for function names containing this string
          is printed.

          * An integer: only these many lines are printed.

          * A float (between 0 and 1): this fraction of the report is printed
          (for example, use a limit of 0.4 to see the topmost 40% only).

        You can combine several limits with repeated use of the option. For
        example, '-l __init__ -l 5' will print only the topmost 5 lines of
        information about class constructors.

        -r: return the pstats.Stats object generated by the profiling. This
        object has all the information about the profile in it, and you can
        later use it for further analysis or in other functions.

       -s <key>: sort profile by given key. You can provide more than one key
        by using the option several times: '-s key1 -s key2 -s key3...'. The
        default sorting key is 'time'.

        The following is copied verbatim from the profile documentation
        referenced below:

        When more than one key is provided, additional keys are used as
        secondary criteria when the there is equality in all keys selected
        before them.

        Abbreviations can be used for any key names, as long as the
        abbreviation is unambiguous.  The following are the keys currently
        defined:

                Valid Arg       Meaning
                  "calls"      call count
                  "cumulative" cumulative time
                  "file"       file name
                  "module"     file name
                  "pcalls"     primitive call count
                  "line"       line number
                  "name"       function name
                  "nfl"        name/file/line
                  "stdname"    standard name
                  "time"       internal time

        Note that all sorts on statistics are in descending order (placing
        most time consuming items first), where as name, file, and line number
        searches are in ascending order (i.e., alphabetical). The subtle
        distinction between "nfl" and "stdname" is that the standard name is a
        sort of the name as printed, which means that the embedded line
        numbers get compared in an odd way.  For example, lines 3, 20, and 40
        would (if the file names were the same) appear in the string order
        "20" "3" and "40".  In contrast, "nfl" does a numeric compare of the
        line numbers.  In fact, sort_stats("nfl") is the same as
        sort_stats("name", "file", "line").

        -T <filename>: save profile results as shown on screen to a text
        file. The profile is still shown on screen.

        -D <filename>: save (via dump_stats) profile statistics to given
        filename. This data is in a format understood by the pstats module, and
        is generated by a call to the dump_stats() method of profile
        objects. The profile is still shown on screen.

        -q: suppress output to the pager.  Best used with -T and/or -D above.

        If you want to run complete programs under the profiler's control, use
        '%run -p [prof_opts] filename.py [args to program]' where prof_opts
        contains profiler specific options as described here.

        You can read the complete documentation for the profile module with::

          In [1]: import profile; profile.help()
        """

        opts_def = Struct(D=[''], l=[], s=['time'], T=[''])

        if user_mode:  # regular user call
            opts, arg_str = self.parse_options(parameter_s,
                                               'D:l:rs:T:q',
                                               list_all=True,
                                               posix=False)
            namespace = self.shell.user_ns
            if cell is not None:
                arg_str += '\n' + cell
        else:  # called to run a program by %run -p
            try:
                filename = get_py_filename(arg_lst[0])
            except IOError as e:
                try:
                    msg = str(e)
                except UnicodeError:
                    msg = e.message
                error(msg)
                return

            arg_str = 'execfile(filename,prog_ns)'
            namespace = {
                'execfile': self.shell.safe_execfile,
                'prog_ns': prog_ns,
                'filename': filename
            }

        opts.merge(opts_def)

        prof = profile.Profile()
        try:
            prof = prof.runctx(arg_str, namespace, namespace)
            sys_exit = ''
        except SystemExit:
            sys_exit = """*** SystemExit exception caught in code being profiled."""

        stats = pstats.Stats(prof).strip_dirs().sort_stats(*opts.s)

        lims = opts.l
        if lims:
            lims = []  # rebuild lims with ints/floats/strings
            for lim in opts.l:
                try:
                    lims.append(int(lim))
                except ValueError:
                    try:
                        lims.append(float(lim))
                    except ValueError:
                        lims.append(lim)

        # Trap output.
        stdout_trap = StringIO()
        stats_stream = stats.stream
        try:
            stats.stream = stdout_trap
            stats.print_stats(*lims)
        finally:
            stats.stream = stats_stream

        output = stdout_trap.getvalue()
        output = output.rstrip()

        if 'q' not in opts:
            page.page(output)
        print sys_exit,

        dump_file = opts.D[0]
        text_file = opts.T[0]
        if dump_file:
            dump_file = unquote_filename(dump_file)
            prof.dump_stats(dump_file)
            print '\n*** Profile stats marshalled to file',\
                  repr(dump_file)+'.',sys_exit
        if text_file:
            text_file = unquote_filename(text_file)
            pfile = open(text_file, 'w')
            pfile.write(output)
            pfile.close()
            print '\n*** Profile printout saved to text file',\
                  repr(text_file)+'.',sys_exit

        if 'r' in opts:
            return stats
        else:
            return None