def test_log_or_raise(mocker): from clldutils.misc import log_or_raise with pytest.raises(ValueError): log_or_raise('') log = mocker.Mock() log_or_raise('test', log=log) assert log.warn.called
def check(self, clts=None, log=None, ipa_col=IPA_COLUMN): """ Check a profile for consistency, logging problems. For each grapheme, raise: - a warning if there are duplicate entries - an error if there are inconsistencies - an error if the mapping has invalid BIPA """ mapping = collections.defaultdict(list) if self.fname: # We read the profile from disk because segments.Profile already skips duplicate # graphemes, which we want to investigate more closely. for spec in dsv.reader(self.fname, dicts=True, delimiter='\t'): mapping[spec[self.GRAPHEME_COL]].append(spec[ipa_col]) for grapheme in mapping: # check mapping consistency if len(mapping[grapheme]) >= 2: if len(set(mapping[grapheme])) == 1: log_or_raise( "Duplicate, redundant entry or entries for grapheme [{}]." .format(grapheme), log=log, level='warning') else: log_or_raise( "Inconsist entries for grapheme [{}]: multiple mappings {}." .format(grapheme, str(mapping[grapheme])), log=log, level='error') # check BIPA consistency if clts: for value in mapping[grapheme]: if value: # check for unknown sounds unknown = [ isinstance(clts.bipa[segment], pyclts.models.UnknownSound) for segment in ipa2tokens(value) if segment and segment != 'NULL' ] if any(unknown): log_or_raise( "Mapping [{}] ({}) -> [{}] ({}) includes an unknown sound." .format(grapheme, unicode2codepointstr(grapheme), value, unicode2codepointstr(value)), log=log, level='error')
def validate(self, log=None, validators=None): validators = validators or [] validators.extend(VALIDATORS) success = True default_tg = TableGroup.from_file( pkg_path('modules', '{0}{1}'.format(self.module, MD_SUFFIX))) for default_table in default_tg.tables: dtable_uri = default_table.common_props['dc:conformsTo'] try: table = self[dtable_uri] except KeyError: log_or_raise('{0} requires {1}'.format(self.module, dtable_uri), log=log) success = False table = None if table: default_cols = { c.propertyUrl.uri for c in default_table.tableSchema.columns if c.required or c.common_props.get('dc:isRequiredBy')} cols = { c.propertyUrl.uri for c in table.tableSchema.columns if c.propertyUrl} table_uri = table.common_props['dc:conformsTo'] for col in default_cols - cols: log_or_raise('{0} requires column {1}'.format(table_uri, col), log=log) success = False for table in self.tables: type_uri = table.common_props.get('dc:conformsTo') if type_uri: try: TERMS.is_cldf_uri(type_uri) except ValueError: success = False log_or_raise('invalid CLDF URI: {0}'.format(type_uri), log=log) # FIXME: check whether table.common_props['dc:conformsTo'] is in validators! validators_ = [] for col in table.tableSchema.columns: if col.propertyUrl: col_uri = col.propertyUrl.uri try: TERMS.is_cldf_uri(col_uri) except ValueError: success = False log_or_raise('invalid CLDF URI: {0}'.format(col_uri), log=log) for table_, col_, v_ in validators: if (not table_ or table is self.get(table_)) and col is self.get((table, col_)): validators_.append((col, v_)) fname = Path(table.url.resolve(table._parent.base)) if fname.exists(): for fname, lineno, row in table.iterdicts(log=log, with_metadata=True): for col, validate in validators_: try: validate(self, table, col, row) except ValueError as e: log_or_raise( '{0}:{1}:{2} {3}'.format(fname.name, lineno, col.name, e), log=log) success = False if not table.check_primary_key(log=log): success = False if not self.tablegroup.check_referential_integrity(log=log): success = False return success
def validate(self, log=None, validators=None): validators = validators or [] validators.extend(VALIDATORS) success = True default_tg = TableGroup.from_file( pkg_path('modules', '{0}{1}'.format(self.module, MD_SUFFIX))) for default_table in default_tg.tables: dtable_uri = default_table.common_props['dc:conformsTo'] try: table = self[dtable_uri] except KeyError: log_or_raise('{0} requires {1}'.format(self.module, dtable_uri), log=log) success = False table = None if table: default_cols = { c.propertyUrl.uri for c in default_table.tableSchema.columns if c.required or c.common_props.get('dc:isRequiredBy')} cols = { c.propertyUrl.uri for c in table.tableSchema.columns if c.propertyUrl} table_uri = table.common_props['dc:conformsTo'] for col in default_cols - cols: log_or_raise('{0} requires column {1}'.format(table_uri, col), log=log) success = False for table in self.tables: type_uri = table.common_props.get('dc:conformsTo') if type_uri: try: TERMS.is_cldf_uri(type_uri) except ValueError: success = False log_or_raise('invalid CLDF URI: {0}'.format(type_uri), log=log) # FIXME: check whether table.common_props['dc:conformsTo'] is in validators! validators_ = [] for col in table.tableSchema.columns: if col.propertyUrl: col_uri = col.propertyUrl.uri try: TERMS.is_cldf_uri(col_uri) except ValueError: success = False log_or_raise('invalid CLDF URI: {0}'.format(col_uri), log=log) for table_, col_, v_ in validators: if (not table_ or table is self.get(table_)) and col is self.get((table, col_)): validators_.append((col, v_)) fname = Path(table.url.resolve(table._parent.base)) if fname.exists(): for fname, lineno, row in table.iterdicts(log=log, with_metadata=True): for col, validate in validators_: try: validate(self, table, col, row) except ValueError as e: log_or_raise( '{0}:{1}:{2} {3}'.format(fname.name, lineno, col.name, e), log=log) success = False if not table.check_primary_key(log=log): success = False else: log_or_raise('{0} does not exist'.format(fname), log=log) success = False if not self.tablegroup.check_referential_integrity(log=log): success = False return success