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.master=frame
66 
67  def setScriptsList(self, scripts, directory):
68  self.directory=directory
69  self.scripts=scripts
70  tmpFrame=Frame(self.master, 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.runScript(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.master, 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.directory=directory
120  self.script=script
121  self.master=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.lines=[]
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.lines.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.save())
141  tmp.grid(row=r, column=0, sticky=N)
142 
143  tmp=Button(bottom, text="Add", command=self.addLine())
144  tmp.grid(row=r, column=1, sticky=N)
145 
146  tmp=Button(bottom, text="Remove", command=self.removeLine())
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.lines:
160  tmp=Entry(self.master)
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.master)
167  self.lines.append(tmp)
168  self.update()
169 
170  def removeLine(self):
171  self.lines.pop()
172  self.update()
173 
174  def save(self):
175  f=open(os.path.join(directory, 'temp.txt'),'w')
176 
177  for l in self.lines:
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()
scripting.printUsage
def printUsage(scriptName)
Definition: scripting.py:33
scripting.EditWindow.__init__
def __init__(self, master, directory, script)
Definition: scripting.py:117
scripting.EditWindow.removeLine
def removeLine(self)
Definition: scripting.py:170
scripting.fileExists
def fileExists(f)
Definition: scripting.py:25
scripting.pruneContexts
def pruneContexts(appDirs, allContexts)
Definition: scripting.py:43
scripting.App.setScriptsList
def setScriptsList(self, scripts, directory)
Definition: scripting.py:67
scripting.EditWindow.script
script
Definition: scripting.py:120
scripting.App.runScript
def runScript(self, script)
Definition: scripting.py:92
scripting.searchScripts
def searchScripts(scriptDir)
Definition: scripting.py:104
scripting.EditWindow.directory
directory
Definition: scripting.py:119
scripting.App.__init__
def __init__(self, master)
Definition: scripting.py:62
scripting.App.editScript
def editScript(self, script)
Definition: scripting.py:101
iCub::ctrl::dbscan::append
void append(const size_t index, shared_ptr< Epsilon_neighbours_t > en)
Definition: clustering.cpp:65
scripting.EditWindow
Definition: scripting.py:116
scripting.appEntry
Definition: scripting.py:56
scripting.EditWindow.addLine
def addLine(self)
Definition: scripting.py:165
scripting.EditWindow.master
master
Definition: scripting.py:121
scripting.App.directory
directory
Definition: scripting.py:68
scripting.EditWindow.update
def update(self)
Definition: scripting.py:157
scripting.EditWindow.lines
lines
Definition: scripting.py:127
scripting.App.scripts
scripts
Definition: scripting.py:69
scripting.App.master
master
Definition: scripting.py:65
scripting.isText
def isText(x)
Definition: scripting.py:53
scripting.App
Definition: scripting.py:61
scripting.EditWindow.save
def save(self)
Definition: scripting.py:174
scripting.ApplicationDescriptor
Definition: scripting.py:39