labkeyDatabaseBrowser.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import labkeyInterface
  2. import json
  3. import chardet
  4. class labkeyDB:
  5. def __init__(self,net):
  6. self.net=net
  7. def selectRows(self,project,schemaName, queryName,qfilter,viewName='default',columns='all'):
  8. debug=False
  9. url=self.net.GetLabkeyUrl()+'/'+project
  10. url+='/query-selectRows.api?schemaName='+schemaName+\
  11. '&query.queryName='+queryName
  12. if viewName!='default':
  13. url+='&query.viewName={}'.format(viewName)
  14. if columns!='all':
  15. url+='&query.columns={}'.format(columns)
  16. for f in qfilter:
  17. url+="&query."+f['variable']+"~"+f['oper']+"="+f['value']
  18. if debug:
  19. print("Sending {}").format(url)
  20. response=self.net.get(url)
  21. return self.toJSON(response)
  22. def modifyRows(self,mode, project,schemaName, queryName, rows):
  23. #mode can be insert/update/delete
  24. debug=True
  25. data={}
  26. data['schemaName']=schemaName
  27. data['queryName']=queryName
  28. data['rows']=rows
  29. url=self.net.GetLabkeyUrl()+'/'+project
  30. url+='/query-'+mode+'Rows.api?'
  31. response=self.net.post(url,json.dumps(data))
  32. return self.toJSON(response)
  33. def addQuery(self,project,schemaName,queryName,fields,options=None):
  34. listDomainDefinition={}
  35. fNames=[f['name'] for f in fields]
  36. if schemaName=='lists':
  37. listDomainDefinition['kind']='IntList'
  38. if not options:
  39. fields.insert(0,{'name':'Key','rangeURI':'int'})
  40. options={}
  41. options['keyName']='Key'
  42. if queryName.find('enum')==0:
  43. options['keyType']='Integer'
  44. else:
  45. options['keyType']='AutoIncrementInteger'
  46. listDomainDefinition['options']=options
  47. if schemaName=='study':
  48. listDomainDefinition['kind']='StudyDatasetVisit'
  49. domainDesign={}
  50. domainDesign['name']=queryName
  51. domainDesign['description']='Some description'
  52. domainDesign['fields']=fields
  53. listDomainDefinition['domainDesign']=domainDesign
  54. url=self.net.GetLabkeyUrl()+'/'+project
  55. url+='/property-createDomain.api'
  56. response=self.net.post(url,json.dumps(listDomainDefinition))
  57. return self.toJSON(response)
  58. def getQueryDesign(self,project,schemaName,queryName):
  59. url=self.net.GetLabkeyUrl()+'/'+project
  60. url+='/property-getDomain.api?'
  61. url+='schemaName={}'.format(schemaName)
  62. url+='&queryName={}'.format(queryName)
  63. response=self.net.get(url)
  64. return self.toJSON(response)
  65. def toJSON(self,response):
  66. data=response.data
  67. encoding=chardet.detect(data)["encoding"]
  68. #try with a set of encodings to maximize probability of success
  69. encodings=[encoding,'utf_8']
  70. for x in encodings:
  71. try:
  72. return json.loads(data.decode(x))
  73. except UnicodeDecodeError:
  74. print(f'Failed to decode with [{x}]: {data}')
  75. return None