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      # Events provided by plugin
     55      # List of (event_name, event_desc) tuples
     56      EVENTS = None
     57
     58 -    def __init__(self, xyz, *args, **kwargs):
     59          self.xyz = xyz
     60
     61          # Integer module version (for possible comparison)
     62          self.intversion = 0
     63
     64          # Public methods dictionary
     65          # Accessed as plugin attribute (plugin.method())
     66          self.public = {}
     67
     68          # Public data dictionary
     69          # Accessed as plugin items (plugin["data"])
     70          self.public_data = {}
     71
     72          self.ns = Namespace(u":".join(("", self.NAMESPACE, self.NAME)))
     73
     74          try:
     75              self.conf = self.xyz.conf[u"plugins"][self.ns.pfull]
     76          except KeyError:
     77              self.conf = {}
     78
     79      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     80
     81 -    def __getattr__(self, method):
     82          """
     83          Provide transparent access to public methods
     84          """
     85
     86          try:
     87              return self.public[method]
     88          except KeyError:
     89              raise AttributeError(_(u"%s is not a public method" % method))
     90
     91      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     92
     93 -    def __getitem__(self, obj):
     94          """
     95          Provide transparent access to public data
     96          """
     97
     98          try:
     99              return self.public_data[obj]
    100          except KeyError:
    101              raise AttributeError(_(u"%s is not a public data object " % obj))
    102
    103      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    104
    105 -    def prepare(self, *args, **kwargs):
    106          """
    107          Plugin constructor
    108          """
    109
    110          pass
    111
    112      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    113
    114 -    def finalize(self, *args, **kwargs):
    115          """
    116          Plugin destructor
    117          """
    118
    119          pass
    120
    121      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    122
    123 -    def export(self, func):
    124          """
    125          Export method
    126          """
    127
    128          _name = func.im_func.__name__
    129
    130          func.im_func.ns = u"%s:%s" % (self.ns.full, _name)
    131
    132          self.public[_name] = func
    133
    134      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    135
    136 -    def export_data(self, name, data):
    137          """
    138          Export data
    139          """
    140
    141          self.public_data[name] = data
    142
    143      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    144
    145 -    def fire_event(self, event, *args):
    146          """
    147          Fire event
    148          """
    149
    150          self.xyz.hm.dispatch(self.event_name(event), *args)
    151
    152      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    153
    154 -    def event_name(self, short):
    155          """
    156          Return full event name
    157          """
    158
    159          return "event%s:%s" % (self.ns.pfull, short)
    160