# Path: nolang/module.py
# def create_module(name, functions):
#     name2index = {}
#     for i, func in enumerate(functions):
#         name2index[func.name] = i
#     return W_Module(name, name2index, functions[:])
# 
# Path: nolang/parser.py
# class ParsingState(object):
#     def __init__(self, filename, input):
#         self.input = input
#         self.filename = filename
# 
# Path: nolang/compiler.py
# def compile_module(space, filename, dotted_name, source, ast, importer):
#     name_mapping = _gather_names(ast, space.builtins_w)
#     if space.builtins_w is not None:
#         globals_w = space.builtins_w[:]
#     else:
#         globals_w = []
#     w_mod = W_Module(filename, name_mapping, globals_w)
#     for item in ast.get_element_list():
#         item.add_global_symbols(space, globals_w, source, w_mod)
#     importer.register_module(space, dotted_name, w_mod)
#     importer.add_missing_imports(space, ast, w_mod, globals_w)
#     return w_mod
# 
# Path: nolang/module.py
# class W_Module(W_Root):
#     def __init__(self, name, name2index, functions):
#         self.name = name
#         self.name2index = name2index
#         self.functions = functions
# 
#     def setup(self, space):
#         for item in self.functions:
#             item.setup(space)
# 
#     def add_element(self, element):
#         no = len(self.functions)
#         self.functions.append(element)
#         self.name2index[element.name] = no
# 
#     def getattr(self, space, name):
#         try:
#             return self.functions[self.name2index[name]]
#         except KeyError:
#             return W_Root.getattr(self, space, name)  # let it raise
# 
#     def __repr__(self):
#         return '<Module %s %d functions>' % (self.name, len(self.functions))


import os
from nolang.module import create_module
from nolang.parser import ParsingState
from nolang.compiler import compile_module
from nolang.module import W_Module

class Importer(object):
    def __init__(self, space, basepath=None, parser=None, lexer=None):
        # XXX basepath is a hack
        self.basepath = basepath
        self.selfmod = create_module('self', [])
        self.parser = parser
        self.lexer = lexer
        self.cache = {'self': self.selfmod}
        self.register_core_module('core', space.coremod)

    def register_core_module(self, name, w_mod):
        self.cache[name] = w_mod
        for w_element in w_mod.functions:
            if isinstance(w_element, W_Module):
                self.register_core_module(name + '.' + w_element.name,
                                          w_element)

    def add_missing_imports(self, space, ast, w_mod, globals_w):
        for item in ast.get_element_list():
            item.add_missing_imports(space, w_mod, globals_w, self)

    def import_module(self, space, path):
        # XXX error handling
        pth = os.path.join(self.basepath, os.path.sep.join(path)) + ".q"
        try:
            source = open(pth).read()
        except (IOError, OSError):
            raise ImportError("foo")
        ast = self.parser.parse(self.lexer.lex(pth, source),

# Path: nolang/module.py
# def create_module(name, functions):
#     name2index = {}
#     for i, func in enumerate(functions):
#         name2index[func.name] = i
#     return W_Module(name, name2index, functions[:])
# 
# Path: nolang/parser.py
# class ParsingState(object):
#     def __init__(self, filename, input):
#         self.input = input
#         self.filename = filename
# 
# Path: nolang/compiler.py
# def compile_module(space, filename, dotted_name, source, ast, importer):
#     name_mapping = _gather_names(ast, space.builtins_w)
#     if space.builtins_w is not None:
#         globals_w = space.builtins_w[:]
#     else:
#         globals_w = []
#     w_mod = W_Module(filename, name_mapping, globals_w)
#     for item in ast.get_element_list():
#         item.add_global_symbols(space, globals_w, source, w_mod)
#     importer.register_module(space, dotted_name, w_mod)
#     importer.add_missing_imports(space, ast, w_mod, globals_w)
#     return w_mod
# 
# Path: nolang/module.py
# class W_Module(W_Root):
#     def __init__(self, name, name2index, functions):
#         self.name = name
#         self.name2index = name2index
#         self.functions = functions
# 
#     def setup(self, space):
#         for item in self.functions:
#             item.setup(space)
# 
#     def add_element(self, element):
#         no = len(self.functions)
#         self.functions.append(element)
#         self.name2index[element.name] = no
# 
#     def getattr(self, space, name):
#         try:
#             return self.functions[self.name2index[name]]
#         except KeyError:
#             return W_Root.getattr(self, space, name)  # let it raise
# 
#     def __repr__(self):
#         return '<Module %s %d functions>' % (self.name, len(self.functions))


# Path: nolang/importer.py
import os
from nolang.module import create_module
from nolang.parser import ParsingState
from nolang.compiler import compile_module
from nolang.module import W_Module


class Importer(object):
    def __init__(self, space, basepath=None, parser=None, lexer=None):
        # XXX basepath is a hack
        self.basepath = basepath
        self.selfmod = create_module('self', [])
        self.parser = parser
        self.lexer = lexer
        self.cache = {'self': self.selfmod}
        self.register_core_module('core', space.coremod)

    def register_core_module(self, name, w_mod):
        self.cache[name] = w_mod
        for w_element in w_mod.functions:
            if isinstance(w_element, W_Module):
                self.register_core_module(name + '.' + w_element.name,
                                          w_element)

    def add_missing_imports(self, space, ast, w_mod, globals_w):
        for item in ast.get_element_list():
            item.add_missing_imports(space, w_mod, globals_w, self)

    def import_module(self, space, path):
        # XXX error handling
        pth = os.path.join(self.basepath, os.path.sep.join(path)) + ".q"
        try:
            source = open(pth).read()
        except (IOError, OSError):
            raise ImportError("foo")
        ast = self.parser.parse(self.lexer.lex(pth, source),

                                ParsingState(pth, source))