File: compile.py

package info (click to toggle)
python-enaml 0.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,284 kB
  • sloc: python: 31,443; cpp: 4,499; makefile: 140; javascript: 68; lisp: 53; sh: 20
file content (51 lines) | stat: -rw-r--r-- 1,471 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
""" Command-line tool to compile .enaml files. """
import os
import sys
import py_compile

from enaml.core.import_hooks import EnamlImporter, make_file_info


class EnamlCompileError(py_compile.PyCompileError):
    pass


def compile(filename, doraise=False, quiet=0):
    """Byte-compile one enaml source file to bytecode.

    :param filename: The source file name.
    :param doraise: Flag indicating whether or not an exception should be
        raised when a compile error is found.  If an exception occurs and this
        flag is set to False, a string indicating the nature of the exception
        will be printed, and the function will return to the caller. If an
        exception occurs and this flag is set to True, a PyCompileError
        exception will be raised.
    :param quiet: Return full output with False or 0, errors only with 1,
        and no output with 2.

    :return: Path to the resulting byte compiled file.
    """
    file_info = make_file_info(filename)

    try:
        importer = EnamlImporter(file_info)
        importer.compile_code()
    except Exception as err:
        py_exc = EnamlCompileError(err.__class__, err, filename)
        if quiet < 2:
            if doraise:
                raise py_exc
            else:
                sys.stderr.write(py_exc.msg + '\n')
        return

    return file_info.cache_path


def main():
    py_compile.compile = compile
    py_compile.main()


if __name__ == "__main__":
    main()