labkeyDatabaseBrowser.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. encoding=chardet.detect(response.data)["encoding"]
  67. #try with a set of encodings to maximize probability of success
  68. encodings=[encoding,'utf_8']
  69. for x in encodings:
  70. try:
  71. return json.loads(response.data.decode(x))
  72. except UnicodeDecodeError:
  73. print(f'Failed to decode with [{x}]: {data}')
  74. return None