Package libxyz :: Package core :: Package plugins :: Module base
[hide private]
[frames] | no frames]

Source Code for Module libxyz.core.plugins.base

  1  #-*- coding: utf8 -* 
  2  # 
  3  # Max E. Kuznecov ~syhpoon <syhpoon@syhpoon.name> 2008 
  4  # 
  5  # This file is part of XYZCommander. 
  6  # XYZCommander is free software: you can redistribute it and/or modify 
  7  # it under the terms of the GNU Lesser Public License as published by 
  8  # the Free Software Foundation, either version 3 of the License, or 
  9  # (at your option) any later version. 
 10  # XYZCommander is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 13  # GNU Lesser Public License for more details. 
 14  # You should have received a copy of the GNU Lesser Public License 
 15  # along with XYZCommander. If not, see <http://www.gnu.org/licenses/>. 
 16   
 17  from libxyz.exceptions import PluginError 
 18  from libxyz.core.plugins import Namespace 
 19   
20 -class BasePlugin(object):
21 """ 22 Parent class for all xyz-plugins 23 """ 24 25 # NAME: Plugin name 26 NAME = None 27 28 # AUTHOR: Author name 29 AUTHOR = None 30 31 # VERSION: Plugin version 32 VERSION = None 33 34 # Brief one line description 35 BRIEF_DESCRIPTION = None 36 37 # Full plugin description 38 FULL_DESCRIPTION = None 39 40 # NAMESPACE: Plugin namespace. For detailed information about 41 # namespaces see Plugins chapter of XYZCommander user manual. 42 NAMESPACE = None 43 44 # MIN_XYZ_VERSION: Minimal XYZCommander version 45 # the plugin is compatible with 46 MIN_XYZ_VERSION = None 47 48 # Plugin documentation 49 DOC = None 50 51 # Plugin home-page 52 HOMEPAGE = None 53
54 - def __init__(self, xyz, *args, **kwargs):
55 self.xyz = xyz 56 57 # Integer module version (for possible comparison) 58 self.intversion = 0 59 60 # Public methods dictionary 61 # Accessed as plugin attribute (plugin.method()) 62 self.public = {} 63 64 # Public data dictionary 65 # Accessed as plugin items (plugin["data"]) 66 self.public_data = {} 67 68 self.ns = Namespace(u":".join(("", self.NAMESPACE, self.NAME))) 69 70 try: 71 self.conf = self.xyz.conf[u"plugins"][self.ns.pfull] 72 except KeyError: 73 self.conf = {}
74 75 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 76
77 - def __getattr__(self, method):
78 """ 79 Provide transparent access to public methods 80 """ 81 82 try: 83 return self.public[method] 84 except KeyError: 85 raise AttributeError(_(u"%s is not a public method" % method))
86 87 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 88
89 - def __getitem__(self, obj):
90 """ 91 Provide transparent access to public data 92 """ 93 94 try: 95 return self.public_data[obj] 96 except KeyError: 97 raise AttributeError(_(u"%s is not a public data object " % obj))
98 99 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 100
101 - def prepare(self, *args, **kwargs):
102 """ 103 Plugin constructor 104 """ 105 106 pass
107 108 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 109
110 - def finalize(self, *args, **kwargs):
111 """ 112 Plugin destructor 113 """ 114 115 pass
116 117 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 118
119 - def export(self, func):
120 """ 121 Export method 122 """ 123 124 _name = func.im_func.__name__ 125 126 func.im_func.ns = u"%s:%s" % (self.ns.full, _name) 127 128 self.public[_name] = func
129 130 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 131
132 - def export_data(self, name, data):
133 """ 134 Export data 135 """ 136 137 self.public_data[name] = data
138 139 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 140
141 - def run_hook(self, hook, *args, **kwargs):
142 """ 143 Run hook 144 """ 145 146 self.xyz.hm.dispatch("hook%s:%s" % (self.ns.pfull, hook), 147 *args, **kwargs)
148