home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1415 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  13.6 KB  |  606 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from calibre.utils.ordered_dict import OrderedDict
  5. from calibre.utils.config import tweaks
  6.  
  7. class TagsIcons(dict):
  8.     category_icons = [
  9.         'authors',
  10.         'series',
  11.         'formats',
  12.         'publisher',
  13.         'rating',
  14.         'news',
  15.         'tags',
  16.         ':custom',
  17.         ':user',
  18.         'search']
  19.     
  20.     def __init__(self, icon_dict):
  21.         for a in self.category_icons:
  22.             if a not in icon_dict:
  23.                 raise ValueError('Missing category icon [%s]' % a)
  24.             a not in icon_dict
  25.             self[a] = icon_dict[a]
  26.         
  27.  
  28.  
  29. category_icon_map = {
  30.     'authors': 'user_profile.png',
  31.     'series': 'series.png',
  32.     'formats': 'book.png',
  33.     'publisher': 'publisher.png',
  34.     'rating': 'rating.png',
  35.     'news': 'news.png',
  36.     'tags': 'tags.png',
  37.     ':custom': 'column.png',
  38.     ':user': 'drawer.png',
  39.     'search': 'search.png' }
  40.  
  41. class FieldMetadata(dict):
  42.     VALID_DATA_TYPES = frozenset([
  43.         None,
  44.         'rating',
  45.         'text',
  46.         'comments',
  47.         'datetime',
  48.         'int',
  49.         'float',
  50.         'bool',
  51.         'series',
  52.         'composite'])
  53.     _field_metadata = [
  54.         ('authors', {
  55.             'table': 'authors',
  56.             'column': 'name',
  57.             'link_column': 'author',
  58.             'category_sort': 'sort',
  59.             'datatype': 'text',
  60.             'is_multiple': ',',
  61.             'kind': 'field',
  62.             'name': _('Authors'),
  63.             'search_terms': [
  64.                 'authors',
  65.                 'author'],
  66.             'is_custom': False,
  67.             'is_category': True }),
  68.         ('series', {
  69.             'table': 'series',
  70.             'column': 'name',
  71.             'link_column': 'series',
  72.             'category_sort': '(title_sort(name))',
  73.             'datatype': 'series',
  74.             'is_multiple': None,
  75.             'kind': 'field',
  76.             'name': _('Series'),
  77.             'search_terms': [
  78.                 'series'],
  79.             'is_custom': False,
  80.             'is_category': True }),
  81.         ('formats', {
  82.             'table': None,
  83.             'column': None,
  84.             'datatype': 'text',
  85.             'is_multiple': ',',
  86.             'kind': 'field',
  87.             'name': _('Formats'),
  88.             'search_terms': [
  89.                 'formats',
  90.                 'format'],
  91.             'is_custom': False,
  92.             'is_category': True }),
  93.         ('publisher', {
  94.             'table': 'publishers',
  95.             'column': 'name',
  96.             'link_column': 'publisher',
  97.             'category_sort': 'name',
  98.             'datatype': 'text',
  99.             'is_multiple': None,
  100.             'kind': 'field',
  101.             'name': _('Publishers'),
  102.             'search_terms': [
  103.                 'publisher'],
  104.             'is_custom': False,
  105.             'is_category': True }),
  106.         ('rating', {
  107.             'table': 'ratings',
  108.             'column': 'rating',
  109.             'link_column': 'rating',
  110.             'category_sort': 'rating',
  111.             'datatype': 'rating',
  112.             'is_multiple': None,
  113.             'kind': 'field',
  114.             'name': _('Ratings'),
  115.             'search_terms': [
  116.                 'rating'],
  117.             'is_custom': False,
  118.             'is_category': True }),
  119.         ('news', {
  120.             'table': 'news',
  121.             'column': 'name',
  122.             'category_sort': 'name',
  123.             'datatype': None,
  124.             'is_multiple': None,
  125.             'kind': 'category',
  126.             'name': _('News'),
  127.             'search_terms': [],
  128.             'is_custom': False,
  129.             'is_category': True }),
  130.         ('tags', {
  131.             'table': 'tags',
  132.             'column': 'name',
  133.             'link_column': 'tag',
  134.             'category_sort': 'name',
  135.             'datatype': 'text',
  136.             'is_multiple': ',',
  137.             'kind': 'field',
  138.             'name': _('Tags'),
  139.             'search_terms': [
  140.                 'tags',
  141.                 'tag'],
  142.             'is_custom': False,
  143.             'is_category': True }),
  144.         ('author_sort', {
  145.             'table': None,
  146.             'column': None,
  147.             'datatype': 'text',
  148.             'is_multiple': None,
  149.             'kind': 'field',
  150.             'name': None,
  151.             'search_terms': [],
  152.             'is_custom': False,
  153.             'is_category': False }),
  154.         ('comments', {
  155.             'table': None,
  156.             'column': None,
  157.             'datatype': 'text',
  158.             'is_multiple': None,
  159.             'kind': 'field',
  160.             'name': _('Comments'),
  161.             'search_terms': [
  162.                 'comments',
  163.                 'comment'],
  164.             'is_custom': False,
  165.             'is_category': False }),
  166.         ('cover', {
  167.             'table': None,
  168.             'column': None,
  169.             'datatype': None,
  170.             'is_multiple': None,
  171.             'kind': 'field',
  172.             'name': None,
  173.             'search_terms': [
  174.                 'cover'],
  175.             'is_custom': False,
  176.             'is_category': False }),
  177.         ('flags', {
  178.             'table': None,
  179.             'column': None,
  180.             'datatype': 'text',
  181.             'is_multiple': None,
  182.             'kind': 'field',
  183.             'name': None,
  184.             'search_terms': [],
  185.             'is_custom': False,
  186.             'is_category': False }),
  187.         ('id', {
  188.             'table': None,
  189.             'column': None,
  190.             'datatype': 'int',
  191.             'is_multiple': None,
  192.             'kind': 'field',
  193.             'name': None,
  194.             'search_terms': [],
  195.             'is_custom': False,
  196.             'is_category': False }),
  197.         ('isbn', {
  198.             'table': None,
  199.             'column': None,
  200.             'datatype': 'text',
  201.             'is_multiple': None,
  202.             'kind': 'field',
  203.             'name': None,
  204.             'search_terms': [
  205.                 'isbn'],
  206.             'is_custom': False,
  207.             'is_category': False }),
  208.         ('lccn', {
  209.             'table': None,
  210.             'column': None,
  211.             'datatype': 'text',
  212.             'is_multiple': None,
  213.             'kind': 'field',
  214.             'name': None,
  215.             'search_terms': [],
  216.             'is_custom': False,
  217.             'is_category': False }),
  218.         ('all_metadata', {
  219.             'table': None,
  220.             'column': None,
  221.             'datatype': None,
  222.             'is_multiple': None,
  223.             'kind': 'field',
  224.             'name': None,
  225.             'search_terms': [],
  226.             'is_custom': False,
  227.             'is_category': False }),
  228.         ('ondevice', {
  229.             'table': None,
  230.             'column': None,
  231.             'datatype': 'text',
  232.             'is_multiple': None,
  233.             'kind': 'field',
  234.             'name': _('On Device'),
  235.             'search_terms': [
  236.                 'ondevice'],
  237.             'is_custom': False,
  238.             'is_category': False }),
  239.         ('path', {
  240.             'table': None,
  241.             'column': None,
  242.             'datatype': 'text',
  243.             'is_multiple': None,
  244.             'kind': 'field',
  245.             'name': None,
  246.             'search_terms': [],
  247.             'is_custom': False,
  248.             'is_category': False }),
  249.         ('pubdate', {
  250.             'table': None,
  251.             'column': None,
  252.             'datatype': 'datetime',
  253.             'is_multiple': None,
  254.             'kind': 'field',
  255.             'name': _('Published'),
  256.             'search_terms': [
  257.                 'pubdate'],
  258.             'is_custom': False,
  259.             'is_category': False }),
  260.         ('series_index', {
  261.             'table': None,
  262.             'column': None,
  263.             'datatype': 'float',
  264.             'is_multiple': None,
  265.             'kind': 'field',
  266.             'name': None,
  267.             'search_terms': [
  268.                 'series_index'],
  269.             'is_custom': False,
  270.             'is_category': False }),
  271.         ('sort', {
  272.             'table': None,
  273.             'column': None,
  274.             'datatype': 'text',
  275.             'is_multiple': None,
  276.             'kind': 'field',
  277.             'name': _('Title Sort'),
  278.             'search_terms': [
  279.                 'title_sort'],
  280.             'is_custom': False,
  281.             'is_category': False }),
  282.         ('size', {
  283.             'table': None,
  284.             'column': None,
  285.             'datatype': 'float',
  286.             'is_multiple': None,
  287.             'kind': 'field',
  288.             'name': _('Size (MB)'),
  289.             'search_terms': [
  290.                 'size'],
  291.             'is_custom': False,
  292.             'is_category': False }),
  293.         ('timestamp', {
  294.             'table': None,
  295.             'column': None,
  296.             'datatype': 'datetime',
  297.             'is_multiple': None,
  298.             'kind': 'field',
  299.             'name': _('Date'),
  300.             'search_terms': [
  301.                 'date'],
  302.             'is_custom': False,
  303.             'is_category': False }),
  304.         ('title', {
  305.             'table': None,
  306.             'column': None,
  307.             'datatype': 'text',
  308.             'is_multiple': None,
  309.             'kind': 'field',
  310.             'name': _('Title'),
  311.             'search_terms': [
  312.                 'title'],
  313.             'is_custom': False,
  314.             'is_category': False }),
  315.         ('uuid', {
  316.             'table': None,
  317.             'column': None,
  318.             'datatype': 'text',
  319.             'is_multiple': None,
  320.             'kind': 'field',
  321.             'name': None,
  322.             'search_terms': [],
  323.             'is_custom': False,
  324.             'is_category': False })]
  325.     search_items = [
  326.         'all',
  327.         'search']
  328.     
  329.     def __init__(self):
  330.         self._tb_cats = OrderedDict()
  331.         self._search_term_map = { }
  332.         self.custom_label_to_key_map = { }
  333.         for k, v in self._field_metadata:
  334.             if v['kind'] == 'field' and v['datatype'] not in self.VALID_DATA_TYPES:
  335.                 raise ValueError('Unknown datatype %s for field %s' % (v['datatype'], k))
  336.             v['datatype'] not in self.VALID_DATA_TYPES
  337.             self._tb_cats[k] = v
  338.             self._tb_cats[k]['label'] = k
  339.             self._tb_cats[k]['display'] = { }
  340.             self._tb_cats[k]['is_editable'] = True
  341.             self._add_search_terms_to_map(k, v['search_terms'])
  342.         
  343.         self._tb_cats['timestamp']['display'] = {
  344.             'date_format': tweaks['gui_timestamp_display_format'] }
  345.         self._tb_cats['pubdate']['display'] = {
  346.             'date_format': tweaks['gui_pubdate_display_format'] }
  347.         self.custom_field_prefix = '#'
  348.         self.get = self._tb_cats.get
  349.  
  350.     
  351.     def __getitem__(self, key):
  352.         return self._tb_cats[key]
  353.  
  354.     
  355.     def __setitem__(self, key, val):
  356.         raise AttributeError('Assigning to this object is forbidden')
  357.  
  358.     
  359.     def __delitem__(self, key):
  360.         del self._tb_cats[key]
  361.  
  362.     
  363.     def __iter__(self):
  364.         for key in self._tb_cats:
  365.             yield key
  366.         
  367.  
  368.     
  369.     def __contains__(self, key):
  370.         return self.has_key(key)
  371.  
  372.     
  373.     def has_key(self, key):
  374.         return key in self._tb_cats
  375.  
  376.     
  377.     def keys(self):
  378.         return self._tb_cats.keys()
  379.  
  380.     
  381.     def sortable_field_keys(self):
  382.         return _[1]
  383.  
  384.     
  385.     def standard_field_keys(self):
  386.         return _[1]
  387.  
  388.     
  389.     def custom_field_keys(self, include_composites = True):
  390.         res = []
  391.         for k in self._tb_cats.keys():
  392.             fm = self._tb_cats[k]
  393.             if fm['kind'] == 'field' and fm['is_custom']:
  394.                 if fm['datatype'] != 'composite' or include_composites:
  395.                     res.append(k)
  396.                     continue
  397.         
  398.         return res
  399.  
  400.     
  401.     def all_field_keys(self):
  402.         return _[1]
  403.  
  404.     
  405.     def iterkeys(self):
  406.         for key in self._tb_cats:
  407.             yield key
  408.         
  409.  
  410.     
  411.     def itervalues(self):
  412.         return self._tb_cats.itervalues()
  413.  
  414.     
  415.     def values(self):
  416.         return self._tb_cats.values()
  417.  
  418.     
  419.     def iteritems(self):
  420.         for key in self._tb_cats:
  421.             yield (key, self._tb_cats[key])
  422.         
  423.  
  424.     
  425.     def items(self):
  426.         return list(self.iteritems())
  427.  
  428.     
  429.     def is_custom_field(self, key):
  430.         return key.startswith(self.custom_field_prefix)
  431.  
  432.     
  433.     def key_to_label(self, key):
  434.         if 'label' not in self._tb_cats[key]:
  435.             return key
  436.         return self._tb_cats[key]['label']
  437.  
  438.     
  439.     def label_to_key(self, label, prefer_custom = False):
  440.         if prefer_custom:
  441.             if label in self.custom_label_to_key_map:
  442.                 return self.custom_label_to_key_map[label]
  443.         
  444.         if 'label' in self._tb_cats:
  445.             return label
  446.         raise ValueError('Unknown key [%s]' % label)
  447.  
  448.     
  449.     def all_metadata(self):
  450.         l = { }
  451.         for k in self._tb_cats:
  452.             l[k] = self._tb_cats[k]
  453.         
  454.         return l
  455.  
  456.     
  457.     def custom_field_metadata(self, include_composites = True):
  458.         l = { }
  459.         for k in self.custom_field_keys(include_composites):
  460.             l[k] = self._tb_cats[k]
  461.         
  462.         return l
  463.  
  464.     
  465.     def add_custom_field(self, label, table, column, datatype, colnum, name, display, is_editable, is_multiple, is_category):
  466.         key = self.custom_field_prefix + label
  467.         if key in self._tb_cats:
  468.             raise ValueError('Duplicate custom field [%s]' % label)
  469.         key in self._tb_cats
  470.         if datatype not in self.VALID_DATA_TYPES:
  471.             raise ValueError('Unknown datatype %s for field %s' % (datatype, key))
  472.         datatype not in self.VALID_DATA_TYPES
  473.         self._tb_cats[key] = {
  474.             'table': table,
  475.             'column': column,
  476.             'datatype': datatype,
  477.             'is_multiple': is_multiple,
  478.             'kind': 'field',
  479.             'name': name,
  480.             'search_terms': [
  481.                 key],
  482.             'label': label,
  483.             'colnum': colnum,
  484.             'display': display,
  485.             'is_custom': True,
  486.             'is_category': is_category,
  487.             'link_column': 'value',
  488.             'category_sort': 'value',
  489.             'is_editable': is_editable }
  490.         self._add_search_terms_to_map(key, [
  491.             key])
  492.         self.custom_label_to_key_map[label] = key
  493.         if datatype == 'series':
  494.             key += '_index'
  495.             self._tb_cats[key] = {
  496.                 'table': None,
  497.                 'column': None,
  498.                 'datatype': 'float',
  499.                 'is_multiple': None,
  500.                 'kind': 'field',
  501.                 'name': '',
  502.                 'search_terms': [
  503.                     key],
  504.                 'label': label + '_index',
  505.                 'colnum': None,
  506.                 'display': { },
  507.                 'is_custom': False,
  508.                 'is_category': False,
  509.                 'link_column': None,
  510.                 'category_sort': None,
  511.                 'is_editable': False }
  512.             self._add_search_terms_to_map(key, [
  513.                 key])
  514.             self.custom_label_to_key_map[label + '_index'] = key
  515.         
  516.  
  517.     
  518.     def remove_dynamic_categories(self):
  519.         for key in list(self._tb_cats.keys()):
  520.             val = self._tb_cats[key]
  521.             if val['is_category'] and val['kind'] in ('user', 'search'):
  522.                 del self._tb_cats[key]
  523.                 continue
  524.         
  525.  
  526.     
  527.     def cc_series_index_column_for(self, key):
  528.         return self._tb_cats[key]['rec_index'] + 1
  529.  
  530.     
  531.     def add_user_category(self, label, name):
  532.         if label in self._tb_cats:
  533.             raise ValueError('Duplicate user field [%s]' % label)
  534.         label in self._tb_cats
  535.         self._tb_cats[label] = {
  536.             'table': None,
  537.             'column': None,
  538.             'datatype': None,
  539.             'is_multiple': None,
  540.             'kind': 'user',
  541.             'name': name,
  542.             'search_terms': [],
  543.             'is_custom': False,
  544.             'is_category': True }
  545.  
  546.     
  547.     def add_search_category(self, label, name):
  548.         if label in self._tb_cats:
  549.             raise ValueError('Duplicate user field [%s]' % label)
  550.         label in self._tb_cats
  551.         self._tb_cats[label] = {
  552.             'table': None,
  553.             'column': None,
  554.             'datatype': None,
  555.             'is_multiple': None,
  556.             'kind': 'search',
  557.             'name': name,
  558.             'search_terms': [],
  559.             'is_custom': False,
  560.             'is_category': True }
  561.  
  562.     
  563.     def set_field_record_index(self, label, index, prefer_custom = False):
  564.         if prefer_custom:
  565.             key = self.custom_field_prefix + label
  566.             if key not in self._tb_cats:
  567.                 key = label
  568.             
  569.         elif label in self._tb_cats:
  570.             key = label
  571.         else:
  572.             key = self.custom_field_prefix + label
  573.         self._tb_cats[key]['rec_index'] = index
  574.  
  575.     
  576.     def get_search_terms(self):
  577.         s_keys = sorted(self._search_term_map.keys())
  578.         for v in self.search_items:
  579.             s_keys.append(v)
  580.         
  581.         return s_keys
  582.  
  583.     
  584.     def _add_search_terms_to_map(self, key, terms):
  585.         if terms is not None:
  586.             for t in terms:
  587.                 t = t.lower()
  588.                 if t in self._search_term_map:
  589.                     raise ValueError('Attempt to add duplicate search term "%s"' % t)
  590.                 t in self._search_term_map
  591.                 self._search_term_map[t] = key
  592.             
  593.         
  594.  
  595.     
  596.     def search_term_to_field_key(self, term):
  597.         if term in self._search_term_map:
  598.             return self._search_term_map[term]
  599.         return term
  600.  
  601.     
  602.     def searchable_fields(self):
  603.         return _[1]
  604.  
  605.  
  606.