Add a config file to the plugins by default

This commit is contained in:
mathieui 2011-09-25 02:39:00 +02:00
parent cac130e754
commit 1a6d903e34
2 changed files with 38 additions and 2 deletions

View file

@ -1,3 +1,26 @@
import os
from configparser import ConfigParser
class PluginConfig(ConfigParser):
def __init__(self, filename):
ConfigParser.__init__(self)
self.__config_file__ = filename
self.read()
def read(self):
"""Read the config file"""
ConfigParser.read(self, self.__config_file__)
def write(self):
"""Write the config to the disk"""
try:
fp = open(self.__config_file__, 'w')
ConfigParser.write(self, fp)
fp.close()
return True
except IOError:
return False
class BasePlugin(object):
"""
Class that all plugins derive from. Any methods beginning with command_
@ -5,9 +28,11 @@ class BasePlugin(object):
event handlers
"""
def __init__(self, plugin_manager, core):
def __init__(self, plugin_manager, core, plugins_conf_dir):
self.core = core
self.plugin_manager = plugin_manager
conf = os.path.join(plugins_conf_dir, self.__module__+'.cfg')
self.config = PluginConfig(conf)
self.init()
def init(self):
@ -17,6 +42,7 @@ class BasePlugin(object):
pass
def unload(self):
self.cleanup()
def add_command(self, name, handler, help, completion=None):

View file

@ -9,11 +9,21 @@ plugins_dir = plugins_dir or\
os.path.join(os.environ.get('XDG_DATA_HOME') or\
os.path.join(os.environ.get('HOME'), '.local', 'share'),
'poezio', 'plugins')
plugins_conf_dir = os.path.join(os.environ.get('XDG_CONFIG_HOME'), 'poezio',\
'plugins')
try:
os.makedirs(plugins_dir)
except OSError:
pass
try:
os.makedirs(plugins_conf_dir)
except OSError:
pass
sys.path.append(plugins_dir)
class PluginManager(object):
@ -49,7 +59,7 @@ class PluginManager(object):
self.modules[name] = module
self.commands[name] = {}
self.event_handlers[name] = []
self.plugins[name] = module.Plugin(self, self.core)
self.plugins[name] = module.Plugin(self, self.core, plugins_conf_dir)
def unload(self, name):
if name in self.plugins: