瀏覽代碼

Adding updates to calling schema (importXML)

NIX Worker 1 月之前
父節點
當前提交
06b19e7e34
共有 1 個文件被更改,包括 30 次插入16 次删除
  1. 30 16
      pythonScripts/importXML.py

+ 30 - 16
pythonScripts/importXML.py

@@ -19,12 +19,10 @@ def getFileBrowser(db):
    import labkeyFileBrowser
    return labkeyFileBrowser.labkeyFileBrowser(db.net)
 
-def getVal(xmlRoot,aliasVal,xPath='',aliasReplace={}):
+def getVal(xmlRoot,aliasVal,xPath=''):
     q=aliasVal.split(':')
     xName=q[0]
     
-    for x in aliasReplace:
-        xName=xName.replace(x,aliasReplace[x])
     if len(xPath)>0:
         if len(xName)>0:
             elementPath='/'.join([xPath,xName])
@@ -37,7 +35,15 @@ def getVal(xmlRoot,aliasVal,xPath='',aliasReplace={}):
         return xmlRoot.find(elementPath).get(attributeName)
     except AttributeError:
         return None
-    
+
+def updateTxt(txt,replacePatterns):
+   for x in replacePatterns:
+      txt=txt.replace(x,replacePatterns[x])
+   return txt
+
+def updateAliases(aliasValues,aliasReplace):
+   return {a:updateTxt(aliasValues[a],aliasReplace) for a in aliasValues}
+
 def parseJSON(x):
     print(f'Decoding [{x}]')
     try:
@@ -57,9 +63,13 @@ def readSetup(importXLSX,pars):
     return setupRows
         
     
-        
+def getID(root):
+    return root.find('Patient/PatientID').get('val')
+
+def getXMLRoot(xmlFile):
+   return lxml.etree.ElementTree(file=xmlFile).getroot()
     
-def importXML(importXLSX,pars,xmlFile,dryRun=True):
+def importXML(importXLSX,pars,xmlRoot,dryRun=True):
     #def importData(pars,filename,getId=getId,modify=modify,convertLookup=convertLookup,dryRun=True,debug=True):
 #master routine that imports data based on pars, 
 #applies user supplied functions modify, convertLookup and get Id and 
@@ -91,28 +101,26 @@ def importXML(importXLSX,pars,xmlFile,dryRun=True):
     print(f'dateVars: {dateVars}')
     lookupMap={f:importXLSX.getLookupMap(pars,fields,f) for f in lookupVars}
     alias=importXLSX.invertMap(importXLSX.getAlias(fields))
+    alias=updateAliases(alias,aliasReplace)
     print(f'aliases: {alias}')
     row={}
     
-    tree = lxml.etree.ElementTree(file=xmlFile)
-    print(tree.docinfo.xml_version)
-    root=tree.getroot()#Element
     #patient id can be either set in pars (takes precedence) or from xml record
-    pid=pars.get('id',root.find('Patient/PatientID').get('val'))
+    pid=pars.get('id',getID(xmlRoot))
     
     row={'ParticipantId':pid,'SequenceNum':seqNumOffset+1}
     row.update(presetValues)
     
     for f in fields:
         try:
-            row[f]=getVal(root,alias[f],xPath,aliasReplace)
+            row[f]=getVal(xmlRoot,alias[f],xPath)
         except KeyError:
             print(f'Alias for field {f} not found')
             continue
     print(row)
     db=importXLSX.getDB(pars)
     project=pars.get('project','DCIS/Study')
-    schema=pars.get('schema','demographics')
+    schema=pars.get('schema','study')
     query=pars.get('query','demographics')
 
     selVal=['ParticipantId','SequenceNum']
@@ -156,17 +164,23 @@ def main(parameterFile):
 #needs project
    ds=db.selectRows(pars['project'],'lists','importXML',[keyFilter])
    r=ds['rows'][0]
-   filename=os.path.join(fhome,'temp','DCIS','data.xml')
+   xmlFile=os.path.join(fhome,'temp','DCIS','data.xml')
    url=db.net.connectionConfig['host']+r['_labkeyurl_fileUpload']
    fb=getFileBrowser(db)
-   fb.readFileToFile(url,filename)
+   fb.readFileToFile(url,xmlFile)
+   xmlRoot=getXMLRoot(xmlFile)
    #needs project
    setupRows=readSetup(importXLSX,pars)
-   setupRows=setupRows[0:1]
+   #setupRows=setupRows[17:18]
    for s in setupRows:
 #needs project and schema for queries 
-      importXML(importXLSX,pars|s,filename,dryRun=True)
+      importXML(importXLSX,pars|s,xmlRoot,dryRun=False)
 
+#make calling importXML updates
+   r['status']=1
+   r['ID']=getID(xmlRoot)
+   del r['fileUpload']
+   db.modifyRows('update',pars['project'],'lists','importXML',[r])
 
 
 if __name__ == "__main__" :