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

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/conversion/config.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
#!/usr/bin/env python
 
2
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
 
3
from __future__ import with_statement
 
4
 
 
5
__license__   = 'GPL v3'
 
6
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
 
7
__docformat__ = 'restructuredtext en'
 
8
 
 
9
import os
 
10
 
 
11
from calibre.utils.config import config_dir
 
12
from calibre.utils.lock import ExclusiveFile
 
13
from calibre import sanitize_file_name
 
14
from calibre.customize.conversion import OptionRecommendation
 
15
 
 
16
 
 
17
config_dir = os.path.join(config_dir, 'conversion')
 
18
if not os.path.exists(config_dir):
 
19
    os.makedirs(config_dir)
 
20
 
 
21
def name_to_path(name):
 
22
    return os.path.join(config_dir, sanitize_file_name(name)+'.py')
 
23
 
 
24
def save_defaults(name, recs):
 
25
    path = name_to_path(name)
 
26
    raw = str(recs)
 
27
    with open(path, 'wb'):
 
28
        pass
 
29
    with ExclusiveFile(path) as f:
 
30
        f.write(raw)
 
31
 
 
32
def load_defaults(name):
 
33
    path = name_to_path(name)
 
34
    if not os.path.exists(path):
 
35
        open(path, 'wb').close()
 
36
    with ExclusiveFile(path) as f:
 
37
        raw = f.read()
 
38
    r = GuiRecommendations()
 
39
    if raw:
 
40
        r.from_string(raw)
 
41
    return r
 
42
 
 
43
def save_specifics(db, book_id, recs):
 
44
    raw = str(recs)
 
45
    db.set_conversion_options(book_id, 'PIPE', raw)
 
46
 
 
47
def load_specifics(db, book_id):
 
48
    raw = db.conversion_options(book_id, 'PIPE')
 
49
    r = GuiRecommendations()
 
50
    if raw:
 
51
        r.from_string(raw)
 
52
    return r
 
53
 
 
54
class GuiRecommendations(dict):
 
55
 
 
56
    def __new__(cls, *args):
 
57
        dict.__new__(cls)
 
58
        obj = super(GuiRecommendations, cls).__new__(cls, *args)
 
59
        obj.disabled_options = set([])
 
60
        return obj
 
61
 
 
62
    def to_recommendations(self, level=OptionRecommendation.LOW):
 
63
        ans = []
 
64
        for key, val in self.items():
 
65
            ans.append((key, val, level))
 
66
        return ans
 
67
 
 
68
    def __str__(self):
 
69
        ans = ['{']
 
70
        for key, val in self.items():
 
71
            ans.append('\t'+repr(key)+' : '+repr(val)+',')
 
72
        ans.append('}')
 
73
        return '\n'.join(ans)
 
74
 
 
75
    def from_string(self, raw):
 
76
        try:
 
77
            d = eval(raw)
 
78
        except SyntaxError:
 
79
            d = None
 
80
        if d:
 
81
            self.update(d)
 
82
 
 
83
    def merge_recommendations(self, get_option, level, options,
 
84
            only_existing=False):
 
85
        for name in options:
 
86
            if only_existing and name not in self:
 
87
                continue
 
88
            opt = get_option(name)
 
89
            if opt is None: continue
 
90
            if opt.level == OptionRecommendation.HIGH:
 
91
                self[name] = opt.recommended_value
 
92
                self.disabled_options.add(name)
 
93
            elif opt.level > level or name not in self:
 
94
                self[name] = opt.recommended_value
 
95
 
 
96