Mangle can now write CBZ files, CBZ-only is not yet implemented

This commit is contained in:
Marek Kubica 2011-10-30 12:38:01 +09:00
parent be90061fa5
commit e86128dee1
5 changed files with 93 additions and 4 deletions

View File

@ -24,10 +24,11 @@ from about import DialogAbout
from options import DialogOptions
from convert import DialogConvert
class Book:
class Book(object):
DefaultDevice = 'Kindle 3'
DefaultOverwrite = True
DefaultImageFlags = ImageFlags.Orient | ImageFlags.Resize | ImageFlags.Quantize
DefaultOutputFormat = 'image+cbz'
def __init__(self):
@ -38,6 +39,7 @@ class Book:
self.device = Book.DefaultDevice
self.overwrite = Book.DefaultOverwrite
self.imageFlags = Book.DefaultImageFlags
self.outputFormat = Book.DefaultOutputFormat
def save(self, filename):
@ -50,6 +52,7 @@ class Book:
root.setAttribute('overwrite', 'true' if self.overwrite else 'false')
root.setAttribute('device', self.device)
root.setAttribute('imageFlags', self.imageFlags)
root.setAttribute('outputFormat', self.outputFormat)
for filenameImg in self.images:
itemImg = document.createElement('image')
@ -89,13 +92,14 @@ class Book:
self.title = root.attribute('title', 'Untitled')
self.overwrite = root.attribute('overwrite', 'true' if Book.DefaultOverwrite else 'false') == 'true'
self.device = root.attribute('device', Book.DefaultDevice)
self.outputFormat = root.attribute('outputFormat', 'image+cbz')
self.imageFlags = int(root.attribute('imageFlags', str(Book.DefaultImageFlags)))
self.filename = filename
self.modified = False
self.images = []
items = root.elementsByTagName('image')
if items == None:
if items is None:
return
for i in xrange(0, len(items)):

39
mangle/cbz.py Normal file
View File

@ -0,0 +1,39 @@
# Copyright (C) 2011 Marek Kubica <marek@xivilization.net>
#
# 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.path
from zipfile import ZipFile, ZIP_STORED
class Archive(object):
def __init__(self, path):
outputDirectory = os.path.dirname(path)
outputFileName = '%s.cbz' % os.path.basename(path)
outputPath = os.path.join(outputDirectory, outputFileName)
self.zipfile = ZipFile(outputPath, 'w', ZIP_STORED)
def addFile(self, filename):
arcname = os.path.basename(filename)
self.zipfile.write(filename, arcname)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
def close(self):
self.zipfile.close()

View File

@ -17,11 +17,13 @@ import os
from PyQt4 import QtGui, QtCore
import image
import cbz
class DialogConvert(QtGui.QProgressDialog):
def __init__(self, parent, book, directory):
QtGui.QProgressDialog.__init__(self)
#self.setAutoReset(False)
self.book = book
self.directory = directory
@ -31,14 +33,25 @@ class DialogConvert(QtGui.QProgressDialog):
self.setMaximum(len(self.book.images))
self.setValue(0)
self.archive = None
if 'cbz' in self.book.outputFormat:
# TODO: switch to API 2, to get rid of the unicode conversions
self.archive = cbz.Archive(
os.path.join(unicode(self.directory), unicode(self.book.title)))
def showEvent(self, event):
if self.timer == None:
if self.timer is None:
self.timer = QtCore.QTimer()
self.connect(self.timer, QtCore.SIGNAL('timeout()'), self.onTimer)
self.timer.start(0)
def closeEvent(self, event):
print "closeEvent"
self.closing.emit()
event.accept()
def onTimer(self):
index = self.value()
directory = os.path.join(unicode(self.directory), unicode(self.book.title))
@ -80,6 +93,8 @@ class DialogConvert(QtGui.QProgressDialog):
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)
except RuntimeError, error:
result = QtGui.QMessageBox.critical(
self,

View File

@ -37,6 +37,8 @@ class DialogOptions(QtGui.QDialog):
def moveOptionsToDialog(self):
self.lineEditTitle.setText(self.book.title or 'Untitled')
self.comboBoxDevice.setCurrentIndex(max(self.comboBoxDevice.findText(self.book.device), 0))
formats = {'image+cbz' : 0, 'image' : 1, 'cbz' : 2}
self.comboBoxFormat.setCurrentIndex(formats.get(self.book.outputFormat, formats['image+cbz']))
self.checkboxOverwrite.setChecked(self.book.overwrite)
self.checkboxOrient.setChecked(self.book.imageFlags & ImageFlags.Orient)
self.checkboxResize.setChecked(self.book.imageFlags & ImageFlags.Resize)

View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>350</width>
<height>350</height>
<height>363</height>
</rect>
</property>
<property name="windowTitle">
@ -44,6 +44,9 @@
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QFormLayout" name="formLayout_2">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::AllNonFixedFieldsGrow</enum>
</property>
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
@ -80,6 +83,32 @@
</item>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="labelFormat">
<property name="text">
<string>Format</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="comboBoxFormat">
<item>
<property name="text">
<string>Images &amp; CBZ</string>
</property>
</item>
<item>
<property name="text">
<string>Images only</string>
</property>
</item>
<item>
<property name="text">
<string>CBZ only</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>