~stub/ubuntu/precise/calibre/devel

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-04-12 11:29:25 UTC
  • mfrom: (42.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110412112925-c7171kt2bb5rmft4
Tags: 0.7.50+dfsg-2
* debian/control: Build with libpodofo-dev to enable PDF metadata.
  (Closes: #619632)
* debian/control: Add libboost1.42-dev build dependency. Apparently it is
  needed in some setups. (Closes: #619807)
* debian/rules: Call dh_sip to generate a proper sip API dependency, to
  prevent crashes like #616372 for partial upgrades.
* debian/control: Bump python-qt4 dependency to >= 4.8.3-2, which reportedly
  fixes crashes on startup. (Closes: #619701, #620125)

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
__license__   = 'GPL v3'
3
3
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
4
4
 
5
 
import textwrap, os, glob, functools
 
5
import textwrap, os, glob, functools, re
 
6
from calibre import guess_type
6
7
from calibre.customize import FileTypePlugin, MetadataReaderPlugin, \
7
8
    MetadataWriterPlugin, PreferencesPlugin, InterfaceActionBase
8
9
from calibre.constants import numeric_version
9
10
from calibre.ebooks.metadata.archive import ArchiveExtract, get_cbz_metadata
 
11
from calibre.ebooks.metadata.opf2 import metadata_to_opf
 
12
from calibre.ebooks.oeb.base import OEB_IMAGES
10
13
 
11
14
# To archive plugins {{{
12
15
class HTML2ZIP(FileTypePlugin):
82
85
 
83
86
        return of.name
84
87
 
 
88
class TXT2TXTZ(FileTypePlugin):
 
89
    name = 'TXT to TXTZ'
 
90
    author = 'John Schember'
 
91
    description = _('Create a TXTZ archive when a TXT file is imported '
 
92
        'containing Markdown or Textile references to images. The referenced '
 
93
        'images as well as the TXT file are added to the archive.')
 
94
    version = numeric_version
 
95
    file_types = set(['txt', 'text'])
 
96
    supported_platforms = ['windows', 'osx', 'linux']
 
97
    on_import = True
 
98
 
 
99
    def _get_image_references(self, txt, base_dir):
 
100
        images = []
 
101
 
 
102
        # Textile
 
103
        for m in re.finditer(ur'(?mu)(?:[\[{])?\!(?:\. )?(?P<path>[^\s(!]+)\s?(?:\(([^\)]+)\))?\!(?::(\S+))?(?:[\]}]|(?=\s|$))', txt):
 
104
            path = m.group('path')
 
105
            if path and not os.path.isabs(path) and guess_type(path)[0] in OEB_IMAGES and os.path.exists(os.path.join(base_dir, path)):
 
106
                images.append(path)
 
107
 
 
108
        # Markdown inline
 
109
        for m in re.finditer(ur'(?mu)\!\[([^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*)\]\s*\((?P<path>[^\)]*)\)', txt):
 
110
            path = m.group('path')
 
111
            if path and not os.path.isabs(path) and guess_type(path)[0] in OEB_IMAGES and os.path.exists(os.path.join(base_dir, path)):
 
112
                images.append(path)
 
113
 
 
114
        # Markdown reference
 
115
        refs = {}
 
116
        for m in re.finditer(ur'(?mu)^(\ ?\ ?\ ?)\[(?P<id>[^\]]*)\]:\s*(?P<path>[^\s]*)$', txt):
 
117
            if m.group('id') and m.group('path'):
 
118
                refs[m.group('id')] = m.group('path')
 
119
        for m in re.finditer(ur'(?mu)\!\[([^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*(\[[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*\])*[^\]\[]*)\]\s*\[(?P<id>[^\]]*)\]', txt):
 
120
            path = refs.get(m.group('id'), None)
 
121
            if path and not os.path.isabs(path) and guess_type(path)[0] in OEB_IMAGES and os.path.exists(os.path.join(base_dir, path)):
 
122
                images.append(path)
 
123
 
 
124
        # Remove duplicates
 
125
        return list(set(images))
 
126
 
 
127
    def run(self, path_to_ebook):
 
128
        with open(path_to_ebook, 'rb') as ebf:
 
129
            txt = ebf.read()
 
130
        base_dir = os.path.dirname(path_to_ebook)
 
131
        images = self._get_image_references(txt, base_dir)
 
132
 
 
133
        if images:
 
134
            # Create TXTZ and put file plus images inside of it.
 
135
            import zipfile
 
136
            of = self.temporary_file('_plugin_txt2txtz.txtz')
 
137
            txtz = zipfile.ZipFile(of.name, 'w')
 
138
            # Add selected TXT file to archive.
 
139
            txtz.write(path_to_ebook, os.path.basename(path_to_ebook), zipfile.ZIP_DEFLATED)
 
140
            # metadata.opf
 
141
            if os.path.exists(os.path.join(base_dir, 'metadata.opf')):
 
142
                txtz.write(os.path.join(base_dir, 'metadata.opf'), 'metadata.opf', zipfile.ZIP_DEFLATED)
 
143
            else:
 
144
                from calibre.ebooks.metadata.txt import get_metadata
 
145
                with open(path_to_ebook, 'rb') as ebf:
 
146
                    mi = get_metadata(ebf)
 
147
                opf = metadata_to_opf(mi)
 
148
                txtz.writestr('metadata.opf', opf, zipfile.ZIP_DEFLATED)
 
149
            # images
 
150
            for image in images:
 
151
                txtz.write(os.path.join(base_dir, image), image)
 
152
            txtz.close()
 
153
 
 
154
            return of.name
 
155
        else:
 
156
            # No images so just import the TXT file.
 
157
            return path_to_ebook
 
158
 
85
159
# }}}
86
160
 
87
161
# Metadata reader plugins {{{
325
399
        from calibre.ebooks.metadata.txt import get_metadata
326
400
        return get_metadata(stream)
327
401
 
 
402
class TXTZMetadataReader(MetadataReaderPlugin):
 
403
 
 
404
    name        = 'Read TXTZ metadata'
 
405
    file_types  = set(['txtz'])
 
406
    description = _('Read metadata from %s files') % 'TXTZ'
 
407
    author      = 'John Schember'
 
408
 
 
409
    def get_metadata(self, stream, ftype):
 
410
        from calibre.ebooks.metadata.txtz import get_metadata
 
411
        return get_metadata(stream)
 
412
 
328
413
class ZipMetadataReader(MetadataReaderPlugin):
329
414
 
330
415
    name = 'Read ZIP metadata'
412
497
        from calibre.ebooks.metadata.topaz import set_metadata
413
498
        set_metadata(stream, mi)
414
499
 
 
500
class TXTZMetadataWriter(MetadataWriterPlugin):
 
501
 
 
502
    name        = 'Set TXTZ metadata'
 
503
    file_types  = set(['txtz'])
 
504
    description = _('Set metadata from %s files') % 'TXTZ'
 
505
    author      = 'John Schember'
 
506
 
 
507
    def set_metadata(self, stream, mi, type):
 
508
        from calibre.ebooks.metadata.txtz import set_metadata
 
509
        set_metadata(stream, mi)
 
510
 
415
511
# }}}
416
512
 
417
513
from calibre.ebooks.comic.input import ComicInput
446
542
from calibre.ebooks.rtf.output import RTFOutput
447
543
from calibre.ebooks.tcr.output import TCROutput
448
544
from calibre.ebooks.txt.output import TXTOutput
 
545
from calibre.ebooks.txt.output import TXTZOutput
449
546
from calibre.ebooks.html.output import HTMLOutput
450
547
from calibre.ebooks.snb.output import SNBOutput
451
548
 
474
571
from calibre.devices.hanvon.driver import N516, EB511, ALEX, AZBOOKA, THEBOOK
475
572
from calibre.devices.edge.driver import EDGE
476
573
from calibre.devices.teclast.driver import TECLAST_K3, NEWSMY, IPAPYRUS, \
477
 
        SOVOS, PICO, SUNSTECH_EB700, ARCHOS7O
 
574
        SOVOS, PICO, SUNSTECH_EB700, ARCHOS7O, STASH, WEXLER
478
575
from calibre.devices.sne.driver import SNE
479
 
from calibre.devices.misc import PALMPRE, AVANT, SWEEX, PDNOVEL, KOGAN, \
480
 
        GEMEI, VELOCITYMICRO, PDNOVEL_KOBO, Q600, LUMIREAD, ALURATEK_COLOR, \
 
576
from calibre.devices.misc import PALMPRE, AVANT, SWEEX, PDNOVEL, \
 
577
        GEMEI, VELOCITYMICRO, PDNOVEL_KOBO, LUMIREAD, ALURATEK_COLOR, \
481
578
        TREKSTOR, EEEREADER, NEXTBOOK
482
579
from calibre.devices.folder_device.driver import FOLDER_DEVICE_FOR_CONFIG
483
580
from calibre.devices.kobo.driver import KOBO
484
581
from calibre.devices.bambook.driver import BAMBOOK
485
582
 
486
583
from calibre.ebooks.metadata.fetch import GoogleBooks, ISBNDB, Amazon, \
487
 
    LibraryThing
 
584
    KentDistrictLibrary
488
585
from calibre.ebooks.metadata.douban import DoubanBooks
489
586
from calibre.ebooks.metadata.nicebooks import NiceBooks, NiceBooksCovers
490
587
from calibre.ebooks.metadata.covers import OpenLibraryCovers, \
491
 
        LibraryThingCovers, DoubanCovers
 
588
        AmazonCovers, DoubanCovers
492
589
from calibre.library.catalog import CSV_XML, EPUB_MOBI, BIBTEX
493
590
from calibre.ebooks.epub.fix.unmanifested import Unmanifested
494
591
from calibre.ebooks.epub.fix.epubcheck import Epubcheck
495
592
 
496
 
plugins = [HTML2ZIP, PML2PMLZ, ArchiveExtract, GoogleBooks, ISBNDB, Amazon,
497
 
        LibraryThing, DoubanBooks, NiceBooks, CSV_XML, EPUB_MOBI, BIBTEX, Unmanifested,
498
 
        Epubcheck, OpenLibraryCovers, LibraryThingCovers, DoubanCovers,
 
593
plugins = [HTML2ZIP, PML2PMLZ, TXT2TXTZ, ArchiveExtract, GoogleBooks, ISBNDB, Amazon,
 
594
        KentDistrictLibrary, DoubanBooks, NiceBooks, CSV_XML, EPUB_MOBI, BIBTEX, Unmanifested,
 
595
        Epubcheck, OpenLibraryCovers, AmazonCovers, DoubanCovers,
499
596
        NiceBooksCovers]
500
597
plugins += [
501
598
    ComicInput,
531
628
    RTFOutput,
532
629
    TCROutput,
533
630
    TXTOutput,
 
631
    TXTZOutput,
534
632
    HTMLOutput,
535
633
    SNBOutput,
536
634
]
581
679
    ELONEX,
582
680
    TECLAST_K3,
583
681
    NEWSMY,
584
 
    PICO, SUNSTECH_EB700, ARCHOS7O,
 
682
    PICO, SUNSTECH_EB700, ARCHOS7O, SOVOS, STASH, WEXLER,
585
683
    IPAPYRUS,
586
 
    SOVOS,
587
684
    EDGE,
588
685
    SNE,
589
686
    ALEX,
594
691
    AVANT,
595
692
    MENTOR,
596
693
    SWEEX,
597
 
    Q600,
598
 
    KOGAN,
599
694
    PDNOVEL,
600
695
    SPECTRA,
601
696
    GEMEI,
767
862
    description = _('Customize the toolbars and context menus, changing which'
768
863
            ' actions are available in each')
769
864
 
 
865
class Search(PreferencesPlugin):
 
866
    name = 'Search'
 
867
    icon = I('search.png')
 
868
    gui_name = _('Customize searching')
 
869
    category = 'Interface'
 
870
    gui_category = _('Interface')
 
871
    category_order = 1
 
872
    name_order = 5
 
873
    config_widget = 'calibre.gui2.preferences.search'
 
874
    description = _('Customize the way searching for books works in calibre')
 
875
 
770
876
class InputOptions(PreferencesPlugin):
771
877
    name = 'Input Options'
772
878
    icon = I('arrow-down.png')
917
1023
    config_widget = 'calibre.gui2.preferences.misc'
918
1024
    description = _('Miscellaneous advanced configuration')
919
1025
 
920
 
plugins += [LookAndFeel, Behavior, Columns, Toolbar, InputOptions,
 
1026
plugins += [LookAndFeel, Behavior, Columns, Toolbar, Search, InputOptions,
921
1027
        CommonOptions, OutputOptions, Adding, Saving, Sending, Plugboard,
922
1028
        Email, Server, Plugins, Tweaks, Misc, TemplateFunctions]
923
1029
 
924
1030
#}}}
 
1031
 
 
1032
# New metadata download plugins {{{
 
1033
from calibre.ebooks.metadata.sources.google import GoogleBooks
 
1034
from calibre.ebooks.metadata.sources.amazon import Amazon
 
1035
 
 
1036
plugins += [GoogleBooks, Amazon]
 
1037
 
 
1038
# }}}