mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Back out metadata/json changes I made
This commit is contained in:
parent
064bda5feb
commit
7969c9ead5
@ -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()
|
|
||||||
|
|
@ -9,14 +9,20 @@ import os, re, time, sys
|
|||||||
from calibre.ebooks.metadata import MetaInformation
|
from calibre.ebooks.metadata import MetaInformation
|
||||||
from calibre.devices.mime import mime_type_ext
|
from calibre.devices.mime import mime_type_ext
|
||||||
from calibre.devices.interface import BookList as _BookList
|
from calibre.devices.interface import BookList as _BookList
|
||||||
from calibre.devices.metadata_serializer import MetadataSerializer
|
from calibre.constants import filesystem_encoding, preferred_encoding
|
||||||
from calibre.constants import preferred_encoding
|
|
||||||
from calibre import isbytestring
|
from calibre import isbytestring
|
||||||
|
|
||||||
class Book(MetaInformation, MetadataSerializer):
|
class Book(MetaInformation):
|
||||||
|
|
||||||
BOOK_ATTRS = ['lpath', 'size', 'mime', 'device_collections']
|
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):
|
def __init__(self, prefix, lpath, size=None, other=None):
|
||||||
from calibre.ebooks.metadata.meta import path_to_ext
|
from calibre.ebooks.metadata.meta import path_to_ext
|
||||||
@ -76,6 +82,19 @@ class Book(MetaInformation, MetadataSerializer):
|
|||||||
val = getattr(other, attr, None)
|
val = getattr(other, attr, None)
|
||||||
setattr(self, attr, val)
|
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):
|
class BookList(_BookList):
|
||||||
|
|
||||||
def supports_collections(self):
|
def supports_collections(self):
|
||||||
|
@ -17,7 +17,6 @@ from itertools import cycle
|
|||||||
|
|
||||||
from calibre import prints, isbytestring
|
from calibre import prints, isbytestring
|
||||||
from calibre.constants import filesystem_encoding
|
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.cli import CLI
|
||||||
from calibre.devices.usbms.device import Device
|
from calibre.devices.usbms.device import Device
|
||||||
from calibre.devices.usbms.books import BookList, Book
|
from calibre.devices.usbms.books import BookList, Book
|
||||||
@ -261,7 +260,8 @@ class USBMS(CLI, Device):
|
|||||||
os.makedirs(self.normalize_path(prefix))
|
os.makedirs(self.normalize_path(prefix))
|
||||||
js = [item.to_json() for item in booklists[listid] if
|
js = [item.to_json() for item in booklists[listid] if
|
||||||
hasattr(item, 'to_json')]
|
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._main_prefix, 0)
|
||||||
write_prefix(self._card_a_prefix, 1)
|
write_prefix(self._card_a_prefix, 1)
|
||||||
write_prefix(self._card_b_prefix, 2)
|
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))
|
cache_file = cls.normalize_path(os.path.join(prefix, name))
|
||||||
if os.access(cache_file, os.R_OK):
|
if os.access(cache_file, os.R_OK):
|
||||||
try:
|
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:
|
for item in js:
|
||||||
book = cls.book_class(prefix, item.get('lpath', None))
|
book = cls.book_class(prefix, item.get('lpath', None))
|
||||||
for key in item.keys():
|
for key in item.keys():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user