def request_json_property(self, uri): """Execute a REST call on the server and return string""" try: return self.interface.request_json_property(uri) except Exception as exc: self.reporter.error('Failure executing GET call {}: {}'.format( uri, message_from_exception(exc))) raise exc
def _show_exception_to_user(exc): message = AnsiCodes.RED + "CopyXnat could not complete due to the " \ "following error: {}" + AnsiCodes.END if isinstance(exc, UserError): six.print_(message.format(exc.user_friendly_message), file=sys.stderr) else: six.print_(message.format(message_from_exception(exc)), file=sys.stderr) six.print_("If you think this may be a bug, please create an issue at " "https://github.com/tomdoel/copyxnat/issues and include " "the following error details:", file=sys.stderr) traceback.print_exc()
def request_string(self, uri, error_on_failure=True): """Execute a REST call on the server and return string""" try: return self.interface.request_string(uri) except Exception as exc: message = 'Failure executing GET call {}: {}'.format( uri, message_from_exception(exc)) if error_on_failure: self.reporter.error(message) else: self.reporter.log(message) raise exc
def _parse_metadata(self, local_file): metadata = {} try: if pydicom.misc.is_dicom(local_file): tags = pydicom.dcmread(local_file, stop_before_pixels=True, specific_tags=['SeriesInstanceUID']) if 'SeriesInstanceUID' in tags: metadata['series_instance_uid'] = \ tags['SeriesInstanceUID'].value except Exception as exc: # pylint: disable=broad-except self.reporter.warning('Error when attempting to parse file {}: ' 'Error: {}'.format( local_file, message_from_exception(exc))) return metadata
def does_request_succeed(self, uri, method='GET'): """Execute a REST call on the server and return True if it succeeds""" if self.app_settings.dry_run: self.reporter.warning( 'DRY RUN: will not execute call {} {}'.format(method, uri)) return False if self.read_only and method != 'GET': raise RuntimeError( 'Programming error: attempting {} request {} in ' 'read-only mode to server {}'.format(method, uri, self.full_name)) try: self.interface.request(uri=uri, method=method) self.reporter.debug('Success executing {} call {}'.format( method, uri)) return True except Exception as exc: # pylint: disable=broad-except self.reporter.debug('Failure executing {} call {}: {}'.format( method, uri, message_from_exception(exc))) return False
def run_command(command, src_params, dst_params=None, project_filter=None, app_settings=None, reporter=None): """Runs the command on the specified XNAT servers @param command: the command class to run @param src_params: XnatServerParams for source server @param dst_params: XnatServerParams for destination server @param project_filter: array of project names to process or None to process all projects visible on the server. If a project name needs to be different on the source and destination servers, the string should be of the form src_project_name:dst_project_name @param app_settings: Global settings; if None then defaults will be used @param reporter: PyReporter object for user input/output and logging """ if not app_settings: app_settings = AppSettings() if not reporter: reporter = PyReporter(data_dir=app_settings.data_dir, verbose=app_settings.verbose) src_xnat = None dst_xnat = None try: reporter.output('{}: running {} command'.format( get_version_string(), command.NAME)) cache_box = CacheBox(root_path=app_settings.data_dir, reporter=reporter) cache_type = command.CACHE_TYPE base_cache = cache_box.new_cache(cache_type=cache_type) factory = ServerFactory(app_settings.backend) src_xnat = XnatServer(factory=factory, params=src_params, base_cache=base_cache, app_settings=app_settings, reporter=reporter, read_only=app_settings.dry_run or not command.MODIFY_SRC_SERVER) if dst_params and command.USE_DST_SERVER: dst_xnat = XnatServer(factory=factory, params=dst_params, base_cache=base_cache, app_settings=app_settings, reporter=reporter, read_only=app_settings.dry_run or not command.MODIFY_DST_SERVER) else: dst_xnat = None reporter.info('Download and cache files will be stored in {}'.format( src_xnat.cache.full_path())) result = run_command_on_servers(command=command, src_xnat_server=src_xnat, dst_xnat_server=dst_xnat, project_filter=project_filter, app_settings=app_settings, reporter=reporter) output_path = src_xnat.cache.full_path() reporter.debug('Output files are in {}'.format(output_path)) reporter.output('Completed running {} command'.format(command.NAME)) except Exception as exc: # pylint: disable=broad-except message = message_from_exception(exc) reporter.log('Command {} failed due to error {}'.format( command.NAME, message)) six.reraise(*sys.exc_info()) finally: if src_xnat: src_xnat.logout() if dst_xnat: dst_xnat.logout() reporter.debug('Output from run_command:{} '.format(str(result))) return result