iCub-main
scripting.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
3 
18 
19 import fnmatch
20 import os
21 import sys
22 import subprocess
23 from Tkinter import *
24 
25 def fileExists(f):
26  try:
27  file=open(f)
28  except IOError:
29  return 0
30  else:
31  return 1
32 
33 def printUsage(scriptName):
34  print "scripting.py: a python gui for scripting"
35  print "Usage:"
36  print scriptName,
37  print "dir"
38 
40  title=''
41  applications=[]
42 
43 def pruneContexts(appDirs, allContexts):
44  ret=[]
45  for ap in appDirs:
46  for c in allContexts:
47  #print ap,
48  #print c
49  if (c==ap):
50  ret.append(c)
51  return ret
52 
53 def isText(x):
54  return fnmatch.fnmatch(x, '*.txt')
55 
56 class appEntry:
57  path=''
58  context=''
59  xmls=[]
60 
61 class App:
62  def __init__(self, master):
63  frame = Frame(master)
64  frame.pack()
65  self.mastermaster=frame
66 
67  def setScriptsList(self, scripts, directory):
68  self.directorydirectory=directory
69  self.scriptsscripts=scripts
70  tmpFrame=Frame(self.mastermaster, relief=SUNKEN)
71 
72  tmpFrame.columnconfigure(0, minsize=150)
73  tmpFrame.columnconfigure(1, minsize=150)
74 
75  Label(tmpFrame, text="Scripts:").grid(row=0,column=0, sticky=N+W)
76 
77  r=0
78  c=1
79 
80  for sc in scripts:
81  tmpFrame.grid(row=r, column=c, sticky=W)
82  tmpApp=Label(tmpFrame, text=sc, relief=SUNKEN).grid(row=r, column=0, sticky=N+W+E)
83 
84  tmp=Button(tmpFrame, text="Run", command=lambda i=sc:self.runScriptrunScript(i))
85  tmp.grid(row=r, column=2, sticky=N)
86  #tmp=Button(tmpFrame, text="Edit", command=lambda i=sc:self.editScript(i))
87  #tmp.grid(row=r, column=3, sticky=N)
88  #r=r+1
89 
90  r=r+1
91 
92  def runScript(self, script):
93  f=open(os.path.join(directory, script),'r')
94  for line in f:
95  line=line.rstrip('\r\n')
96  #print line
97  #ret=subprocess.Popen('cmd -c', line);
98  os.system(line)
99  f.close()
100 
101  def editScript(self, script):
102  w=EditWindow(self.mastermaster, directory, script)
103 
104 def searchScripts(scriptDir):
105  all=os.listdir(scriptDir)
106 
107  ret=[]
108 
109  for c in all:
110  if (not os.path.isdir(c)):
111  ret.append(c)
112 
113  ret.sort();
114  return ret
115 
117  def __init__(self, master, directory, script):
118  frame=Toplevel()
119  self.directorydirectory=directory
120  self.scriptscript=script
121  self.mastermaster=frame
122 
123  Label(frame, text=script).grid(row=0, column=0, sticky=W)
124  bottom=Frame(frame).grid(row=1, column=0)
125  f=open(os.path.join(directory, script),'r')
126 
127  self.lineslines=[]
128  r=1
129  for line in f:
130  line=line.rstrip('\r\n')
131  tmp=Entry(bottom)
132  if (not line==[]):
133  print line
134  tmp.insert(END, 'ciao')
135  self.lineslines.append(tmp)
136  tmp.grid(row=r,column=0)
137  r=r+1
138  f.close()
139 
140  tmp=Button(bottom, text="Save", command=self.savesave())
141  tmp.grid(row=r, column=0, sticky=N)
142 
143  tmp=Button(bottom, text="Add", command=self.addLineaddLine())
144  tmp.grid(row=r, column=1, sticky=N)
145 
146  tmp=Button(bottom, text="Remove", command=self.removeLineremoveLine())
147  tmp.grid(row=r, column=2, sticky=N)
148 
149  #finally place window close to top left corner
150  #frame.update_idletasks() #update current geometry
151  width=frame.winfo_width()
152  height=frame.winfo_height()
153  rootx=master.winfo_rootx()
154  rooty=master.winfo_rooty()
155  #frame.geometry('%dx%d+%d+%d' %(width, height, rootx, rooty))
156 
157  def update(self):
158  r=0
159  for l in self.lineslines:
160  tmp=Entry(self.mastermaster)
161  tmp.insert(END, l)
162  tmp.grid(row=r,column=0)
163  r=r+1
164 
165  def addLine(self):
166  tmp=Entry(self.mastermaster)
167  self.lineslines.append(tmp)
168  self.updateupdate()
169 
170  def removeLine(self):
171  self.lineslines.pop()
172  self.updateupdate()
173 
174  def save(self):
175  f=open(os.path.join(directory, 'temp.txt'),'w')
176 
177  for l in self.lineslines:
178  tmp=l.get()
179  f.write(tmp)
180  f.close()
181 
182 if __name__ == '__main__':
183 
184  argc = len(sys.argv)
185 
186  if (argc==2):
187  directory=sys.argv[1]
188  else:
189  printUsage(sys.argv[0])
190  sys.exit(1)
191 
192  scripts=searchScripts(directory)
193  print scripts
194 
195  root = Tk()
196  app = App(root)
197  app.setScriptsList(scripts, directory)
198 
199  root.mainloop()
def editScript(self, script)
Definition: scripting.py:101
def __init__(self, master)
Definition: scripting.py:62
def setScriptsList(self, scripts, directory)
Definition: scripting.py:67
def runScript(self, script)
Definition: scripting.py:92
def removeLine(self)
Definition: scripting.py:170
def __init__(self, master, directory, script)
Definition: scripting.py:117
void append(const size_t index, shared_ptr< Epsilon_neighbours_t > en)
Definition: clustering.cpp:65
def printUsage(scriptName)
Definition: scripting.py:33
def searchScripts(scriptDir)
Definition: scripting.py:104
def pruneContexts(appDirs, allContexts)
Definition: scripting.py:43
def isText(x)
Definition: scripting.py:53
def fileExists(f)
Definition: scripting.py:25