Changes between Version 4 and Version 5 of Development/1.2/Plugin


Ignore:
Timestamp:
08/27/2009 07:10:38 PM (12 years ago)
Author:
andar
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Development/1.2/Plugin

    v4 v5  
    6262It's as simple as that.  You can also use this method to create an egg for distribution to other Deluge users.  The egg will be located in the `dist` directory. 
    6363 
     64== Core Plugin == 
    6465 
     66=== Introduction === 
     67 
     68We'll start off by writing the Core portion of our plugin.  In this example, we'll do some pretty trivial things to demonstrate some of the ideas and components associated with the plugin development process.  We'll look at configuration, exporting rpc functions, interacting with Core components and timers. 
     69 
     70=== Hello Wo...UI! === 
     71 
     72Let's start with a look at what our `create_plugin.py` script created for us in the `core.py` file. 
     73 
     74{{{ 
     75#!python 
     76 
     77from deluge.log import LOG as log 
     78from deluge.plugins.pluginbase import CorePluginBase 
     79import deluge.component as component 
     80import deluge.configmanager 
     81from deluge.core.rpcserver import export 
     82 
     83DEFAULT_PREFS = { 
     84    "test":"NiNiNi" 
     85} 
     86 
     87class Core(CorePluginBase): 
     88    def enable(self): 
     89        self.config = deluge.configmanager.ConfigManager("myplugin.conf", DEFAULT_PREFS) 
     90 
     91    def disable(self): 
     92        pass 
     93 
     94    def update(self): 
     95        pass 
     96 
     97    @export 
     98    def set_config(self, config): 
     99        "sets the config dictionary" 
     100        for key in config.keys(): 
     101            self.config[key] = config[key] 
     102        self.config.save() 
     103 
     104    @export 
     105    def get_config(self): 
     106        "returns the config dictionary" 
     107        return self.config.config 
     108}}} 
     109 
     110We won't worry about the imports at the top, you should be able to surmise what these are for and if not, you'll see them in use later in this example.  What we will worry about, and what seems to be right in our face, is plugin configuration. 
     111 
     112{{{ 
     113#!python 
     114self.config = deluge.configmanager.ConfigManager("myplugin.conf", DEFAULT_PREFS) 
     115}}} 
     116 
     117We see here that we're using Deluge's ConfigManager to handle loading our config with some default preferences.  All you really need to know is that this `config` object will act much like a dictionary object, but has some special methods like `save()` for writing the config to disk.  You don't need to necessarily call `save()` yourself, as the config object will handle this on it's own when the config values change. 
     118 
     119You can read here: http://deluge-torrent.org/docs/current/modules/config.html for more information on the Config class. 
     120 
     121Before we go too much further, you'll notice that this config file loading is done within the `enable` method of our Core plugin class.  Whenever a plugin is enabled by the user or on start-up, this method will be called.  Likewise for the `disable` method which is called whenever the plugin is disabled.  Simple, right? 
     122 
     123{{{ 
     124#!python 
     125 
     126    @export 
     127    def set_config(self, config): 
     128        "sets the config dictionary" 
     129        for key in config.keys(): 
     130            self.config[key] = config[key] 
     131        self.config.save() 
     132 
     133    @export 
     134    def get_config(self): 
     135        "returns the config dictionary" 
     136        return self.config.config 
     137}}} 
     138 
     139Whoa, what's this?  It's our first two rpc exported methods of course!