home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / plugin_manager / plugin_registry.pyo (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  10.1 KB  |  285 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from __future__ import with_statement
  5. import traceback
  6. import sys
  7. import logging
  8. import config
  9. import util
  10. import util.data_importer as importers
  11. from path import path
  12. log = logging.getLogger('plugin_registry')
  13. METAINFO_FILENAME = 'info.yaml'
  14. type_handlers = { }
  15.  
  16. def register_plugin_default(name, metainfo):
  17.     log.error('Not registering type %r because of unknown "type" key (%r). its metainfo is: %r', name, metainfo.get('type', None), metainfo)
  18.  
  19.  
  20. def register_type_handler(typename, handler):
  21.     old_handler = type_handlers.get(typename, None)
  22.     if old_handler is not None:
  23.         log.warning('Overwriting handler for type %r: was %r, now %r', typename, old_handler, handler)
  24.     
  25.     type_handlers[typename] = handler
  26.  
  27.  
  28. def get_type_handler(typename):
  29.     
  30.     try:
  31.         return type_handlers[typename]
  32.     except KeyError:
  33.         return register_plugin_default
  34.  
  35.  
  36.  
  37. def type_handler(typename):
  38.     
  39.     def register_handler(f):
  40.         register_type_handler(typename, f)
  41.         return f
  42.  
  43.     return register_handler
  44.  
  45.  
  46. def register_type(name, metainfo):
  47.     a_type = metainfo.get('type', None)
  48.     if a_type is None:
  49.         log.error('Not registering type %r because it did not have a "type" key. its metainfo is: %r', name, metainfo)
  50.         return None
  51.     else:
  52.         platforms = metainfo.get('platforms', None)
  53.         if platforms is None or config.platformName in platforms:
  54.             PluginType = get_type_handler(a_type)
  55.             plugin = PluginType(metainfo.__file__.parent, metainfo)
  56.             plugin.init()
  57.             return plugin
  58.         
  59.  
  60.  
  61. class PluginLoader(object):
  62.     
  63.     def __init__(self, dirpath, metainfo):
  64.         self.path = dirpath
  65.         self.shortname = self.path.name
  66.         self.info = metainfo
  67.  
  68.     
  69.     def init(self):
  70.         self.name = self.info.name
  71.         log.info('registered account type: %r', self.name)
  72.  
  73.     
  74.     def get_component(self, attr, yamlname = None):
  75.         attempts = [
  76.             None,
  77.             (None, ((None, None), (lambda : getattr(self, attr, None))), (lambda : getattr(self.info, attr, None))),
  78.             (lambda : if not yamlname:
  79. passgetattr(self.yaml_load(attr), '__content__', None))]
  80.         for attempt in attempts:
  81.             thing = attempt()
  82.             if thing is not None:
  83.                 break
  84.                 continue
  85.         else:
  86.             return None
  87.         self.set_component(attr, thing)
  88.         return thing
  89.  
  90.     
  91.     def set_component(self, attr, thing):
  92.         setattr(self, attr, thing)
  93.         setattr(self.info, attr, thing)
  94.  
  95.     
  96.     def yaml_load(self, yamlname):
  97.         
  98.         try:
  99.             return importers.yaml_import(yamlname, loadpath = [
  100.                 self.path])
  101.         except ImportError:
  102.             return None
  103.  
  104.  
  105.  
  106.  
  107. class ProtocolPluginLoader(PluginLoader):
  108.     
  109.     def init(self, dictnames = None):
  110.         PluginLoader.init(self)
  111.         self.init_info(dictnames)
  112.         self.init_skin(dictnames)
  113.         self.init_actions(dictnames)
  114.         self.init_notifications(dictnames)
  115.  
  116.     
  117.     def init_skin(self, dictnames = None):
  118.         self.get_component('skin')
  119.  
  120.     
  121.     def init_info(self, dictnames = None):
  122.         import common.protocolmeta as pm
  123.         plugin_info = util.Storage(self.info)
  124.         if dictnames is not None:
  125.             for dictname in dictnames:
  126.                 d = getattr(pm, dictname, None)
  127.                 if d is not None:
  128.                     d[self.shortname] = plugin_info
  129.                     continue
  130.             
  131.         
  132.         pm.protocols[self.shortname] = plugin_info
  133.  
  134.     
  135.     def init_actions(self, dictnames = None):
  136.         actions = self.get_component('actions')
  137.         if actions is None:
  138.             return None
  139.         
  140.         import common.actions as common
  141.         common.actions.add_actions(actions)
  142.  
  143.     
  144.     def init_notifications(self, dictnames = None):
  145.         nots = self.get_component('notifications')
  146.         if nots is None:
  147.             return None
  148.         
  149.         import common.notifications as n
  150.         defaults = { }
  151.         for d in nots:
  152.             for k in d:
  153.                 not_ = d[k]
  154.                 default = not_.get('default', { })
  155.                 default_reactions = [ {
  156.                     'reaction': reaction } for reaction in default.get('reaction', []) ]
  157.                 defaults[k.replace('_', '.')] = default_reactions
  158.             
  159.         
  160.         n.add_notifications(nots)
  161.         n.add_default_notifications(defaults)
  162.         self.set_component('notifications', nots)
  163.  
  164.  
  165.  
  166. class IMProtocolPluginLoader(ProtocolPluginLoader):
  167.     
  168.     def init(self):
  169.         return ProtocolPluginLoader.init(self, [
  170.             'improtocols'])
  171.  
  172.  
  173. register_type_handler('im', IMProtocolPluginLoader)
  174.  
  175. class EmailProtocolPluginLoader(ProtocolPluginLoader):
  176.     
  177.     def init(self):
  178.         return ProtocolPluginLoader.init(self, [
  179.             'emailprotocols'])
  180.  
  181.  
  182. register_type_handler('email', EmailProtocolPluginLoader)
  183.  
  184. class SocialProtocolPluginLoader(ProtocolPluginLoader):
  185.     
  186.     def init(self):
  187.         return ProtocolPluginLoader.init(self, [
  188.             'socialprotocols'])
  189.  
  190.  
  191. register_type_handler('social', SocialProtocolPluginLoader)
  192.  
  193. class MetaProtocolPluginLoader(ProtocolPluginLoader):
  194.     
  195.     def init(self):
  196.         return ProtocolPluginLoader.init(self)
  197.  
  198.  
  199. register_type_handler('meta', MetaProtocolPluginLoader)
  200.  
  201. class StatusPluginLoader(PluginLoader):
  202.     
  203.     def init(self):
  204.         PluginLoader.init(self)
  205.         log.info('Got new status type: %r', self.name)
  206.         import_path = 'plugins.%s' % self.shortname
  207.         log.info('Importing %r as %r', self.name, import_path)
  208.         
  209.         try:
  210.             module = util.import_module(import_path)
  211.         except ImportError:
  212.             return None
  213.  
  214.         init = getattr(module, 'initialize', None)
  215.         if init is not None:
  216.             init()
  217.         
  218.  
  219.  
  220. register_type_handler('status', StatusPluginLoader)
  221. pkg_dirs = set()
  222.  
  223. def scan(dirname):
  224.     root = path(dirname)
  225.     for pkg_dir in root.dirs() + root.files('*.zip') + root.files('*.egg'):
  226.         name = pkg_dir.namebase
  227.         if exclude_dir(name):
  228.             continue
  229.         
  230.         
  231.         try:
  232.             plugin = _load_plugin_info_from_item(pkg_dir)
  233.             if plugin is None:
  234.                 log.error('No protocol info found in %r', pkg_dir)
  235.             else:
  236.                 pkg_dirs.add(pkg_dir.abspath())
  237.                 register_type(name, plugin)
  238.         continue
  239.         except Exception:
  240.             traceback.print_exc()
  241.             continue
  242.         
  243.  
  244.     
  245.  
  246.  
  247. def exclude_dir(dirname):
  248.     return dirname.startswith('.')
  249.  
  250.  
  251. def _load_plugin_info_from_item(pkgdir):
  252.     
  253.     try:
  254.         return importers.yaml_import('info', loadpath = [
  255.             pkgdir])
  256.     except ImportError:
  257.         return None
  258.  
  259.  
  260.  
  261. def plugins_skintrees():
  262.     trees = []
  263.     import common.protocolmeta as pm
  264.     for name, infodict in pm.protocols.iteritems():
  265.         if 'skin' in infodict:
  266.             trees.append(infodict['skin'])
  267.             continue
  268.     
  269.     return trees
  270.  
  271.  
  272. def plugins_skinpaths():
  273.     return list(pkg_dirs)
  274.  
  275. from peak.util.plugins import Hook
  276. Hook('digsby.skin.load.trees', 'plugins_skin').register(plugins_skintrees)
  277. Hook('digsby.skin.load.skinpaths', 'plugins_skin').register(plugins_skinpaths)
  278. if __name__ == '__main__':
  279.     import wx
  280.     a = wx.App()
  281.     logging.basicConfig()
  282.     log.setLevel(1)
  283.     scan('c:\\workspace\\digsby\\src\\plugins')
  284.  
  285.