Package arcjobtool :: Package plugins :: Module XrslPlugin
[hide private]
[frames] | no frames]

Source Code for Module arcjobtool.plugins.XrslPlugin

  1  # -*- coding: iso-8859-15 -*- 
  2  # generated by wxGlade HG on Fri Aug 28 10:05:56 2009 
  3   
  4  import wx 
  5   
  6  # begin wxGlade: extracode 
  7  # end wxGlade 
  8   
  9  from wx.lib.dialogs import * 
 10   
 11  import os 
 12   
 13  from arcjobtool.ArcUtils import * 
 14  from arcjobtool.Tasks import * 
 15  from arcjobtool.plugins.Plugins import * 
 16  from arcjobtool.PlatformUtils import * 
 17   
 18  runScriptTemplate = """#!/bin/sh 
 19  chmod +x ./%s 
 20  ./%s 
 21  """ 
 22   
23 -class XrslTask(BaseTask):
24 - def __init__(self):
25 BaseTask.__init__(self, createRunScript=False) 26 27 self.description = "Xrsl" 28 29 # Task specific attributes 30 31 self.jobDescriptionFilename = "job.df" 32 self.jobDescription = """&(executable="/bin/echo") 33 (arguments="hello") 34 (inputFiles=("run.sh" "")) 35 (outputFiles=("result.txt" "")) 36 (stdout="stdout.txt") 37 (stderr="stderr.txt") 38 (wallTime="5 minutes") 39 """
40
41 - def onCreateRunScript(self, taskName, taskId):
42 """ 43 Abstract routine responsible for returning a 44 run-script for the job. 45 """ 46 return runScriptTemplate % (self.__mainFile, self.__mainFile)
47
48 - def onCreateJobDescription(self, taskName, taskId, taskDir):
49 """ 50 Abstract routines responsible for returning a jobdescription for 51 the job. 52 """ 53 54 # Create a managed job description 55 56 job = arc.JobDescription(); 57 job.Parse(str(self.jobDescription % {"id":taskId, "taskdir":taskDir})) 58 job.Identification.JobName = str(taskName) 59 return job
60
61 - def loadJobDescription(self):
62 """ 63 Loads the job description file from job definition dir. 64 """ 65 descrFile = open(os.path.join(self.workDir, self.jobDescriptionFilename), "r") 66 self.jobDescription = descrFile.read() 67 descrFile.close()
68
69 - def saveJobDescription(self):
70 """ 71 Saves the job definition file to job definition dir. 72 """ 73 descrFile = open(os.path.join(self.workDir, self.jobDescriptionFilename), "w") 74 descrFile.write(self.jobDescription) 75 descrFile.close()
76 77
78 - def onSaveConfig(self, config):
79 """ 80 Save configuration data specific to this plugin. 81 """ 82 config.add_section("xrslplugin") 83 config.set("xrslplugin", "descrfilename", self.jobDescriptionFilename) 84 self.saveJobDescription()
85
86 - def onLoadConfig(self, config):
87 """ 88 Loads configuration settings specific to this plugin. 89 """ 90 try: 91 self.jobDescriptionFilename = config.get("xrslplugin", "descrfilename") 92 except: 93 return 94 95 self.loadJobDescription()
96
97 -class XrslPlugin(PluginBase):
98 - def onInit(self):
99 self.__task = XrslTask()
100
101 - def onDestroy(self):
102 pass
103
104 - def onEdit(self):
105 dlg = XrslPluginDialog(None, -1, "") 106 dlg.task = self.__task 107 dlg.ShowModal() 108 dlg.Destroy() 109 self.__task = dlg.task
110
111 - def onSave(self, workDir):
112 (taskLocation, taskDirName) = os.path.split(workDir) 113 self.__task.name = taskDirName.split(".")[0] 114 self.__task.workDir = workDir 115 self.__task.save()
116
117 - def onLoad(self, workDir):
118 (taskLocation, taskDirName) = os.path.split(workDir) 119 self.__task.name = taskDirName.split(".")[0] 120 self.__task.workDir = workDir 121 self.__task.load()
122
123 - def onClean(self):
124 self.__task.clean()
125
126 - def onSetup(self):
127 self.__task.setup()
128
129 - def onGetId(self):
130 return "XrslPlugin"
131
132 - def onGetVersion(self):
133 return (0,1,0)
134
135 - def onGetDescription(self):
136 return "Xrsl job definition plugin."
137
138 - def onGetShortName(self):
139 return "Xrsl job definition."
140
141 - def onNeedSave(self):
142 return self.__task.dirty
143
144 - def onGetTask(self):
145 return self.__task
146 147 id = property(onGetId) 148 version = property(onGetVersion) 149 shortname = property(onGetShortName) 150 description = property(onGetDescription) 151 task = property(onGetTask)
152 153
154 -class XrslPluginDialog(wx.Dialog):
155 - def __init__(self, *args, **kwds):
156 157 self.__applicationDir = os.path.dirname(sys.argv[0]) 158 159 if os.environ.has_key("ARCJOBTOOL_SHARE"): 160 self.__imageDir = os.path.join(os.environ["ARCJOBTOOL_SHARE"], "images") 161 else: 162 self.__imageDir = os.path.join(self.__applicationDir, "../share/arcjobtool/images") 163 164 ARCGUI_VALIDATE_XRSL = os.path.join(self.__imageDir, "preview-xrsl.png") 165 ARCGUI_EDIT_XRSL = os.path.join(self.__imageDir, "document-new.png") 166 ARCGUI_IMPORT_XRSL = os.path.join(self.__imageDir, "folder-open.png") 167 168 # begin wxGlade: XrslPluginDialog.__init__ 169 kwds["style"] = wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER|wx.THICK_FRAME 170 wx.Dialog.__init__(self, *args, **kwds) 171 self.notebook_1 = wx.Notebook(self, -1, style=0) 172 self.generalPane = wx.Panel(self.notebook_1, -1) 173 self.validateDescriptionButton = wx.BitmapButton(self.generalPane, -1, wx.Bitmap(ARCGUI_VALIDATE_XRSL,wx.BITMAP_TYPE_ANY), style=wx.NO_BORDER) 174 self.editDescriptionButton = wx.BitmapButton(self.generalPane, -1, wx.Bitmap(ARCGUI_EDIT_XRSL,wx.BITMAP_TYPE_ANY), style=wx.NO_BORDER) 175 self.importJobDescriptionButton = wx.BitmapButton(self.generalPane, -1, wx.Bitmap(ARCGUI_IMPORT_XRSL,wx.BITMAP_TYPE_ANY), style=wx.NO_BORDER) 176 self.jobDescriptionText = wx.TextCtrl(self.generalPane, -1, "", style=wx.TE_MULTILINE|wx.HSCROLL|wx.TE_RICH) 177 self.inputFilePane = wx.Panel(self.notebook_1, -1) 178 self.inputFilesLabel = wx.StaticText(self.inputFilePane, -1, "Input files") 179 self.inputFileList = wx.ListBox(self.inputFilePane, -1, choices=[]) 180 self.addFileButton = wx.Button(self.inputFilePane, -1, "Add...") 181 self.createFileButton = wx.Button(self.inputFilePane, -1, "Create...") 182 self.removeFileButton = wx.Button(self.inputFilePane, -1, "Remove") 183 self.clearFilesButton = wx.Button(self.inputFilePane, -1, "Clear") 184 self.inputFileEditButton = wx.Button(self.inputFilePane, -1, "Edit...") 185 self.parameterPane = wx.Panel(self.notebook_1, -1) 186 self.parameterFilesLabel = wx.StaticText(self.parameterPane, -1, "Parameter substitution files") 187 self.paramFilesList = wx.ListBox(self.parameterPane, -1, choices=[]) 188 self.AddParamFileButton = wx.Button(self.parameterPane, -1, "Add...") 189 self.removeParamFileButton = wx.Button(self.parameterPane, -1, "Remove") 190 self.clearParamFilesButton = wx.Button(self.parameterPane, -1, "Clear") 191 self.paramFileEditButton = wx.Button(self.parameterPane, -1, "Edit...") 192 self.static_line_1 = wx.StaticLine(self.parameterPane, -1) 193 self.sweepSizeLabel = wx.StaticText(self.parameterPane, -1, "Sweep size", style=wx.ALIGN_RIGHT) 194 self.sweepSizeSpinner = wx.SpinCtrl(self.parameterPane, -1, "0", min=1, max=1000) 195 self.saveButton = wx.Button(self, -1, "Save") 196 self.closeButton = wx.Button(self, -1, "Close") 197 198 self.__set_properties() 199 self.__do_layout() 200 201 self.Bind(wx.EVT_BUTTON, self.onValidateDescription, self.validateDescriptionButton) 202 self.Bind(wx.EVT_BUTTON, self.onEditDescription, self.editDescriptionButton) 203 self.Bind(wx.EVT_BUTTON, self.onImportJobDescription, self.importJobDescriptionButton) 204 self.Bind(wx.EVT_BUTTON, self.onAddFile, self.addFileButton) 205 self.Bind(wx.EVT_BUTTON, self.onCreateFile, self.createFileButton) 206 self.Bind(wx.EVT_BUTTON, self.onRemoveFile, self.removeFileButton) 207 self.Bind(wx.EVT_BUTTON, self.onClearFiles, self.clearFilesButton) 208 self.Bind(wx.EVT_BUTTON, self.onEditInputFile, self.inputFileEditButton) 209 self.Bind(wx.EVT_BUTTON, self.onAddParamFile, self.AddParamFileButton) 210 self.Bind(wx.EVT_BUTTON, self.onRemoveParamFileButton, self.removeParamFileButton) 211 self.Bind(wx.EVT_BUTTON, self.onClearParamFilesButton, self.clearParamFilesButton) 212 self.Bind(wx.EVT_BUTTON, self.onEditParamFile, self.paramFileEditButton) 213 self.Bind(wx.EVT_BUTTON, self.onSave, self.saveButton) 214 self.Bind(wx.EVT_BUTTON, self.onClose, self.closeButton)
215 # end wxGlade 216
217 - def __set_properties(self):
218 # begin wxGlade: XrslPluginDialog.__set_properties 219 self.SetTitle("XRSL job definition") 220 self.SetSize((488, 421)) 221 self.validateDescriptionButton.SetToolTipString("Validate job description") 222 self.validateDescriptionButton.SetSize(self.validateDescriptionButton.GetBestSize()) 223 self.editDescriptionButton.SetToolTipString("Edit with external editor") 224 self.editDescriptionButton.SetSize(self.editDescriptionButton.GetBestSize()) 225 self.importJobDescriptionButton.SetToolTipString("Import job description") 226 self.importJobDescriptionButton.SetSize(self.importJobDescriptionButton.GetBestSize()) 227 self.jobDescriptionText.SetFont(wx.Font(10, wx.SCRIPT, wx.NORMAL, wx.NORMAL, 0, "Courier")) 228 self.sweepSizeLabel.SetMinSize((120, -1))
229 # end wxGlade 230
231 - def __do_layout(self):
232 # begin wxGlade: XrslPluginDialog.__do_layout 233 sizer_1 = wx.BoxSizer(wx.VERTICAL) 234 sizer_2 = wx.BoxSizer(wx.HORIZONTAL) 235 sizer_11 = wx.BoxSizer(wx.VERTICAL) 236 sizer_12 = wx.BoxSizer(wx.HORIZONTAL) 237 sizer_13 = wx.BoxSizer(wx.HORIZONTAL) 238 sizer_14 = wx.BoxSizer(wx.VERTICAL) 239 sizer_9 = wx.BoxSizer(wx.VERTICAL) 240 sizer_8 = wx.BoxSizer(wx.HORIZONTAL) 241 sizer_9_copy = wx.BoxSizer(wx.VERTICAL) 242 sizer_34 = wx.BoxSizer(wx.VERTICAL) 243 sizer_35 = wx.BoxSizer(wx.HORIZONTAL) 244 sizer_35.Add(self.validateDescriptionButton, 0, wx.ADJUST_MINSIZE, 0) 245 sizer_35.Add(self.editDescriptionButton, 0, wx.ADJUST_MINSIZE, 0) 246 sizer_35.Add(self.importJobDescriptionButton, 0, wx.ADJUST_MINSIZE, 0) 247 sizer_34.Add(sizer_35, 0, wx.ALL|wx.EXPAND, 4) 248 sizer_34.Add(self.jobDescriptionText, 1, wx.ALL|wx.EXPAND|wx.ADJUST_MINSIZE, 4) 249 self.generalPane.SetSizer(sizer_34) 250 sizer_9.Add(self.inputFilesLabel, 0, wx.LEFT|wx.TOP|wx.ADJUST_MINSIZE, 4) 251 sizer_8.Add(self.inputFileList, 1, wx.ALL|wx.EXPAND|wx.ADJUST_MINSIZE, 4) 252 sizer_9_copy.Add(self.addFileButton, 0, wx.ADJUST_MINSIZE, 0) 253 sizer_9_copy.Add(self.createFileButton, 0, wx.ADJUST_MINSIZE, 0) 254 sizer_9_copy.Add(self.removeFileButton, 0, wx.ADJUST_MINSIZE, 0) 255 sizer_9_copy.Add(self.clearFilesButton, 0, wx.ADJUST_MINSIZE, 0) 256 sizer_9_copy.Add(self.inputFileEditButton, 0, wx.TOP|wx.ADJUST_MINSIZE, 7) 257 sizer_8.Add(sizer_9_copy, 0, wx.ALL|wx.EXPAND, 4) 258 sizer_9.Add(sizer_8, 1, wx.EXPAND, 0) 259 self.inputFilePane.SetSizer(sizer_9) 260 sizer_11.Add(self.parameterFilesLabel, 0, wx.LEFT|wx.TOP|wx.ADJUST_MINSIZE, 4) 261 sizer_13.Add(self.paramFilesList, 1, wx.ALL|wx.EXPAND|wx.ADJUST_MINSIZE, 4) 262 sizer_14.Add(self.AddParamFileButton, 0, wx.ADJUST_MINSIZE, 0) 263 sizer_14.Add(self.removeParamFileButton, 0, wx.ADJUST_MINSIZE, 0) 264 sizer_14.Add(self.clearParamFilesButton, 0, wx.ADJUST_MINSIZE, 0) 265 sizer_14.Add(self.paramFileEditButton, 0, wx.TOP|wx.ADJUST_MINSIZE, 7) 266 sizer_13.Add(sizer_14, 0, wx.ALL|wx.EXPAND, 4) 267 sizer_11.Add(sizer_13, 1, wx.EXPAND, 0) 268 sizer_11.Add(self.static_line_1, 0, wx.TOP|wx.EXPAND, 5) 269 sizer_12.Add(self.sweepSizeLabel, 0, wx.ALIGN_CENTER_VERTICAL|wx.ADJUST_MINSIZE, 0) 270 sizer_12.Add(self.sweepSizeSpinner, 0, wx.TOP|wx.ADJUST_MINSIZE, 2) 271 sizer_11.Add(sizer_12, 0, wx.TOP|wx.BOTTOM|wx.EXPAND, 4) 272 self.parameterPane.SetSizer(sizer_11) 273 self.notebook_1.AddPage(self.generalPane, "XRSL") 274 self.notebook_1.AddPage(self.inputFilePane, "Input files") 275 self.notebook_1.AddPage(self.parameterPane, "Parameters") 276 sizer_1.Add(self.notebook_1, 1, wx.ALL|wx.EXPAND, 8) 277 sizer_2.Add(self.saveButton, 0, wx.ADJUST_MINSIZE, 0) 278 sizer_2.Add(self.closeButton, 0, wx.ADJUST_MINSIZE, 0) 279 sizer_1.Add(sizer_2, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.ALIGN_CENTER_HORIZONTAL, 4) 280 self.SetSizer(sizer_1) 281 self.Layout()
282 # end wxGlade 283
284 - def __setupInputFiles(self):
285 """ 286 Setup listbox for input files and choice box 287 for selecting main file. 288 """ 289 self.inputFileList.Clear() 290 291 filenameDict = {} 292 count = 0 293 294 # Fill listbox and choice box 295 296 if self.__task!=None: 297 for inputFile in self.__task.inputFiles.keys(): 298 self.inputFileList.Append(inputFile) 299 filenameDict[inputFile] = count 300 count = count + 1
301
302 - def __setupSweepFiles(self):
303 304 self.paramFilesList.Clear() 305 306 # Fill listbox 307 308 if self.__task!=None: 309 for sweepFile in self.__task.sweepFiles.keys(): 310 self.paramFilesList.Append(sweepFile)
311 312
313 - def onSave(self, event): # wxGlade: XrslPluginDialog.<event_handler>
314 #self.__task.cpuTime = int(self.cpuTimeText.GetValue()) 315 #self.__task.notify = self.notifyText.GetValue() 316 #self.__task.memory = int(self.memoryText.GetValue()) 317 self.__task.sweepSize = int(self.sweepSizeSpinner.GetValue()) 318 self.__task.jobDescription = str(self.jobDescriptionText.GetValue()) 319 self.__task.dirty = True 320 self.Close()
321
322 - def onClose(self, event): # wxGlade: XrslPluginDialog.<event_handler>
323 self.__task.dirty = False 324 self.Close() 325
326 - def setTask(self, task):
327 self.__task = task 328 329 #self.jobNameText.SetValue(self.__task.name) 330 #self.cpuTimeText.SetValue(str(self.__task.cpuTime)) 331 #self.notifyText.SetValue(self.__task.notify) 332 #self.memoryText.SetValue(str(self.__task.memory)) 333 self.sweepSizeSpinner.SetValue(int(self.__task.sweepSize)) 334 self.jobDescriptionText.SetValue(self.task.jobDescription) 335 336 self.__setupInputFiles() 337 self.__setupSweepFiles()
338
339 - def getTask(self):
340 return self.__task
341
342 - def onAddFile(self, event): # wxGlade: XrslPluginDialog.<event_handler>
343 inputFilename = wx.FileSelector("Add input file", default_path=os.getcwd()) 344 if inputFilename != "": 345 self.__task.addAndCopyInputFile(inputFilename) 346 self.__setupInputFiles() 347
348 - def onRemoveFile(self, event): # wxGlade: XrslPluginDialog.<event_handler>
349 if self.inputFileList.GetStringSelection()!="": 350 self.__task.removeInputFile(self.inputFileList.GetStringSelection()) 351 self.__setupInputFiles() 352
353 - def onClearFiles(self, event): # wxGlade: XrslPluginDialog.<event_handler>
354 self.__task.clearInputFiles() 355 self.__setupInputFiles() 356
357 - def onAddParamFile(self, event): # wxGlade: XrslPluginDialog.<event_handler>
358 result = singleChoiceDialog(self, "Add file for substitution", "Input files", self.__task.inputFiles) 359 if result.accepted: 360 self.__task.addSweepFile(result.selection) 361 self.__setupSweepFiles() 362
363 - def onRemoveParamFileButton(self, event): # wxGlade: XrslPluginDialog.<event_handler>
364 if self.paramFilesList.GetStringSelection()!="": 365 self.__task.removeSweepFile(self.paramFilesList.GetStringSelection()) 366 self.__setupSweepFiles() 367
368 - def onClearParamFilesButton(self, event): # wxGlade: XrslPluginDialog.<event_handler>
369 self.__task.clearSweepFiles() 370 self.__setupSweepFiles() 371
372 - def onEditInputFile(self, event): # wxGlade: XrslPluginDialog.<event_handler>
373 if self.inputFileList.GetStringSelection()!="": 374 fullPath = os.path.join(self.__task.workDir,self.inputFileList.GetStringSelection()) 375 editFile(fullPath) 376
377 - def onEditParamFile(self, event): # wxGlade: XrslPluginDialog.<event_handler>
378 if self.paramFilesList.GetStringSelection()!="": 379 fullPath = os.path.join(self.__task.workDir,self.paramFilesList.GetStringSelection()) 380 editFile(fullPath) 381
382 - def onCreateFile(self, event): # wxGlade: XrslPluginDialog.<event_handler>
383 result = textEntryDialog(self, "Create file", "Filename", "noname.txt") 384 385 if result.accepted: 386 fullPath = os.path.join(self.__task.workDir, str(result.text)) 387 388 newFile = open(fullPath, "w") 389 newFile.close() 390 391 editFile(fullPath) 392 393 self.__task.addAndCopyInputFile(fullPath, copyFile = False) 394 self.__setupInputFiles() 395
396 - def onValidateDescription(self, event): # wxGlade: XrslPluginDialog.<event_handler>
397 """ 398 Validate job description. 399 """ 400 401 currDir = os.getcwd() 402 403 os.chdir(self.__task.workDir) 404 405 job = ManagedJobDescription() 406 407 self.__task.jobDescription = str(self.jobDescriptionText.GetValue()) 408 409 #try: 410 jobDescription = self.__task.jobDescription % {"taskdir":self.__task.workDir} 411 #except: 412 # pass 413 414 print jobDescription 415 416 if job.Parse(str(jobDescription)) and job.UnParse("xrsl"): 417 wx.MessageBox("Job description checks out ok.") 418 else: 419 wx.MessageBox("Job description does not validate. Please check log for more details.") 420 421 os.chdir(currDir) 422
423 - def onEditDescription(self, event): # wxGlade: XrslPluginDialog.<event_handler>
424 """ 425 Edit job description with external editor. 426 """ 427 editFile(os.path.join(self.__task.workDir, self.__task.jobDescriptionFilename)) 428 self.__task.loadJobDescription() 429
430 - def onImportJobDescription(self, event): # wxGlade: XrslPluginDialog.<event_handler>
431 inputFilename = wx.FileSelector("Import XRSL file", default_path=os.getcwd()) 432 if inputFilename != "": 433 descrFile = open(inputFilename, "r") 434 jobDescription = descrFile.read() 435 descrFile.close() 436 437 self.jobDescriptionText.SetValue(jobDescription) 438 439 task = property(getTask, setTask) 440 441 442 443 # end of class XrslPluginDialog 444 445 if __name__ == "__main__": 446 app = wx.PySimpleApp(0) 447 wx.InitAllImageHandlers() 448 mainDialog = XrslPluginDialog(None, -1, "") 449 app.SetTopWindow(mainDialog) 450 mainDialog.Show() 451 app.MainLoop() 452