예제 #1
0
class HandleService(object):
    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 upload(self,infile) :

        handle = self.dsi.new_handle()
	url = "{}/node/{}".format(handle["url"], handle["id"]);
        ref_data = {}
        try:
            ref_data = _upload_file_to_shock(url,filePath=infile,token=self.token)
        except:
            raise

	remote_md5 = ref_data["file"]["checksum"]["md5"]
	#remote_sha1 = ref_data["file"]["checksum"]["sha1"] #SHOCK PUT would not create remote_sha1

        if remote_md5 is None: raise Exception("looks like upload failed and no md5 returned from remote server")
	
	handle["remote_md5"] = remote_md5
	#handle["remote_sha1"] = remote_sha1 	
        handle["file_name"] = _os.path.basename(infile)

	self.dsi.persist_handle(handle)
	return handle


    def download (self, handle, outfile):

        if( isinstance(handle, dict)): raise Exception("hande is not a dictionary")
        if "id" not in handle: raise Exception("no id in handle")
        if "url" not in handle: raise Exception("no url in handle")
        if outfile is None: raise Exception("outfile is not defined")
        raise Exception("Not implemented yet")

    def new_handle(self, *arg):
        return self.dsi.new_handle(*arg)

    def localize_handle(self, *arg):
        return self.dsi.localize_handle(*arg)

    def initialize_handle (self, *arg):
        return self.dsi.initialize_handle (*arg)

    def persist_handle(self, *arg):
        return self.dsi.persist_handle (*arg)

    def upload_metadata(self, handle, infile):
        raise Exception("Not implemented yet")

    def download_metadata (self, handle, outfile):
        raise Exception("Not implemented yet")

    def list_handles (self, *arg):
        return self.dsi.list_handles (*arg)

    def are_readable (self, *arg):
        return self.dsi.are_readable(*arg)

    def is_readable(self, *arg):
        return self.dsi.is_readable(*arg)

    def hids_to_handles(self, *arg):
        return self.dsi.hids_to_handles(*arg)

    def ids_to_handles(self, *arg):
        return self.dsi.ids_to_handles(*arg)