mirror of
				https://github.com/kovidgoyal/calibre.git
				synced 2025-10-31 10:37:00 -04:00 
			
		
		
		
	Fix #275
This commit is contained in:
		
							parent
							
								
									620bbb04c7
								
							
						
					
					
						commit
						85718558aa
					
				| @ -614,6 +614,19 @@ def option_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): | ||||
|     import os.path | ||||
|  | ||||
| @ -14,12 +14,16 @@ | ||||
| ##    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||||
| 
 | ||||
| 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.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 | ||||
| 
 | ||||
| def get_metadata(stream, stream_type='lrf'): | ||||
|     if stream_type: stream_type = stream_type.lower() | ||||
|     if stream_type == 'rtf': | ||||
|         return rtf_metadata(stream) | ||||
|     if stream_type == 'lrf': | ||||
| @ -29,4 +33,11 @@ def get_metadata(stream, stream_type='lrf'): | ||||
|     if stream_type == 'lit': | ||||
|         return lit_metadata(stream) | ||||
|     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) | ||||
|      | ||||
|  | ||||
| @ -15,7 +15,7 @@ | ||||
| """ | ||||
| Edit metadata in RTF files. | ||||
| """ | ||||
| import re, cStringIO, sys | ||||
| import re, cStringIO, sys, copy | ||||
| 
 | ||||
| from libprs500.ebooks.metadata import MetaInformation, get_parser | ||||
| 
 | ||||
| @ -99,16 +99,16 @@ def get_metadata(stream): | ||||
| def create_metadata(stream, options): | ||||
|     md = r'{\info' | ||||
|     if options.title: | ||||
|         title = options.title.encode('ascii', 'replace') | ||||
|         title = options.title.encode('ascii', 'ignore') | ||||
|         md += r'{\title %s}'%(title,) | ||||
|     if options.authors: | ||||
|         author = options.authors.encode('ascii', 'replace') | ||||
|         author = options.authors.encode('ascii', 'ignore') | ||||
|         md += r'{\author %s}'%(author,) | ||||
|     if options.category: | ||||
|         category = options.category.encode('ascii', 'replace') | ||||
|         category = options.category.encode('ascii', 'ignore') | ||||
|         md += r'{\category %s}'%(category,) | ||||
|     if options.comment: | ||||
|         comment = options.comment.encode('ascii', 'replace') | ||||
|         comment = options.comment.encode('ascii', 'ignore') | ||||
|         md += r'{\subject %s}'%(comment,) | ||||
|     if len(md) > 6: | ||||
|         md += '}' | ||||
| @ -118,7 +118,13 @@ def create_metadata(stream, options): | ||||
|         stream.seek(0) | ||||
|         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 | ||||
|     @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) | ||||
|         author = options.authors | ||||
|         if author != None: | ||||
|             author = author.encode('ascii', 'replace') | ||||
|             author = author.encode('ascii', 'ignore') | ||||
|             pat = re.compile(base_pat.replace('name', 'author'), re.DOTALL)         | ||||
|             if pat.search(src): | ||||
|                 src = pat.sub(r'{\\author ' + author + r'}', src) | ||||
| @ -180,7 +186,7 @@ def main(args=sys.argv): | ||||
|         parser.print_help() | ||||
|         sys.exit(1) | ||||
|     stream = open(args[1], 'r+b') | ||||
|     set_metadata(stream, options) | ||||
|     set_metadata_(stream, options) | ||||
|     mi = get_metadata(stream) | ||||
|     return mi | ||||
| 
 | ||||
|  | ||||
| @ -12,7 +12,7 @@ | ||||
| ##    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., | ||||
| ##    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, \ | ||||
|                          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 option_parser as lrfviewerop | ||||
| 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): | ||||
| @ -451,6 +453,14 @@ class Main(MainWindow, Ui_MainWindow): | ||||
|             if f is None: | ||||
|                 bad.append(mi['title']) | ||||
|             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) | ||||
|                 gf.append(f) | ||||
|                 names.append('%s_%d%s'%(__appname__, id, os.path.splitext(f.name)[1])) | ||||
|  | ||||
| @ -16,9 +16,12 @@ | ||||
| Backend that implements storage of ebooks in an sqlite database. | ||||
| ''' | ||||
| import sqlite3 as sqlite | ||||
| import datetime, re, os, cPickle | ||||
| import datetime, re, os, cPickle, traceback | ||||
| from zlib import compress, decompress | ||||
| 
 | ||||
| from libprs500.ebooks.metadata.meta import set_metadata | ||||
| from libprs500.ebooks.metadata import MetaInformation | ||||
| 
 | ||||
| class Concatenate(object): | ||||
|     '''String concatenation aggregator for sqlite''' | ||||
|     def __init__(self, sep=','): | ||||
| @ -1113,8 +1116,18 @@ ALTER TABLE books ADD COLUMN isbn TEXT DEFAULT "" COLLATE NOCASE; | ||||
|                     data = self.format(idx, fmt) | ||||
|                     name = au + ' - ' + title if byauthor else title + ' - ' + au | ||||
|                     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.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__': | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user