__init__.py
Go to the documentation of this file.00001 from silme.core import L10nObject
00002 import os
00003
00004 class Manager(object):
00005 formats = {}
00006 names = {}
00007
00008 @classmethod
00009 def register(cls, *args):
00010 """
00011 register for Manager
00012 """
00013 for fp in args:
00014 if isinstance(fp, str):
00015 if fp in cls.names:
00016 continue
00017 module = __import__(fp, globals(), locals(), [''], 1)
00018 module.register(cls)
00019 else:
00020 for ext in fp.extensions:
00021 cls.formats[ext.lower()] = fp
00022 name = fp.__module__
00023 cls.names[name[name.rfind('.')+1:]] = fp
00024
00025 @classmethod
00026 def register_all(cls):
00027 dir = os.path.dirname(__file__)
00028 list = os.listdir(dir)
00029 modules = [f for f in list if os.path.isdir(os.path.join(dir, f))]
00030 for name in modules:
00031 try:
00032 module = __import__(name, globals(), locals(), [''], 1)
00033 module.register(cls)
00034 except:
00035 pass
00036
00037 @classmethod
00038 def get(cls, name=None, path=None):
00039 if name:
00040 if name in cls.names:
00041 return cls.names[name]
00042 else:
00043 try:
00044 module = __import__(name, globals(), locals(), [''], 1)
00045 except ImportError:
00046 raise Exception ('no matching format')
00047 module.register(cls)
00048 return cls.formats[name]
00049 elif path:
00050 if path.find('.') != -1:
00051 ext = os.path.splitext(path.lower())[1][1:]
00052 else:
00053 ext = path.lower()
00054 if ext in cls.formats:
00055 return cls.formats[ext]
00056 raise Exception('no matching parser')