Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

aiebreaker.py

By: ChallengerAppeared on Sep 12th, 2013  |  syntax: Python  |  size: 3.37 KB  |  hits: 32  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #       AiE story breaker
  2. #
  3. #
  4. #
  5. #       Author: Andrew Melnick
  6. #       Date:   12/Sep/2013
  7. #       License: GNU General Public License
  8. #
  9. #       Purpose:
  10. #           Split a collection of lines of text into parts, all under
  11. #               a certain character limit
  12. #
  13. #       Usage:
  14. #           aiebreaker.py <file to split> <maxiumum character per part>
  15.  
  16.  
  17. from sys import argv
  18. from os.path import dirname,join,basename,isdir
  19. from os import mkdir,getcwd,chdir
  20.  
  21. # Script function
  22. def run(args):
  23.     # If the argument list does not contain 2 arguments
  24.     #   alert the user, and exit
  25.     if len(args) != 3:
  26.         print 'bad argument list'
  27.         print 'usage: aiebreaker.py <filename> <maximum character limit>'
  28.         return
  29.     else:
  30.         # Retreive the arguments
  31.         filename = args[1]
  32.         try:
  33.             part_length = int(args[2])
  34.         except:
  35.             # If the part length is not an integer, alert the user and exit
  36.             print 'bad part length'
  37.             print 'usage: aiebreaker.py <filename> <maximum character limit>'
  38.             return
  39.         current_length = 0
  40.  
  41.         # Try to open the file and read its contents
  42.         try:
  43.             with open(filename, 'r') as f:
  44.                 input_text = f.read()
  45.         except:
  46.             # If the file cannot be opened, alert the user and exit
  47.             print 'bad filename'
  48.             print 'usage: aiebreaker.py <filename> <maximum character limit>'
  49.             return
  50.  
  51.         # Split the text into lines
  52.         lines = input_text.split('\n')
  53.  
  54.         # Create a list of parts
  55.         parts = []
  56.         parts.append([])
  57.  
  58.         # For each line in the file
  59.         for line in lines:
  60.            
  61.             # If the line itself is too long for the maximum character limit,
  62.             #   alert the user and exit
  63.             if len(line) > part_length:
  64.                 print line + ' : line too long, exiting'
  65.                
  66.             # Otherwise
  67.             else:
  68.  
  69.                 # If the line would make the current part too long,
  70.                 #   Create a new part
  71.                 to_append = line+'\n'
  72.                 if current_length + len(to_append) > part_length:
  73.                     parts.append([])
  74.                     print 'breaking at '+str(current_length)
  75.                     current_length = 0
  76.                    
  77.                 # Add the line to the current part
  78.                 parts[-1].append(to_append)
  79.                 current_length += len(to_append)
  80.  
  81.         # Make a directory for the parts
  82.         base_name = basename(filename)
  83.         extension = base_name.split('.')[-1]
  84.         if len(extension) == 0:
  85.             bare_name = base_name
  86.         else:
  87.             extension = '.'+extension
  88.             bare_name = base_name[0:-len(extension)]
  89.            
  90.         parts_dir = join(dirname(filename),bare_name+'_parts')
  91.         if not isdir(parts_dir):
  92.             mkdir(parts_dir)
  93.         cwd = getcwd()
  94.         chdir(parts_dir)
  95.         # Then make a file in that directory for each part
  96.         current_part = 0
  97.         for part in parts:
  98.             with open(bare_name+"_part_"+str(current_part)+extension, 'w') as f:
  99.                 for line in part:
  100.                     f.write(line)
  101.             current_part += 1
  102.         chdir(cwd)
  103.        
  104.         # End of process
  105.  
  106. # Invoke the Script
  107. run(argv)