vimja

package_use_convert.py

Jul 24th, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. #!/usr/bin/python3
  2. ## Copyright (c) 2013 by Vimja
  3. ## This code is released under the terms
  4. ## of the WTFPL (What the Fuck Pulic License) version 2.0
  5. ## http://sam.zoy.org/wtfpl/
  6.  
  7. import sys
  8. import re
  9. import os
  10.  
  11. if len(sys.argv) < 3:
  12.   sys.exit("At least two arguemnts are required!")
  13.  
  14. usefile = open(sys.argv[1], 'r')
  15. regex = re.compile("[=>]*(([^/]+)/[^\s]+)\s")
  16. usemap = dict()
  17. currentstring = ""
  18. for line in usefile:
  19.   match = regex.match(line)
  20.   if match and not line.startswith("#"):
  21.     currentstring = currentstring + line
  22. #   print(line)
  23. #   if usemap[match.group(2)]:
  24.     if match.group(2) in usemap:
  25.       # Hinzufuegen
  26.       usemap[match.group(2)][match.group(1)] = currentstring
  27.       currentstring = ""
  28.     else:
  29.       keyword = match.group(1)
  30.       usemap[match.group(2)] = dict({keyword:currentstring})
  31.       currentstring = ""
  32.   else:
  33.     if not line in ['\n', '\r\n']:
  34.       #print("Comment found", line)
  35.       currentstring = currentstring + line
  36. #    else:
  37. #      print("Empty line")
  38.  
  39. if not os.path.exists(sys.argv[2]):
  40.   sys.exit("That's not a valid path! You suck!")
  41. if not os.path.isdir(sys.argv[2]):
  42.   sys.exit("Given target is a file, needs to be a directory")
  43.  
  44. dirprefix = sys.argv[2]
  45. for key in sorted(usemap):
  46.   print(key)
  47.   filename = dirprefix + key
  48.   print(filename)
  49.   if os.path.isdir(filename):
  50.     print("Path '", filename, "exists but is a directory instead of a file")
  51.   else:
  52.     destfile = open(filename, 'w+')
  53.     for subkey in sorted(usemap[key]):
  54.       print("\t", subkey)
  55.       destfile.write(usemap[key][subkey])
  56.     destfile.closed
Advertisement
Add Comment
Please, Sign In to add comment