Skip to content

Commit a526e89

Browse files
author
Christopher Doris
committed
experimental julia file importer [skip ci]
1 parent 3147fb8 commit a526e89

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

pysrc/juliacall/importer.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import os
2+
import sys
3+
4+
from . import newmodule, Base
5+
from importlib.machinery import ModuleSpec
6+
7+
class Finder:
8+
def find_spec(self, fullname, path, target=None):
9+
if path is None:
10+
path = sys.path
11+
if '.' in fullname:
12+
return
13+
name = fullname
14+
else:
15+
name = fullname.split('.')[-1]
16+
for root in path:
17+
origin = os.path.join(root, name + '.py.jl')
18+
if os.path.isfile(origin):
19+
origin = os.path.realpath(origin)
20+
return ModuleSpec(fullname, Loader(), origin=origin)
21+
22+
class Loader:
23+
def create_module(self, spec):
24+
return None
25+
26+
def exec_module(self, module):
27+
spec = module.__spec__
28+
name = spec.name
29+
m = module.__jl_module__ = newmodule(name)
30+
with open(spec.origin) as fp:
31+
src = fp.read()
32+
m.seval("begin;]\n" + src + "\nend")
33+
ks = [str(k) for k in Base.names(m)]
34+
ks = [k for k in ks if k != name]
35+
if not ks:
36+
ks = [str(k) for k in Base.names(m, all=True)]
37+
ks = [k for k in ks if not (k == name or k.startswith('_') or '#' in k)]
38+
for k in ks:
39+
module.__dict__[k] = getattr(m, k)
40+
41+
sys.meta_path.append(Finder())

0 commit comments

Comments
 (0)