/
convenience.py
154 lines (120 loc) · 4.73 KB
/
convenience.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------------------------------------------------
# INFO:
# -----------------------------------------------------------------------------------------------------------------------
"""
Author: Evan Hubinger
License: Apache 2.0
Description: Convenience functions for using Coconut as a module.
"""
# -----------------------------------------------------------------------------------------------------------------------
# IMPORTS:
# -----------------------------------------------------------------------------------------------------------------------
from __future__ import print_function, absolute_import, unicode_literals, division
from coconut.root import * # NOQA
import sys
import os.path
from coconut.exceptions import CoconutException
from coconut.command import Command
from coconut.constants import (
version_tag,
version_long,
code_exts,
coconut_import_hook_args,
)
# -----------------------------------------------------------------------------------------------------------------------
# COMMAND:
# -----------------------------------------------------------------------------------------------------------------------
CLI = Command()
def cmd(args, interact=False):
"""Process command-line arguments."""
if isinstance(args, (str, bytes)):
args = args.split()
return CLI.cmd(args=args, interact=interact)
VERSIONS = {
"num": VERSION,
"name": VERSION_NAME,
"spec": VERSION_STR,
"tag": version_tag,
"-v": version_long,
}
def version(which="num"):
"""Get the Coconut version."""
if which in VERSIONS:
return VERSIONS[which]
else:
raise CoconutException(
"invalid version type " + ascii(which),
extra="valid versions are " + ", ".join(VERSIONS),
)
# -----------------------------------------------------------------------------------------------------------------------
# COMPILER:
# -----------------------------------------------------------------------------------------------------------------------
setup = CLI.setup
PARSERS = {
"sys": lambda comp: comp.parse_sys,
"exec": lambda comp: comp.parse_exec,
"file": lambda comp: comp.parse_file,
"package": lambda comp: comp.parse_package,
"block": lambda comp: comp.parse_block,
"single": lambda comp: comp.parse_single,
"eval": lambda comp: comp.parse_eval,
"any": lambda comp: comp.parse_any,
}
def parse(code="", mode="sys"):
"""Compile Coconut code."""
if CLI.comp is None:
setup()
if mode in PARSERS:
return PARSERS[mode](CLI.comp)(code)
else:
raise CoconutException(
"invalid parse mode " + ascii(mode),
extra="valid modes are " + ", ".join(PARSERS),
)
# -----------------------------------------------------------------------------------------------------------------------
# IMPORTER:
# -----------------------------------------------------------------------------------------------------------------------
class CoconutImporter(object):
"""Finder and loader for compiling Coconut files at import time."""
ext = code_exts[0]
@staticmethod
def run_compiler(path):
"""Run the Coconut compiler on the given path."""
cmd([path] + list(coconut_import_hook_args))
def find_module(self, fullname, path=None):
"""Searches for a Coconut file of the given name and compiles it."""
basepaths = [""] + list(sys.path)
if fullname.startswith("."):
if path is None:
# we can't do a relative import if there's no package path
return
fullname = fullname[1:]
basepaths.insert(0, path)
fullpath = os.path.join(*fullname.split("."))
for head in basepaths:
path = os.path.join(head, fullpath)
filepath = path + self.ext
dirpath = os.path.join(path, "__init__" + self.ext)
if os.path.exists(filepath):
self.run_compiler(filepath)
# Coconut file was found and compiled, now let Python import it
return
if os.path.exists(dirpath):
self.run_compiler(path)
# Coconut package was found and compiled, now let Python import it
return
return
coconut_importer = CoconutImporter()
def auto_compilation(on=True):
"""Turn automatic compilation of Coconut files on or off."""
if on:
if coconut_importer not in sys.meta_path:
sys.meta_path.insert(0, coconut_importer)
else:
try:
sys.meta_path.remove(coconut_importer)
except ValueError:
pass
auto_compilation()