parser.python

Here are the examples of the python api parser.python taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

1 Examples 7

Example 1

Project: python-literate Source File: loader.py
Function: importer
def importer(name, globals=globals(), locals=locals(), fromlist=[], level=-1):
    try:
        return builtin_import(name, globals, locals, fromlist, level)
    except ImportError as error:
        location = exists(name)
        if location:
            name, path = location
            source = open(path).read()
            code = parser.python(source)
            dest = name + '.py'
            cache_dest = name + '.pyc'

            # the importer doesn't create plain `.py` files by itself, but 
            # the pylit executable can (if asked) and if they're there, we 
            # keep them up to date
            if os.path.exists(dest):
                with open(dest, 'w'):
                    dest.write(code)
            else:
                with tempfile(code) as buff:
                    py_compile.compile(buff, cache_dest, path, doraise=True)

            return builtin_import(name)
        else:
            raise error