| 183 | ==== Functionality ==== |
| 184 | |
| 185 | Now that we have configuration covered, it's time to start expanding our plugin so that it does something useful. In this tutorial, we will have our plugin keep track of our overall downloaded and uploaded byte totals and make this information available to the UI portions of the plugin. We will also be using the configuration system we just examined to save the totals during restarts and shutdowns of the daemon so that our values will remain persistent between sessions. |
| 186 | |
| 187 | First off, since we're going to be storing values we want to be persistent, let's add these to our config dictionary. |
| 188 | |
| 189 | {{{ |
| 190 | #!python |
| 191 | |
| 192 | DEFAULT_PREFS = { |
| 193 | "total_download": 0, |
| 194 | "total_upload": 0 |
| 195 | } |
| 196 | |
| 197 | }}} |
| 198 | |
| 199 | Great! Now we have a place to store our data that will be persistent between sessions. |
| 200 | |
| 201 | Let's modify our `enable()` method to initialize some object members to the values contained in the config file. |
| 202 | |
| 203 | {{{ |
| 204 | #!python |
| 205 | |
| 206 | def enable(self): |
| 207 | self.config = deluge.configmanager.ConfigManager("myplugin.conf", DEFAULT_PREFS) |
| 208 | self.total_download = self.config["total_download"] |
| 209 | self.total_upload = self.config["total_upload"] |
| 210 | }}} |
| 211 | |
| 212 | Now, every time the plugin is enabled, it will initialize these member variables to what was saved in the config file. This means that we need to make sure we're saving this data when then plugin is disabled or shutdown! |
| 213 | |
| 214 | {{{ |
| 215 | #!python |
| 216 | |
| 217 | def disable(self): |
| 218 | self.config["total_upload"] = self.total_upload |
| 219 | self.config["total_download"] = self.total_download |
| 220 | |
| 221 | }}} |
| 222 | |
| 223 | Ok, now our stats should be persistent and we can now move on to updating our `total_upload` and `total_download` member variables based on the session status from the Core. |
| 224 | |
| 225 | We'll want to poll the core every few seconds to get the latest byte count. What we'll do is setup a timer function to get the latest data and update our member variables. |
| 226 | |
| 227 | Let's start by creating the method that will be called by the timer. |
| 228 | |
| 229 | {{{ |
| 230 | #!python |
| 231 | |
| 232 | def update_stats(self): |
| 233 | session_status = component.get("Core").get_session_status(["total_download", "total_upload"]) |
| 234 | self.total_upload = self.config["total_upload"] + session_status["total_upload"] |
| 235 | self.total_download = self.config["total_download"] + session_status["total_download"] |
| 236 | |
| 237 | }}} |
| 238 | |
| 239 | The first thing you'll notice about this method is that it's using the `component` module to access the Core object. You can view the documentation for the Core object here: http://deluge-torrent.org/docs/current/modules/core/core.html |
| 240 | |
| 241 | You'll see that we're updating our member variables `total_upload` and `total_download` with the values in our config dictionary plus the session byte count. This will give us a total download/upload count over all our sessions. |
| 242 | |
| 243 | At this point the function isn't doing anything because it isn't called anywhere! So it's time to add our LoopingCall to take care of this for us. |
| 244 | |
| 245 | We need to import the LoopingCall class first. Since we use Twisted for our mainloop, we import this class from there. You will want to put this import with the rest at the top of the file. |
| 246 | |
| 247 | {{{ |
| 248 | #!python |
| 249 | |
| 250 | from twisted.internet.task import LoopingCall |
| 251 | |
| 252 | }}} |
| 253 | |
| 254 | Now, we want to setup and start the LoopingCall when the plugin is enabled, so lets modify `enable()` again. |
| 255 | |
| 256 | {{{ |
| 257 | #!python |
| 258 | |
| 259 | def enable(self): |
| 260 | self.config = deluge.configmanager.ConfigManager("myplugin.conf", DEFAULT_PREFS) |
| 261 | self.total_download = self.config["total_download"] |
| 262 | self.total_upload = self.config["total_upload"] |
| 263 | |
| 264 | self.update_status_timer = LoopingCall(self.update_stats) |
| 265 | self.update_status_timer.start(2) |
| 266 | |
| 267 | }}} |
| 268 | |
| 269 | The syntax for the LoopingCall is simple, it just takes function you wish to call in the timer. Next, we simply `start()` the timer with a value in seconds that determines it's frequency, in this case the function will be called every 2 seconds. |
| 270 | |