You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
4.1 KiB

14 years ago
from couchpotato.core.event import fireEvent
from couchpotato.core.logger import CPLog
import glob
import os
import traceback
log = CPLog(__name__)
class Loader(object):
plugins = {}
providers = {}
modules = {}
def preload(self, root = ''):
core = os.path.join(root, 'couchpotato', 'core')
self.paths = {
'core': (0, 'couchpotato.core._base', os.path.join(core, '_base')),
'plugin': (1, 'couchpotato.core.plugins', os.path.join(core, 'plugins')),
'notifications': (20, 'couchpotato.core.notifications', os.path.join(core, 'notifications')),
'downloaders': (20, 'couchpotato.core.downloaders', os.path.join(core, 'downloaders')),
}
# Add providers to loader
provider_dir = os.path.join(root, 'couchpotato', 'core', 'providers')
for provider in os.listdir(provider_dir):
path = os.path.join(provider_dir, provider)
if os.path.isdir(path):
self.paths[provider + '_provider'] = (25, 'couchpotato.core.providers.' + provider, path)
13 years ago
for plugin_type, plugin_tuple in self.paths.iteritems():
priority, module, dir_name = plugin_tuple
self.addFromDir(plugin_type, priority, module, dir_name)
def run(self):
did_save = 0
for priority in self.modules:
for module_name, plugin in sorted(self.modules[priority].iteritems()):
# Load module
try:
m = getattr(self.loadModule(module_name), plugin.get('name'))
log.info("Loading %s: %s" % (plugin['type'], plugin['name']))
# Save default settings for plugin/provider
did_save += self.loadSettings(m, module_name, save = False)
self.loadPlugins(m, plugin.get('name'))
except ImportError:
log.debug('Import error, remove the empty folder: %s' % plugin.get('module'))
except:
log.error('Can\'t import %s: %s' % (module_name, traceback.format_exc()))
if did_save:
fireEvent('settings.save')
13 years ago
def addFromDir(self, plugin_type, priority, module, dir_name):
13 years ago
for cur_file in glob.glob(os.path.join(dir_name, '*')):
name = os.path.basename(cur_file)
13 years ago
if os.path.isdir(os.path.join(dir_name, name)):
module_name = '%s.%s' % (module, name)
13 years ago
self.addModule(priority, plugin_type, module_name, name)
def loadSettings(self, module, name, save = True):
try:
for section in module.config:
14 years ago
fireEvent('settings.options', section['name'], section)
options = {}
for group in section['groups']:
for option in group['options']:
options[option['name']] = option
14 years ago
fireEvent('settings.register', section_name = section['name'], options = options, save = save)
return True
13 years ago
except:
log.debug("Failed loading settings for '%s': %s" % (name, traceback.format_exc()))
return False
def loadPlugins(self, module, name):
try:
klass = module.start()
klass.registerPlugin()
if klass and getattr(klass, 'auto_register_static'):
klass.registerStatic(module.__file__)
return True
except Exception, e:
log.error("Failed loading plugin '%s': %s" % (module.__file__, traceback.format_exc()))
return False
13 years ago
def addModule(self, priority, plugin_type, module, name):
if not self.modules.get(priority):
self.modules[priority] = {}
self.modules[priority][module] = {
'priority': priority,
'module': module,
13 years ago
'type': plugin_type,
'name': name,
}
def loadModule(self, name):
try:
m = __import__(name)
splitted = name.split('.')
for sub in splitted[1:-1]:
m = getattr(m, sub)
return m
except:
raise