~ubuntu-branches/ubuntu/karmic/calibre/karmic

« back to all changes in this revision

Viewing changes to src/calibre/customize/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-30 12:49:41 UTC
  • mfrom: (1.3.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090730124941-qjdsmri25zt8zocn
Tags: 0.6.3+dfsg-0ubuntu1
* New upstream release. Please see http://calibre.kovidgoyal.net/new_in_6/
  for the list of new features and changes.
* remove_postinstall.patch: Update for new version.
* build_debug.patch: Does not apply any more, disable for now. Might not be
  necessary any more.
* debian/copyright: Fix reference to versionless GPL.
* debian/rules: Drop obsolete dh_desktop call.
* debian/rules: Add workaround for weird Python 2.6 setuptools behaviour of
  putting compiled .so files into src/calibre/plugins/calibre/plugins
  instead of src/calibre/plugins.
* debian/rules: Drop hal fdi moving, new upstream version does not use hal
  any more. Drop hal dependency, too.
* debian/rules: Install udev rules into /lib/udev/rules.d.
* Add debian/calibre.preinst: Remove unmodified
  /etc/udev/rules.d/95-calibre.rules on upgrade.
* debian/control: Bump Python dependencies to 2.6, since upstream needs
  it now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
import sys
6
6
 
7
7
from calibre.ptempfile import PersistentTemporaryFile
8
 
from calibre.constants import __version__, __author__
 
8
from calibre.constants import numeric_version
9
9
 
10
10
class Plugin(object):
11
11
    '''
12
12
    A calibre plugin. Useful members include:
13
 
    
 
13
 
14
14
       * ``self.plugin_path``: Stores path to the zip file that contains
15
15
                               this plugin or None if it is a builtin
16
16
                               plugin
17
17
       * ``self.site_customization``: Stores a customization string entered
18
18
                                      by the user.
19
 
                                      
 
19
 
20
20
    Methods that should be overridden in sub classes:
21
 
    
 
21
 
22
22
       * :meth:`initialize`
23
23
       * :meth:`customization_help`
24
 
    
 
24
 
25
25
    Useful methods:
26
 
    
 
26
 
27
27
        * :meth:`temporary_file`
28
 
        
 
28
 
29
29
    '''
30
30
    #: List of platforms this plugin works on
31
31
    #: For example: ``['windows', 'osx', 'linux']
32
32
    supported_platforms = []
33
 
    
 
33
 
34
34
    #: The name of this plugin
35
35
    name           = 'Trivial Plugin'
36
 
    
 
36
 
37
37
    #: The version of this plugin as a 3-tuple (major, minor, revision)
38
38
    version        = (1, 0, 0)
39
 
    
 
39
 
40
40
    #: A short string describing what this plugin does
41
41
    description    = _('Does absolutely nothing')
42
 
    
 
42
 
43
43
    #: The author of this plugin
44
44
    author         = _('Unknown')
45
 
    
 
45
 
46
46
    #: When more than one plugin exists for a filetype,
47
47
    #: the plugins are run in order of decreasing priority
48
48
    #: i.e. plugins with higher priority will be run first.
49
49
    #: The highest possible priority is ``sys.maxint``.
50
50
    #: Default pririty is 1.
51
51
    priority = 1
52
 
    
 
52
 
53
53
    #: The earliest version of calibre this plugin requires
54
54
    minimum_calibre_version = (0, 4, 118)
55
 
    
 
55
 
56
56
    #: If False, the user will not be able to disable this plugin. Use with
57
57
    #: care.
58
58
    can_be_disabled = True
59
 
    
 
59
 
60
60
    #: The type of this plugin. Used for categorizing plugins in the
61
61
    #: GUI
62
62
    type = _('Base')
64
64
    def __init__(self, plugin_path):
65
65
        self.plugin_path        = plugin_path
66
66
        self.site_customization = None
67
 
        
 
67
 
68
68
    def initialize(self):
69
69
        '''
70
70
        Called once when calibre plugins are initialized. Plugins are re-initialized
71
71
        every time a new plugin is added.
72
 
        
 
72
 
73
73
        Perform any plugin specific initialization here, such as extracting
74
74
        resources from the plugin zip file. The path to the zip file is
75
75
        available as ``self.plugin_path``.
76
 
        
 
76
 
77
77
        Note that ``self.site_customization`` is **not** available at this point.
78
78
        '''
79
79
        pass
80
 
    
 
80
 
81
81
    def customization_help(self, gui=False):
82
82
        '''
83
83
        Return a string giving help on how to customize this plugin.
84
84
        By default raise a :class:`NotImplementedError`, which indicates that
85
85
        the plugin does not require customization.
86
 
        
 
86
 
87
87
        If you re-implement this method in your subclass, the user will
88
88
        be asked to enter a string as customization for this plugin.
89
 
        The customization string will be available as 
 
89
        The customization string will be available as
90
90
        ``self.site_customization``.
91
 
        
 
91
 
92
92
        Site customization could be anything, for example, the path to
93
93
        a needed binary on the user's computer.
94
 
        
95
 
        :param gui: If True return HTML help, otherwise return plain text help. 
96
 
        
 
94
 
 
95
        :param gui: If True return HTML help, otherwise return plain text help.
 
96
 
97
97
        '''
98
98
        raise NotImplementedError
99
 
        
 
99
 
100
100
    def temporary_file(self, suffix):
101
101
        '''
102
102
        Return a file-like object that is a temporary file on the file system.
103
103
        This file will remain available even after being closed and will only
104
104
        be removed on interpreter shutdown. Use the ``name`` member of the
105
105
        returned object to access the full path to the created temporary file.
106
 
        
 
106
 
107
107
        :param suffix: The suffix that the temporary file will have.
108
108
        '''
109
109
        return PersistentTemporaryFile(suffix)
110
 
    
 
110
 
111
111
    def is_customizable(self):
112
112
        try:
113
113
            self.customization_help()
114
114
            return True
115
115
        except NotImplementedError:
116
116
            return False
117
 
        
 
117
 
118
118
    def __enter__(self, *args):
119
119
        if self.plugin_path is not None:
120
120
            sys.path.insert(0, self.plugin_path)
121
 
            
 
121
 
122
122
    def __exit__(self, *args):
123
123
        if self.plugin_path in sys.path:
124
124
            sys.path.remove(self.plugin_path)
125
 
            
126
 
            
 
125
 
 
126
 
127
127
class FileTypePlugin(Plugin):
128
128
    '''
129
129
    A plugin that is associated with a particular set of file types.
130
130
    '''
131
 
    
 
131
 
132
132
    #: Set of file types for which this plugin should be run
133
133
    #: For example: ``set(['lit', 'mobi', 'prc'])``
134
134
    file_types     = set([])
135
 
    
 
135
 
136
136
    #: If True, this plugin is run when books are added
137
137
    #: to the database
138
138
    on_import      = False
139
 
    
 
139
 
140
140
    #: If True, this plugin is run whenever an any2* tool
141
141
    #: is used, on the file passed to the any2* tool.
142
142
    on_preprocess  = False
143
 
    
 
143
 
144
144
    #: If True, this plugin is run after an any2* tool is
145
145
    #: used, on the final file produced by the tool.
146
146
    on_postprocess = False
147
 
    
 
147
 
148
148
    type = _('File type')
149
149
 
150
150
    def run(self, path_to_ebook):
151
151
        '''
152
152
        Run the plugin. Must be implemented in subclasses.
153
 
        It should perform whatever modifications are required 
154
 
        on the ebook and return the absolute path to the 
 
153
        It should perform whatever modifications are required
 
154
        on the ebook and return the absolute path to the
155
155
        modified ebook. If no modifications are needed, it should
156
156
        return the path to the original ebook. If an error is encountered
157
157
        it should raise an Exception. The default implementation
158
158
        simply return the path to the original ebook.
159
 
        
160
 
        The modified ebook file should be created with the 
 
159
 
 
160
        The modified ebook file should be created with the
161
161
        :meth:`temporary_file` method.
162
 
        
 
162
 
163
163
        :param path_to_ebook: Absolute path to the ebook.
164
164
 
165
 
        :return: Absolute path to the modified ebook. 
 
165
        :return: Absolute path to the modified ebook.
166
166
        '''
167
167
        # Default implementation does nothing
168
168
        return path_to_ebook
169
 
    
 
169
 
170
170
class MetadataReaderPlugin(Plugin):
171
171
    '''
172
172
    A plugin that implements reading metadata from a set of file types.
174
174
    #: Set of file types for which this plugin should be run
175
175
    #: For example: ``set(['lit', 'mobi', 'prc'])``
176
176
    file_types     = set([])
177
 
    
 
177
 
178
178
    supported_platforms = ['windows', 'osx', 'linux']
179
 
    version = tuple(map(int, (__version__.split('.'))[:3]))
 
179
    version = numeric_version
180
180
    author  = 'Kovid Goyal'
181
 
    
 
181
 
182
182
    type = _('Metadata reader')
183
 
    
 
183
 
 
184
    def __init__(self, *args, **kwargs):
 
185
        Plugin.__init__(self, *args, **kwargs)
 
186
        self.quick = False
 
187
 
184
188
    def get_metadata(self, stream, type):
185
189
        '''
186
190
        Return metadata for the file represented by stream (a file like object
187
 
        that supports reading). Raise an exception when there is an error 
 
191
        that supports reading). Raise an exception when there is an error
188
192
        with the input data.
189
 
        
 
193
 
190
194
        :param type: The type of file. Guaranteed to be one of the entries
191
195
        in :attr:`file_types`.
192
196
 
193
 
        :return: A :class:`calibre.ebooks.metadata.MetaInformation` object 
 
197
        :return: A :class:`calibre.ebooks.metadata.MetaInformation` object
194
198
        '''
195
199
        return None
196
 
    
 
200
 
197
201
class MetadataWriterPlugin(Plugin):
198
202
    '''
199
203
    A plugin that implements reading metadata from a set of file types.
201
205
    #: Set of file types for which this plugin should be run
202
206
    #: For example: ``set(['lit', 'mobi', 'prc'])``
203
207
    file_types     = set([])
204
 
    
 
208
 
205
209
    supported_platforms = ['windows', 'osx', 'linux']
206
 
    version = tuple(map(int, (__version__.split('.'))[:3]))
 
210
    version = numeric_version
207
211
    author  = 'Kovid Goyal'
208
 
    
 
212
 
209
213
    type = _('Metadata writer')
210
 
    
 
214
 
211
215
    def set_metadata(self, stream, mi, type):
212
216
        '''
213
217
        Set metadata for the file represented by stream (a file like object
214
 
        that supports reading). Raise an exception when there is an error 
 
218
        that supports reading). Raise an exception when there is an error
215
219
        with the input data.
216
 
        
 
220
 
217
221
        :param type: The type of file. Guaranteed to be one of the entries
218
222
        in :attr:`file_types`.
219
 
        :param mi: A :class:`calibre.ebooks.metadata.MetaInformation` object 
 
223
        :param mi: A :class:`calibre.ebooks.metadata.MetaInformation` object
220
224
 
221
225
        '''
222
226
        pass
223
 
    
 
227
 
 
228