From 3bab77322efa757a84e466fce06b685678a07ae4 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 2 Jun 2007 19:42:53 +0000 Subject: [PATCH] Support for editing RTF metadata and documentation updates. --- src/libprs500/__init__.py | 2 +- src/libprs500/devices/prs500/prstypes.py | 3 +- src/libprs500/ebooks/lrf/__init__.py | 8 +-- src/libprs500/ebooks/metadata/__init__.py | 18 ++++++ src/libprs500/ebooks/metadata/rtf.py | 75 ++++++++++++++++++++--- 5 files changed, 90 insertions(+), 16 deletions(-) diff --git a/src/libprs500/__init__.py b/src/libprs500/__init__.py index af5c1e23e6..7680e1f161 100644 --- a/src/libprs500/__init__.py +++ b/src/libprs500/__init__.py @@ -13,7 +13,7 @@ ## with this program; if not, write to the Free Software Foundation, Inc., ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ''' E-book management software''' -__version__ = "0.3.44" +__version__ = "0.3.45" __docformat__ = "epytext" __author__ = "Kovid Goyal " diff --git a/src/libprs500/devices/prs500/prstypes.py b/src/libprs500/devices/prs500/prstypes.py index ac24728f34..b30adb9235 100755 --- a/src/libprs500/devices/prs500/prstypes.py +++ b/src/libprs500/devices/prs500/prstypes.py @@ -95,7 +95,8 @@ class TransferBuffer(list): """ Return a string representation of this buffer. - Packets are represented as hex strings, in 2-byte pairs, S{<=} 16 bytes to a line. An ASCII representation is included. For example:: + Packets are represented as hex strings, in 2-byte pairs, S{<=} 16 bytes to a line. + An ASCII representation is included. For example:: 0700 0100 0000 0000 0000 0000 0c00 0000 ................ 0200 0000 0400 0000 4461 7461 ........Data """ diff --git a/src/libprs500/ebooks/lrf/__init__.py b/src/libprs500/ebooks/lrf/__init__.py index 2cd385967e..8e8e041581 100644 --- a/src/libprs500/ebooks/lrf/__init__.py +++ b/src/libprs500/ebooks/lrf/__init__.py @@ -13,8 +13,8 @@ ## with this program; if not, write to the Free Software Foundation, Inc., ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. """ -This package contains logic to read and write LRF files. The LRF file format is documented at U{http://www.sven.de/librie/Librie/LrfFormat}. -At the time fo writing, this package only supports reading and writing LRF meat information. See L{meta}. +This package contains logic to read and write LRF files. +The LRF file format is documented at U{http://www.sven.de/librie/Librie/LrfFormat}. """ from optparse import OptionParser, OptionValueError @@ -53,8 +53,8 @@ def option_parser(usage): dest="title", help="Set the title. Default: filename.") metadata.add_option("-a", "--author", action="store", type="string", \ dest="author", help="Set the author. Default: %default", default='Unknown') - metadata.add_option("--freetext", action="store", type="string", \ - dest="freetext", help="Set the comments.", default=' ') + metadata.add_option("--comment", action="store", type="string", \ + dest="freetext", help="Set the comment.", default=' ') metadata.add_option("--category", action="store", type="string", \ dest="category", help="Set the category", default=' ') metadata.add_option('--title-sort', action='store', default='', dest='title_sort', diff --git a/src/libprs500/ebooks/metadata/__init__.py b/src/libprs500/ebooks/metadata/__init__.py index 4d4f9d4dfb..5f31a481e5 100644 --- a/src/libprs500/ebooks/metadata/__init__.py +++ b/src/libprs500/ebooks/metadata/__init__.py @@ -19,8 +19,26 @@ the L{libprs500.lrf.meta} module. __docformat__ = "epytext" __author__ = "Kovid Goyal " +from optparse import OptionParser +from libprs500 import __version__ as VERSION + +def get_parser(extension): + ''' Return an option parser with the basic metadata options already setup''' + parser = OptionParser(version='libprs500 version: '+VERSION, + usage='''%prog [options] myfile.'''+extension) + parser.add_option("-t", "--title", action="store", type="string", \ + dest="title", help="Set the book title") + parser.add_option("-a", "--authors", action="store", type="string", \ + dest="authors", help="Set the authors", default=None) + parser.add_option("-c", "--category", action="store", type="string", \ + dest="category", help="The category this book belongs"+\ + " to. E.g.: History", default=None) + parser.add_option('--comment', dest='comment', default=None, action='store', + help='Set the comment') + return parser class MetaInformation(object): + '''Convenient encapsulation of book metadata''' def __init__(self, title, author): self.title = title diff --git a/src/libprs500/ebooks/metadata/rtf.py b/src/libprs500/ebooks/metadata/rtf.py index 37527a5920..a1b264ecca 100644 --- a/src/libprs500/ebooks/metadata/rtf.py +++ b/src/libprs500/ebooks/metadata/rtf.py @@ -13,11 +13,11 @@ ## with this program; if not, write to the Free Software Foundation, Inc., ## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. """ -Read metadata from RTF files. +Edit metadata in RTF files. """ -import re, cStringIO +import re, cStringIO, sys -from libprs500.ebooks.metadata import MetaInformation +from libprs500.ebooks.metadata import MetaInformation, get_parser title_pat = re.compile(r'\{\\info.*?\{\\title(.*?)(?> sys.stderr, "Usage:", sys.argv[0], " mybook.rtf" +def set_metadata(stream, options): + ''' + Modify/add RTF metadata in stream + @param options: Object with metadata attributes title, author, comment, category + ''' + def add_metadata_item(src, name, val): + index = src.rindex('}') + return src[:index] + r'{\ '[:-1] + name + ' ' + val + '}}' + src, pos = get_document_info(stream) + olen = len(src) + base_pat = r'\{\\name(.*?)(?