Beispiel #1
0
    def test_classification(self):
        cases = [
            ('', False, False, False, False, True, True),
            ('#', False, False, False, False, True, True),
            ('#f', False, False, False, False, True, True),
            ('?', False, False, False, False, True, False),
            ('?q', False, False, False, False, True, False),
            ('p', False, False, False, False, True, False),
            ('/p', False, False, False, True, False, False),
            ('/p?', False, False, False, True, False, False),
            ('/p?q', False, False, False, True, False, False),
            ('/p#', False, False, False, True, False, False),
            ('/p#f', False, False, False, True, False, False),
            ('/p?q#f', False, False, False, True, False, False),
            ('//', False, False, True, False, False, False),
            ('//n?', False, False, True, False, False, False),
            ('//n?q', False, False, True, False, False, False),
            ('//n#', False, False, True, False, False, False),
            ('//n#f', False, False, True, False, False, False),
            ('//n?q#f', False, False, True, False, False, False),
            ('s:', True, True, False, False, False, False),
            ('s:p', True, True, False, False, False, False),
            ('s:p?', True, True, False, False, False, False),
            ('s:p?q', True, True, False, False, False, False),
            ('s:p#', True, False, False, False, False, False),
            ('s:p#f', True, False, False, False, False, False),
            ('s://', True, True, False, False, False, False),
            ('s://h', True, True, False, False, False, False),
            ('s://h/', True, True, False, False, False, False),
            ('s://h/p', True, True, False, False, False, False),
            ('s://h/p?', True, True, False, False, False, False),
            ('s://h/p?q', True, True, False, False, False, False),
            ('s://h/p#', True, False, False, False, False, False),
            ('s://h/p#f', True, False, False, False, False, False),

        ]
        for s, uri, absuri, netpath, abspath, relpath, samedoc in cases:
            for ref in [s, s.encode('ascii')]:
                parts = uritools.urisplit(ref)
                self.assertEqual(parts.isuri(), uri)
                self.assertEqual(parts.isabsuri(), absuri)
                self.assertEqual(parts.isnetpath(), netpath)
                self.assertEqual(parts.isabspath(), abspath)
                self.assertEqual(parts.isrelpath(), relpath)
                self.assertEqual(parts.issamedoc(), samedoc)
                self.assertEqual(uritools.isuri(ref), uri)
                self.assertEqual(uritools.isabsuri(ref), absuri)
                self.assertEqual(uritools.isnetpath(ref), netpath)
                self.assertEqual(uritools.isabspath(ref), abspath)
                self.assertEqual(uritools.isrelpath(ref), relpath)
                self.assertEqual(uritools.issamedoc(ref), samedoc)
Beispiel #2
0
    def _load_configuration(self, uri: str) -> None:
        """
        Loads in the configuration from the specified file resource.

        #see `adobe himl` package for more information:
        https://github.com/adobe/himl

        :param uri:
            RFC-3986 Uniform Resource Identifier (URI)
        """
        if not uritools.isuri(uri):
            raise ValueError(URI_ERROR_MSG)
        uri_tuple = uritools.urisplit(uri)
        yaml_file = uri_tuple[2][1:]
        try:
            self.settings = dict(self.yaml.process(path=yaml_file, output_format="yaml", print_data=True))
        except OSError:
            self._log.critical(OS_ERROR_MSG, yaml_file, exc_info=True)
            sys.exit()
 def validate_type_URI(value):
     """ Uses the distutils library to validate date time. Returns True/False """
     return uritools.isuri(value)
def validate_biosimulations_metadata_for_uri(metadata,
                                             validate_minimal_metadata=False,
                                             archive=None,
                                             working_dir=None):
    """ Validate BioSimulations metadata for a file in a COMBINE/OMEX archive

    Args:
        metadata (:obj:`dict`): BioSimulations metadata
        validate_minimal_metadata (:obj:`bool`, optional): whether to check that all required metadata attributes
            are defined
        archive (:obj:`CombineArchive`, optional): parent COMBINE archive
        working_dir (:obj:`str`, optional): working directory (e.g., directory of the parent COMBINE/OMEX archive)

    Returns:
        :obj:`tuple`:

            * nested :obj:`list` of :obj:`str`: nested list of errors with the metadata
            * nested :obj:`list` of :obj:`str`: nested list of warnings with the metadata
    """
    errors = []
    warnings = []

    # required attributes are present
    if validate_minimal_metadata:
        for predicate_type in BIOSIMULATIONS_PREDICATE_TYPES.values():
            if predicate_type['required'] and (
                (not predicate_type['multiple_allowed']
                 and metadata[predicate_type['attribute']] is None) or
                (predicate_type['multiple_allowed']
                 and metadata[predicate_type['attribute']] == [])):
                errors.append([
                    'Attribute `{}` ({}) is required.'.format(
                        predicate_type['attribute'], predicate_type['uri'])
                ])

    # URIs are URLs
    # Identifiers.org URLs point to valid identifiers
    for predicate_type in BIOSIMULATIONS_PREDICATE_TYPES.values():
        if predicate_type['has_uri'] and predicate_type['has_label']:
            if predicate_type['multiple_allowed']:
                objects = metadata[predicate_type['attribute']]
            else:
                objects = [metadata[predicate_type['attribute']]]

            for object in objects:
                if object and object['uri']:
                    if not uritools.isuri(object['uri']):
                        errors.append([
                            'URI `{}` of attribute `{}` ({}) is not a valid URI.'
                            .format(object['uri'], predicate_type['attribute'],
                                    predicate_type['uri'])
                        ])
                    else:
                        match = re.match(
                            r'^https?://identifiers\.org/(([^/:]+/[^/:]+|[^/:]+)[/:](.+))$',
                            object['uri'])
                        if match:
                            try:
                                validate_identifiers_org_uri(object['uri'])
                            except InvalidIdentifiersOrgUri as exception:
                                msg = (
                                    'URI `{}` of attribute `{}` ({}) is not a valid Identifiers.org identifier.'
                                ).format(object['uri'],
                                         predicate_type['attribute'],
                                         predicate_type['uri'])
                                errors.append([msg, [[str(exception)]]])

    # thumbnail is a file; file type is checked by COMBINE validation
    if working_dir:
        for thumbnail in metadata['thumbnails']:
            thumbnail = os.path.relpath(thumbnail, '.')
            thumbnail_filename = os.path.join(working_dir, thumbnail)
            if os.path.isfile(thumbnail_filename):
                if archive:
                    for content in archive.contents:
                        if (content and content.location
                                and thumbnail == os.path.relpath(
                                    content.location, '.')):
                            is_valid = False
                            for format in BIOSIMULATIONS_THUMBNAIL_FORMATS:
                                if content.format and re.match(
                                        CombineArchiveContentFormatPattern[
                                            format], content.format):
                                    is_valid = True
                                    break
                            if not is_valid:
                                errors.append([
                                    'The format of thumbnail `{}` must be one of the following:'
                                    .format(thumbnail),
                                    sorted([[format] for format in
                                            BIOSIMULATIONS_THUMBNAIL_FORMATS])
                                ])

            else:
                errors.append(
                    ['Thumbnail `{}` is not a file.'.format(thumbnail)])
    else:
        if metadata['thumbnails']:
            warnings.append([
                ('The locations of the thumbnails could not be validated '
                 'because a working directory was not provided.')
            ])

    # created is a date
    if metadata['created'] is not None:
        try:
            dateutil.parser.parse(metadata['created'])
        except dateutil.parser.ParserError:
            errors.append([
                'Created date `{}` is not a valid date.'.format(
                    metadata['created'])
            ])

    # modified are dates
    for date in metadata['modified']:
        try:
            dateutil.parser.parse(date)
        except dateutil.parser.ParserError:
            errors.append(
                ['Modified date `{}` is not a valid date.'.format(date)])

    # return errors and warnings
    return (errors, warnings)