def __init__(self, url=None, timeout=30 * 60, user_id=None, password=None, token=None, ignore_authrc=False, trust_all_ssl_certificates=False): self.url = url self.timeout = int(timeout) self._headers = dict() self.trust_all_ssl_certificates = trust_all_ssl_certificates self.token = token # token overrides user_id and password if token is not None: pass elif user_id is not None and password is not None: self.token = _get_token(user_id, password) elif 'KB_AUTH_TOKEN' in _os.environ: self.token = _os.environ.get('KB_AUTH_TOKEN') elif not ignore_authrc: authdata = _read_inifile() if authdata is None: authdata = _read_rcfile() if authdata is not None: if authdata.get('token') is not None: self.token = authdata['token'] elif(authdata.get('user_id') is not None and authdata.get('password') is not None): self.token = _get_token( authdata['user_id'], authdata['password']) if self.timeout < 1: raise ValueError('Timeout value must be at least 1 second') self.dsi = AbstractHandle(url=url, token=self.token, trust_all_ssl_certificates=trust_all_ssl_certificates)
def getHandles(logger, shock_url = "https://kbase.us/services/shock-api/", handle_url = "https://kbase.us/services/handle_service/", shock_ids = None, handle_ids = None, token = None): hs = AbstractHandle(url=handle_url, token=token) handles = list() if shock_ids is not None: header = dict() header["Authorization"] = "Oauth {0}".format(token) for sid in shock_ids: info = None try: logger.info("Found shock id {0}, retrieving information about the data.".format(sid)) response = requests.get("{0}/node/{1}".format(shock_url, sid), headers=header, verify=True) info = response.json()["data"] except: logger.error("There was an error retrieving information about the shock node id {0} from url {1}".format(sid, shock_url)) try: logger.info("Retrieving a handle id for the data.") handle_id = hs.persist_handle({"id" : sid, "type" : "shock", "url" : shock_url, "file_name": info["file"]["name"], "remote_md5": info["file"]["md5"]}) except: try: handle_id = hs.ids_to_handles([sid])[0]["hid"] single_handle = hs.hids_to_handles([handle_id]) assert len(single_handle) != 0 if info is not None: single_handle[0]["file_name"] = info["file"]["name"] single_handle[0]["remote_md5"] = info["file"]["md5"] print >> sys.stderr, single_handle handles.append(single_handle[0]) except: logger.error("The input shock node id {} is already registered or could not be registered".format(sid)) raise elif handle_ids is not None: for hid in handle_ids: try: single_handle = hs.hids_to_handles([hid]) assert len(single_handle) != 0 handles.append(single_handle[0]) except: logger.error("Invalid handle id {0}".format(hid)) raise return handles
def main(argv): parser = argparse.ArgumentParser( formatter_class=argparse.RawDescriptionHelpFormatter, prog='trnf_Convert_fastq', epilog=desc3) parser.add_argument('-s', '--shock_url', help='Shock url', action='store', dest='shock_url', default='https://kbase.us/services/shock-api') parser.add_argument('-n', '--handle_service_url', help='Handle service url', action='store', dest='hndl_url', default='https://kbase.us/services/handle_service') parser.add_argument('-i', '--in_ids', help='Two input Shock node ids (comma separated)', action='store', dest='inobj_id', default=None, required=False) parser.add_argument( '-f', '--file_names', help='Two optional handle file names (comma separated)', action='store', dest='loc_filepath', default=None, nargs=1, required=False) parser.add_argument('-d', '--hids', help='Two handle ids (comma separated)', action='store', dest='hid', default=None, required=False) parser.add_argument('-m', '--ins_mean', help='Mean insert size', action='store', dest='ins_mean', type=float, default=None) parser.add_argument('-k', '--std_dev', help='Standard deviation', action='store', dest='std_dev', type=float, default=None) parser.add_argument('-l', '--inl', help='Interleaved -- true/false', action='store', dest='inl', default=None) parser.add_argument('-r', '--r_ori', help='Read Orientation -- true/false', action='store', dest='read_orient', default=None) parser.add_argument('-o', '--out_file_name', help='Output file name', action='store', dest='out_fn', default=None, required=True) usage = parser.format_usage() parser.description = desc1 + ' ' + usage + desc2 parser.usage = argparse.SUPPRESS args = parser.parse_args() if args.inobj_id is None and args.hid is None: print >> sys.stderr, parser.description print >> sys.stderr, "Need to provide either shock node ids or handle ids" exit(1) kb_token = os.environ.get('KB_AUTH_TOKEN') hs = AbstractHandle(url=args.hndl_url, token=kb_token) hids = [] if args.hid is None: snids = args.inobj_id.split(',') if len(snids) != 2: print >> sys.stderr, "Please provide two shock node ids for pairend library" exit(4) try: hids.append( hs.persist_handle({ "id": snids[0], "type": "shock", "url": args.shock_url })) except: try: hids.append(hs.ids_to_handles([snids[0]])[0]["hid"]) except: traceback.print_exc(file=sys.stderr) e, v = sys.exc_info()[:2] print >> sys.stderr, "Please provide handle id.\nThe input shock node id {} is already registered or could not be registered : {} -- {}".format( snids[0], str(e), str(v)) exit(3) try: hids.append( hs.persist_handle({ "id": snids[1], "type": "shock", "url": args.shock_url })) except: try: hids.append(hs.ids_to_handles([snids[1]])[0]["hid"]) except: traceback.print_exc(file=sys.stderr) e, v = sys.exc_info()[:2] print >> sys.stderr, "Please provide handle id.\nThe input shock node id {} is already registered or could not be registered : {} -- {}".format( snids[1], str(e), str(v)) exit(3) else: hids = args.hid.split(',') if len(hids) != 2: print >> sys.stderr, "Please provide two handle ids for pairend library" exit(5) hds = hs.hids_to_handles(hids) if len(hds) != 2: print >> sys.stderr, 'Could not register a new handle with shock node id {} or wrong input handle id'.format( args.inobj_id) exit(2) ret = {"handle_1": hds[0], "handle_2": hds[1]} if args.ins_mean is not None: ret["insert_size_mean"] = args.ins_mean if args.std_dev is not None: ret["insert_size_std_dev"] = args.std_dev if args.inl == 'true': ret["interleaved"] = 0 if args.read_orient == 'true': ret["read_orientation_outward"] = 0 of = open(args.out_fn, "w") of.write(to_JSON(ret)) of.close()