CoolisTheName007

Untitled

Dec 30th, 2012
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.89 KB | None | 0 0
  1. #!/usr/bin/python
  2. import time,sys,os,platform,subprocess
  3.  
  4. ###############################################
  5. # Configurable options #
  6. ###############################################
  7. #time between keystrokes
  8. key_press_delay = 0.01 #-kd
  9. #time for editor commands
  10. editor_wait_delay = 0.1 #-ed
  11. #name of window to autoswap to if able
  12. window_name = "Direwolf20 Pack" #-wn
  13. # removes .lua from filenames
  14. crop_endings = False #-ce
  15. # how long to delay start if autoswap fails
  16. switch_delay = 5 #-sd
  17. # compress if lua is installed
  18. try_compress = False #-tc
  19. #tab size in spaces
  20. tab_size=4 #-ts
  21. #inactivity delay (/minutes): pauses writting after in_delay minutes to stop being kicked for inactivity
  22. in_delay=5 #-id
  23. ###############################################
  24.  
  25. def description():
  26. print "Kazagistar\'s Computercraft Minecopy V2"
  27. print "(Command line based config and tabs by CoolisTheName007)"
  28. print "Usage: minecopy.py -arg1 value1 -arg2 value2 ... file1 file 2 ..."
  29. print "file1 .. file2 can be full paths, or relative to the current working directory or minecopy directory"
  30. print "Arguments:"
  31. print "-wn [window name]; default:%s" %window_name
  32. print "-kd [delay between key presses] default:%f" %key_press_delay
  33. print "-sd [window switch delay] default:%f" %switch_delay
  34. print "-ed [delay between editor commmands] default:%f " %editor_wait_delay
  35. print "-ce [remove .lua file endings] default:%s" %crop_endings
  36. print "-tc [compress Lua code] default:%s " %try_compress
  37. print "-ts [tab size in spaces] default: %d " %tab_size
  38. print "-id [pauses writting after in_delay minutes to stop being kicked for inactivity] default : %d " %in_delay
  39.  
  40. # location of lua compression script
  41. lua_path = os.path.join(os.path.dirname(__file__),"app_maximum.lua")
  42. #location of temporary compressed file
  43. temp_path = os.path.join(os.path.dirname(__file__),"TEMPORARY_LUA_FILE")
  44.  
  45. if platform.system() == "Linux":
  46. # Import libxdo, for key output
  47. from ctypes import *
  48. xdo=cdll.LoadLibrary("libxdo.so.2")
  49. xdo_context=xdo.xdo_new(None)
  50.  
  51. def typeout(string):
  52. xdo.xdo_type(xdo_context, 0, string, key_press_delay*1000000) # microseconds
  53.  
  54. def savefile():
  55. typekeys(["ctrl","Return","ctrl","Left","Return"])
  56.  
  57. def typekeys(keysyms): # separation might be useful eventually?
  58. for key in keysyms:
  59. xdo.xdo_keysequence(xdo_context, 0, key, key_press_delay*1000000) # microseconds
  60. time.sleep(editor_wait_delay)
  61.  
  62. if platform.system() == "Windows":
  63. def windowswitch():
  64. # Load the stuff for writing strings using com
  65. from win32com import client
  66. shell = client.Dispatch("WScript.Shell")
  67. shell.AppActivate(window_name)
  68.  
  69. import SendKeysCtypes as key
  70.  
  71. def typeout(string):
  72. newstring = ""
  73. for char in string:
  74. if char in ["{","}","[","]","+","^","%","~","(",")"]:
  75. char = "{%s}"%char
  76. newstring += char
  77. key.SendKeys(newstring, pause=key_press_delay, with_spaces=True, tab_size=tab_size, in_delay=in_delay, with_newlines=True)
  78. def enter_seq():
  79. sequence = ("{ENTER}")
  80. key.SendKeys(sequence, pause=editor_wait_delay)
  81. def savefile():
  82. sequence = ("{VK_CONTROL}{ENTER}{VK_CONTROL}{RIGHT}{ENTER}")
  83. key.SendKeys(sequence, pause=editor_wait_delay)
  84.  
  85. def loadfile(filepath):
  86. global try_compress
  87. if try_compress:
  88. try:
  89. print("Trying to compress")
  90. subprocess.call(["lua", lua_path, "-o", temp_path, "--quiet", "--maximum",filepath])
  91. print("File compressed")
  92. filepath = temp_path
  93. except:
  94. print("Optional compression failed, maybe Lua is not installed.")
  95. try_compress = False
  96. try:
  97. f = open(filepath)
  98. except IOError as e:
  99. print "I/O error({0}): {1}".format(e.errno, e.strerror)
  100. sys.exit()
  101. data = f.read()
  102. f.close()
  103. if try_compress:
  104. os.remove(temp_path)
  105. return data
  106.  
  107. def copyfile(filename,filepath):
  108. data = loadfile(filepath)
  109. if crop_endings:
  110. filename = filename.replace(".lua","")
  111. time.sleep(editor_wait_delay) # make sure it has time to start writing (otherwise 'rm' is typed 'm')
  112. typeout("rm "+filename)
  113. enter_seq()
  114. typeout("edit "+filename)
  115. enter_seq()
  116. time.sleep(editor_wait_delay) # make sure it has time to finish
  117. typeout(data)
  118. savefile()
  119.  
  120. if __name__ == "__main__":
  121. files=[]
  122. for i in range(1,len(sys.argv),2):
  123. if sys.argv[i] == "-kd":
  124. key_press_delay = float(sys.argv[i+1])
  125. continue
  126. if sys.argv[i] == "-ed":
  127. editor_wait_delay = float(sys.argv[i+1])
  128. continue
  129. if sys.argv[i] == "-wn":
  130. window_name = sys.argv[i+1]
  131. continue
  132. if sys.argv[i] == "-ce":
  133. crop_endings = sys.argv[i+1] in ['true', '1', 't', 'y', 'yes', 'yeah', 'yup', 'certainly', 'uh-huh']
  134. continue
  135. if sys.argv[i] == "-sd":
  136. switch_delay = float(sys.argv[i+1])
  137. continue
  138. if sys.argv[i] == "-tc":
  139. try_compress = sys.argv[i+1] in ['true', '1', 't', 'y', 'yes', 'yeah', 'yup', 'certainly', 'uh-huh']
  140. continue
  141. if sys.argv[i] == "-ts":
  142. tab_size = sys.argv[i+1]
  143. continue
  144. if sys.argv[i] == "-id":
  145. in_delay = sys.argv[i+1]
  146. continue
  147. if sys.argv[i][0] == "-":
  148. print "You entered an incorrect argument identifier or a file name starting with - (not accepted):"
  149. print sys.argv[i]
  150. description()
  151. sys.exit()
  152. files=sys.argv[i:]
  153. break
  154.  
  155.  
  156. if len(files) == 0:
  157. print "No files supplied."
  158. description()
  159. sys.exit()
  160. else:
  161. try:
  162. windowswitch()
  163. except:
  164. print ''
  165. print "Select minecraft window manually and make sure a computercraft terminal is open!"
  166. print "You have % seconds to click on the terminal or bad things will happen!!!" %switch_delay
  167. time.sleep(switch_delay)
  168. for filename in files:
  169. if os.path.isfile(os.path.join(os.getcwd(),filename)):
  170. filepath=os.path.join(os.getcwd(),filename)
  171. elif os.path.isfile(filename):
  172. filepath=filename
  173. else:
  174. print "File %s not found" %os.path.basename(filename)
  175. print "in current directory: %s" %(os.getcwd())
  176. print "using filename as path: %s" %filename
  177. print "Make sure you use forward slashes: /"
  178. sys.exit()
  179. filename=os.path.basename(filepath)
  180. print("Printing %s ...") %filename
  181. copyfile(filename,filepath)
  182. print("Done printing %s") %filename
Advertisement
Add Comment
Please, Sign In to add comment