importer: doc update, MetaImport refactor

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2013-04-20 22:11:53 +02:00
parent aadf47ed99
commit 07e99dbd33
2 changed files with 48 additions and 44 deletions

View File

@ -42,37 +42,41 @@ else:
long_type = long # NOQA long_type = long # NOQA
def compile_(ast, filename, mode): def ast_compile(ast, filename, mode):
"""Compile AST.
Like Python's compile, but with some special flags."""
return compile(ast, filename, mode, __future__.CO_FUTURE_DIVISION) return compile(ast, filename, mode, __future__.CO_FUTURE_DIVISION)
def import_buffer_to_hst(fd): def import_buffer_to_hst(buf):
tree = tokenize(fd.read() + "\n") """Import content from buf and return an Hy AST."""
tree = process(tree) return process(tokenize(buf + "\n"))
return tree
def import_file_to_hst(fpath): def import_file_to_hst(fpath):
return import_buffer_to_hst(open(fpath, 'r', encoding='utf-8')) """Import content from fpath and return an Hy AST."""
with open(fpath, 'r', encoding='utf-8') as f:
return import_buffer_to_hst(f.read())
def import_buffer_to_ast(buf):
""" Import content from buf and return a Python AST."""
return hy_compile(import_buffer_to_hst(buf))
def import_file_to_ast(fpath): def import_file_to_ast(fpath):
tree = import_file_to_hst(fpath) """Import content from fpath and return a Python AST."""
_ast = hy_compile(tree) return hy_compile(import_file_to_hst(fpath))
return _ast
def import_string_to_ast(buff): def import_file_to_module(module_name, fpath):
tree = import_buffer_to_hst(StringIO(buff)) """Import content from fpath and puts it into a Python module.
_ast = hy_compile(tree)
return _ast
Returns the module."""
def import_file_to_module(name, fpath):
_ast = import_file_to_ast(fpath) _ast = import_file_to_ast(fpath)
mod = imp.new_module(name) mod = imp.new_module(module_name)
mod.__file__ = fpath mod.__file__ = fpath
eval(compile_(_ast, fpath, "exec"), mod.__dict__) eval(ast_compile(_ast, fpath, "exec"), mod.__dict__)
return mod return mod
@ -84,7 +88,7 @@ def hy_eval(hytree, namespace):
foo.end_column = 0 foo.end_column = 0
hytree.replace(foo) hytree.replace(foo)
_ast = hy_compile(hytree, root=ast.Expression) _ast = hy_compile(hytree, root=ast.Expression)
return eval(compile_(_ast, "<eval>", "eval"), namespace) return eval(ast_compile(_ast, "<eval>", "eval"), namespace)
def write_hy_as_pyc(fname): def write_hy_as_pyc(fname):
@ -96,7 +100,7 @@ def write_hy_as_pyc(fname):
timestamp = long_type(st.st_mtime) timestamp = long_type(st.st_mtime)
_ast = import_file_to_ast(fname) _ast = import_file_to_ast(fname)
code = compile_(_ast, fname, "exec") code = ast_compile(_ast, fname, "exec")
cfile = "%s.pyc" % fname[:-len(".hy")] cfile = "%s.pyc" % fname[:-len(".hy")]
if sys.version_info[0] >= 3: if sys.version_info[0] >= 3:
@ -118,7 +122,10 @@ def write_hy_as_pyc(fname):
fc.write(MAGIC) fc.write(MAGIC)
class HyFinder(object): class MetaLoader(object):
def __init__(self, path):
self.path = path
def is_package(self, fullname): def is_package(self, fullname):
dirpath = "/".join(fullname.split(".")) dirpath = "/".join(fullname.split("."))
for pth in sys.path: for pth in sys.path:
@ -128,33 +135,20 @@ class HyFinder(object):
return True return True
return False return False
def find_on_path(self, fullname):
fls = ["%s/__init__.hy", "%s.hy"]
dirpath = "/".join(fullname.split("."))
for pth in sys.path:
pth = os.path.abspath(pth)
for fp in fls:
composed_path = fp % ("%s/%s" % (pth, dirpath))
if os.path.exists(composed_path):
return composed_path
class MetaLoader(HyFinder):
def load_module(self, fullname): def load_module(self, fullname):
if fullname in sys.modules: if fullname in sys.modules:
return sys.modules[fullname] return sys.modules[fullname]
pth = self.find_on_path(fullname) if not self.path:
if pth is None:
return return
sys.modules[fullname] = None sys.modules[fullname] = None
mod = import_file_to_module(fullname, pth) mod = import_file_to_module(fullname,
self.path)
ispkg = self.is_package(fullname) ispkg = self.is_package(fullname)
mod.__file__ = pth mod.__file__ = self.path
mod.__loader__ = self mod.__loader__ = self
mod.__name__ = fullname mod.__name__ = fullname
@ -168,12 +162,22 @@ class MetaLoader(HyFinder):
return mod return mod
class MetaImporter(HyFinder): class MetaImporter(object):
def find_on_path(self, fullname):
fls = ["%s/__init__.hy", "%s.hy"]
dirpath = "/".join(fullname.split("."))
for pth in sys.path:
pth = os.path.abspath(pth)
for fp in fls:
composed_path = fp % ("%s/%s" % (pth, dirpath))
if os.path.exists(composed_path):
return composed_path
def find_module(self, fullname, path=None): def find_module(self, fullname, path=None):
pth = self.find_on_path(fullname) path = self.find_on_path(fullname)
if pth is None: if path:
return return MetaLoader(path)
return MetaLoader()
sys.meta_path.append(MetaImporter()) sys.meta_path.append(MetaImporter())

View File

@ -1,4 +1,4 @@
from hy.importer import import_file_to_module, import_string_to_ast from hy.importer import import_file_to_module, import_buffer_to_ast
import ast import ast
@ -10,5 +10,5 @@ def test_basics():
def test_stringer(): def test_stringer():
"Make sure the basics of the importer work" "Make sure the basics of the importer work"
_ast = import_string_to_ast("(defn square [x] (* x x))") _ast = import_buffer_to_ast("(defn square [x] (* x x))")
assert type(_ast.body[0]) == ast.FunctionDef assert type(_ast.body[0]) == ast.FunctionDef