From fc47831ed76048d381f17bc92c1894919e3e66d1 Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Sun, 16 Dec 2012 18:15:18 -0500 Subject: [PATCH] um, awesome? --- hy/lang/importer.py | 38 +++++++++++++++++++++++++++++++++++++- test.hy => hylander.hy | 0 test.py | 6 ++---- 3 files changed, 39 insertions(+), 5 deletions(-) rename test.hy => hylander.hy (100%) diff --git a/hy/lang/importer.py b/hy/lang/importer.py index bd0642e..ff17fab 100644 --- a/hy/lang/importer.py +++ b/hy/lang/importer.py @@ -1,11 +1,47 @@ from hy.compiler.modfaker import forge_module from hy.lex.tokenize import tokenize +import sys +import imp +import os -def _hy_import_file(name, fd): +def _hy_import_file(fd): + name = 'hython file' + m = forge_module( name, fd, tokenize(open(fd, 'r').read()) ) return m + + + +class MetaImporter(object): + def find_module(self, fullname, path=None): + lastname = fullname.rsplit(".", 1)[-1] + for d in path or sys.path: + hy = os.path.join(d, lastname + ".hy") + pkg = os.path.join(d, lastname, "__init__.py") + pkgc = getattr(imp, "cache_from_source", + lambda path: path + "c")(pkg) + if (os.path.exists(hy) and + not (os.path.exists(pkg) or os.path.exists(pkgc))): + self.path = hy + return self + return None + + def load_module(self, name): + if name not in sys.modules: + sys.modules[name] = None + sys.modules[name] = _hy_import_file(self.path) + sys.modules[name].__loader__ = self + + return sys.modules[name] + + def is_package(self, name): + return False + + +sys.meta_path.append(MetaImporter()) +sys.path.insert(0, "") diff --git a/test.hy b/hylander.hy similarity index 100% rename from test.hy rename to hylander.hy diff --git a/test.py b/test.py index c77f926..dffaf52 100644 --- a/test.py +++ b/test.py @@ -1,4 +1,2 @@ -from hy.lang.importer import _hy_import_file -import sys - -mod = _hy_import_file('test', sys.argv[1]) +import hy.lang.importer +import hylander