home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / plugin_manager / plugin_registry.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  12.7 KB  |  274 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. import traceback
  6. import logging
  7. import config
  8. import util.primitives as util
  9. import util.data_importer as importers
  10. import peak.util.plugins as Plugins
  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.     platforms = metainfo.get('platforms', None)
  52.     if platforms is None or config.platformName in platforms:
  53.         PluginType = get_type_handler(a_type)
  54.         plugin = PluginType(metainfo.__file__.parent, metainfo)
  55.         plugin.init()
  56.         return plugin
  57.  
  58.  
  59. class PluginLoader(object):
  60.     
  61.     def __init__(self, dirpath, metainfo):
  62.         self.path = dirpath
  63.         self.shortname = metainfo.get('shortname', self.path.name)
  64.         self.info = metainfo
  65.  
  66.     
  67.     def init(self):
  68.         self.name = self.info.name
  69.         self.init_skin()
  70.         self.init_notifications()
  71.         self.init_actions()
  72.         log.info('registered plugin type: %r', self.name)
  73.  
  74.     
  75.     def init_skin(self):
  76.         self.skin = self.resolve_paths(self.get_component('skin'))
  77.  
  78.     
  79.     def resolve_paths(self, d):
  80.         if not isinstance(d, dict):
  81.             return d
  82.         ret = { }
  83.         for k, v in d.items():
  84.             if isinstance(v, basestring):
  85.                 pth = self.path / v
  86.                 if pth.isfile() or pth.isdir():
  87.                     v = pth
  88.                 
  89.             elif isinstance(v, dict):
  90.                 v = self.resolve_paths(v)
  91.             
  92.             ret[k] = v
  93.         
  94.         return ret
  95.  
  96.     
  97.     def init_actions(self):
  98.         actions = self.get_component('actions')
  99.         if actions is None:
  100.             return None
  101.         import common.actions as common
  102.         common.actions.add_actions(actions)
  103.  
  104.     
  105.     def init_notifications(self):
  106.         nots = self.get_component('notifications')
  107.         if nots is None:
  108.             return None
  109.         import common.notifications as n
  110.         defaults = { }
  111.         for d in nots:
  112.             for k in d:
  113.                 not_ = d[k]
  114.                 default = not_.get('default', { })
  115.                 default_reactions = [ {
  116.                     'reaction': reaction } for reaction in default.get('reaction', []) ]
  117.                 defaults[k.replace('_', '.')] = default_reactions
  118.             
  119.         
  120.         n.add_notifications(nots)
  121.         n.add_default_notifications(defaults)
  122.         self.set_component('notifications', nots)
  123.  
  124.     
  125.     def get_component(self, attr, yamlname = None):
  126.         attempts = [
  127.             None,
  128.             (None, ((None, None), (lambda : getattr(self, attr, None))), (lambda : getattr(self.info, attr, None))),
  129.             (lambda : if not yamlname:
  130. passgetattr(self.yaml_load(attr), '__content__', None))]
  131.         for attempt in attempts:
  132.             thing = attempt()
  133.             if thing is not None:
  134.                 break
  135.                 continue
  136.         else:
  137.             return None
  138.         None.set_component(attr, thing)
  139.         return thing
  140.  
  141.     
  142.     def set_component(self, attr, thing):
  143.         setattr(self, attr, thing)
  144.         setattr(self.info, attr, thing)
  145.  
  146.     
  147.     def yaml_load(self, yamlname):
  148.         
  149.         try:
  150.             return importers.yaml_import(yamlname, loadpath = [
  151.                 self.path])
  152.         except ImportError:
  153.             return None
  154.  
  155.  
  156.     
  157.     def load_entry_points(self):
  158.         import pkg_resources
  159.         PluginDistribution = PluginDistribution
  160.         import plugin_resources
  161.         import config as digsbyconfig
  162.         platforms = self.get_component('platforms')
  163.         if platforms is None or digsbyconfig.platformName in platforms:
  164.             pkg_resources.working_set.add(PluginDistribution(location = str(self.path.parent), project_name = self.info.name, ep_yaml = self.get_component('entry_points')))
  165.         
  166.  
  167.     
  168.     def __repr__(self):
  169.         return '<%s %r>' % (type(self).__name__, self.name)
  170.  
  171.  
  172.  
  173. class ProtocolPluginLoader(PluginLoader):
  174.     
  175.     def init(self, dictnames = None):
  176.         PluginLoader.init(self)
  177.         self.init_info(dictnames)
  178.  
  179.     
  180.     def init_info(self, dictnames = None):
  181.         import common.protocolmeta as pm
  182.         plugin_info = util.primitives.mapping.Storage(self.info)
  183.         if dictnames is not None:
  184.             for dictname in dictnames:
  185.                 d = getattr(pm, dictname, None)
  186.                 if d is not None:
  187.                     d[self.shortname] = plugin_info
  188.                     continue
  189.             
  190.         
  191.         pm.protocols[self.shortname] = plugin_info
  192.  
  193.  
  194. IMProtocolPluginLoader = type_handler('im')(<NODE:12>)
  195. EmailProtocolPluginLoader = type_handler('email')(<NODE:12>)
  196. SocialProtocolPluginLoader = type_handler('social')(<NODE:12>)
  197. MetaProtocolPluginLoader = type_handler('meta')(<NODE:12>)
  198. PurePluginLoader = type_handler('pure')(<NODE:12>)
  199. MultiPluginLoader = type_handler('multi')(<NODE:12>)
  200. ServiceProviderPlugin = type_handler('service_provider')(<NODE:12>)
  201. ServiceComponentPlugin = type_handler('service_component')(<NODE:12>)
  202. pkg_dirs = set()
  203.  
  204. def scan(dirname):
  205.     root = path(dirname)
  206.     plugins = []
  207.     for pkg_dir in root.dirs() + root.files('*.zip') + root.files('*.egg'):
  208.         name = pkg_dir.namebase
  209.         if exclude_dir(name):
  210.             continue
  211.         
  212.         
  213.         try:
  214.             plugin = _load_plugin_info_from_item(pkg_dir)
  215.             if plugin is None:
  216.                 log.info('No protocol info found in %r', pkg_dir)
  217.             else:
  218.                 pkg_dirs.add(pkg_dir.abspath())
  219.                 ret = register_type(name, plugin)
  220.                 plugins.append(ret)
  221.                 if hasattr(ret, 'plugins'):
  222.                     plugins.extend(ret.plugins)
  223.             except Exception:
  224.                 plugin is None
  225.                 plugin is None
  226.                 traceback.print_exc()
  227.                 continue
  228.             
  229.         return plugins
  230.  
  231.  
  232.  
  233. def exclude_dir(dirname):
  234.     return dirname.startswith('.')
  235.  
  236.  
  237. def _load_plugin_info_from_item(pkgdir):
  238.     
  239.     try:
  240.         return importers.yaml_import('info', loadpath = [
  241.             pkgdir])
  242.     except ImportError:
  243.         return None
  244.  
  245.  
  246.  
  247. def plugins_skintrees():
  248.     import wx
  249.     plugins = wx.GetApp().plugins
  250.     trees = []
  251.     for plugin in plugins:
  252.         if not plugin.get_component('skin'):
  253.             pass
  254.         skin = None
  255.         if skin is not None:
  256.             trees.append(skin)
  257.             continue
  258.     
  259.     return trees
  260.  
  261.  
  262. def plugins_skinpaths():
  263.     return list(pkg_dirs)
  264.  
  265. Plugins.Hook('digsby.skin.load.trees', 'plugins_skin').register(plugins_skintrees)
  266. Plugins.Hook('digsby.skin.load.skinpaths', 'plugins_skin').register(plugins_skinpaths)
  267. if __name__ == '__main__':
  268.     import wx
  269.     a = wx.App()
  270.     logging.basicConfig()
  271.     log.setLevel(1)
  272.     scan('c:\\workspace\\digsby\\src\\plugins')
  273.  
  274.