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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __version__ = '6'
  5. import os
  6. import sys
  7. import getopt
  8. import tokenize
  9. if not hasattr(tokenize, 'NL'):
  10.     raise ValueError("tokenize.NL doesn't exist -- tokenize module too old")
  11. hasattr(tokenize, 'NL')
  12. __all__ = [
  13.     'check',
  14.     'NannyNag',
  15.     'process_tokens']
  16. verbose = 0
  17. filename_only = 0
  18.  
  19. def errprint(*args):
  20.     sep = ''
  21.     for arg in args:
  22.         sys.stderr.write(sep + str(arg))
  23.         sep = ' '
  24.     
  25.     sys.stderr.write('\n')
  26.  
  27.  
  28. def main():
  29.     global filename_only, verbose
  30.     
  31.     try:
  32.         (opts, args) = getopt.getopt(sys.argv[1:], 'qv')
  33.     except getopt.error:
  34.         msg = None
  35.         errprint(msg)
  36.         return None
  37.  
  38.     for o, a in opts:
  39.         if o == '-q':
  40.             filename_only = filename_only + 1
  41.         
  42.         if o == '-v':
  43.             verbose = verbose + 1
  44.             continue
  45.     
  46.     if not args:
  47.         errprint('Usage:', sys.argv[0], '[-v] file_or_directory ...')
  48.         return None
  49.     for arg in args:
  50.         check(arg)
  51.     
  52.  
  53.  
  54. class NannyNag(Exception):
  55.     
  56.     def __init__(self, lineno, msg, line):
  57.         self.lineno = lineno
  58.         self.msg = msg
  59.         self.line = line
  60.  
  61.     
  62.     def get_lineno(self):
  63.         return self.lineno
  64.  
  65.     
  66.     def get_msg(self):
  67.         return self.msg
  68.  
  69.     
  70.     def get_line(self):
  71.         return self.line
  72.  
  73.  
  74.  
  75. def check(file):
  76.     if os.path.isdir(file) and not os.path.islink(file):
  77.         if verbose:
  78.             print '%r: listing directory' % (file,)
  79.         
  80.         names = os.listdir(file)
  81.         for name in names:
  82.             fullname = os.path.join(file, name)
  83.             if os.path.isdir(fullname) or not os.path.islink(fullname) or os.path.normcase(name[-3:]) == '.py':
  84.                 check(fullname)
  85.                 continue
  86.         
  87.         return None
  88.     
  89.     try:
  90.         f = open(file)
  91.     except IOError:
  92.         not os.path.islink(file)
  93.         msg = not os.path.islink(file)
  94.         errprint('%r: I/O Error: %s' % (file, msg))
  95.         return None
  96.  
  97.     
  98.     try:
  99.         process_tokens(tokenize.generate_tokens(f.readline))
  100.     except tokenize.TokenError:
  101.         None if verbose > 1 else not os.path.islink(file)
  102.         msg = None if verbose > 1 else not os.path.islink(file)
  103.         errprint('%r: Token Error: %s' % (file, msg))
  104.         return None
  105.         except IndentationError:
  106.             msg = None
  107.             errprint('%r: Indentation Error: %s' % (file, msg))
  108.             return None
  109.             except NannyNag:
  110.                 nag = None
  111.                 badline = nag.get_lineno()
  112.                 line = nag.get_line()
  113.                 if verbose:
  114.                     print '%r: *** Line %d: trouble in tab city! ***' % (file, badline)
  115.                     print 'offending line: %r' % (line,)
  116.                     print nag.get_msg()
  117.                 elif ' ' in file:
  118.                     file = '"' + file + '"'
  119.                 
  120.                 if filename_only:
  121.                     print file
  122.                 else:
  123.                     print file, badline, repr(line)
  124.                 return None
  125.             elif verbose:
  126.                 print '%r: Clean bill of health.' % (file,)
  127.             
  128.  
  129.  
  130.  
  131. class Whitespace:
  132.     (S, T) = ' \t'
  133.     
  134.     def __init__(self, ws):
  135.         self.raw = ws
  136.         S = Whitespace.S
  137.         T = Whitespace.T
  138.         count = []
  139.         b = n = nt = 0
  140.         for ch in self.raw:
  141.             if ch == S:
  142.                 n = n + 1
  143.                 b = b + 1
  144.                 continue
  145.             if ch == T:
  146.                 n = n + 1
  147.                 nt = nt + 1
  148.                 if b >= len(count):
  149.                     count = count + [
  150.                         0] * ((b - len(count)) + 1)
  151.                 
  152.                 count[b] = count[b] + 1
  153.                 b = 0
  154.                 continue
  155.         
  156.         self.n = n
  157.         self.nt = nt
  158.         self.norm = (tuple(count), b)
  159.         self.is_simple = len(count) <= 1
  160.  
  161.     
  162.     def longest_run_of_spaces(self):
  163.         (count, trailing) = self.norm
  164.         return max(len(count) - 1, trailing)
  165.  
  166.     
  167.     def indent_level(self, tabsize):
  168.         (count, trailing) = self.norm
  169.         il = 0
  170.         for i in range(tabsize, len(count)):
  171.             il = il + (i / tabsize) * count[i]
  172.         
  173.         return trailing + tabsize * (il + self.nt)
  174.  
  175.     
  176.     def equal(self, other):
  177.         return self.norm == other.norm
  178.  
  179.     
  180.     def not_equal_witness(self, other):
  181.         n = max(self.longest_run_of_spaces(), other.longest_run_of_spaces()) + 1
  182.         a = []
  183.         for ts in range(1, n + 1):
  184.             if self.indent_level(ts) != other.indent_level(ts):
  185.                 a.append((ts, self.indent_level(ts), other.indent_level(ts)))
  186.                 continue
  187.         
  188.         return a
  189.  
  190.     
  191.     def less(self, other):
  192.         if self.n >= other.n:
  193.             return False
  194.         if self.is_simple and other.is_simple:
  195.             return self.nt <= other.nt
  196.         n = max(self.longest_run_of_spaces(), other.longest_run_of_spaces()) + 1
  197.         for ts in range(2, n + 1):
  198.             if self.indent_level(ts) >= other.indent_level(ts):
  199.                 return False
  200.         
  201.         return True
  202.  
  203.     
  204.     def not_less_witness(self, other):
  205.         n = max(self.longest_run_of_spaces(), other.longest_run_of_spaces()) + 1
  206.         a = []
  207.         for ts in range(1, n + 1):
  208.             if self.indent_level(ts) >= other.indent_level(ts):
  209.                 a.append((ts, self.indent_level(ts), other.indent_level(ts)))
  210.                 continue
  211.         
  212.         return a
  213.  
  214.  
  215.  
  216. def format_witnesses(w):
  217.     firsts = map((lambda tup: str(tup[0])), w)
  218.     prefix = 'at tab size'
  219.     if len(w) > 1:
  220.         prefix = prefix + 's'
  221.     
  222.     return prefix + ' ' + ', '.join(firsts)
  223.  
  224.  
  225. def process_tokens(tokens):
  226.     INDENT = tokenize.INDENT
  227.     DEDENT = tokenize.DEDENT
  228.     NEWLINE = tokenize.NEWLINE
  229.     JUNK = (tokenize.COMMENT, tokenize.NL)
  230.     indents = [
  231.         Whitespace('')]
  232.     check_equal = 0
  233.     for type, token, start, end, line in tokens:
  234.         if type == NEWLINE:
  235.             check_equal = 1
  236.             continue
  237.         if type == INDENT:
  238.             check_equal = 0
  239.             thisguy = Whitespace(token)
  240.             if not indents[-1].less(thisguy):
  241.                 witness = indents[-1].not_less_witness(thisguy)
  242.                 msg = 'indent not greater e.g. ' + format_witnesses(witness)
  243.                 raise NannyNag(start[0], msg, line)
  244.             indents[-1].less(thisguy)
  245.             indents.append(thisguy)
  246.             continue
  247.         if type == DEDENT:
  248.             check_equal = 1
  249.             del indents[-1]
  250.             continue
  251.         if check_equal and type not in JUNK:
  252.             check_equal = 0
  253.             thisguy = Whitespace(line)
  254.             if not indents[-1].equal(thisguy):
  255.                 witness = indents[-1].not_equal_witness(thisguy)
  256.                 msg = 'indent not equal e.g. ' + format_witnesses(witness)
  257.                 raise NannyNag(start[0], msg, line)
  258.             indents[-1].equal(thisguy)
  259.             continue
  260.     
  261.  
  262. if __name__ == '__main__':
  263.     main()
  264.  
  265.