labkeyDatabaseBrowser.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #prefer utf8
  70. encodings=['utf_8',encoding]
  71. for x in encodings:
  72. try:
  73. return json.loads(data.decode(x))
  74. except UnicodeDecodeError:
  75. print(f'Failed to decode with [{x}]: {data}')
  76. return None