Skip to content

Instantly share code, notes, and snippets.

@blackball
Created July 26, 2014 07:41
Show Gist options
  • Save blackball/363d4b4714ed431a8b30 to your computer and use it in GitHub Desktop.
Save blackball/363d4b4714ed431a8b30 to your computer and use it in GitHub Desktop.
Lazy pipe executable task in python, wrote in ~3 years ago.
#!/usr/bin/python
"""
Simulate a command pipe in windows.
reason: I always need to do batch processing
in my dailylife, rename a bunch of files,
generate some sets from it, and move files
into different folders. Or, I have a module,
and I need to processing lots of file, but,
this module can only process one at most, and I
don't skillful in shell or CMD command, so,
python become my choice.
"""
class Simulator:
'''
Execute multiple missions using python in windows.
'''
def __init__(self):
self.cmds = []
self.paths = []
def isExist(self, cmd):
sz = self.cmds.__len__()
for i in xrange(sz):
if cmd == self.cmds[i]:
return i
return -1
def registe(self, names_paths):
'''
add a application into
list.
'''
for item in names_paths.items():
if self.isExist(item[0]) != -1:
print "CMD %s exists, "\
"please rename to anothe one." \
% item[0]
else:
self.cmds.append(item[0])
self.paths.append(item[1])
def run(self, cmd, args="", inShell=True):
'''
run a command
'''
from subprocess import check_call
# get original module
idx = self.isExist(cmd)
if idx == -1:
print "CMD %s not exists!" % cmd
return None
#print [self.paths[idx], args]
return check_call([self.paths[idx]] + args.split(),shell = inShell)
if __name__ == '__main__':
# resigster module
# right command
# then do it.
s = Simulator()
exe0 = {"v":"vi"}
exe1 = {"p":"python"}
s.registe(exe0)
s.registe(exe1)
s.run("p")
s.run("v")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment