示例#1
0
    def __init__(self, description, rule, availableFieldNames, location=None):
        super(IsUniqueCheck, self).__init__(description, rule, availableFieldNames, location)

        self.fieldNamesToCheck = []

        # Extract field names to check from rule.
        ruleReadLine = StringIO.StringIO(rule).readline
        toky = tokenize.generate_tokens(ruleReadLine)
        afterComma = True
        nextToken = toky.next()
        uniqueFieldNames = set()
        while not _tools.isEofToken(nextToken):
            tokenType = nextToken[0]
            tokenValue = nextToken[1]
            if afterComma:
                if tokenType != tokenize.NAME:
                    raise CheckSyntaxError(u"field name must contain only ASCII letters, numbers and underscores (_) "
                                           + "but found: %r [token type=%r]" % (tokenValue, tokenType))
                try:
                    fields.getFieldNameIndex(tokenValue, availableFieldNames)
                    if tokenValue in uniqueFieldNames:
                        raise CheckSyntaxError(u"duplicate field name for unique check must be removed: %s" % tokenValue)
                    uniqueFieldNames.add(tokenValue)
                except fields.FieldLookupError, error:
                    raise CheckSyntaxError(unicode(error))
                self.fieldNamesToCheck.append(tokenValue)
            elif not _tools.isCommaToken(nextToken):
                raise CheckSyntaxError(u"after field name a comma (,) must follow but found: %r" % (tokenValue))
示例#2
0
    def __init__(self, description, rule, availableFieldNames, location=None):
        super(DistinctCountCheck, self).__init__(description, rule, availableFieldNames, location)
        ruleReadLine = StringIO.StringIO(rule).readline
        tokens = tokenize.generate_tokens(ruleReadLine)
        firstToken = tokens.next()

        # Obtain and validate field to count.
        if firstToken[0] != tokenize.NAME:
            raise CheckSyntaxError(u"rule must start with a field name but found: %r" % firstToken[1])
        self.fieldNameToCount = firstToken[1]
        fields.getFieldNameIndex(self.fieldNameToCount, availableFieldNames)
        lineWhereFieldNameEnds, columnWhereFieldNameEnds = firstToken[3]
        assert columnWhereFieldNameEnds > 0
        assert lineWhereFieldNameEnds == 1

        # Build and test Python expression for validation.
        self.expression = DistinctCountCheck._COUNT_NAME + rule[columnWhereFieldNameEnds:]
        self.reset()
        self._eval()