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

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/txt/output.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:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
__license__ = 'GPL 3'
 
4
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
 
5
__docformat__ = 'restructuredtext en'
 
6
 
 
7
import os
 
8
 
 
9
from calibre.customize.conversion import OutputFormatPlugin, \
 
10
    OptionRecommendation
 
11
from calibre.ebooks.txt.txtml import TXTMLizer
 
12
from calibre.ebooks.txt.newlines import TxtNewlines, specified_newlines
 
13
 
 
14
class TXTOutput(OutputFormatPlugin):
 
15
 
 
16
    name = 'TXT Output'
 
17
    author = 'John Schember'
 
18
    file_type = 'txt'
 
19
 
 
20
    options = set([
 
21
        OptionRecommendation(name='newline', recommended_value='system',
 
22
            level=OptionRecommendation.LOW,
 
23
            short_switch='n', choices=TxtNewlines.NEWLINE_TYPES.keys(),
 
24
            help=_('Type of newline to use. Options are %s. Default is \'system\'. '
 
25
                'Use \'old_mac\' for compatibility with Mac OS 9 and earlier. '
 
26
                'For Mac OS X use \'unix\'. \'system\' will default to the newline '
 
27
                'type used by this OS.') % sorted(TxtNewlines.NEWLINE_TYPES.keys())),
 
28
        OptionRecommendation(name='output_encoding', recommended_value='utf-8',
 
29
            level=OptionRecommendation.LOW,
 
30
            help=_('Specify the character encoding of the output document. ' \
 
31
            'The default is utf-8. Note: This option is not honored by all ' \
 
32
            'formats.')),
 
33
                 ])
 
34
 
 
35
    def convert(self, oeb_book, output_path, input_plugin, opts, log):
 
36
        writer = TXTMLizer(log)
 
37
        txt = writer.extract_content(oeb_book, opts)
 
38
        
 
39
        log.debug('\tReplacing newlines with selected type...')
 
40
        txt = specified_newlines(TxtNewlines(opts.newline).newline, txt)
 
41
 
 
42
        close = False
 
43
        if not hasattr(output_path, 'write'):
 
44
            close = True
 
45
            if not os.path.exists(os.path.dirname(output_path)) and os.path.dirname(output_path) != '':
 
46
                os.makedirs(os.path.dirname(output_path))
 
47
            out_stream = open(output_path, 'wb')
 
48
        else:
 
49
            out_stream = output_path
 
50
 
 
51
        out_stream.seek(0)
 
52
        out_stream.truncate()
 
53
        out_stream.write(txt.encode(opts.output_encoding, 'replace'))
 
54
 
 
55
        if close:
 
56
            out_stream.close()
 
57