Advertisement
ChallengerAppeared

aiebreaker.py

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