Esempio n. 1
0
def test_read_file():
    read_specified_enc = io.open(nonascii_path, encoding='iso-8859-5').read()
    read_detected_enc = openpy.read_py_file(nonascii_path, skip_encoding_cookie=False)
    nt.assert_equal(read_detected_enc, read_specified_enc)
    assert u'coding: iso-8859-5' in read_detected_enc
    
    read_strip_enc_cookie = openpy.read_py_file(nonascii_path, skip_encoding_cookie=True)
    assert u'coding: iso-8859-5' not in read_strip_enc_cookie
Esempio n. 2
0
def test_read_file():
    with io.open(nonascii_path, encoding="iso-8859-5") as f:
        read_specified_enc = f.read()
    read_detected_enc = openpy.read_py_file(nonascii_path,
                                            skip_encoding_cookie=False)
    assert read_detected_enc == read_specified_enc
    assert "coding: iso-8859-5" in read_detected_enc

    read_strip_enc_cookie = openpy.read_py_file(nonascii_path,
                                                skip_encoding_cookie=True)
    assert "coding: iso-8859-5" not in read_strip_enc_cookie
Esempio n. 3
0
    def pfile(self, obj, oname=""):
        """Show the whole file where an object was defined."""

        lineno = find_source_lines(obj)
        if lineno is None:
            self.noinfo("file", oname)
            return

        ofile = find_file(obj)
        # run contents of file through pager starting at line where the object
        # is defined, as long as the file isn't binary and is actually on the
        # filesystem.
        if ofile.endswith((".so", ".dll", ".pyd")):
            print("File %r is binary, not printing." % ofile)
        elif not os.path.isfile(ofile):
            print("File %r does not exist, not printing." % ofile)
        else:
            # Print only text files, not extension binaries.  Note that
            # getsourcelines returns lineno with 1-offset and page() uses
            # 0-offset, so we must adjust.
            page.page(
                self.format(
                    openpy.read_py_file(ofile, skip_encoding_cookie=False)),
                lineno - 1,
            )
Esempio n. 4
0
    def less(self, arg_s):
        """Show a file through the pager.

        Files ending in .py are syntax-highlighted."""
        if not arg_s:
            raise UsageError('Missing filename.')

        if arg_s.endswith('.py'):
            cont = self.shell.pycolorize(openpy.read_py_file(arg_s, skip_encoding_cookie=False))
        else:
            cont = open(arg_s).read()
        page.page(cont)
Esempio n. 5
0
    def less(self, arg_s):
        """Show a file through the pager.

        Files ending in .py are syntax-highlighted."""
        if not arg_s:
            raise UsageError('Missing filename.')

        if arg_s.endswith('.py'):
            cont = self.shell.pycolorize(openpy.read_py_file(arg_s, skip_encoding_cookie=False))
        else:
            cont = open(arg_s).read()
        page.page(cont)
Esempio n. 6
0
    def pfile(self, obj, oname=''):
        """Show the whole file where an object was defined."""
        
        lineno = find_source_lines(obj)
        if lineno is None:
            self.noinfo('file', oname)
            return

        ofile = find_file(obj)
        # run contents of file through pager starting at line where the object
        # is defined, as long as the file isn't binary and is actually on the
        # filesystem.
        if ofile.endswith(('.so', '.dll', '.pyd')):
            print('File %r is binary, not printing.' % ofile)
        elif not os.path.isfile(ofile):
            print('File %r does not exist, not printing.' % ofile)
        else:
            # Print only text files, not extension binaries.  Note that
            # getsourcelines returns lineno with 1-offset and page() uses
            # 0-offset, so we must adjust.
            page.page(self.format(openpy.read_py_file(ofile, skip_encoding_cookie=False)), lineno - 1)
Esempio n. 7
0
    def pfile(self, parameter_s='', namespaces=None):
        """Print (or run through pager) the file where an object is defined.

        The file opens at the line where the object definition begins. IPython
        will honor the environment variable PAGER if set, and otherwise will
        do its best to print the file in a convenient form.

        If the given argument is not an object currently defined, IPython will
        try to interpret it as a filename (automatically adding a .py extension
        if needed). You can thus use %pfile as a syntax highlighting code
        viewer."""

        # first interpret argument as an object name
        out = self.shell._inspect('pfile',parameter_s, namespaces)
        # if not, try the input as a filename
        if out == 'not found':
            try:
                filename = get_py_filename(parameter_s)
            except IOError as msg:
                print(msg)
                return
            page.page(self.shell.pycolorize(read_py_file(filename, skip_encoding_cookie=False)))
Esempio n. 8
0
    def pfile(self, parameter_s='', namespaces=None):
        """Print (or run through pager) the file where an object is defined.

        The file opens at the line where the object definition begins. IPython
        will honor the environment variable PAGER if set, and otherwise will
        do its best to print the file in a convenient form.

        If the given argument is not an object currently defined, IPython will
        try to interpret it as a filename (automatically adding a .py extension
        if needed). You can thus use %pfile as a syntax highlighting code
        viewer."""

        # first interpret argument as an object name
        out = self.shell._inspect('pfile',parameter_s, namespaces)
        # if not, try the input as a filename
        if out == 'not found':
            try:
                filename = get_py_filename(parameter_s)
            except IOError as msg:
                print(msg)
                return
            page.page(self.shell.pycolorize(read_py_file(filename, skip_encoding_cookie=False)))
Esempio n. 9
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)