File: gui.py

package info (click to toggle)
calibre 7.8.0%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 432,660 kB
  • sloc: python: 428,548; ansic: 80,714; javascript: 56,201; cpp: 18,154; xml: 1,894; sh: 903; sql: 735; objc: 599; makefile: 66; sed: 7
file content (87 lines) | stat: -rw-r--r-- 3,221 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python


__license__   = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'

import os
from contextlib import suppress

from setup import Command, __appname__


class GUI(Command):
    description = 'Compile all GUI forms'
    PATH  = os.path.join(Command.SRC, __appname__, 'gui2')
    QRC = os.path.join(Command.RESOURCES, 'images.qrc')
    RCC = os.path.join(Command.RESOURCES, 'icons.rcc')

    def add_options(self, parser):
        parser.add_option('--summary', default=False, action='store_true',
                help='Only display a summary about how many files were compiled')

    def find_forms(self):
        # We do not use the calibre function find_forms as
        # importing calibre.gui2 may not work
        forms = []
        for root, _, files in os.walk(self.PATH):
            for name in files:
                path = os.path.abspath(os.path.join(root, name))
                if name.endswith('.ui'):
                    forms.append(path)
                elif name.endswith('_ui.py') or name.endswith('_ui.pyc'):
                    fname = path.rpartition('_')[0] + '.ui'
                    if not os.path.exists(fname):
                        os.remove(path)
        return forms

    @classmethod
    def form_to_compiled_form(cls, form):
        # We do not use the calibre function form_to_compiled_form as
        # importing calibre.gui2 may not work
        return form.rpartition('.')[0]+'_ui.py'

    def run(self, opts):
        self.build_forms(summary=opts.summary)
        self.build_images()

    def build_images(self):
        cwd = os.getcwd()
        try:
            os.chdir(self.RESOURCES)
            sources, files = [], []
            for root, dirs, files2 in os.walk('images'):
                dirs.sort()
                files2.sort()
                for name in files2:
                    sources.append(os.path.join(root, name))
            if self.newer(self.RCC, sources):
                self.info('Creating icon theme resource file')
                from calibre.utils.rcc import compile_icon_dir_as_themes
                compile_icon_dir_as_themes('images', self.RCC)
            if self.newer(self.QRC, sources):
                self.info('Creating images.qrc')
                for s in sources:
                    files.append('<file>%s</file>'%s)
                manifest = '<RCC>\n<qresource prefix="/">\n%s\n</qresource>\n</RCC>'%'\n'.join(sorted(files))
                if not isinstance(manifest, bytes):
                    manifest = manifest.encode('utf-8')
                with open('images.qrc', 'wb') as f:
                    f.write(manifest)
        finally:
            os.chdir(cwd)

    def build_forms(self, summary=False):
        from calibre.build_forms import build_forms
        build_forms(self.SRC, info=self.info, summary=summary, check_icons=False)

    def clean(self):
        forms = self.find_forms()
        for form in forms:
            c = self.form_to_compiled_form(form)
            if os.path.exists(c):
                os.remove(c)
        for x in (self.QRC, self.RCC):
            with suppress(FileNotFoundError):
                os.remove(x)