~matthieu-rakotojaona/zim/couchdb-store

« back to all changes in this revision

Viewing changes to zim/plugins/automount.py

  • Committer: Matthieu Rakotojaona
  • Date: 2012-05-07 19:50:49 UTC
  • Revision ID: matthieu.rakotojaona@gmail.com-20120507195049-fza1a6q5oleuyhp6
working tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
# Copyright 2011 Jaap Karssenberg <jaap.karssenberg@gmail.com>
 
4
 
 
5
'''Plugin to auto-mount notebooks when needed'''
 
6
 
 
7
from zim.plugins import PluginClass
 
8
 
 
9
from zim.fs import Dir
 
10
from zim.config import config_file
 
11
from zim.applications import Application
 
12
 
 
13
 
 
14
class AutomountPlugin(PluginClass):
 
15
 
 
16
        plugin_info = {
 
17
                'name': _('Automount'), # T: plugin name
 
18
                'description': _('''\
 
19
This plugin can automatically "mount" notebooks when needed. It can
 
20
e.g. be used to connect with remote drives or unlock an encrypted drive
 
21
when zim is trying to open a specific notebook.
 
22
 
 
23
This is a core plugin shipping with zim.
 
24
'''), # T: plugin description
 
25
                'author': 'Jaap Karssenberg',
 
26
                'help': 'Plugins:Automount',
 
27
        }
 
28
 
 
29
        # this plugin is profile independent
 
30
        is_profile_independent = True
 
31
 
 
32
        def get_config(self, uri):
 
33
                '''Return the automount config for a specific notebook uri or C{None}
 
34
                @param uri: a notebook uri
 
35
                @returns: a config dict
 
36
                '''
 
37
                config = config_file('automount.conf')
 
38
                groups = [k for k in config.keys() if k.startswith('Path')]
 
39
                for group in groups:
 
40
                        path = group[4:].strip() # len('Path') = 4
 
41
                        myuri = Dir(path).uri # Allow "~/Folder" syntax
 
42
                        if uri.startswith(myuri):
 
43
                                return config[group]
 
44
                else:
 
45
                        return None
 
46
 
 
47
        def initialize_notebook(self, uri):
 
48
                # check if the notebook exists
 
49
                if not uri.startswith('file:') \
 
50
                or Dir(uri).file('notebook.zim').exists():
 
51
                        return
 
52
 
 
53
                # if it doesn't, see if we know how to mount it
 
54
                config = self.get_config(uri)
 
55
                if config and 'mount' in config:
 
56
                        if 'passwd' in config:
 
57
                                passwd = self.prompt
 
58
                        Application(config['mount']).run()