Advertisement
hkbruvold

luawriter.py

Apr 25th, 2014
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. # function to easily add backspaces
  4. def insertBS(amount):
  5.     if amount > 1:
  6.         return "Send {Backspace %i}" %(amount)
  7.     elif amount == 1:
  8.         return "Send {Backspace}"
  9.     return ""
  10.  
  11. # function to insert newline (ahk code)
  12. def insertEnter():
  13.     return "Send {Enter}"
  14.  
  15. # function to insert space (ahk code)
  16. def insertSpace(amount):
  17.     if amount > 1:
  18.         return "Send {Space %i}" %(amount)
  19.     elif amount == 1:
  20.         return "Send {Space}"
  21.     return ""
  22.  
  23. # function to insert x newlines
  24. def nl(x):
  25.     return "\n"*x
  26.  
  27. # function to detect the amount of indentation
  28. def detectIndentation(text):
  29.     for i in range(len(text)):
  30.         if text[i] != " ":
  31.             return i
  32.     return 0
  33.  
  34. # function to add SendRaw
  35. def addSend(text):
  36.     return "SendRaw " + text.rstrip('\n').replace("%", "`%")
  37.  
  38. # Open and read lua code
  39. fileName = input("Type in filename: ")
  40. f = open(fileName, 'r')
  41. luaCode = f.readlines()
  42. f.close()
  43.  
  44. # initiate ahk code
  45. ahkCode = "numpad1::" + nl(1)
  46.  
  47. curInd = 0
  48.  
  49. # loop through lua code lines
  50. for line in luaCode:
  51.     # detecting and controlling indendtation
  52.     prevInd = curInd
  53.     curInd = detectIndentation(line)
  54.     if curInd < prevInd:
  55.         ahkCode += insertBS(prevInd - curInd) + nl(1)
  56.    
  57.     # remove unecessary leading spaces (indentation)
  58.     line = line.lstrip(' ')
  59.     if curInd > prevInd:
  60.         ahkCode += insertSpace(curInd - prevInd) + nl(1)
  61.    
  62.     if len(line.rstrip('\n')) != 0:
  63.         ahkCode += addSend(line) + nl(1)
  64.     ahkCode += insertEnter() + nl(1)
  65.  
  66. # add exit command
  67. ahkCode += nl(1) + "exitapp"
  68.  
  69. # save file
  70. ahkName = fileName.replace(".lua", ".ahk")
  71. f = open(ahkName, 'w')
  72. f.write(ahkCode)
  73. f.close()
  74.  
  75. input("Done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement