예제 #1
0
class NodeBuilder():

    def __init__(self, queryOptions, export_id=None):
        print "initializing nodebuilder"
        # initialize dbobjects
        self.mailSystem = smtpInterface(settings)
        # fixme, need to decipher the query objects against the configuration (table, object, ini, conf..)
        # this should then pull the correct module below and run the process.
        self.generateOutputformat = outputConfiguration.Configuration[queryOptions.configID]['outputFormat']
        self.transport = outputConfiguration.Configuration[queryOptions.configID]['transportConfiguration']
        self.encryption = outputConfiguration.Configuration[queryOptions.configID]['encryption']
        self.outputFilesPath = outputConfiguration.Configuration[queryOptions.configID]['destination']
        self.export_id = export_id
        
        self.queryOptions = queryOptions	# Passed in from QueryObject.getOptions()
        print '==== Output Format', self.generateOutputformat		# JCS
        if self.generateOutputformat == 'svcpoint5':
            try:
                from synthesis.svcpointxml5writer import SvcPointXML5Writer
                #svcptxml5writer_loaded = True
                print "import of Svcpt XML Writer, version 5 was successful"
            except Exception as e:
                print "import of Svcpt XML Writer, version 5 failed", e
                #svcptxml5writer_loaded = False
            if self.transport == "save":
                self.writer = SvcPointXML5Writer(self.outputFilesPath, queryOptions)
                print '==== self.writer:', self.writer
                self.validator = SvcPoint5XMLTest()
                print '==== self.validator:', self.validator
        
        elif self.generateOutputformat == 'hl7ccd':     # JCS
            try:
                from synthesis.hl7CCDwriter import hl7CCDwriter
                hl7CCDwriter_loaded = True
                print "import of HL7 XML Writer was successful"
            except Exception as e:
                print "import of HL7 XML Writer failed", e
                hl7CCDwriter_loaded = False
            if self.transport in ("save", "rest", "soap"):
                print "queryOptions: ", queryOptions
                self.writer = hl7CCDwriter(self.outputFilesPath, queryOptions, export_id=self.export_id)
                print '==== self.writer:', self.writer
                self.validator = hl7CCDXMLTest()
                print '==== self.validator:', self.validator

#         elif self.generateOutputformat == 'svcpoint406':	# JCS
#             try:
#                 from synthesis.svcpointxml406writer import SvcPointXMLWriter
#                 svcptxml406writer_loaded = True
#                 print "import of Svcpt XML Writer, version 4.06 was successful"
#             except:
#                 print "import of Svcpt XML Writer, version 4.06 failed"
#                 svcptxml406writer_loaded = False
#             if self.transport == "save":
#                 self.writer = SvcPointXMLWriter(self.outputFilesPath, queryOptions)
#                 self.validator = SvcPoint406XMLTest()
            
#        elif self.generateOutputformat == 'svcpoint20':
#            #from svcPointXML20writer import SvcPointXML20Writer
#            # pick the plug-in to import
#            try:
#                from svcPointXML20writer import SvcPointXMLWriter 
#                svcptxml20writer_loaded = True
#                print "import of Svcpt XML Writer, version 2.0 was successful"
#            except:
#                print "import of Svcpt XML Writer, version 2.0 failed"
#                svcptxml20writer_loaded = False
#            self.writer = SvcPointXMLWriter(settings.OUTPUTFILES_PATH, queryOptions)
#            self.validator = SVCPOINT20XMLTest()
                
        elif self.generateOutputformat == 'hmisxml28':
            #try:
                #from hmisxml28writer import HMISXML28Writer#IGNORE:@ImportRedefinition
                #hmisxml28writer_loaded = True
            #except:
                #print "import of HMISXMLWriter, version 2.8, failed"
                #hmisxml28writer_loaded = False
            if self.transport == "save":
                if settings.DEBUG:
                    print "destination is ", self.outputFilesPath
                #self.writer = HMISXML28Writer(self.outputFilesPath, queryOptions)           
                self.validator = HUDHMIS28XMLTest()
            
        elif self.generateOutputformat == 'hmisxml30':
            try:
                from hmisxml30writer import HMISXMLWriter#IGNORE:@ImportRedefinition
                print "import of HMISXMLWriter, version 3.0 occurred successfully"
                hmisxml30writer_loaded = True
            except Exception as e:
                print "import of HMISXMLWriter, version 3.0, failed", e
                hmisxml30writer_loaded = False
            if self.transport == "save":
                if settings.DEBUG:
                    print "destination is ", self.outputFilesPath
                self.writer = HMISXMLWriter(self.outputFilesPath, queryOptions)                    
                self.validator = HUDHMIS30XMLTest() 
            
        #elif self.generateOutputformat == 'hmiscsv30':
            #try:
                #from hmiscsv30writer import HmisCSV30Writer
                #hmiscsv30writer_loaded = True
            #except:
                #hmiscsv30writer_loaded = False
            #if self.transport == "save":
                #self.writer = HmisCsv30Writer(self.outputFilesPath, queryOptions, debug=True)                    
            #self.validator = HmisCsv30Test()           
        #elif self.generateOutputformat == 'jfcsxml':
            #print "Need to hook up the JFCSWriter in Nodebuilder"
#            self.writer = JFCSXMLWriter()                   
#            self.validator = JFCSXMLTest()
        elif self.generateOutputformat == 'pseudo':
            print "Pseudo writer encountered. Skipping..."
        else:
            # new error cataloging scheme, pull the error from the catalog, then raise the exception (centralize error catalog management)
            err = catalog.errorCatalog[1001]
            raise exceptions.UndefinedXMLWriter, (err[0], err[1], 'NodeBuilder.__init__() ' + self.generateOutputformat)
            
        #fileStream = open(new_file,'r')
        # validate the file prior to uploading it
        #if self.validator.validate(fileStream):
        
        #setup the postprocessing module    
        self.pprocess = postprocessing.PostProcessing(queryOptions.configID)
        #print '==== self.pprocess:', self.pprocess	# JCS - empty??

    def encrypt_file(self, filename):
        if self.encryption == "openpgp":
            gpg = GPG()
            gpg.encryptFile(filename, os.path.splitext(filename)[0] + '.gpg')
            shutil.move(filename, outputConfiguration.PROCESSEDFILES_PATH)
            return os.path.splitext(filename)[0] + '.gpg'
        elif self.encryption == "3des":
            fo = open(filename, 'r')
            data = fo.read()
            fo.close()
            keyiv = get_incoming_3des_key_iv()
            des = DES3()
            encrypted_data = des.encrypt(data, keyiv['key'], iv=keyiv['iv'])
            fo = open(os.path.splitext(filename)[0] + '.des3', 'w')
            fo.write(encrypted_data)
            fo.flush()
            fo.close()
            shutil.move(filename, outputConfiguration.PROCESSEDFILES_PATH)
            return os.path.splitext(filename)[0] + '.des3'
        else:
            # do nothing. return as is
            return filename
        
    def run(self):
        '''This is the main method controlling this entire program.'''
        
        # Load the data via dbobjects
        
        
        # try to write the output file and then validate it.
        #for writer,validator in map(None, self.writer, self.validator):
            #result = item.validate(instance_doc)
            # if results is True, we can process against this reader.
        if self.transport == 'soap':
            try:
                hl7_output = self.writer.get()
                for ccd_data, referredToProviderID in hl7_output:
                    soap = soaptransport.SoapEnv(self.queryOptions.configID)
                    result, details = soap.send_soap_envelope(ccd_data,
                        referredToProviderID, self.queryOptions.configID)
                    print result, details
            except:
                print "*****************************************************************"
                print "*****************************************************************"
                print "*****************************************************************"
                synthesis_error = traceback.format_exc()
                print synthesis_error
                smtp = smtpInterface(settings)
                smtp.setMessageSubject("ERROR -- Synthesis:NodeBuilder:%s:%s" %
                    (self.transport.upper(), self.generateOutputformat.capitalize()))
                smtp.setRecipients(inputConfiguration.SMTPRECIPIENTS['testSource'])#make this [(self.queryOptions.configID)]?
                smtp.setMessage("%s\r\n" % synthesis_error )
                try:
                    print "trying to send message"
                    smtp.sendMessage()
                except:
                    print 'send failed'                
                print "*****************************************************************"
                print "*****************************************************************"
                print "*****************************************************************"
        elif self.transport == 'rest':
            try:
                ccd_data = self.writer.get()
                rest = resttransport.REST(self.queryOptions.configID)
                #assert (rest.post(ccd_data)[0] == True), "Sending CCD via REST transport failed!"
                result, details = rest.post('CCD_%s' % str(uuid.uuid4()).replace('-',''), ccd_data)
                print result, details
            except:
                print "*****************************************************************"
                print "*****************************************************************"
                print "*****************************************************************"
                synthesis_error = traceback.format_exc()
                print synthesis_error
                smtp = smtpInterface(settings)
                smtp.setMessageSubject("ERROR -- Synthesis:NodeBuilder:%s:%s" %
                    (self.transport.upper(), self.generateOutputformat.capitalize()))
                smtp.setRecipients(inputConfiguration.SMTPRECIPIENTS['testSource'])
                smtp.setMessage("%s\r\n" % synthesis_error )
                try:
                    print "trying to send message"
                    smtp.sendMessage()
                except:
                    print 'send failed'                
                print "*****************************************************************"
                print "*****************************************************************"
                print "*****************************************************************"
        else:
            # the remaining transport require file IO
            if self.generateOutputformat != "pseudo":
                try:
                    if settings.DEBUG:
                        print "destination is ", self.outputFilesPath
                    if self.writer.write():
                        #filesToTransfer = fileutils.grabFiles(os.path.join(settings.OUTPUTFILES_PATH, "*.xml"))
                        filesToTransfer = fileutils.grabFiles(os.path.join(self.outputFilesPath, "*.xml"))
                        
                        # create a list of valid files to upload
                        validFiles = []
                        # Loop over each file and validate it.
                        for eachFile in filesToTransfer:
                            fs = open(eachFile, 'r')
                            if self.validator.validate(fs):
                                validFiles.append(eachFile)
                                print 'oK'
                                # since the file was validated, its OK to encrypt it now.
                                possible_new_file_name = self.encrypt_file(eachFile)
                                validFiles[validFiles.index(eachFile)] = possible_new_file_name
                                filesToTransfer[filesToTransfer.index(eachFile)] = possible_new_file_name
                            else:
                                pass                # Fixme append invalid files to list and report this.
                            
                            fs.close()
                        
                        # upload the valid files
                        # how to transport the files (debugging)
                        if self.transport == 'save':
                            print 'Output Complete...Please see output files: %s' % filesToTransfer
                            
                        if self.transport == 'sys.stdout':
                            for eachFile in validFiles:
                                fs = open(eachFile, 'r')
                                # open the file and echo it to stdout
                                lines = fs.readlines()
                                fs.close()              # done with file close handle
                                for line in lines:
                                    print line        
                                    
                        if self.transport == 'sftp':
                            self.pprocess.processFileSFTP(validFiles)
                        elif self.transport == 'email':
                            # Loop over the list and each file needs to be emailed separately (size)
                            for eachFile in validFiles:
                                self.email = XMLProcessorNotifier("", eachFile)     # fixme (zip and encrypt?)
                                msgBody = self.formatMsgBody()
                                self.email.sendDocumentAttachment('Your report results', msgBody, eachFile)
                        elif self.transport == 'vpnftp':
                            # SBB20100430 Only upload if we have a validated file(s)
                            if len(validFiles) > 0:
                                pd = iniutils.LoadConfig('fileConverter.ini')
                                self.pprocess.setINI(pd)
                                self.pprocess.processFileVPN(validFiles)
                        elif self.transport == 'vpncp':
                            pass
                                
                            #print results
                            #print 'This is the result before it goes back to the test_unit:', \
                            #results
                            #return results
                except:
                    print "*****************************************************************"
                    print "*****************************************************************"
                    print "*****************************************************************"
                    synthesis_error = traceback.format_exc()
                    print synthesis_error
                    smtp = smtpInterface(settings)
                    smtp.setMessageSubject("ERROR -- Synthesis:NodeBuilder:%s:%s" %
                        (self.transport.upper(), self.generateOutputformat.capitalize()))
                    smtp.setRecipients(inputConfiguration.SMTPRECIPIENTS['testSource'])
                    smtp.setMessage("%s\r\n" % synthesis_error )
                    try:
                        print "trying to send message"
                        smtp.sendMessage()
                    except:
                        print 'send failed'                
                    print "*****************************************************************"
                    print "*****************************************************************"
                    print "*****************************************************************"
                    
    def formatMsgBody(self):
        msgBody = "Your report was requested on %s. /r/n The report criteria is: \r\n\t StartDate: %s /r/n \t EndDate: %s /r/n /t Previously Reported: %s /r/n /t Previously UnReported: %s' % (datetime.today() ,self.queryOptions.startDate, self.queryOptions.endDate, self.queryOptions.reported, self.queryOptions.unreported)"#IGNORE:@UnusedVariable

    def selectNodes(self, start_date, end_date, nodename):
        pass
    
    def flagNodes(self):
        pass
예제 #2
0
 def run(self):
     '''This is the main method controlling this entire program.'''
     
     # Load the data via dbobjects
     
     
     # try to write the output file and then validate it.
     #for writer,validator in map(None, self.writer, self.validator):
         #result = item.validate(instance_doc)
         # if results is True, we can process against this reader.
     if self.transport == 'soap':
         try:
             hl7_output = self.writer.get()
             for ccd_data, referredToProviderID in hl7_output:
                 soap = soaptransport.SoapEnv(self.queryOptions.configID)
                 result, details = soap.send_soap_envelope(ccd_data,
                     referredToProviderID, self.queryOptions.configID)
                 print result, details
         except:
             print "*****************************************************************"
             print "*****************************************************************"
             print "*****************************************************************"
             synthesis_error = traceback.format_exc()
             print synthesis_error
             smtp = smtpInterface(settings)
             smtp.setMessageSubject("ERROR -- Synthesis:NodeBuilder:%s:%s" %
                 (self.transport.upper(), self.generateOutputformat.capitalize()))
             smtp.setRecipients(inputConfiguration.SMTPRECIPIENTS['testSource'])#make this [(self.queryOptions.configID)]?
             smtp.setMessage("%s\r\n" % synthesis_error )
             try:
                 print "trying to send message"
                 smtp.sendMessage()
             except:
                 print 'send failed'                
             print "*****************************************************************"
             print "*****************************************************************"
             print "*****************************************************************"
     elif self.transport == 'rest':
         try:
             ccd_data = self.writer.get()
             rest = resttransport.REST(self.queryOptions.configID)
             #assert (rest.post(ccd_data)[0] == True), "Sending CCD via REST transport failed!"
             result, details = rest.post('CCD_%s' % str(uuid.uuid4()).replace('-',''), ccd_data)
             print result, details
         except:
             print "*****************************************************************"
             print "*****************************************************************"
             print "*****************************************************************"
             synthesis_error = traceback.format_exc()
             print synthesis_error
             smtp = smtpInterface(settings)
             smtp.setMessageSubject("ERROR -- Synthesis:NodeBuilder:%s:%s" %
                 (self.transport.upper(), self.generateOutputformat.capitalize()))
             smtp.setRecipients(inputConfiguration.SMTPRECIPIENTS['testSource'])
             smtp.setMessage("%s\r\n" % synthesis_error )
             try:
                 print "trying to send message"
                 smtp.sendMessage()
             except:
                 print 'send failed'                
             print "*****************************************************************"
             print "*****************************************************************"
             print "*****************************************************************"
     else:
         # the remaining transport require file IO
         if self.generateOutputformat != "pseudo":
             try:
                 if settings.DEBUG:
                     print "destination is ", self.outputFilesPath
                 if self.writer.write():
                     #filesToTransfer = fileutils.grabFiles(os.path.join(settings.OUTPUTFILES_PATH, "*.xml"))
                     filesToTransfer = fileutils.grabFiles(os.path.join(self.outputFilesPath, "*.xml"))
                     
                     # create a list of valid files to upload
                     validFiles = []
                     # Loop over each file and validate it.
                     for eachFile in filesToTransfer:
                         fs = open(eachFile, 'r')
                         if self.validator.validate(fs):
                             validFiles.append(eachFile)
                             print 'oK'
                             # since the file was validated, its OK to encrypt it now.
                             possible_new_file_name = self.encrypt_file(eachFile)
                             validFiles[validFiles.index(eachFile)] = possible_new_file_name
                             filesToTransfer[filesToTransfer.index(eachFile)] = possible_new_file_name
                         else:
                             pass                # Fixme append invalid files to list and report this.
                         
                         fs.close()
                     
                     # upload the valid files
                     # how to transport the files (debugging)
                     if self.transport == 'save':
                         print 'Output Complete...Please see output files: %s' % filesToTransfer
                         
                     if self.transport == 'sys.stdout':
                         for eachFile in validFiles:
                             fs = open(eachFile, 'r')
                             # open the file and echo it to stdout
                             lines = fs.readlines()
                             fs.close()              # done with file close handle
                             for line in lines:
                                 print line        
                                 
                     if self.transport == 'sftp':
                         self.pprocess.processFileSFTP(validFiles)
                     elif self.transport == 'email':
                         # Loop over the list and each file needs to be emailed separately (size)
                         for eachFile in validFiles:
                             self.email = XMLProcessorNotifier("", eachFile)     # fixme (zip and encrypt?)
                             msgBody = self.formatMsgBody()
                             self.email.sendDocumentAttachment('Your report results', msgBody, eachFile)
                     elif self.transport == 'vpnftp':
                         # SBB20100430 Only upload if we have a validated file(s)
                         if len(validFiles) > 0:
                             pd = iniutils.LoadConfig('fileConverter.ini')
                             self.pprocess.setINI(pd)
                             self.pprocess.processFileVPN(validFiles)
                     elif self.transport == 'vpncp':
                         pass
                             
                         #print results
                         #print 'This is the result before it goes back to the test_unit:', \
                         #results
                         #return results
             except:
                 print "*****************************************************************"
                 print "*****************************************************************"
                 print "*****************************************************************"
                 synthesis_error = traceback.format_exc()
                 print synthesis_error
                 smtp = smtpInterface(settings)
                 smtp.setMessageSubject("ERROR -- Synthesis:NodeBuilder:%s:%s" %
                     (self.transport.upper(), self.generateOutputformat.capitalize()))
                 smtp.setRecipients(inputConfiguration.SMTPRECIPIENTS['testSource'])
                 smtp.setMessage("%s\r\n" % synthesis_error )
                 try:
                     print "trying to send message"
                     smtp.sendMessage()
                 except:
                     print 'send failed'                
                 print "*****************************************************************"
                 print "*****************************************************************"
                 print "*****************************************************************"