This file is indexed.

/usr/share/pyshared/Epigrass/epigrass.py is in epigrass 2.0.4-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging, os
import logging.handlers
LOG_FILENAME = os.path.join(os.environ['HOME'],'epigrass_errors.out')
epigrassLogger = logging.getLogger('epigrassLogger')
epigrassLogger.setLevel(logging.ERROR)
handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=20000, backupCount=1)
epigrassLogger.addHandler(handler)

try:
    from PyQt4 import QtCore, QtGui
    from PyQt4.QtGui import *
except ImportError: 
    epigrassLogger.error("Please install PyQT 4")
    print "Please install PyQT 4"

from Epigrass.manager import *
import threading,  subprocess,  glob
from Epigrass.Ui_cpanel4new import Ui_MainWindow
from Epigrass.Ui_about4 import Ui_aboutDialog as aboutDialog
import sys,ConfigParser, string, copy, commands,getpass
import Epigrass.epiplay as epi
from Epigrass import spread,  dgraph
try:
    import psyco
    psyco.full()
except:
    epigrassLogger.info("Install Psyco for better performance." )
    pass


class MainWindow_Impl(QtGui.QMainWindow, Ui_MainWindow):
    sequenceNumber = 1
    windowList = []
    def __init__(self):
        QtGui.QMainWindow.__init__(self, None)
        self.app=app
        self.QtCore = QtCore
        self.setupUi(self)
        #determining the user's home directory
        home = os.curdir                        # Default
        if 'HOME' in os.environ:
            home = os.environ['HOME']
        elif os.name == 'posix':
            home = os.path.expanduser("~/")
        elif os.name == 'nt':                   
            if 'HOMEPATH' in os.environ:
                if 'HOMEDRIVE' in os.environ:
                    home = os.environ['HOMEDRIVE'] + os.environ['HOMEPATH']
                else:
                    home = os.environ['HOMEPATH']
        self.home = home
        self.epigrassrc = os.path.join(home, ".epigrassrc")
        # set Translation
        self.curdir = os.getcwd()
        self.rootdir = os.getcwd()
        if os.access(self.epigrassrc,os.F_OK):
            try:
                self.conf = self.loadRcFile(self.epigrassrc)
                lang = self.conf['settings.language']
            except: 
                lang = ''
            #print lang
        else: lang = ''
        tr = QtCore.QTranslator(app)
        loadLang(app,tr,lang)
       
        # Overload connections
        self.connect(self.editButton,QtCore.SIGNAL("released()"),self.editScript)
        self.connect(self.chooseButton,QtCore.SIGNAL("released()"),self.chooseScript)
        self.connect(self.buttonExit,QtCore.SIGNAL("released()"),self.onExit)
        self.connect(self.buttonRun,QtCore.SIGNAL("released()"),self.onRun)#Thread)
        self.connect(self.buttonHelp,QtCore.SIGNAL("released()"),self.onHelp)
        self.connect(self.dbBackup,QtCore.SIGNAL("released()"),self.onDbBackup)
        self.connect(self.dbInfo,QtCore.SIGNAL("released()"),self.onDbInfo)
#        self.connect(self.repOpen,QtCore.SIGNAL("released()"),self.onRepOpen)
        self.connect(self.playButton,QtCore.SIGNAL("released()"),self.onPlayButton)
#        self.connect(self.playButton_2D,QtCore.SIGNAL("released()"),self.onPlayButton_2D)
        self.connect(self.dbscanButton,QtCore.SIGNAL("released()"),self.onVisual)
        self.connect(self.consensusButton,QtCore.SIGNAL("released()"),self.onConsensus)
        self.connect(self.tableList,QtCore.SIGNAL("activated(int)"),self.getVariables)
        self.connect(self.dbType,QtCore.SIGNAL("activated(int)"),self.setBackend)
        QtCore.QMetaObject.connectSlotsByName(self)
        self.upGUITimer = QtCore.QTimer()
        self.connect(self.upGUITimer, QtCore.SIGNAL("timeout()"), app.processEvents)
        self.upGUITimer.start(100)
        
        usern = getpass.getuser() #Get the user name
        #check if rc file exists if not, creates one
        if not os.access(self.epigrassrc,os.F_OK):
            self.initRc()

        else:#read existing configuration file
            self.conf = self.loadRcFile(self.epigrassrc)
            self.fillGui(self.conf)
        self.sim = None
            
    def openGraphDisplay(self, shp='', namefield='', geocfield='', nlist=[], elist=[]):
        """
        Starts the Qt map display
        shp: shapefile fname
        """
        self.graphDisplay = dgraph.MapWindow()
        self.windowList.append(self.graphDisplay)
        #self.graphDisplay.move(self.x()+40, self.y()+40)
        if shp:
            self.graphDisplay.drawMap(shp, namefield, geocfield)
        else:
            self.graphDisplay.drawGraph(nlist, elist)
        self.graphDisplay.show()
    
    def initRc(self):
        """
        Initializes the epigrassrc file.
        """
        usern = getpass.getuser() #Get the user name
        os.mknod(self.epigrassrc)
        f = open(self.epigrassrc,'w')
        
        rcskel = ['#EpiGrass Configuration File',
            '[model]',
            'script=script.epg',
            '[database]',
            'backend=sqlite',
            'host=localhost',
            'port=3306',
            'user=epigrass',
            '[settings]',
            'name=John Doe',
            'editor=kate',
            'pdfviewer=kghostview',
            'language=en'
            ]
        for i in rcskel:
           f.write(i)
           f.write('\n')
        f.close()
        #fills the gui with default values
        self.conf = self.loadRcFile(self.epigrassrc)
        self.fillGui(self.conf)
        os.chdir(self.curdir)
    
    def updateRc(self):
        """
        Updates the epigrassrc file with the contents of the configuration
        dictionary.
        """
        usern = getpass.getuser() #Get the user name
        
        if os.access(self.epigrassrc,os.F_OK):
            os.remove(self.epigrassrc)
            os.mknod(self.epigrassrc)
        f = open(self.epigrassrc,'w')
        
        rcskel = ['#EpiGrass Configuration File',
            '[model]',
            'script=%s'%self.conf['model.script'],
            '[database]',
            'backend=%s'%self.conf['database.backend'],
            'host=%s'%self.conf['database.host'],
            'port=%s'%self.conf['database.port'],
            'user=%s'%self.conf['database.user'],
            '[settings]',
            'name=%s'%self.conf['settings.name'],
            'editor=%s'%self.conf['settings.editor'],
            'pdfviewer=%s'%self.conf['settings.pdfviewer'],
            'language=%s'%self.conf['settings.language']
            ]
        for i in rcskel:
           f.write(i)
           f.write('\n')
        f.close()
    
    
    def loadRcFile(self,fname,config={}):
        """
        Loads epigrassrc.
        Returns a dictionary with keys of the form
        <section>.<option> and the corresponding values.
        """
        config = config.copy()
        cp =ConfigParser.SafeConfigParser()
        cp.read(fname)
        for sec in cp.sections():
            name =  string.lower(sec)
            for opt in cp.options(sec):
                config[name+"."+string.lower(opt)] = string.strip(cp.get(sec,opt))
        os.chdir(self.curdir)
        return config
        
    def fillGui(self,conf):
        """
        Fill the gui fields from configuration dictionary conf.
        """
        if self.conf['settings.language'] == 'pt_BR':
            self.langCombo.setCurrentIndex(1)
        elif self.conf['settings.language'] == 'fr':
            self.langCombo.setCurrentIndex(2)
        elif self.conf['settings.language'] == 'ru':
            self.langCombo.setCurrentIndex(3)
        elif self.conf['settings.language'] == 'es':
            self.langCombo.setCurrentIndex(4)
        else:
            self.langCombo.setCurrentIndex(0)
        try:
            self.scriptNameEdit.setText(conf['model.script'])
            self.hostnEdit.setText(conf['database.host'])
            self.portEdit.setText(conf['database.port'])
            self.uidEdit.setText(conf['database.user'])
            self.unameEdit.setText(conf['settings.name'])
            self.editorEdit.setText(conf['settings.editor'])
            self.pdfEdit.setText(conf['settings.pdfviewer'])
        except KeyError, v:
            V = v.__str__().split('.')
            QMessageBox.information(None,
                self.trUtf8("Syntax Error"),
                self.trUtf8("""Please check file .epigrassrc for missing %s keyword
                from section %s """%(V[1],V[0])))
##            usern = pwd.getpwuid(os.getuid())[0] #Get the user name
##            os.system('rm /home/'+usern+'.epigrassrc')
##            self.initRc()

    def checkConf(self):
        """
        Check if the configuration dictionary is complete
        """
        if not self.conf:
            self.initRc()
        for i in self.conf.items():
            if not i[1]:
                QMessageBox.information(None,
                    self.trUtf8("Incomplete Configuration"),
                    self.trUtf8("""Please enter a value for %s."""%i[0]),
                    self.trUtf8("&OK"))
                return 0
        return 1

        
    def editScript(self):
        """
        Opens the model script with the user's preferred editor.
        """
        if self.editorEdit.text() and self.scriptNameEdit.text():
            ed = str(self.editorEdit.text())
            scr = str(self.scriptNameEdit.text())
            os.system('%s %s'%(ed,scr))
        else:
            QMessageBox.warning(None,
                self.trUtf8("Editor or Script not selected"),
                self.trUtf8("""Please make sure you have selected both
an editor and your model's script."""),
                self.trUtf8("&OK"))

        
        
    def chooseScript(self):
        """
        starts the file selction dialog for the script.
        """
        scrname = QFileDialog.getOpenFileName(\
            None,
            self.trUtf8("Select your Model's Script"),
            QtCore.QString(),
            self.trUtf8("*.epg"),
            None)


#        scrname = QFileDialog.getOpenFileName(\
#                    QtCore.QString.null,
#                    self.trUtf8("*.epg"),
#                    None, None,
#                    self.trUtf8("Select your Model's Script"),
#                    None, 1)
        self.scriptNameEdit.setText(scrname)
        self.conf['model.script'] = scrname
    
    def readGui(self):
        """
        Gets the info from the GUI into the configuration dictionary.
        """
        self.conf['model.script'] = str(self.scriptNameEdit.text())
        self.conf['database.host'] = str(self.hostnEdit.text())
        self.conf['database.port'] = int(str(self.portEdit.text()))
        self.conf['database.user'] = str(self.uidEdit.text())
        self.conf['database.backend']= str(self.dbType.currentText()).lower()
        self.conf['settings.name'] = unicode(str(self.unameEdit.text()))
        self.conf['settings.editor'] = str(self.editorEdit.text())
        self.conf['settings.pdfviewer'] = str(self.pdfEdit.text())
        
        os.chdir(os.path.split(self.conf['model.script'])[0])
        #print os.getcwd()
        
        if int(self.langCombo.currentIndex()) == 1:
            self.conf['settings.language'] = 'pt_BR'
        elif int(self.langCombo.currentIndex()) == 2:
            self.conf['settings.language'] = 'fr'
        elif int(self.langCombo.currentIndex()) == 3:
            self.conf['settings.language'] = 'ru'
        elif int(self.langCombo.currentIndex()) == 4:
            self.conf['settings.language'] = 'es'
        else:
            self.conf['settings.language'] = 'en'
    
        
    
    def onRun(self):
        """
        starts the simulation when the run button is pressed
        """
        self.stepLCD.display(0)
        self.tabWidget.setCurrentIndex(2)
        self.readGui()
        self.updateRc()
        if self.checkConf():
            if not self.dbType.currentIndex():
                if not self.pwEdit.text():
                    QMessageBox.warning(None,
                        self.trUtf8("Missing Database Password"),
                        self.trUtf8("""Please enter password for MySQL database."""))
                    return
            self.buttonRun.setEnabled(0)
            self.sim = S = simulate(fname=self.conf['model.script'],host=self.conf['database.host'],port=int(self.conf['database.port']),
                        db='epigrass',user=self.conf['database.user'], password=str(self.pwEdit.text()),backend=str(self.dbType.currentText()).lower())
            S.gui = self
#            if S.shapefile:
#                self.openGraphDisplay(S.shapefile[0], S.shapefile[1],S.shapefile[2] )
            
            self.RT = RunThread(S)
#            QtCore.QObject.connect(self.RT,QtCore.SIGNAL("drawStep"), self.graphDisplay.drawStep)
            if not S.replicas:
                self.RT.start()
                
                
                self.buttonRun.setEnabled(1)
            else: #Repeated runs 
                self.repRuns(S)
                self.buttonRun.setEnabled(1)
                
            
            # In case of a batch
            if S.Batch:
                self.textEdit1.insertPlainText('Simulation Started.')
                
                # run the batch list
                for i in S.Batch:
                    #Makes sure it comes back to original directory before opening models in the batch list
                    os.chdir(self.sim.dir)
                    # Generates the simulation object        
                    self.sim = T = simulate(fname=i,host=self.conf['database.host'],port=int(self.conf['database.port']),
                        db='epigrass',user=self.conf['database.user'], password=str(self.pwEdit.text()),backend=str(self.dbType.currentText()).lower())
                    T.gui = self
                    print 'starting model %s'%i
                    T.start()  # Start the simulation 

            self.textEdit1.insertPlainText('Done!')
            self.buttonRun.setEnabled(1)
            #print 'Agora...'
            spread.Spread(self.sim.g, self.sim.outdir,self.sim.encoding)
            
            #spdisp.display()
        else:
            self.buttonRun.setEnabled(1)
            return
            
    
    def repRuns(self,S):
        """
        Do repeated runs
        """
        nseeds= S.seed[0][2] #number o individual to be used as seeds
        randseed=S.randomize_seeds  
        print "replicas type", randseed
        if randseed: 
            seeds = S.randomizeSeed(randseed)
        reps = S.replicas
        for i in xrange(reps):
            print "Starting replica number %s"%i
            self.textEdit1.insertPlainText("Starting replica number %s"%i)
            self.sim = S = simulate(fname=self.conf['model.script'],host=self.conf['database.host'],port=int(self.conf['database.port']),
                db='epigrass',user=self.conf['database.user'], password=str(self.pwEdit.text()),backend=str(str(self.dbType.currentText()).lower()))
            S.gui = self
            if randseed:
                S.setSeed(seeds[i], nseeds)
            S.round = i
            S.shpout = False
            S.start()
            del S,  self.sim
            
#            if S.Rep: #Generate report if necessary
#                rep = Rp.report(S)
#                self.textEdit1.insertPlainText('Report generation started.')
#                rep.Assemble(S.Rep)
#            del S
    
    def onDbBackup(self):
        """
        Dumps the epigrass databse to an sql file.
        """
        if self.conf['database.host'] !='localhost':
            QMessageBox.warning(None,
                self.trUtf8("Non-local Database Server"),
                self.trUtf8("""Currently, the database backup function only
                works on local database servers."""),
                self.trUtf8("&OK"))


        os.system('mysqldump -u %s -p%s --opt epigrass > epigrass.sql'%(str(self.uidEdit.text()),self.pwEdit.text()))
        
        QMessageBox.information(None,
            self.trUtf8("Restore Instructions:"),
            self.trUtf8("""The epigrass database has been Backed up
to a file named "epigrass.sql".
This file is in you current working directory.
To restore the database type:
epigrass.sql > mysql"""),
            self.trUtf8("&OK"))
    
    def onDbInfo(self):
        pass
    
    def onHelp(self):
        """
        Opens the userguide
        """
        aboutD = QtGui.QDialog()
        ab = aboutDialog()
        ab.setupUi(aboutD)
        aboutD.show()
        aboutD.exec_()
#        if os.path.exists('/usr/share/epigrass/docs/Epigrass.pdf'):
#            try:
#                os.system('%s /usr/share/epigrass/docs/Epigrass.pdf'%str(self.pdfEdit.text()))
#            except:
#                QMessageBox.warning(None,
#                    self.trUtf8("Help not available"),
#                    self.trUtf8("""Could not open user guide.
#    Please make sure your chosen PDF viewer is installed."""),
#                    self.trUtf8("&OK"))

    def onRepOpen(self):
        """
        Opens the report PDF
        """
        
        out = commands.getstatusoutput('%s %s'%(self.conf['settings.pdfviewer'],self.sim.repname))
        if out[0] != 0:
            QMessageBox.warning(None,
                self.trUtf8("No Report"),
                self.trUtf8("""Could not open the report.
Make sure you have generated it."""),
                self.trUtf8("&OK"))
            
    
    
    def onVisual(self):
        """
        Scan the epigrass database an shows available simulations.
        """
        basedir = self.rootdir
        if not self.dbType.currentIndex():
            if not self.pwEdit.text():
                QMessageBox.warning(None,
                    self.trUtf8("Missing Database Password"),
                    self.trUtf8("""Please enter the password for the MySQL database in the first tab."""))
                return
        # for SQLite databases check for the existence os the database file.
        if self.dbType.currentIndex() == 1: 
            if not os.path.exists('Epigrass.sqlite'):
                basedir = str(QFileDialog.getExistingDirectory(\
                    None,
                    self.trUtf8("No SQLite database found. Please select a new directory."),
                    QtCore.QString(),
                    QFileDialog.Options(QFileDialog.ShowDirsOnly)))
            if not glob.glob(basedir+'*.sqlite'):
                QMessageBox.information(None,
                    self.trUtf8("No Database found"),
                    self.trUtf8("""No file ending with ".sqlite" was found. 
Please try a different  directory."""),
                    QMessageBox.StandardButtons(\
                        QMessageBox.Ok),
                    QMessageBox.Ok)
            else:
                os.chdir(basedir)
                
        self.Display=epi.viewer(host=str(self.hostnEdit.text()),port=int(str(self.portEdit.text())),
                            db='epigrass',user=str(self.uidEdit.text()), pw=str(self.pwEdit.text()),backend=self.conf['database.backend'], gui=self)
        os.chdir(self.curdir)

        for s in self.Display.tables:
            if s.endswith('_meta'):
                self.tableList.insertItem(0, s[:-5])
        #check for available maps
        maplist = [i for i in os.listdir(os.getcwd()) if i.endswith('.shp')]
        for m in maplist:
            self.mapList.insertItem(0, m)
    
    def getVariables(self):
        """
        Fill the variables list whenever a table is selected in the table list
        """
        self.variableList.clear()
        table = str(self.tableList.currentText())
        self.vars = self.Display.getFields(table)
        for s in self.vars:
            if s not in ['id','name','geocode','lat','longit','time']:
                self.variableList.insertItem(0, s)
        #TODO: read meta-info from table and show in the tooltip
#        self.tableList.setToolTip('')
    
    def onPlayButton(self):
        """
        Calls the epiplay module to replay the epidemic from a database table.
        """
        
        #get variable to animate and its position in the database
        var = str(self.variableList.currentText())
        pos = self.vars.index(var)
        r = self.rateSpinBox.value() 
        table = str(self.tableList.currentText())
        if self.Display.shapefile:
            mapa = self.Display.shapefile[0]
        else:
            mapa =None
        modname = table.split('_')[0] #self.sim.modelName.split('/')[-1]
        #change the the outdata directory
        if not os.path.split(os.getcwd())[1].startswith("outdata"):
            os.chdir("outdata-"+modname)
        nodes,am = self.Display.readNodes(modname, table)
        if not nodes:
            QMessageBox.warning(None,
                self.trUtf8("Empty Table"),
                self.trUtf8("""You have selected an empty table.
Please select another table from the menu."""))
            return

        numbnodes = len(nodes)
        data = self.Display.readData(table)
        edata = self.Display.readEdges(table)
        os.chdir(self.rootdir)
        numbsteps = len(data)/numbnodes
        self.Display.viewGraph(nodes,am,var,mapa)
        self.Display.anim(data,edata,numbsteps,pos,r)
        #Future(self.Display.keyin,data,edata,numbsteps,pos,r)
        #self.Display.keyin(data,edata,numbsteps,pos,r)
    
    
        
    def onConsensus(self):
        """
        Build the consensus tree
        """

        modelpath = os.path.split(self.conf['model.script'])[0]
        print type(modelpath)

        path = QFileDialog.getExistingDirectory(\
            self.trUtf8(modelpath),
            None, None,
            self.trUtf8("Select Directory with Tree Files (epipath*.csv)"),
            1, 1)
        cutoff = QInputDialog.getDouble(\
            self.trUtf8("Branch Support"),
            self.trUtf8("Enter minimum branch support level to display:"),
            50.0, 0.0, 100.0, 1)
        cons = spread.Consensus(str(path),cutoff)

    
    def setBackend(self):
        """
        activate/deactivate db spec groupbox
        """
        self.backend = self.conf['database.backend']= str(self.dbType.currentText()).lower()
        #print self.backend
        if self.dbType.currentIndex():
            self.dbSpecGroupBox.setEnabled(0)
            self.dbBackup.setEnabled(0)
        else:
            self.dbSpecGroupBox.setEnabled(1)
            self.dbBackup.setEnabled(1)
    
    def onExit(self):
        """
        Close the gui
        """
        self.close()
        
class Future:
    """
    By David Perry - http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/84317
    To run a function in a separate thread, simply put it in a Future:

    >>> A=Future(longRunningFunction, arg1, arg2 ...)

    It will continue on its merry way until you need the result of your function. 
    You can read the result by calling the Future like a function, for example:

    >>> print A()

    If the Future has completed executing, the call returns immediately. 
    If it is still running, then the call blocks until the function completes. 
    The result of the function is stored in the Future, so subsequent calls to 
    it return immediately.

    A few caveats:
    Since one wouldn't expect to be able to change the result of a function, 
    Futures are not meant to be mutable. This is enforced by requiring the 
    Future to be "called", rather than directly reading __result. If desired, 
    stronger enforcement of this rule can be achieved by playing 
    with __getattr__ and __setattr__.

    The Future only runs the function once, no matter how many times you 
    read it. You will have to re-create the Future if you want to re-run your 
    function; for example, if the function is sensitive to the time of day.

    For more information on Futures, and other useful parallel programming 
    constructs, read Gregory V. Wilson's _Practical Parallel Programming_.
    """
    def __init__(self,func,*param):
        # Constructor
        self.__done=0
        self.__result=None
        self.__status='working'

        self.__C=Condition()   # Notify on this Condition when result is ready

        # Run the actual function in a separate thread
        self.__T=Thread(target=self.Wrapper,args=(func,param))
        self.__T.setName("FutureThread")
        self.__T.start()

    def __repr__(self):
        return '<Future at '+hex(id(self))+':'+self.__status+'>'

    def __call__(self):
        self.__C.acquire()
        while self.__done==0:
            self.__C.wait()
        self.__C.release()
        # We deepcopy __result to prevent accidental tampering with it.
        a=copy.deepcopy(self.__result)
        return a

    def Wrapper(self, func, param):
        # Run the actual function, and let us housekeep around it
        self.__C.acquire()
        try:
            self.__result=func(*param)
        except:
            self.__result="Exception raised within Future"
        self.__done=1
        self.__status=`self.__result`
        self.__C.notify()
        self.__C.release()

def loadLang(app,tr,lang):
    """
    loads the language based on the user's choice.
    """
    if lang == 'pt_BR':
        tr.load('manager_pt_BR','.')
        app.installTranslator(tr)
    elif lang == 'fr':
        tr.load('epigrass_fr','.')
        app.installTranslator(tr)
    elif lang == 'es':
        tr.load('epigrass_es','.')
        app.installTranslator(tr)
    elif lang == 'ru':
        tr.load('epigrass_ru_RU','.')
        app.installTranslator(tr)
    else:
        pass
        
def main():
    global app
    app = QtGui.QApplication(sys.argv)
    w = MainWindow_Impl()
    w.show()
    sys.exit(app.exec_())
class RunThread(QtCore.QThread):
    """
    Worker Thread to run the simulation in a separate thread in order to allow the updating of the GUI to take place
    """
    def __init__(self,sim,  parent=None):
        QtCore.QThread.__init__(self, parent)
        self.mutex = QtCore.QMutex()
        self.condition = QtCore.QWaitCondition()
        self.sim = sim
    def __del__(self):
        self.mutex.lock()
        self.condition.wakeOne()
        self.mutex.unlock()
        self.wait()
    def go(self):
        locker = QtCore.QMutexLocker(self.mutex)
        self.start()
    def run(self):
        self.sim.start()
        self.mutex.lock()
        self.condition.wait(self.mutex)
        self.mutex.unlock()

if __name__ == "__main__":
    main()