#!/bin/env python # -*- coding: iso-8859-1 -*- # Script to import tellico book database to scribus (c) damu@iki.fi GPL-2 # # This script expects to find images from ~/.kde/share/apps/tellico/data/ # # To get translation file # xgettext --keyword="N_" -o tellico2scribus.po tellico2scribus.py from zipfile import ZipFile from xml.dom import minidom import os, sys import time import re from gettext import GNUTranslations scribus = True try: import scribus except ImportError,err: scribus = False def N_(message): return message if scribus: scribus.statusMessage("Running script...") scribus.progressReset() def genre_sort(a, b): try: an = int(a[0]) bn = int(b[0]) if(an == 8): an *= -10 if(bn == 8): bn *= -10 return an - bn except: return cmp(a, b) # Define sort order sort_keys = ['genre', 'author', 'series', 'series_num', 'cr_year', 'title'] sort_funcs = {'genre': genre_sort} # Define colors color_field = 'genre' color = { 'default': ('default', (255, 0, 0, 255)), '^0': ('dgray', (50, 50, 50, 60)), '^1': ('yellow', (0, 0, 50, 60)), '^3': ('red', (0, 50, 50, 60)), '^4': ('green', (25, 0, 25, 60)), '^5': ('gray', (25, 25, 25, 60)), '^6': ('purple', (0, 50, 0, 60)), '^8': ('orange', (0, 25, 50, 60)), '^9': ('blue', (50, 0, 0, 60)) } # Other constants A4 = (210, 297) MARGIN = (10,10,10,10) image_margin = 2 item_spacing = 2 image_ratio = 0.657471 txt_height = 4.2 label_width = 20 items = [ ('title', ''), ('subtitle', ''), ('cr_year', '#'), ('author', N_('Author:')), ('isbn', N_('ISBN:')), ('binding', N_('Binding:')), ('publisher', N_('Publisher:')), ('edition', N_('Edition:')), ('pub_year', '#'), ('pages', N_('Pages:')), ('language', N_('Language:')), ('genre', '#'), ('series', '#'), ('series_num', '#'), ('cover', '#') ] bindings = [N_('Paperback'), N_('Hardback')] languages = [N_('Suomi'), N_('suomi'), N_('English'), N_('Englanti')] image_dir = '~/.kde/share/apps/tellico/data/' file = scribus.fileDialog('tellico2scribus', 'Tellico files (*.tc)') if file == '': sys.exit(1) def getData(entry, key): elems = entry.getElementsByTagName(key) temp = [] for elem in elems: temp.append(elem.firstChild.data) return ', '.join(temp) def cmpData(a, b): for sort_key in sort_keys: if sort_funcs.has_key(sort_key): n = sort_funcs[sort_key](a[sort_key], b[sort_key]) else: n = cmp(a[sort_key].lower(), b[sort_key].lower()) if n != 0: return n return 0 pathname = os.path.abspath(os.path.dirname(sys.argv[0])) if os.path.isfile(pathname + '/tellico2scribus.mo'): fp = open(pathname + '/tellico2scribus.mo') gt = GNUTranslations(fp) _ = gt.ugettext else: _ = lambda(x): x zf = ZipFile(file) xml = zf.read('tellico.xml') zf.close() doc = minidom.parseString(xml) entries = doc.getElementsByTagName('entry') data = [] for entry in entries: d = {} for item in items: d[item[0]] = getData(entry, item[0]) data.append(d) data.sort(cmpData) # Modify data for d in data: s = d['subtitle'] if s != '': d['subtitle'] = '%s (%s)' % (s, d['cr_year']) else: d['subtitle'] = '(%s)' % d['cr_year'] s = d['edition'] d['edition'] = '%s. (%s)' % (s, d['pub_year']) d['binding'] = _(d['binding']) d['language'] = _(d['language']) if scribus == False: for n, d in enumerate(data): print '--------------------------------------------------' for item in items: if item[1] == '#': continue s = '' if item[1] != '': s = _(item[1]).encode('iso-8859-1').ljust(13) print s + d[item[0]].encode('iso-8859-1') file = os.path.expanduser(image_dir + d['cover']) print file print '--------------------------------------------------' else: t = time.time() if scribus.newDoc(A4, MARGIN, scribus.PORTRAIT, 1, \ scribus.UNIT_MILLIMETERS, scribus.NOFACINGPAGES, \ scribus.FIRSTPAGELEFT): size = scribus.getPageSize() width = (size[0] - MARGIN[0] -MARGIN[1]) / 2 master_x = (MARGIN[0], MARGIN[0] + width) height = (size[1] - MARGIN[2] -MARGIN[3]) / 5 master_y = (MARGIN[2], MARGIN[2] + height, MARGIN[2] + (height * 2), MARGIN[2] + (height * 3), MARGIN[2] + (height * 4)) for key, col in color.iteritems(): scribus.defineColor(col[0], col[1][0], col[1][1], col[1][2], col[1][3]) image_height = height - 2 * image_margin - item_spacing image_width = image_height * image_ratio scribus.statusMessage('%dx%d' % (image_width, image_height)) scribus.progressTotal(len(data)) for n, d in enumerate(data): scribus.statusMessage(d['title']) if (n%10 == 0 and n != 0): scribus.newPage(-1) x = int(n%2) y = int((n/2)%5) c = 'default' for key, col in color.iteritems(): if re.search(key, d[color_field]): c = col[0] break name = scribus.createRect(master_x[x], master_y[y], width, 14) scribus.setFillColor(c, name) scribus.setLineWidth(0.0, name) name = scribus.createRect(master_x[x], master_y[y], image_width + 2 * image_margin, image_height + 2 * image_margin) scribus.setFillColor(c, name) scribus.setLineWidth(0.0, name) name = scribus.createImage(master_x[x] + image_margin, master_y[y] + image_margin, image_width, image_height) file = os.path.expanduser(image_dir + d['cover']) scribus.setFillColor(c, name) scribus.setLineWidth(0.0, name) scribus.loadImage(file, name) scribus.setScaleImageToFrame(True, False, name) txt_x = master_x[x] + image_width + 2 * image_margin + item_spacing w = width - image_width - 2 * image_margin - item_spacing txt_y = master_y[y] for item in items: if item[1] == '#': continue if item[0] == 'title': txt_y += 1.5 text = scribus.createText(txt_x - item_spacing, txt_y, (w + item_spacing), 11) scribus.insertText(d[item[0]].encode('utf-8'), 0, text) scribus.setFont("Bitstream Vera Sans Bold", text) scribus.setFontSize(12, text) scribus.setTextColor('white', text) txt_y += 13 continue if item[1] != '': text = scribus.createText(txt_x, txt_y, label_width, txt_height) scribus.insertText(_(item[1]), 0, text) scribus.setFont("Bitstream Vera Sans Roman", text) scribus.setFontSize(9, text) if item[0] != 'subtitle': text = scribus.createText(txt_x + label_width, txt_y, (w - label_width), txt_height) txt_y += txt_height else: text = scribus.createText(txt_x, txt_y, w, txt_height * 2) txt_y += txt_height * 2 scribus.insertText(d[item[0]].encode('utf-8'), 0, text) scribus.setFont("Bitstream Vera Sans Roman", text) scribus.setFontSize(9, text) scribus.progressSet(n+1) t = time.time() - t scribus.statusMessage('Ready: %fs' % t)