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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import re
  5. import string
  6. import traceback
  7. from calibre.constants import DEBUG
  8. from calibre.utils.titlecase import titlecase
  9.  
  10. class TemplateFormatter(string.Formatter):
  11.     composite_values = { }
  12.     
  13.     def __init__(self):
  14.         string.Formatter.__init__(self)
  15.         self.book = None
  16.         self.kwargs = None
  17.  
  18.     
  19.     def _lookup(self, val, *args):
  20.         if len(args) == 2:
  21.             if val:
  22.                 return self.vformat('{' + args[0].strip() + '}', [], self.kwargs)
  23.             return self.vformat('{' + args[1].strip() + '}', [], self.kwargs)
  24.         len(args) == 2
  25.         if len(args) % 2 != 1:
  26.             raise ValueError(_('lookup requires either 2 or an odd number of arguments'))
  27.         len(args) % 2 != 1
  28.         i = 0
  29.         while i < len(args):
  30.             if i + 1 >= len(args):
  31.                 return self.vformat('{' + args[i].strip() + '}', [], self.kwargs)
  32.             if re.search(args[i], val):
  33.                 return self.vformat('{' + args[i + 1].strip() + '}', [], self.kwargs)
  34.             i += 2
  35.             continue
  36.             re.search(args[i], val)
  37.  
  38.     
  39.     def _test(self, val, value_if_set, value_not_set):
  40.         if val:
  41.             return value_if_set
  42.         return value_not_set
  43.  
  44.     
  45.     def _contains(self, val, test, value_if_present, value_if_not):
  46.         if re.search(test, val):
  47.             return value_if_present
  48.         return value_if_not
  49.  
  50.     
  51.     def _switch(self, val, *args):
  52.         if len(args) % 2 != 1:
  53.             raise ValueError(_('switch requires an odd number of arguments'))
  54.         len(args) % 2 != 1
  55.         i = 0
  56.         while i < len(args):
  57.             if i + 1 >= len(args):
  58.                 return args[i]
  59.             if re.search(args[i], val):
  60.                 return args[i + 1]
  61.             i += 2
  62.             continue
  63.             re.search(args[i], val)
  64.  
  65.     
  66.     def _re(self, val, pattern, replacement):
  67.         return re.sub(pattern, replacement, val)
  68.  
  69.     
  70.     def _ifempty(self, val, value_if_empty):
  71.         if val:
  72.             return val
  73.         return value_if_empty
  74.  
  75.     
  76.     def _shorten(self, val, leading, center_string, trailing):
  77.         l = max(0, int(leading))
  78.         t = max(0, int(trailing))
  79.         if len(val) > l + len(center_string) + t:
  80.             return None + val[0:l] + center_string if t == 0 else val[-t:]
  81.         return val
  82.  
  83.     functions = {
  84.         'uppercase': (0, (lambda s, x: x.upper())),
  85.         'lowercase': (0, (lambda s, x: x.lower())),
  86.         'titlecase': (0, (lambda s, x: titlecase(x))),
  87.         'capitalize': (0, (lambda s, x: x.capitalize())),
  88.         'contains': (3, _contains),
  89.         'ifempty': (1, _ifempty),
  90.         'lookup': (-1, _lookup),
  91.         're': (2, _re),
  92.         'shorten': (3, _shorten),
  93.         'switch': (-1, _switch),
  94.         'test': (2, _test) }
  95.     format_string_re = re.compile('^(.*)\\|(.*)\\|(.*)$')
  96.     compress_spaces = re.compile('\\s+')
  97.     backslash_comma_to_comma = re.compile('\\\\,')
  98.     arg_parser = re.Scanner([
  99.         (',', (lambda x, t: '')),
  100.         ('.*?((?<!\\\\),)', (lambda x, t: t[:-1])),
  101.         ('.*?\\)', (lambda x, t: t[:-1]))])
  102.     
  103.     def get_value(self, key, args, kwargs):
  104.         raise Exception('get_value must be implemented in the subclass')
  105.  
  106.     
  107.     def _explode_format_string(self, fmt):
  108.         
  109.         try:
  110.             matches = self.format_string_re.match(fmt)
  111.             if matches is None or matches.lastindex != 3:
  112.                 return (fmt, '', '')
  113.             return matches.groups()
  114.         except:
  115.             if DEBUG:
  116.                 traceback.print_exc()
  117.             
  118.             return (fmt, '', '')
  119.  
  120.  
  121.     
  122.     def format_field(self, val, fmt):
  123.         (fmt, prefix, suffix) = self._explode_format_string(fmt)
  124.         p = fmt.find('(')
  125.         dispfmt = fmt
  126.         if val:
  127.             val = string.Formatter.format_field(self, val, dispfmt)
  128.         
  129.         if not val:
  130.             return ''
  131.         return prefix + val + suffix
  132.  
  133.     
  134.     def vformat(self, fmt, args, kwargs):
  135.         ans = string.Formatter.vformat(self, fmt, args, kwargs)
  136.         return self.compress_spaces.sub(' ', ans).strip()
  137.  
  138.     
  139.     def safe_format(self, fmt, kwargs, error_value, book):
  140.         self.kwargs = kwargs
  141.         self.book = book
  142.         self.composite_values = { }
  143.         
  144.         try:
  145.             ans = self.vformat(fmt, [], kwargs).strip()
  146.         except:
  147.             if DEBUG:
  148.                 traceback.print_exc()
  149.             
  150.             ans = error_value
  151.  
  152.         return ans
  153.  
  154.  
  155.  
  156. class ValidateFormat(TemplateFormatter):
  157.     
  158.     def get_value(self, key, args, kwargs):
  159.         return 'this is some text that should be long enough'
  160.  
  161.     
  162.     def validate(self, x):
  163.         return self.vformat(x, [], { })
  164.  
  165.  
  166. validation_formatter = ValidateFormat()
  167.