Fix parsing of device book list on computers with a non English locale.

This commit is contained in:
Kovid Goyal 2007-10-13 00:07:35 +00:00
parent d26b3383ac
commit c1b6d4c136
2 changed files with 23 additions and 4 deletions

View File

@ -15,3 +15,22 @@
'''
Device drivers.
'''
import time
DAY_MAP = dict(Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6)
MONTH_MAP = dict(Jan=1, Feb=2, Mar=3, Apr=4, May=5, Jun=6, Jul=7, Aug=8, Sep=9, Oct=10, Nov=11, Dec=12)
INVERSE_DAY_MAP = dict(zip(DAY_MAP.values(), DAY_MAP.keys()))
INVERSE_MONTH_MAP = dict(zip(MONTH_MAP.values(), MONTH_MAP.keys()))
def strptime(src):
src = src.strip()
src = src.split()
src[0] = str(DAY_MAP[src[0][:-1]])+','
src[2] = str(MONTH_MAP[src[2]])
return time.strptime(' '.join(src), '%w, %d %m %Y %H:%M:%S %Z')
def strftime(epoch):
src = time.strftime("%w, %d %m %Y %H:%M:%S GMT", time.gmtime(epoch)).split()
src[0] = INVERSE_DAY_MAP[int(src[0][:-1])]+','
src[2] = INVERSE_MONTH_MAP[int(src[2])]
return ' '.join(src)

View File

@ -19,9 +19,10 @@ in the reader cache.
import xml.dom.minidom as dom
from base64 import b64decode as decode
from base64 import b64encode as encode
import time, re
import re
from libprs500.devices.interface import BookList as _BookList
from libprs500.devices import strftime, strptime
MIME_MAP = { \
"lrf":"application/x-sony-bbeb", \
@ -54,6 +55,7 @@ class book_metadata_field(object):
class Book(object):
""" Provides a view onto the XML element that represents a book """
title = book_metadata_field("title")
authors = book_metadata_field("author", \
formatter=lambda x: x if x and x.strip() else "Unknown")
@ -63,9 +65,7 @@ class Book(object):
sourceid = book_metadata_field("sourceid", formatter=int)
size = book_metadata_field("size", formatter=int)
# When setting this attribute you must use an epoch
datetime = book_metadata_field("date", \
formatter=lambda x: time.strptime(x.strip(), "%a, %d %b %Y %H:%M:%S %Z"),
setter=lambda x: time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(x)))
datetime = book_metadata_field("date", formatter=strptime, setter=strftime)
@apply
def title_sorter():
doc = '''String to sort the title. If absent, title is returned'''