This repository has been archived by the owner on Oct 31, 2022. It is now read-only.
/
convert.py
127 lines (100 loc) · 4.33 KB
/
convert.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Copyright (C) 2010 Alex Yatskov
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import shutil
from PyQt4 import QtGui, QtCore
import cbz
import image
import pdfimage
class DialogConvert(QtGui.QProgressDialog):
def __init__(self, parent, book, directory):
QtGui.QProgressDialog.__init__(self)
self.book = book
self.bookPath = os.path.join(unicode(directory), unicode(self.book.title))
self.timer = None
self.setWindowTitle('Exporting book...')
self.setMaximum(len(self.book.images))
self.setValue(0)
self.archive = None
if 'CBZ' in self.book.outputFormat:
self.archive = cbz.Archive(self.bookPath)
self.pdf = None
if "PDF" in self.book.outputFormat:
self.pdf = pdfimage.PDFImage(self.bookPath, str(self.book.title), str(self.book.device))
def showEvent(self, event):
if self.timer is None:
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.onTimer)
self.timer.start(0)
def hideEvent(self, event):
"""Called when the dialog finishes processing."""
# Close the archive if we created a CBZ file
if self.archive is not None:
self.archive.close()
# Close and generate the PDF File
if self.pdf is not None:
self.pdf.close()
# Remove image directory if the user didn't wish for images
if 'Image' not in self.book.outputFormat:
shutil.rmtree(self.bookPath)
def onTimer(self):
index = self.value()
target = os.path.join(self.bookPath, '%05d.png' % index)
source = unicode(self.book.images[index])
if index == 0:
try:
if not os.path.isdir(self.bookPath):
os.makedirs(self.bookPath)
except OSError:
QtGui.QMessageBox.critical(self, 'Mangle', 'Cannot create directory %s' % self.bookPath)
self.close()
return
try:
base = os.path.join(self.bookPath, unicode(self.book.title))
mangaName = base + '.manga'
if self.book.overwrite or not os.path.isfile(mangaName):
manga = open(mangaName, 'w')
manga.write('\x00')
manga.close()
mangaSaveName = base + '.manga_save'
if self.book.overwrite or not os.path.isfile(mangaSaveName):
mangaSave = open(base + '.manga_save', 'w')
saveData = u'LAST=/mnt/us/pictures/%s/%s' % (self.book.title, os.path.split(target)[1])
mangaSave.write(saveData.encode('utf-8'))
mangaSave.close()
except IOError:
QtGui.QMessageBox.critical(self, 'Mangle', 'Cannot write manga file(s) to directory %s' % self.bookPath)
self.close()
return False
self.setLabelText('Processing %s...' % os.path.split(source)[1])
try:
if self.book.overwrite or not os.path.isfile(target):
image.convertImage(source, target, str(self.book.device), self.book.imageFlags)
if self.archive is not None:
self.archive.addFile(target)
if self.pdf is not None:
self.pdf.addImage(target)
except RuntimeError, error:
result = QtGui.QMessageBox.critical(
self,
'Mangle',
str(error),
QtGui.QMessageBox.Abort | QtGui.QMessageBox.Ignore,
QtGui.QMessageBox.Ignore
)
if result == QtGui.QMessageBox.Abort:
self.close()
return
self.setValue(index + 1)