Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import time,sys,os,platform,subprocess
- ###############################################
- # Configurable options #
- ###############################################
- #time between keystrokes
- key_press_delay = 0.01 #-kd
- #time for editor commands
- editor_wait_delay = 0.1 #-ed
- #name of window to autoswap to if able
- window_name = "Direwolf20 Pack" #-wn
- # removes .lua from filenames
- crop_endings = False #-ce
- # how long to delay start if autoswap fails
- switch_delay = 5 #-sd
- # compress if lua is installed
- try_compress = False #-tc
- #tab size in spaces
- tab_size=4 #-ts
- #inactivity delay (/minutes): pauses writting after in_delay minutes to stop being kicked for inactivity
- in_delay=5 #-id
- ###############################################
- def description():
- print "Kazagistar\'s Computercraft Minecopy V2"
- print "(Command line based config and tabs by CoolisTheName007)"
- print "Usage: minecopy.py -arg1 value1 -arg2 value2 ... file1 file 2 ..."
- print "file1 .. file2 can be full paths, or relative to the current working directory or minecopy directory"
- print "Arguments:"
- print "-wn [window name]; default:%s" %window_name
- print "-kd [delay between key presses] default:%f" %key_press_delay
- print "-sd [window switch delay] default:%f" %switch_delay
- print "-ed [delay between editor commmands] default:%f " %editor_wait_delay
- print "-ce [remove .lua file endings] default:%s" %crop_endings
- print "-tc [compress Lua code] default:%s " %try_compress
- print "-ts [tab size in spaces] default: %d " %tab_size
- print "-id [pauses writting after in_delay minutes to stop being kicked for inactivity] default : %d " %in_delay
- # location of lua compression script
- lua_path = os.path.join(os.path.dirname(__file__),"app_maximum.lua")
- #location of temporary compressed file
- temp_path = os.path.join(os.path.dirname(__file__),"TEMPORARY_LUA_FILE")
- if platform.system() == "Linux":
- # Import libxdo, for key output
- from ctypes import *
- xdo=cdll.LoadLibrary("libxdo.so.2")
- xdo_context=xdo.xdo_new(None)
- def typeout(string):
- xdo.xdo_type(xdo_context, 0, string, key_press_delay*1000000) # microseconds
- def savefile():
- typekeys(["ctrl","Return","ctrl","Left","Return"])
- def typekeys(keysyms): # separation might be useful eventually?
- for key in keysyms:
- xdo.xdo_keysequence(xdo_context, 0, key, key_press_delay*1000000) # microseconds
- time.sleep(editor_wait_delay)
- if platform.system() == "Windows":
- def windowswitch():
- # Load the stuff for writing strings using com
- from win32com import client
- shell = client.Dispatch("WScript.Shell")
- shell.AppActivate(window_name)
- import SendKeysCtypes as key
- def typeout(string):
- newstring = ""
- for char in string:
- if char in ["{","}","[","]","+","^","%","~","(",")"]:
- char = "{%s}"%char
- newstring += char
- key.SendKeys(newstring, pause=key_press_delay, with_spaces=True, tab_size=tab_size, in_delay=in_delay, with_newlines=True)
- def enter_seq():
- sequence = ("{ENTER}")
- key.SendKeys(sequence, pause=editor_wait_delay)
- def savefile():
- sequence = ("{VK_CONTROL}{ENTER}{VK_CONTROL}{RIGHT}{ENTER}")
- key.SendKeys(sequence, pause=editor_wait_delay)
- def loadfile(filepath):
- global try_compress
- if try_compress:
- try:
- print("Trying to compress")
- subprocess.call(["lua", lua_path, "-o", temp_path, "--quiet", "--maximum",filepath])
- print("File compressed")
- filepath = temp_path
- except:
- print("Optional compression failed, maybe Lua is not installed.")
- try_compress = False
- try:
- f = open(filepath)
- except IOError as e:
- print "I/O error({0}): {1}".format(e.errno, e.strerror)
- sys.exit()
- data = f.read()
- f.close()
- if try_compress:
- os.remove(temp_path)
- return data
- def copyfile(filename,filepath):
- data = loadfile(filepath)
- if crop_endings:
- filename = filename.replace(".lua","")
- time.sleep(editor_wait_delay) # make sure it has time to start writing (otherwise 'rm' is typed 'm')
- typeout("rm "+filename)
- enter_seq()
- typeout("edit "+filename)
- enter_seq()
- time.sleep(editor_wait_delay) # make sure it has time to finish
- typeout(data)
- savefile()
- if __name__ == "__main__":
- files=[]
- for i in range(1,len(sys.argv),2):
- if sys.argv[i] == "-kd":
- key_press_delay = float(sys.argv[i+1])
- continue
- if sys.argv[i] == "-ed":
- editor_wait_delay = float(sys.argv[i+1])
- continue
- if sys.argv[i] == "-wn":
- window_name = sys.argv[i+1]
- continue
- if sys.argv[i] == "-ce":
- crop_endings = sys.argv[i+1] in ['true', '1', 't', 'y', 'yes', 'yeah', 'yup', 'certainly', 'uh-huh']
- continue
- if sys.argv[i] == "-sd":
- switch_delay = float(sys.argv[i+1])
- continue
- if sys.argv[i] == "-tc":
- try_compress = sys.argv[i+1] in ['true', '1', 't', 'y', 'yes', 'yeah', 'yup', 'certainly', 'uh-huh']
- continue
- if sys.argv[i] == "-ts":
- tab_size = sys.argv[i+1]
- continue
- if sys.argv[i] == "-id":
- in_delay = sys.argv[i+1]
- continue
- if sys.argv[i][0] == "-":
- print "You entered an incorrect argument identifier or a file name starting with - (not accepted):"
- print sys.argv[i]
- description()
- sys.exit()
- files=sys.argv[i:]
- break
- if len(files) == 0:
- print "No files supplied."
- description()
- sys.exit()
- else:
- try:
- windowswitch()
- except:
- print ''
- print "Select minecraft window manually and make sure a computercraft terminal is open!"
- print "You have % seconds to click on the terminal or bad things will happen!!!" %switch_delay
- time.sleep(switch_delay)
- for filename in files:
- if os.path.isfile(os.path.join(os.getcwd(),filename)):
- filepath=os.path.join(os.getcwd(),filename)
- elif os.path.isfile(filename):
- filepath=filename
- else:
- print "File %s not found" %os.path.basename(filename)
- print "in current directory: %s" %(os.getcwd())
- print "using filename as path: %s" %filename
- print "Make sure you use forward slashes: /"
- sys.exit()
- filename=os.path.basename(filepath)
- print("Printing %s ...") %filename
- copyfile(filename,filepath)
- print("Done printing %s") %filename
Advertisement
Add Comment
Please, Sign In to add comment