def science_object_get(self, pid, path): """First try the MN set in the session. Then try to resolve via the CN set in the session.""" mn_client = cli_client.CLIMNClient( **self._mn_client_connect_params_from_session()) try: response = mn_client.get(pid) except d1_common.types.exceptions.DataONEException: pass else: self._output(response, path) return cn_client = cli_client.CLICNClient( **self._cn_client_connect_params_from_session()) object_location_list = cn_client.resolve(pid) for location in object_location_list.objectLocation: try: params = self._mn_client_connect_params_from_session() params['base_url'] = location.baseURL mn_client = cli_client.CLIMNClient(**params) response = mn_client.get(pid) except d1_common.types.exceptions.DataONEException: pass else: self._output(response, path) return raise cli_exceptions.CLIError(u'Could not find object: {}'.format(pid))
def copy_requests_stream_to_file(response, path): try: with open(os.path.expanduser(path), 'wb') as f: for chunk_str in response.iter_content(): f.write(chunk_str) except EnvironmentError as xxx_todo_changeme2: (errno, strerror) = xxx_todo_changeme2.args error_line_list = [ 'Could not write to object_file: {}'.format(path), 'I/O error({}): {}'.format(errno, strerror), ] error_message = '\n'.join(error_line_list) raise cli_exceptions.CLIError(error_message)
def system_metadata_get(self, pid, path): metadata = None try: client = cli_client.CLICNClient( **self._cn_client_connect_params_from_session()) metadata = client.getSystemMetadata(pid) except d1_common.types.exceptions.DataONEException: pass if metadata is None: try: client = cli_client.CLIMNClient( **self._mn_client_connect_params_from_session()) metadata = client.getSystemMetadata(pid) except d1_common.types.exceptions.DataONEException: pass if metadata is None: raise cli_exceptions.CLIError( 'Unable to get System Metadata: {}'.format(pid)) self._system_metadata_print(metadata, path)
def copy_file_like_object_to_file(file_like_object, path): try: fsrc = sys.stdin if file_like_object: fsrc = file_like_object if path: # fdst = open(os.path.expanduser(path), 'w', encoding='utf-8') fdst = open(os.path.expanduser(path), 'wb') shutil.copyfileobj(fsrc, fdst) fdst.close() else: shutil.copyfileobj(fsrc, sys.stdout) except EnvironmentError as xxx_todo_changeme1: (errno, strerror) = xxx_todo_changeme1.args error_line_list = [ 'Could not write to object_file: {}'.format(path), 'I/O error({}): {}'.format(errno, strerror), ] error_message = '\n'.join(error_line_list) raise cli_exceptions.CLIError(error_message)
def output(file_like_object, path, verbose=False): """Display or save file like object""" if not path: for line in file_like_object: if verbose: print_info(line.rstrip()) else: print(line.rstrip()) else: try: object_file = open(os.path.expanduser(path), 'w', encoding='utf-8') shutil.copyfileobj(file_like_object, object_file) object_file.close() except EnvironmentError as xxx_todo_changeme: (errno, strerror) = xxx_todo_changeme.args error_line_list = [ 'Could not write to object_file: {}'.format(path), 'I/O error({}): {}'.format(errno, strerror), ] error_message = '\n'.join(error_line_list) raise cli_exceptions.CLIError(error_message)