how to apply metaclass in module level in python 2.7?

128 views Asked by At

I want to register defined modules with metaclass like this :

lib/importer.py

import modulelist.moduleimport
module_list = os.listdir('modulelist/')
for i in module_list
  imp.load_source(modulename, path)

for module in moduleimport:
  print module

and moduleimport(defined in lib/modulelist/moduleimport.py) is,

class moduleimport():
  __metaclass__ = moduleMetaClass

class moduleMetaClass(type):
  def __init__(cls, name, bases, nmspc):
    #register cls to global variable.

and other some stuff class.

if importer.py is in modulelist folder, it works fine.

but not working if importer.py is in parent directory.

if I print in moduleMetaClass.__init__, it prints all inherited class.

but it has just baseclass(moduleimport class) in for loop.

what I want to do is,

hello.py - import lib.importer.py, call importer.getModulelist()

and getMoulelist() returns classes that inherited from moduleimport class in modulelist/*.py

lib/importer.py - import lib/modulelist/*.py

what I want is find inherited class with metaclass.

(register in class.__init__ is almost impossible. it can changed dynamically)

0

There are 0 answers