plugins/base.py
author Nicolas Chauvat <nicolas.chauvat@logilab.fr>
Mon, 23 Jun 2008 13:42:22 +0200
changeset 6 6aad8b499e86
parent 0 7710b138d4eb
permissions -rw-r--r--
fix reST markup

"""Basic plugin for python modules
"""

__revision__ = '$Id: base.py,v 1.5 2006-03-26 20:19:59 nico Exp $'

import os
import mailcap
CAPS = mailcap.getcaps()


from logilab.astng.builder import ASTNGBuilder

from oobrother.plugins import AbstractPlugIn

DEFAULT_EDITOR = os.environ.get('EDITOR', 'emacsclient --no-wait')

class TextPlugin(AbstractPlugIn):
    """a plugin for text file, propo"""
    name = 'text'
    mimetypes = 'text/*',

    options = (
        ('editor_command', {'type': 'string',
                            'default': DEFAULT_EDITOR }),
        )
    
    def __init__(self):
        AbstractPlugIn.__init__(self, 'base_text.ini')
        self.text_editor = os.environ.get('EDITOR', 'emacsclient --no-wait') 
        
    def get_actions(self, thefile):
        """return actions provided by this plugin for the give file"""
        return [('view', self.cb_view_file),
                ('edit', self.cb_edit_file),
                ]

    def cb_view_file(self, menuitem, thefile):
        """call back for the 'view file' action : display the file
        according to the mailcap configuration
        """
        commandline = mailcap.findmatch(CAPS, thefile.mimetype, thefile.abspath)[0]
        os.system(commandline)
        
    def cb_edit_file(self, menuitem, thefile):
        """call back for the 'view file' action : display the file
        according to the mailcap configuration
        """
        os.system('%s %s' % (self.text_editor, thefile.abspath))
        

def register(registry):
    """register plugins from this module to the plugins registry"""
    registry.register(TextPlugin())