"""
    demo_multi_mtzdump.py: CCP4 GUI Project
     Copyright (C) 2010 University of York

     This library is free software: you can redistribute it and/or
     modify it under the terms of the GNU Lesser General Public License
     version 3, modified in accordance with the provisions of the 
     license to address the requirements of UK law.
 
     You should have received a copy of the modified GNU Lesser General 
     Public License along with this library.  If not, copies may be 
     downloaded from http://www.ccp4.ac.uk/ccp4license.php
 
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU Lesser General Public License for more details.
"""

# Simple example of running external processes in blocking mode
# Multiple calls to mtzdump to find the cell in a list of mtzfiles

from CCP4Modules import QTAPPLICATION,PROCESSMANAGER # Utility to access CCP4i modules
from CCP4PluginScript import CPluginScript

     
class demo_multi_mtzdump(CPluginScript):

    TASKMODULE = 'demo'
    TASKTITLE = 'Demo multi MTZ dump'
    TASKNAME = 'demo_multi_mtzdump'
    TASKVERSION= 0.0
    ASYNCHRONOUS = True
    TIMEOUT_PERIOD = 0.1

    def process(self):
      # Run mtzdump on multiple mtzs to get the cell parameters
      import glob,os,time
      from CCP4Utils import getCCP4I2Dir
      # Get a list on MTZ files to work on
      self.mtzlist = glob.glob(os.path.join(getCCP4I2Dir(),'wrappers','*','test_data','*.mtz'))
      #self.mtzlist.extend(glob.glob(os.path.join(getCCP4I2Dir(),'pipelines','*','test_data','*.mtz')))
      self.subProcessList = []
      self.startTime = time.time()

      # Loop over the MTZ files calling mtzdump for each one
      for mtz in self.mtzlist:
        # Create an instance of mtzdump plugin and set the HKLIN
        mtzdump = self.makePluginObject(pluginName='mtzdump',reportToDatabase=False)
        mtzdump.container.inputData.HKLIN.fullPath  = mtz
        # Set to run asynchronously and set a callback
        mtzdump.async = True
        self.connectSignal(mtzdump,'finished',self.handleDone)
        #Start process
        rv = mtzdump.process()
        #The mtzdump instance must be saved to keep it in scope and everything else can be got from that.
        self.subProcessList.append(mtzdump)

      # Set a callback to end the entire process after a given time 
      self.timedCallback(demo_multi_mtzdump.TIMEOUT_PERIOD,self.handleTimeout)

      return CPluginScript.SUCCEEDED
        

    def handleDone(self,ret):
      # callback is passed the jobId (=Non
      # if not in ccp4i2-db context) and processId that
      # can serve at identifier for subProcess
      import sys
      import time
      # Get the exit status and if successful get the CELL from the outputData
      pid =  ret.get('pid',None)
      #print 'demo_multi_mtzdump.handleDone',ret
      for p in self.subProcessList:
        if p.processId == pid:
          if ret.get('finishStatus') == CPluginScript.SUCCEEDED:
               print p.container.inputData.HKLIN,p.container.outputData.CELL;sys.stdout.flush()
          self.subProcessList.remove(p)
          break

      if len(self.subProcessList)==0:
        self.reportStatus(CPluginScript.SUCCEEDED)
        print 'FINISHED'
      #else:
      #  print len(self.subProcessList),'jobs remaining';sys.stdout.flush()

      return

    def handleTimeout(self):
      #print 'demo_multi_mtzdump.handleTimout', self.subProcessList
      import sys;sys.stdout.flush()
      
      for p in self.subProcessList:
        print 'TERMINATING', p.processId,sys.stdout.flush()
        try:
          p.terminate()
        except:
          pass

      self.appendErrorReport(40,str(demo_multi_mtzdump.TIMEOUT_PERIOD))
      self.reportStatus(CPluginScript.FAILED)
      
        
        
      
#=======================================================================================================
import unittest

class test_demo_multi_mtzdump(unittest.TestCase):

  
  def setUp(self):
    # make all background jobs wait for completion
    #print 'test_demo_multi_mtzdump setUp graphical',GRAPHICAL()
    if not GRAPHICAL():
      PROCESSMANAGER().setWaitForFinished(10000)

  def tearDown(self):
    if not GRAPHICAL():
      PROCESSMANAGER().setWaitForFinished(-1)
  
  
  def test_1(self):
    import os

    # Run the pipeline
    pipe = demo_multi_mtzdump(parent=QTAPPLICATION(),name='demo_multi_mtzdump')
    pipe.process()
      

def testSuite():
  suite = unittest.TestLoader().loadTestsFromTestCase(test_demo_multi_mtzdump)
  return suite

def runAllTests():
  suite = testSuite()
  unittest.TextTestRunner(verbosity=2).run(suite)
