home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1368 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.6 KB  |  76 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
  6. __docformat__ = 'restructuredtext en'
  7. import __builtin__
  8. import cherrypy
  9. from lxml.builder import ElementMaker
  10. from lxml import etree
  11. from calibre.library.server.utils import strftime
  12. from calibre.ebooks.metadata import fmt_sidx
  13. from calibre.constants import preferred_encoding
  14. from calibre import isbytestring
  15. E = ElementMaker()
  16.  
  17. class XMLServer(object):
  18.     
  19.     def add_routes(self, connect):
  20.         connect('xml', '/xml', self.xml)
  21.  
  22.     
  23.     def xml(self, start = '0', num = '50', sort = None, search = None, _ = None, order = 'ascending'):
  24.         
  25.         try:
  26.             start = int(start)
  27.         except ValueError:
  28.             raise cherrypy.HTTPError(400, 'start: %s is not an integer' % start)
  29.  
  30.         
  31.         try:
  32.             num = int(num)
  33.         except ValueError:
  34.             raise cherrypy.HTTPError(400, 'num: %s is not an integer' % num)
  35.  
  36.         order = order.lower().strip() == 'ascending'
  37.         ids = None if search and search.strip() else self.db.data.universal_set()
  38.         FM = self.db.FIELD_MAP
  39.         items = _[1]
  40.         books = []
  41.         
  42.         def serialize(x):
  43.             if isinstance(x, unicode):
  44.                 return x
  45.             if isbytestring(x):
  46.                 return x.decode(preferred_encoding, 'replace')
  47.             return unicode(x)
  48.  
  49.         for record in items[start:start + num]:
  50.             kwargs = { }
  51.             aus = None if sort is not None else [] if record[FM['authors']] else __builtin__._('Unknown')
  52.             authors = []([ i.replace('|', ',') for i in aus.split(',') ])
  53.             kwargs['authors'] = authors
  54.             kwargs['series_index'] = fmt_sidx(float(record[FM['series_index']]))
  55.             for x in ('timestamp', 'pubdate'):
  56.                 kwargs[x] = strftime('%Y/%m/%d %H:%M:%S', record[FM[x]])
  57.             
  58.             for x in ('id', 'title', 'sort', 'author_sort', 'rating', 'size'):
  59.                 kwargs[x] = serialize(record[FM[x]])
  60.             
  61.             for x in ('isbn', 'formats', 'series', 'tags', 'publisher', 'comments'):
  62.                 y = record[FM[x]]
  63.                 kwargs[x] = [] if y else ''
  64.             
  65.             c = kwargs.pop('comments')
  66.             books.append(E.book(c, **kwargs))
  67.         
  68.         updated = self.db.last_modified()
  69.         kwargs = dict(start = str(start), updated = updated.strftime('%Y-%m-%dT%H:%M:%S+00:00'), total = str(len(ids)), num = str(len(books)))
  70.         ans = E.library(*books, **kwargs)
  71.         cherrypy.response.headers['Content-Type'] = 'text/xml'
  72.         cherrypy.response.headers['Last-Modified'] = self.last_modified(updated)
  73.         return etree.tostring(ans, encoding = 'utf-8', pretty_print = True, xml_declaration = True)
  74.  
  75.  
  76.