Esempio n. 1
0
 def accept_module(self, fpth_mod):
     if not super(LangletModuleFilter, self).accept_module(fpth_mod):
         if self.dbg:
             dbg_import("module not covered: "+fpth_mod)
         return
     if self.is_mainmodule(fpth_mod):
         m = self._pattern_default.match(fpth_mod.basename())
         if m:
             self._default_groups.append(m.group(2).lower())
         self.langlet.transformer.set_module(self.langlet.importer.module_descr)
         return self
     if fpth_mod.basename() in self.module_blacklist:
         return
     if self.dbg:
         dbg_import("module_path: %s\n"%fpth_mod)
     if not self._deactivate_default:
         m = self._pattern_default.match(fpth_mod.basename())
         if m:
             md = LangletModuleDescriptor()
             md.fpth_mod_full = fpth_mod
             self._default_groups.append(m.group(2).lower())
             self.langlet.transformer.set_module(md)
             return self
         else:
             module_name = fpth_mod.splitext()[0].basename()
             if module_name in self._default_groups:
                 self._default_groups.remove(module_name)
                 md = LangletModuleDescriptor()
                 md.fpth_mod_full = fpth_mod
                 self.langlet.transformer.set_module(md)
                 return self
Esempio n. 2
0
 def register_importer(self):
     self.prepare()
     if self.dbg:
         dbg_import(str(self))
     pth = str(self.fpth_langlet)
     if pth not in sys.path:
         sys.path.append(pth)
     if not self in sys.meta_path:
         sys.meta_path.insert(0,self)
Esempio n. 3
0
 def accept_module(self, fpth_mod):
     '''
     Method used to determine if a langlet accepts module for langlet-specific import.
     @param fpth_mod: a path object specifying the complete module path.
     '''
     if self.dbg:
         sys.stdout.write("accept_module: check-module-for acceptance %s\n"%fpth_mod)
     if self.pre_filter(fpth_mod):
         if self.dbg:
             dbg_import("pre-filtered o.k:`%s`"%fpth_mod)
         if self.is_mainmodule(fpth_mod):
             return True
         if self.is_langletmodule(fpth_mod):
             return True
     return False
Esempio n. 4
0
    def load_module(self, fullname):
        '''
        Framework function load_module(). See

            http://www.python.org/dev/peps/pep-0302/

        :param fullname: fully qualified name e.g. 'spam.ham'
        '''
        mod = self.fpth_mod
        # compile module if given path is source path
        if self.loader.is_source(mod):
            if self.dbg:
                dbg_import("-"*(len(mod)+20))
                dbg_import("compile source: `%s`"%mod)
                dbg_import("-"*(len(mod)+20))
            self.langlet.compile_file(mod)
            compiled_module_path = mod.stripext()+self.langlet.config.compiled_ext
        else:
            compiled_module_path = mod

        # Load compiled module. Check if module M has been passed as python <langlet>.py M.ext .
        # If yes, load M as __main__
        _dir = self.fpth_mod.dirname()
        if _dir not in sys.path:
            sys.path.append(_dir)
        if self.modulefilter.is_mainmodule(self.fpth_mod):
            if self.dbg:
                dbg_import("load main module: `%s`\n"%self.fpth_mod)
            compiled = self.langlet.compiler.load_compiled("__main__", compiled_module_path)
        else:
            if self.dbg:
                dbg_import("load compiled: `%s` at `%s`\n"%(fullname, compiled_module_path))
            compiled = self.langlet.compiler.load_compiled(fullname, compiled_module_path)
        if self.langlet.config.remove_compiled_after_exec:
            if self.dbg:
                dbg_import("load compiled: removed `%s`\n"%compiled_module_path)
            compiled_module_path.remove()
        return compiled
Esempio n. 5
0
    def find_module(self, mpth_mod, mpth_pack = None):
        '''
        Framework function find_module(). See

            http://www.python.org/dev/peps/pep-0302/
        '''
        if mpth_pack and ".egg" in mpth_pack[0]:  # TODO - enable zipimport of langlet modules
            return
        if mpth_mod.startswith("langscape.base."):
            mpth_mod = mpth_mod[15:]
            if mpth_pack:
                if mpth_pack[0].endswith("base"):
                    del mpth_pack[0]
        package = ""
        idx = mpth_mod.rfind(".")  # maybe dotted module name ?
        if idx>0:
            package, mpth_mod = mpth_mod[:idx], mpth_mod[idx+1:]
            mpth_pack = sys.modules[package].__path__

        if mpth_pack and mpth_pack[0].endswith("encodings"):
            return

        if self.dbg:
            dbg_import("input: module: `%s`"%mpth_mod)
            dbg_import("input: package:`%s`"%mpth_pack)

        moduledata  = self.loader.find_module(mpth_mod, mpth_pack)

        if isinstance(moduledata, zipimport.zipimporter):
            if self.dbg:
                dbg_import("zipimport: `%s`"%mpth_mod)
            moduledata.load_module(mpth_mod)
            return

        if not moduledata:
            if self.dbg:
                dbg_import("no-data: `%s`, `%s`, `%s`"%(mpth_mod, package, mpth_pack))
            if mpth_pack:
                raise ImportError("No module named %s found at %s."%(mpth_mod, mpth_pack))
            else:
                raise ImportError("No module named %s found."%mpth_mod)
        if self.dbg:
            dbg_import("moduledata: `%s`\n"%(moduledata[1:],))
        if not moduledata[1]:
            return

        self.fpth_mod = path(moduledata[1])
        self.mpth_mod = mpth_mod
        # sys.stdout.write("DEBUG import_path: %s, module: %s\n"%(self.mpth_mod, self.fpth_mod))
        if self.modulefilter.accept_module(self.fpth_mod):
            if self.dbg:
                dbg_import("accepted module:`%s`"%self.fpth_mod)
            return self