Example #1
0
    def fetch(self, data_spec):
        """Return the data described by the given specification.

        :param dict data_spec: A data specification object
        :returns: data
        :raises ValidationError: when the validation check fails

        >>> port = Port({'name': 'a', 'type': 'number', 'format': 'number'})
        >>> port.fetch({'format': 'number', 'data': -1})
        -1
        """
        if self.auto_validate and not self.validate(data_spec):
            raise ValidationError(self, data_spec)

        if self.auto_convert:
            _data = self.convert(data_spec, self.format)
            data = _data.get('data')
        elif self.format == data_spec.get('format'):
            # TODO: This doesn't look right...
            if 'data' in self and self['data'] is not None:
                data = self['data']
            else:
                data = io.fetch(data_spec, task_input=self).get('data')
        else:
            raise Exception('Expected matching data formats ({} != {})' % (
                str(data_spec['format']), str(self.format)
            ))

        return data
Example #2
0
    def fetch(self, data_spec):
        """Return the data described by the given specification.

        :param dict data_spec: A data specification object
        :returns: data
        :raises ValidationError: when the validation check fails

        >>> port = Port({'name': 'a', 'type': 'number', 'format': 'number'})
        >>> port.fetch({'format': 'number', 'data': -1})
        -1
        """
        if self.auto_validate and not self.validate(data_spec):
            raise ValidationError(self, data_spec)

        if self.auto_convert:
            _data = self.convert(data_spec, self.format)
            data = _data.get('data')
        elif self.format == data_spec.get('format'):
            # TODO: This doesn't look right...
            if 'data' in self and self['data'] is not None:
                data = self['data']
            else:
                data = io.fetch(data_spec, task_input=self).get('data')
        else:
            raise Exception('Expected matching data formats ({} != {})' %
                            (str(data_spec['format']), str(self.format)))

        return data
Example #3
0
    def get_analysis(filename):
        with open(filename) as f:
            analysis = json.load(f)

            if "script" not in analysis:
                analysis["script"] = fetch(
                    {"mode": analysis.get("script_fetch_mode", "auto"), "url": analysis["script_uri"]}
                )

        return analysis
Example #4
0
def convert(type, input, output, fetch=True, status=None, **kwargs):
    """
    Convert data from one format to another.

    :param type: The type specifier string of the input data.
    :param input: A binding dict of the form
        ``{'format': format, 'data', data}``, where ``format`` is the format
        specifier string, and ``data`` is the raw data to convert.
        The dict may also be of the form
        ``{'format': format, 'uri', uri}``, where ``uri`` is the location of
        the data (see :py:mod:`girder_worker.uri` for URI formats).
    :param output: A binding of the form
        ``{'format': format}``, where ``format`` is the format
        specifier string to convert the data to.
        The binding may also be in the form
        ``{'format': format, 'uri', uri}``, where ``uri`` specifies
        where to place the converted data.
    :param fetch: Whether to do an initial data fetch before conversion
        (default ``True``).
    :returns: The output binding
        dict with an additional field ``'data'`` containing the converted data.
        If ``'uri'`` is present in the output binding, instead saves the data
        to the specified URI and
        returns the output binding unchanged.
    """
    kwargs = kwargs.copy()
    kwargs['auto_convert'] = False

    if fetch:
        input['data'] = io.fetch(input, **kwargs)

    if input['format'] == output['format']:
        data = input['data']
    else:
        data_descriptor = input
        try:
            conversion_path = converter_path(Validator(type, input['format']),
                                             Validator(type, output['format']))
        except NetworkXNoPath:
            raise Exception('No conversion path from %s/%s to %s/%s' %
                            (type, input['format'], type, output['format']))

        # Run data_descriptor through each conversion in the path
        for conversion in conversion_path:
            result = run(conversion, {'input': data_descriptor},
                         status=status,
                         **kwargs)
            data_descriptor = result['output']
        data = data_descriptor['data']

    if status == utils.JobStatus.CONVERTING_OUTPUT:
        job_mgr = kwargs.get('_job_manager')
        set_job_status(job_mgr, utils.JobStatus.PUSHING_OUTPUT)
    io.push(data, output, **kwargs)
    return output
Example #5
0
    def get_analysis(filename):
        with open(filename) as f:
            analysis = json.load(f)

            if 'script' not in analysis:
                analysis['script'] = fetch({
                    'mode': analysis.get('script_fetch_mode', 'auto'),
                    'url': analysis['script_uri']
                })

        return analysis
Example #6
0
    def get_analysis(filename):
        with open(filename) as f:
            analysis = json.load(f)

            if 'script' not in analysis:
                analysis['script'] = fetch({
                    'mode':
                    analysis.get('script_fetch_mode', 'auto'),
                    'url':
                    analysis['script_uri']
                })

        return analysis