Advertisement
MeBeiM

tldr_find_too_long.py

Jul 9th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import os
  4.  
  5. def calc_lengths():
  6.     max_lengths = {}
  7.  
  8.     for root in os.listdir('.'):
  9.         if not root.startswith('pages'):
  10.             continue
  11.  
  12.         for platform in os.listdir(root):
  13.             for page in os.listdir(os.path.join(root, platform)):
  14.                 fname = os.path.join(root, platform, page)
  15.  
  16.                 with open(fname, encoding='utf8') as f:
  17.                     max_length = 0
  18.                    
  19.                     for line in f:
  20.                         line = line.rstrip('\n')
  21.                         if line.startswith('>') or line.startswith('-'):
  22.                             max_length = max(max_length, len(line))
  23.  
  24.                     max_lengths[fname] = max_length
  25.  
  26.     return max_lengths
  27.  
  28. def find_too_long(max_lengths, limit, old_found={}):
  29.     found = {}
  30.    
  31.     for fname, max_length in max_lengths.items():
  32.         diff = max_length - limit
  33.         if diff > 0:
  34.             if fname not in old_found:
  35.                 found[fname] = (max_length, diff)
  36.  
  37.     n = len(found)
  38.  
  39.     if (n <= 50):
  40.         print('Limit %d:' % limit, n, '{}page{} over the limit.'.format('more ' if old_found else '', 's' if n == 0 or n > 1 else ''))
  41.         for fname, (max_length, diff) in sorted(found.items(), key=lambda x: (-x[1][0], x[0])):
  42.             print('{:<50s}  {} ({} over)'.format(fname.replace('\\', '/'), max_length, diff))
  43.     else:
  44.         print('Limit %d:' % limit, n, '{}pages over the limit (avoiding listing).'.format('more ' if old_found else ''))
  45.  
  46.     print('-' * 65)
  47.    
  48.     old_found.update(found)
  49.  
  50. ml = calc_lengths()
  51.  
  52. for limit in (250, 240, 220, 200, 180, 150, 140, 100, 80):
  53.     find_too_long(ml, limit)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement