home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1365 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  8.1 KB  |  177 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 re
  8. import os
  9. import __builtin__
  10. import cherrypy
  11. from lxml import html
  12. from lxml.html.builder import HTML, HEAD, TITLE, LINK, DIV, IMG, BODY, OPTION, SELECT, INPUT, FORM, SPAN, TABLE, TR, TD, A, HR
  13. from calibre.library.server.utils import strftime
  14. from calibre.ebooks.metadata import fmt_sidx
  15. from calibre.constants import __appname__
  16. from calibre import human_readable
  17. from calibre.utils.date import utcfromtimestamp
  18.  
  19. def CLASS(*args, **kwargs):
  20.     kwargs['class'] = ' '.join(args)
  21.     return kwargs
  22.  
  23.  
  24. def build_search_box(num, search, sort, order):
  25.     div = DIV(id = 'search_box')
  26.     form = FORM('Show ', method = 'get', action = 'mobile')
  27.     div.append(form)
  28.     num_select = SELECT(name = 'num')
  29.     for option in (5, 10, 25, 100):
  30.         kwargs = {
  31.             'value': str(option) }
  32.         if option == num:
  33.             kwargs['SELECTED'] = 'SELECTED'
  34.         
  35.         num_select.append(OPTION(str(option), **kwargs))
  36.     
  37.     num_select.tail = ' books matching '
  38.     form.append(num_select)
  39.     searchf = None(INPUT = 'name', search = 'id', s = 'value' if search else '')
  40.     searchf.tail = ' sorted by '
  41.     form.append(searchf)
  42.     sort_select = SELECT(name = 'sort')
  43.     for option in ('date', 'author', 'title', 'rating', 'size', 'tags', 'series'):
  44.         kwargs = {
  45.             'value': option }
  46.         if option == sort:
  47.             kwargs['SELECTED'] = 'SELECTED'
  48.         
  49.         sort_select.append(OPTION(option, **kwargs))
  50.     
  51.     form.append(sort_select)
  52.     order_select = SELECT(name = 'order')
  53.     for option in ('ascending', 'descending'):
  54.         kwargs = {
  55.             'value': option }
  56.         if option == order:
  57.             kwargs['SELECTED'] = 'SELECTED'
  58.         
  59.         order_select.append(OPTION(option, **kwargs))
  60.     
  61.     form.append(order_select)
  62.     form.append(INPUT(id = 'go', type = 'submit', value = 'Search'))
  63.     return div
  64.  
  65.  
  66. def build_navigation(start, num, total, url_base):
  67.     end = min(start + num - 1, total)
  68.     tagline = SPAN('Books %d to %d of %d' % (start, end, total), style = 'display: block; text-align: center;')
  69.     left_buttons = TD(CLASS('button', style = 'text-align:left'))
  70.     right_buttons = TD(CLASS('button', style = 'text-align:right'))
  71.     if start > 1:
  72.         for t, s in [
  73.             ('First', 1),
  74.             ('Previous', max(start - (num + 1), 1))]:
  75.             left_buttons.append(A(t, href = '%s;start=%d' % (url_base, s)))
  76.         
  77.     
  78.     if total > start + num:
  79.         for t, s in [
  80.             ('Next', start + num),
  81.             ('Last', (total - num) + 1)]:
  82.             right_buttons.append(A(t, href = '%s;start=%d' % (url_base, s)))
  83.         
  84.     
  85.     buttons = TABLE(TR(left_buttons, right_buttons), CLASS('buttons'))
  86.     return DIV(tagline, buttons, CLASS('navigation'))
  87.  
  88.  
  89. def build_index(books, num, search, sort, order, start, total, url_base):
  90.     logo = DIV(IMG(src = '/static/calibre.png', alt = __appname__), id = 'logo')
  91.     search_box = build_search_box(num, search, sort, order)
  92.     navigation = build_navigation(start, num, total, url_base)
  93.     bookt = TABLE(id = 'listing')
  94.     body = BODY(logo, search_box, navigation, HR(CLASS('spacer')), bookt)
  95.     for book in books:
  96.         thumbnail = TD(IMG(type = 'image/jpeg', border = '0', src = '/get/thumb/%s' % book['id']), CLASS('thumbnail'))
  97.         data = TD()
  98.         last = None
  99.         for fmt in book['formats'].split(','):
  100.             s = SPAN(A(fmt.lower(), href = '/get/%s/%s-%s_%d.%s' % (fmt, book['authors'], book['title'], book['id'], fmt)), CLASS('button'))
  101.             s.tail = u'ΓÇ»'
  102.             last = s
  103.             data.append(s)
  104.         
  105.         series = None if book['series'] else ''
  106.         tags = None if book['tags'] else ''
  107.         text = u'ΓÇ»%s %s by %s - %s - %s %s' % (book['title'], series, book['authors'], book['size'], book['timestamp'], tags)
  108.         bookt.append(TR(thumbnail, data))
  109.     
  110.     return HTML(HEAD(TITLE(__appname__ + ' Library'), LINK(rel = 'icon', href = 'http://calibre-ebook.com/favicon.ico', type = 'image/x-icon'), LINK(rel = 'stylesheet', type = 'text/css', href = '/mobile/style.css')), body)
  111.  
  112.  
  113. class MobileServer(object):
  114.     MOBILE_UA = re.compile('(?i)(?:iPhone|Opera Mini|NetFront|webOS|Mobile|Android|imode|DoCoMo|Minimo|Blackberry|MIDP|Symbian|HD2)')
  115.     
  116.     def add_routes(self, connect):
  117.         connect('mobile', '/mobile', self.mobile)
  118.         connect('mobile_css', '/mobile/style.css', self.mobile_css)
  119.  
  120.     
  121.     def mobile_css(self, *args, **kwargs):
  122.         path = P('content_server/mobile.css')
  123.         cherrypy.response.headers['Content-Type'] = 'text/css; charset=utf-8'
  124.         updated = utcfromtimestamp(os.stat(path).st_mtime)
  125.         cherrypy.response.headers['Last-Modified'] = self.last_modified(updated)
  126.         return open(path, 'rb').read()
  127.  
  128.     
  129.     def mobile(self, start = '1', num = '25', sort = 'date', search = '', _ = None, order = 'descending'):
  130.         
  131.         try:
  132.             start = int(start)
  133.         except ValueError:
  134.             raise cherrypy.HTTPError(400, 'start: %s is not an integer' % start)
  135.  
  136.         
  137.         try:
  138.             num = int(num)
  139.         except ValueError:
  140.             raise cherrypy.HTTPError(400, 'num: %s is not an integer' % num)
  141.  
  142.         ids = None if search and search.strip() else self.db.data.universal_set()
  143.         FM = self.db.FIELD_MAP
  144.         items = _[1]
  145.         books = []
  146.         for record in items[start - 1:(start - 1) + num]:
  147.             book = {
  148.                 'formats': record[FM['formats']],
  149.                 'size': record[FM['size']] }
  150.             if not book['formats']:
  151.                 book['formats'] = ''
  152.             
  153.             if not book['size']:
  154.                 book['size'] = 0
  155.             
  156.             book['size'] = human_readable(book['size'])
  157.             aus = None if record[FM['authors']] else __builtin__._('Unknown')
  158.             authors = []([ i.replace('|', ',') for i in aus.split(',') ])
  159.             book['authors'] = authors
  160.             book['series_index'] = fmt_sidx(float(record[FM['series_index']]))
  161.             book['series'] = record[FM['series']]
  162.             book['tags'] = record[FM['tags']]
  163.             book['title'] = record[FM['title']]
  164.             for x in ('timestamp', 'pubdate'):
  165.                 book[x] = strftime('%Y/%m/%d %H:%M:%S', record[FM[x]])
  166.             
  167.             book['id'] = record[FM['id']]
  168.             books.append(book)
  169.         
  170.         updated = self.db.last_modified()
  171.         cherrypy.response.headers['Content-Type'] = 'text/html; charset=utf-8'
  172.         cherrypy.response.headers['Last-Modified'] = self.last_modified(updated)
  173.         url_base = '/mobile?search=' + search + ';order=' + order + ';sort=' + sort + ';num=' + str(num)
  174.         return html.tostring(build_index(books, num, search, sort, order, start, len(ids), url_base), encoding = 'utf-8', include_meta_content_type = True, pretty_print = True)
  175.  
  176.  
  177.