From 7969c9ead533418dfced6e439dfd4eed53fc6868 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Sat, 22 May 2010 08:50:22 +0100 Subject: [PATCH] Back out metadata/json changes I made --- src/calibre/devices/metadata_serializer.py | 90 ---------------------- src/calibre/devices/usbms/books.py | 25 +++++- src/calibre/devices/usbms/driver.py | 7 +- 3 files changed, 26 insertions(+), 96 deletions(-) delete mode 100644 src/calibre/devices/metadata_serializer.py diff --git a/src/calibre/devices/metadata_serializer.py b/src/calibre/devices/metadata_serializer.py deleted file mode 100644 index 651ba1d678..0000000000 --- a/src/calibre/devices/metadata_serializer.py +++ /dev/null @@ -1,90 +0,0 @@ -''' -Created on 21 May 2010 - -@author: charles -''' - -from calibre.constants import filesystem_encoding, preferred_encoding -from calibre import isbytestring -import json - -class MetadataSerializer(object): - - SERIALIZED_ATTRS = [ - 'lpath', 'title', 'authors', 'mime', 'size', 'tags', 'author_sort', - 'title_sort', 'comments', 'category', 'publisher', 'series', - 'series_index', 'rating', 'isbn', 'language', 'application_id', - 'book_producer', 'lccn', 'lcc', 'ddc', 'rights', 'publication_type', - 'uuid', - ] - - def to_json(self): - json = {} - for attr in self.SERIALIZED_ATTRS: - val = getattr(self, attr) - if isbytestring(val): - enc = filesystem_encoding if attr == 'lpath' else preferred_encoding - val = val.decode(enc, 'replace') - elif isinstance(val, (list, tuple)): - val = [x.decode(preferred_encoding, 'replace') if - isbytestring(x) else x for x in val] - json[attr] = val - return json - - def read_json(self, cache_file): - with open(cache_file, 'rb') as f: - js = json.load(f, encoding='utf-8') - return js - - def write_json(self, js, cache_file): - with open(cache_file, 'wb') as f: - json.dump(js, f, indent=2, encoding='utf-8') - - def string_to_value(self, string, col_metadata, column_label=None): - ''' - if column_label is none, col_metadata must be a dict containing custom - column metadata for one column. If column_label is not none, then - col_metadata must be a dict of custom column metadata, with column - labels as keys. Metadata for standard columns is always assumed to be in - the col_metadata dict. If column_label is not standard and is not in - col_metadata, check if it matches a custom column. If so, use that - column metadata. See get_column_metadata below. - ''' - pass - - def value_to_display(self, value, col_metadata, column_label=None): - pass - - def value_to_string (self, value, col_metadata, column_label=None): - pass - - def get_column_metadata(self, column_label = None, from_book=None): - ''' - if column_label is None, then from_book must not be None. Returns the - complete set of custom column metadata for that book. - - If column_label is not None, return the column metadata for the given - column. This works even if the label is for a built-in column. If - from_book is None, then column_label must be a current custom column - label or a standard label. If from_book is not None, then the column - metadata from that metadata set is returned if it exists, otherwise the - standard metadata for that column is returned. If neither is found, - return {} - ''' - pass - - def get_custom_column_labels(self, book): - ''' - returns a list of custom column attributes in the book metadata. - ''' - pass - - def get_standard_column_labels(self): - ''' - returns a list of standard attributes that should be in any book's - metadata - ''' - pass - -metadata_serializer = MetadataSerializer() - diff --git a/src/calibre/devices/usbms/books.py b/src/calibre/devices/usbms/books.py index 8d79981ad7..6e8811432a 100644 --- a/src/calibre/devices/usbms/books.py +++ b/src/calibre/devices/usbms/books.py @@ -9,14 +9,20 @@ import os, re, time, sys from calibre.ebooks.metadata import MetaInformation from calibre.devices.mime import mime_type_ext from calibre.devices.interface import BookList as _BookList -from calibre.devices.metadata_serializer import MetadataSerializer -from calibre.constants import preferred_encoding +from calibre.constants import filesystem_encoding, preferred_encoding from calibre import isbytestring -class Book(MetaInformation, MetadataSerializer): +class Book(MetaInformation): BOOK_ATTRS = ['lpath', 'size', 'mime', 'device_collections'] + JSON_ATTRS = [ + 'lpath', 'title', 'authors', 'mime', 'size', 'tags', 'author_sort', + 'title_sort', 'comments', 'category', 'publisher', 'series', + 'series_index', 'rating', 'isbn', 'language', 'application_id', + 'book_producer', 'lccn', 'lcc', 'ddc', 'rights', 'publication_type', + 'uuid', + ] def __init__(self, prefix, lpath, size=None, other=None): from calibre.ebooks.metadata.meta import path_to_ext @@ -76,6 +82,19 @@ class Book(MetaInformation, MetadataSerializer): val = getattr(other, attr, None) setattr(self, attr, val) + def to_json(self): + json = {} + for attr in self.JSON_ATTRS: + val = getattr(self, attr) + if isbytestring(val): + enc = filesystem_encoding if attr == 'lpath' else preferred_encoding + val = val.decode(enc, 'replace') + elif isinstance(val, (list, tuple)): + val = [x.decode(preferred_encoding, 'replace') if + isbytestring(x) else x for x in val] + json[attr] = val + return json + class BookList(_BookList): def supports_collections(self): diff --git a/src/calibre/devices/usbms/driver.py b/src/calibre/devices/usbms/driver.py index 3c30827dbc..97c212775a 100644 --- a/src/calibre/devices/usbms/driver.py +++ b/src/calibre/devices/usbms/driver.py @@ -17,7 +17,6 @@ from itertools import cycle from calibre import prints, isbytestring from calibre.constants import filesystem_encoding -from calibre.devices.metadata_serializer import metadata_serializer as ms from calibre.devices.usbms.cli import CLI from calibre.devices.usbms.device import Device from calibre.devices.usbms.books import BookList, Book @@ -261,7 +260,8 @@ class USBMS(CLI, Device): os.makedirs(self.normalize_path(prefix)) js = [item.to_json() for item in booklists[listid] if hasattr(item, 'to_json')] - ms.write_json(js, self.normalize_path(os.path.join(prefix, self.METADATA_CACHE))) + with open(self.normalize_path(os.path.join(prefix, self.METADATA_CACHE)), 'wb') as f: + json.dump(js, f, indent=2, encoding='utf-8') write_prefix(self._main_prefix, 0) write_prefix(self._card_a_prefix, 1) write_prefix(self._card_b_prefix, 2) @@ -293,7 +293,8 @@ class USBMS(CLI, Device): cache_file = cls.normalize_path(os.path.join(prefix, name)) if os.access(cache_file, os.R_OK): try: - js = ms.read_json(cache_file) + with open(cache_file, 'rb') as f: + js = json.load(f, encoding='utf-8') for item in js: book = cls.book_class(prefix, item.get('lpath', None)) for key in item.keys():