Gfy

txtcleanup.py

Gfy
Nov 5th, 2011
103
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program.  If not, see <http://www.gnu.org/licenses/>
  16.  
  17. """ Cleans up txt files created with the following DOS script:
  18. http://pastebin.com/xzxuGzFT
  19.  
  20. @ECHO OFF
  21. goto start
  22.  
  23. :srs
  24. SET gx=%1
  25. srs %gx% > %gx%.txt
  26. IF EXIST *.srs del *.txt
  27. cd..
  28. goto end
  29.  
  30. :run
  31. SET gx=%1
  32. echo %gx%
  33. cd %gx%
  34. FOR /F "usebackq tokens=*" %%G IN (`dir /B *.avi *.mkv`) DO CALL :srs %%G
  35. goto end
  36.  
  37. :start
  38. FOR /F "usebackq tokens=*" %%G IN (`dir /A:D /B`) DO CALL :run %%G
  39.  
  40. :end
  41. """
  42.  
  43. from __future__ import unicode_literals
  44. import optparse
  45. import sys
  46. import os
  47. import re
  48. import glob
  49. import codecs
  50.  
  51. def fix_txt(filename):
  52.     if options.dry_run:
  53.         print("Fixing '%s'." % filename)
  54.         return
  55.     data = codecs.open(filename, encoding="latin-1", mode="r").read()
  56.     newdata = re.sub(".*Corruption", "Corruption", data)
  57.     newdata = newdata.replace("\xff", ".") # cp850 \xa0
  58.     newdata = re.sub("\r(?!\n)|(?<!\r)\n", "\r\n", newdata)
  59.     if newdata != data:
  60.         print(filename)
  61.         f = open(filename, "wb")
  62.         f.write(newdata)
  63.         f.close()
  64.     else:
  65.         print("Nothing done for %s!" % filename)
  66.  
  67. def main(options, args):
  68.     for element in args:
  69.         element = os.path.abspath(element)
  70.         if os.path.isfile(element) and element[-4:] == ".txt":
  71.             fix_txt(element)
  72.         elif os.path.isdir(element):
  73.             for file in glob.iglob("*.txt"):
  74.                 fix_txt(file)
  75.             for file in glob.iglob("*/*.txt"):
  76.                 fix_txt(file)
  77.         else:
  78.             print("WTF are you supplying me?")
  79.            
  80. if __name__ == '__main__':
  81.     parser = optparse.OptionParser(
  82.         usage="Usage: %prog [txt files] [directories] [options]'\n"
  83.         "This tool will clean up all generated .avi/mkv.txt files.\n",
  84.         version="%prog 0.1 (2011-11-05)") # --help, --version
  85.    
  86.     parser.add_option("-n", "--dry-run", help="do no harm",
  87.                       action="store_true", dest="dry_run", default=False)
  88.    
  89.     # no arguments given
  90.     if len(sys.argv) < 2:
  91.         print(parser.format_help())
  92.     else:
  93.         (options, args) = parser.parse_args()
  94.         main(options, args)
RAW Paste Data