summaryrefslogtreecommitdiffstats
path: root/silpa/common/modulemanager.py
blob: 028c4aeeb00d5baf31944626c20a1e53e3343b6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from utils import *
class ModuleManager:
	def import_module(self,name):
		parts = name.split(".")
		try:
			obj= sys.modules[name]
		except KeyError:
			obj  = __import__(".".join(parts[:-1]))
			print "Loading " , obj
		if(len(parts)>1):	
			for part in parts[1:]:
				obj = getattr(obj,part)
		return obj

	def getModuleInstance(self,action):
		module_name = self.find_module(action)
		if(module_name):
			return self.import_module(module_name).getInstance()
		else:
			return None	
	def find_module(self,action):
		try:
			return getModulesList()[action]
		except:	
			return None
if __name__ == '__main__':
	mm=ModuleManager()
	print mm.getModuleInstance("lemmatize")
	print mm.import_module("modules.lemmatizer").getInstance()