This commit is contained in:
Kovid Goyal 2007-11-14 04:41:13 +00:00
parent 620bbb04c7
commit 85718558aa
5 changed files with 65 additions and 12 deletions

View File

@ -614,6 +614,19 @@ def option_parser():
return parser return parser
def set_metadata(stream, mi):
lrf = LRFMetaFile(stream)
if mi.title:
lrf.title = mi.title
if mi.authors:
lrf.author = ', '.join(mi.authors)
if mi.category:
lrf.category = mi.category
if mi.comments:
lrf.free_text = mi.comments
if mi.author_sort:
lrf.author_reading = mi.author_sort
def main(args=sys.argv): def main(args=sys.argv):
import os.path import os.path

View File

@ -17,9 +17,13 @@ from libprs500.ebooks.metadata.rtf import get_metadata as rtf_metadata
from libprs500.ebooks.lrf.meta import get_metadata as lrf_metadata from libprs500.ebooks.lrf.meta import get_metadata as lrf_metadata
from libprs500.ebooks.metadata.pdf import get_metadata as pdf_metadata from libprs500.ebooks.metadata.pdf import get_metadata as pdf_metadata
from libprs500.ebooks.metadata.lit import get_metadata as lit_metadata from libprs500.ebooks.metadata.lit import get_metadata as lit_metadata
from libprs500.ebooks.metadata.rtf import set_metadata as set_rtf_metadata
from libprs500.ebooks.lrf.meta import set_metadata as set_lrf_metadata
from libprs500.ebooks.metadata import MetaInformation from libprs500.ebooks.metadata import MetaInformation
def get_metadata(stream, stream_type='lrf'): def get_metadata(stream, stream_type='lrf'):
if stream_type: stream_type = stream_type.lower()
if stream_type == 'rtf': if stream_type == 'rtf':
return rtf_metadata(stream) return rtf_metadata(stream)
if stream_type == 'lrf': if stream_type == 'lrf':
@ -30,3 +34,10 @@ def get_metadata(stream, stream_type='lrf'):
return lit_metadata(stream) return lit_metadata(stream)
return MetaInformation(None, None) return MetaInformation(None, None)
def set_metadata(stream, mi, stream_type='lrf'):
if stream_type: stream_type = stream_type.lower()
if stream_type == 'lrf':
set_lrf_metadata(stream, mi)
elif stream_type == 'rtf':
set_rtf_metadata(stream, mi)

View File

@ -15,7 +15,7 @@
""" """
Edit metadata in RTF files. Edit metadata in RTF files.
""" """
import re, cStringIO, sys import re, cStringIO, sys, copy
from libprs500.ebooks.metadata import MetaInformation, get_parser from libprs500.ebooks.metadata import MetaInformation, get_parser
@ -99,16 +99,16 @@ def get_metadata(stream):
def create_metadata(stream, options): def create_metadata(stream, options):
md = r'{\info' md = r'{\info'
if options.title: if options.title:
title = options.title.encode('ascii', 'replace') title = options.title.encode('ascii', 'ignore')
md += r'{\title %s}'%(title,) md += r'{\title %s}'%(title,)
if options.authors: if options.authors:
author = options.authors.encode('ascii', 'replace') author = options.authors.encode('ascii', 'ignore')
md += r'{\author %s}'%(author,) md += r'{\author %s}'%(author,)
if options.category: if options.category:
category = options.category.encode('ascii', 'replace') category = options.category.encode('ascii', 'ignore')
md += r'{\category %s}'%(category,) md += r'{\category %s}'%(category,)
if options.comment: if options.comment:
comment = options.comment.encode('ascii', 'replace') comment = options.comment.encode('ascii', 'ignore')
md += r'{\subject %s}'%(comment,) md += r'{\subject %s}'%(comment,)
if len(md) > 6: if len(md) > 6:
md += '}' md += '}'
@ -118,7 +118,13 @@ def create_metadata(stream, options):
stream.seek(0) stream.seek(0)
stream.write(ans) stream.write(ans)
def set_metadata(stream, options): def set_metadata(stream, mi):
mi = copy.deepcopy(mi)
mi.authors = ', '.join(mi.authors)
mi.comment = mi.comments
set_metadata_(stream, mi)
def set_metadata_(stream, options):
''' '''
Modify/add RTF metadata in stream Modify/add RTF metadata in stream
@param options: Object with metadata attributes title, author, comment, category @param options: Object with metadata attributes title, author, comment, category
@ -151,7 +157,7 @@ def set_metadata(stream, options):
src = add_metadata_item(src, 'subject', comment) src = add_metadata_item(src, 'subject', comment)
author = options.authors author = options.authors
if author != None: if author != None:
author = author.encode('ascii', 'replace') author = author.encode('ascii', 'ignore')
pat = re.compile(base_pat.replace('name', 'author'), re.DOTALL) pat = re.compile(base_pat.replace('name', 'author'), re.DOTALL)
if pat.search(src): if pat.search(src):
src = pat.sub(r'{\\author ' + author + r'}', src) src = pat.sub(r'{\\author ' + author + r'}', src)
@ -180,7 +186,7 @@ def main(args=sys.argv):
parser.print_help() parser.print_help()
sys.exit(1) sys.exit(1)
stream = open(args[1], 'r+b') stream = open(args[1], 'r+b')
set_metadata(stream, options) set_metadata_(stream, options)
mi = get_metadata(stream) mi = get_metadata(stream)
return mi return mi

View File

@ -12,7 +12,7 @@
## You should have received a copy of the GNU General Public License along ## You should have received a copy of the GNU General Public License along
## with this program; if not, write to the Free Software Foundation, Inc., ## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.Warning ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.Warning
import os, sys, textwrap, cStringIO, collections import os, sys, textwrap, cStringIO, collections, copy, traceback
from PyQt4.QtCore import Qt, SIGNAL, QObject, QCoreApplication, \ from PyQt4.QtCore import Qt, SIGNAL, QObject, QCoreApplication, \
QSettings, QVariant, QSize, QThread QSettings, QVariant, QSize, QThread
@ -44,6 +44,8 @@ from libprs500.gui2.dialogs.password import PasswordDialog
from libprs500.gui2.lrf_renderer.main import file_renderer from libprs500.gui2.lrf_renderer.main import file_renderer
from libprs500.gui2.lrf_renderer.main import option_parser as lrfviewerop from libprs500.gui2.lrf_renderer.main import option_parser as lrfviewerop
from libprs500.library.database import DatabaseLocked from libprs500.library.database import DatabaseLocked
from libprs500.ebooks.metadata.meta import set_metadata
from libprs500.ebooks.metadata import MetaInformation
class Main(MainWindow, Ui_MainWindow): class Main(MainWindow, Ui_MainWindow):
@ -451,6 +453,14 @@ class Main(MainWindow, Ui_MainWindow):
if f is None: if f is None:
bad.append(mi['title']) bad.append(mi['title'])
else: else:
aus = mi['authors'].split(',')
for a in copy.copy(aus):
aus += a.split('&')
try:
set_metadata(f, MetaInformation(mi['title'], aus), f.name.rpartition('.')[2])
except:
print 'Error setting metadata in book:', mi['title']
traceback.print_exc()
good.append(mi) good.append(mi)
gf.append(f) gf.append(f)
names.append('%s_%d%s'%(__appname__, id, os.path.splitext(f.name)[1])) names.append('%s_%d%s'%(__appname__, id, os.path.splitext(f.name)[1]))

View File

@ -16,9 +16,12 @@
Backend that implements storage of ebooks in an sqlite database. Backend that implements storage of ebooks in an sqlite database.
''' '''
import sqlite3 as sqlite import sqlite3 as sqlite
import datetime, re, os, cPickle import datetime, re, os, cPickle, traceback
from zlib import compress, decompress from zlib import compress, decompress
from libprs500.ebooks.metadata.meta import set_metadata
from libprs500.ebooks.metadata import MetaInformation
class Concatenate(object): class Concatenate(object):
'''String concatenation aggregator for sqlite''' '''String concatenation aggregator for sqlite'''
def __init__(self, sep=','): def __init__(self, sep=','):
@ -1113,8 +1116,18 @@ ALTER TABLE books ADD COLUMN isbn TEXT DEFAULT "" COLLATE NOCASE;
data = self.format(idx, fmt) data = self.format(idx, fmt)
name = au + ' - ' + title if byauthor else title + ' - ' + au name = au + ' - ' + title if byauthor else title + ' - ' + au
fname = name +'_'+id+'.'+fmt.lower() fname = name +'_'+id+'.'+fmt.lower()
f = open(os.path.join(tpath, fname.replace(os.sep, '_').strip()), 'wb') f = open(os.path.join(tpath, fname.replace(os.sep, '_').strip()), 'w+b')
f.write(data) f.write(data)
f.flush()
aum = self.authors(idx)
if aum: aum = aum.split(',')
mi = MetaInformation(self.title(idx), aum)
mi.author_sort = self.author_sort(idx)
try:
set_metadata(f, mi, fmt.lower())
except:
print 'Error setting metadata for book:', mi.title
traceback.print_exc()
if __name__ == '__main__': if __name__ == '__main__':