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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import sys
  5. AS_IS = None
  6.  
  7. class NullFormatter:
  8.     
  9.     def __init__(self, writer = None):
  10.         if writer is None:
  11.             writer = NullWriter()
  12.         
  13.         self.writer = writer
  14.  
  15.     
  16.     def end_paragraph(self, blankline):
  17.         pass
  18.  
  19.     
  20.     def add_line_break(self):
  21.         pass
  22.  
  23.     
  24.     def add_hor_rule(self, *args, **kw):
  25.         pass
  26.  
  27.     
  28.     def add_label_data(self, format, counter, blankline = None):
  29.         pass
  30.  
  31.     
  32.     def add_flowing_data(self, data):
  33.         pass
  34.  
  35.     
  36.     def add_literal_data(self, data):
  37.         pass
  38.  
  39.     
  40.     def flush_softspace(self):
  41.         pass
  42.  
  43.     
  44.     def push_alignment(self, align):
  45.         pass
  46.  
  47.     
  48.     def pop_alignment(self):
  49.         pass
  50.  
  51.     
  52.     def push_font(self, x):
  53.         pass
  54.  
  55.     
  56.     def pop_font(self):
  57.         pass
  58.  
  59.     
  60.     def push_margin(self, margin):
  61.         pass
  62.  
  63.     
  64.     def pop_margin(self):
  65.         pass
  66.  
  67.     
  68.     def set_spacing(self, spacing):
  69.         pass
  70.  
  71.     
  72.     def push_style(self, *styles):
  73.         pass
  74.  
  75.     
  76.     def pop_style(self, n = 1):
  77.         pass
  78.  
  79.     
  80.     def assert_line_data(self, flag = 1):
  81.         pass
  82.  
  83.  
  84.  
  85. class AbstractFormatter:
  86.     
  87.     def __init__(self, writer):
  88.         self.writer = writer
  89.         self.align = None
  90.         self.align_stack = []
  91.         self.font_stack = []
  92.         self.margin_stack = []
  93.         self.spacing = None
  94.         self.style_stack = []
  95.         self.nospace = 1
  96.         self.softspace = 0
  97.         self.para_end = 1
  98.         self.parskip = 0
  99.         self.hard_break = 1
  100.         self.have_label = 0
  101.  
  102.     
  103.     def end_paragraph(self, blankline):
  104.         if not self.hard_break:
  105.             self.writer.send_line_break()
  106.             self.have_label = 0
  107.         
  108.         if self.parskip < blankline and not (self.have_label):
  109.             self.writer.send_paragraph(blankline - self.parskip)
  110.             self.parskip = blankline
  111.             self.have_label = 0
  112.         
  113.         self.hard_break = self.nospace = self.para_end = 1
  114.         self.softspace = 0
  115.  
  116.     
  117.     def add_line_break(self):
  118.         if not self.hard_break or self.para_end:
  119.             self.writer.send_line_break()
  120.             self.have_label = self.parskip = 0
  121.         
  122.         self.hard_break = self.nospace = 1
  123.         self.softspace = 0
  124.  
  125.     
  126.     def add_hor_rule(self, *args, **kw):
  127.         if not self.hard_break:
  128.             self.writer.send_line_break()
  129.         
  130.         self.writer.send_hor_rule(*args, **kw)
  131.         self.hard_break = self.nospace = 1
  132.         self.have_label = self.para_end = self.softspace = self.parskip = 0
  133.  
  134.     
  135.     def add_label_data(self, format, counter, blankline = None):
  136.         if self.have_label or not (self.hard_break):
  137.             self.writer.send_line_break()
  138.         
  139.         if not self.para_end:
  140.             if not blankline or 1:
  141.                 pass
  142.             self.writer.send_paragraph(0)
  143.         
  144.         if isinstance(format, str):
  145.             self.writer.send_label_data(self.format_counter(format, counter))
  146.         else:
  147.             self.writer.send_label_data(format)
  148.         self.nospace = self.have_label = self.hard_break = self.para_end = 1
  149.         self.softspace = self.parskip = 0
  150.  
  151.     
  152.     def format_counter(self, format, counter):
  153.         label = ''
  154.         for c in format:
  155.             if c == '1':
  156.                 label = label + '%d' % counter
  157.                 continue
  158.             if c in 'aA':
  159.                 if counter > 0:
  160.                     label = label + self.format_letter(c, counter)
  161.                 
  162.             counter > 0
  163.             if c in 'iI':
  164.                 if counter > 0:
  165.                     label = label + self.format_roman(c, counter)
  166.                 
  167.             counter > 0
  168.             label = label + c
  169.         
  170.         return label
  171.  
  172.     
  173.     def format_letter(self, case, counter):
  174.         label = ''
  175.         while counter > 0:
  176.             (counter, x) = divmod(counter - 1, 26)
  177.             s = chr(ord(case) + x)
  178.             label = s + label
  179.         return label
  180.  
  181.     
  182.     def format_roman(self, case, counter):
  183.         ones = [
  184.             'i',
  185.             'x',
  186.             'c',
  187.             'm']
  188.         fives = [
  189.             'v',
  190.             'l',
  191.             'd']
  192.         (label, index) = ('', 0)
  193.         while counter > 0:
  194.             (counter, x) = divmod(counter, 10)
  195.             if x == 9:
  196.                 label = ones[index] + ones[index + 1] + label
  197.             elif x == 4:
  198.                 label = ones[index] + fives[index] + label
  199.             elif x >= 5:
  200.                 s = fives[index]
  201.                 x = x - 5
  202.             else:
  203.                 s = ''
  204.             s = s + ones[index] * x
  205.             label = s + label
  206.             index = index + 1
  207.         if case == 'I':
  208.             return label.upper()
  209.         return label
  210.  
  211.     
  212.     def add_flowing_data(self, data):
  213.         if not data:
  214.             return None
  215.         prespace = data[:1].isspace()
  216.         postspace = data[-1:].isspace()
  217.         data = ' '.join(data.split())
  218.         if self.nospace and not data:
  219.             return None
  220.         self.softspace = postspace
  221.         self.writer.send_flowing_data(data)
  222.  
  223.     
  224.     def add_literal_data(self, data):
  225.         if not data:
  226.             return None
  227.         if self.softspace:
  228.             self.writer.send_flowing_data(' ')
  229.         
  230.         self.hard_break = data[-1:] == '\n'
  231.         self.nospace = self.para_end = self.softspace = self.parskip = self.have_label = 0
  232.         self.writer.send_literal_data(data)
  233.  
  234.     
  235.     def flush_softspace(self):
  236.         if self.softspace:
  237.             self.hard_break = self.para_end = self.parskip = self.have_label = self.softspace = 0
  238.             self.nospace = 1
  239.             self.writer.send_flowing_data(' ')
  240.         
  241.  
  242.     
  243.     def push_alignment(self, align):
  244.         if align and align != self.align:
  245.             self.writer.new_alignment(align)
  246.             self.align = align
  247.             self.align_stack.append(align)
  248.         else:
  249.             self.align_stack.append(self.align)
  250.  
  251.     
  252.     def pop_alignment(self):
  253.         if self.align_stack:
  254.             del self.align_stack[-1]
  255.         
  256.         if self.align_stack:
  257.             self.align = align = self.align_stack[-1]
  258.             self.writer.new_alignment(align)
  259.         else:
  260.             self.align = None
  261.             self.writer.new_alignment(None)
  262.  
  263.     
  264.     def push_font(self, .1):
  265.         (size, i, b, tt) = .1
  266.         if self.softspace:
  267.             self.hard_break = self.para_end = self.softspace = 0
  268.             self.nospace = 1
  269.             self.writer.send_flowing_data(' ')
  270.         
  271.         if self.font_stack:
  272.             (csize, ci, cb, ctt) = self.font_stack[-1]
  273.             if size is AS_IS:
  274.                 size = csize
  275.             
  276.             if i is AS_IS:
  277.                 i = ci
  278.             
  279.             if b is AS_IS:
  280.                 b = cb
  281.             
  282.             if tt is AS_IS:
  283.                 tt = ctt
  284.             
  285.         
  286.         font = (size, i, b, tt)
  287.         self.font_stack.append(font)
  288.         self.writer.new_font(font)
  289.  
  290.     
  291.     def pop_font(self):
  292.         if self.font_stack:
  293.             del self.font_stack[-1]
  294.         
  295.         if self.font_stack:
  296.             font = self.font_stack[-1]
  297.         else:
  298.             font = None
  299.         self.writer.new_font(font)
  300.  
  301.     
  302.     def push_margin(self, margin):
  303.         self.margin_stack.append(margin)
  304.         fstack = filter(None, self.margin_stack)
  305.         if not margin and fstack:
  306.             margin = fstack[-1]
  307.         
  308.         self.writer.new_margin(margin, len(fstack))
  309.  
  310.     
  311.     def pop_margin(self):
  312.         if self.margin_stack:
  313.             del self.margin_stack[-1]
  314.         
  315.         fstack = filter(None, self.margin_stack)
  316.         if fstack:
  317.             margin = fstack[-1]
  318.         else:
  319.             margin = None
  320.         self.writer.new_margin(margin, len(fstack))
  321.  
  322.     
  323.     def set_spacing(self, spacing):
  324.         self.spacing = spacing
  325.         self.writer.new_spacing(spacing)
  326.  
  327.     
  328.     def push_style(self, *styles):
  329.         if self.softspace:
  330.             self.hard_break = self.para_end = self.softspace = 0
  331.             self.nospace = 1
  332.             self.writer.send_flowing_data(' ')
  333.         
  334.         for style in styles:
  335.             self.style_stack.append(style)
  336.         
  337.         self.writer.new_styles(tuple(self.style_stack))
  338.  
  339.     
  340.     def pop_style(self, n = 1):
  341.         del self.style_stack[-n:]
  342.         self.writer.new_styles(tuple(self.style_stack))
  343.  
  344.     
  345.     def assert_line_data(self, flag = 1):
  346.         self.nospace = self.hard_break = not flag
  347.         self.para_end = self.parskip = self.have_label = 0
  348.  
  349.  
  350.  
  351. class NullWriter:
  352.     
  353.     def __init__(self):
  354.         pass
  355.  
  356.     
  357.     def flush(self):
  358.         pass
  359.  
  360.     
  361.     def new_alignment(self, align):
  362.         pass
  363.  
  364.     
  365.     def new_font(self, font):
  366.         pass
  367.  
  368.     
  369.     def new_margin(self, margin, level):
  370.         pass
  371.  
  372.     
  373.     def new_spacing(self, spacing):
  374.         pass
  375.  
  376.     
  377.     def new_styles(self, styles):
  378.         pass
  379.  
  380.     
  381.     def send_paragraph(self, blankline):
  382.         pass
  383.  
  384.     
  385.     def send_line_break(self):
  386.         pass
  387.  
  388.     
  389.     def send_hor_rule(self, *args, **kw):
  390.         pass
  391.  
  392.     
  393.     def send_label_data(self, data):
  394.         pass
  395.  
  396.     
  397.     def send_flowing_data(self, data):
  398.         pass
  399.  
  400.     
  401.     def send_literal_data(self, data):
  402.         pass
  403.  
  404.  
  405.  
  406. class AbstractWriter(NullWriter):
  407.     
  408.     def new_alignment(self, align):
  409.         print 'new_alignment(%r)' % (align,)
  410.  
  411.     
  412.     def new_font(self, font):
  413.         print 'new_font(%r)' % (font,)
  414.  
  415.     
  416.     def new_margin(self, margin, level):
  417.         print 'new_margin(%r, %d)' % (margin, level)
  418.  
  419.     
  420.     def new_spacing(self, spacing):
  421.         print 'new_spacing(%r)' % (spacing,)
  422.  
  423.     
  424.     def new_styles(self, styles):
  425.         print 'new_styles(%r)' % (styles,)
  426.  
  427.     
  428.     def send_paragraph(self, blankline):
  429.         print 'send_paragraph(%r)' % (blankline,)
  430.  
  431.     
  432.     def send_line_break(self):
  433.         print 'send_line_break()'
  434.  
  435.     
  436.     def send_hor_rule(self, *args, **kw):
  437.         print 'send_hor_rule()'
  438.  
  439.     
  440.     def send_label_data(self, data):
  441.         print 'send_label_data(%r)' % (data,)
  442.  
  443.     
  444.     def send_flowing_data(self, data):
  445.         print 'send_flowing_data(%r)' % (data,)
  446.  
  447.     
  448.     def send_literal_data(self, data):
  449.         print 'send_literal_data(%r)' % (data,)
  450.  
  451.  
  452.  
  453. class DumbWriter(NullWriter):
  454.     
  455.     def __init__(self, file = None, maxcol = 72):
  456.         if not file:
  457.             pass
  458.         self.file = sys.stdout
  459.         self.maxcol = maxcol
  460.         NullWriter.__init__(self)
  461.         self.reset()
  462.  
  463.     
  464.     def reset(self):
  465.         self.col = 0
  466.         self.atbreak = 0
  467.  
  468.     
  469.     def send_paragraph(self, blankline):
  470.         self.file.write('\n' * blankline)
  471.         self.col = 0
  472.         self.atbreak = 0
  473.  
  474.     
  475.     def send_line_break(self):
  476.         self.file.write('\n')
  477.         self.col = 0
  478.         self.atbreak = 0
  479.  
  480.     
  481.     def send_hor_rule(self, *args, **kw):
  482.         self.file.write('\n')
  483.         self.file.write('-' * self.maxcol)
  484.         self.file.write('\n')
  485.         self.col = 0
  486.         self.atbreak = 0
  487.  
  488.     
  489.     def send_literal_data(self, data):
  490.         self.file.write(data)
  491.         i = data.rfind('\n')
  492.         if i >= 0:
  493.             self.col = 0
  494.             data = data[i + 1:]
  495.         
  496.         data = data.expandtabs()
  497.         self.col = self.col + len(data)
  498.         self.atbreak = 0
  499.  
  500.     
  501.     def send_flowing_data(self, data):
  502.         if not data:
  503.             return None
  504.         if not self.atbreak:
  505.             pass
  506.         atbreak = data[0].isspace()
  507.         col = self.col
  508.         maxcol = self.maxcol
  509.         write = self.file.write
  510.         for word in data.split():
  511.             if atbreak:
  512.                 if col + len(word) >= maxcol:
  513.                     write('\n')
  514.                     col = 0
  515.                 else:
  516.                     write(' ')
  517.                     col = col + 1
  518.             
  519.             write(word)
  520.             col = col + len(word)
  521.             atbreak = 1
  522.         
  523.         self.col = col
  524.         self.atbreak = data[-1].isspace()
  525.  
  526.  
  527.  
  528. def test(file = None):
  529.     w = DumbWriter()
  530.     f = AbstractFormatter(w)
  531.     if file is not None:
  532.         fp = open(file)
  533.     elif sys.argv[1:]:
  534.         fp = open(sys.argv[1])
  535.     else:
  536.         fp = sys.stdin
  537.     for line in fp:
  538.         if line == '\n':
  539.             f.end_paragraph(1)
  540.             continue
  541.         f.add_flowing_data(line)
  542.     
  543.     f.end_paragraph(0)
  544.  
  545. if __name__ == '__main__':
  546.     test()
  547.  
  548.