Ejemplo n.º 1
0
    def __init__(self, fieldName, isAllowedToBeEmpty, length, rule, dataFormat):
        super(ChoiceFieldFormat, self).__init__(fieldName, isAllowedToBeEmpty, length, rule, dataFormat, emptyValue="")
        self.choices = []

        # Split rule into tokens, ignoring white space.
        tokens = _tools.tokenizeWithoutSpace(rule)

        # Extract choices from rule tokens.
        previousToky = None
        toky = tokens.next()
        while not _tools.isEofToken(toky):
            if _tools.isCommaToken(toky):
                # Handle comma after comma without choice.
                if previousToky:
                    previousTokyText = previousToky[1]
                else:
                    previousTokyText = None
                raise FieldSyntaxError(u"choice value must precede a comma (,) but found: %r" % previousTokyText)
            choice = _tools.tokenText(toky)
            if not choice:
                raise FieldSyntaxError(
                    u"choice field must be allowed to be empty instead of containing an empty choice"
                )
            self.choices.append(choice)
            toky = tokens.next()
            if not _tools.isEofToken(toky):
                if not _tools.isCommaToken(toky):
                    raise FieldSyntaxError(u"comma (,) must follow choice value %r but found: %r" % (choice, toky[1]))
                # Process next choice after comma.
                toky = tokens.next()
                if _tools.isEofToken(toky):
                    raise FieldSyntaxError(u"trailing comma (,) must be removed")
        if not self.isAllowedToBeEmpty and not self.choices:
            raise FieldSyntaxError(u"choice field without any choices must be allowed to be empty")
Ejemplo n.º 2
0
    def __init__(self, fieldName, isAllowedToBeEmpty, length, rule,
                 dataFormat):
        super(ChoiceFieldFormat, self).__init__(fieldName,
                                                isAllowedToBeEmpty,
                                                length,
                                                rule,
                                                dataFormat,
                                                emptyValue="")
        self.choices = []

        # Split rule into tokens, ignoring white space.
        tokens = _tools.tokenizeWithoutSpace(rule)

        # Extract choices from rule tokens.
        previousToky = None
        toky = tokens.next()
        while not _tools.isEofToken(toky):
            if _tools.isCommaToken(toky):
                # Handle comma after comma without choice.
                if previousToky:
                    previousTokyText = previousToky[1]
                else:
                    previousTokyText = None
                raise FieldSyntaxError(
                    u"choice value must precede a comma (,) but found: %r" %
                    previousTokyText)
            choice = _tools.tokenText(toky)
            if not choice:
                raise FieldSyntaxError(
                    u"choice field must be allowed to be empty instead of containing an empty choice"
                )
            self.choices.append(choice)
            toky = tokens.next()
            if not _tools.isEofToken(toky):
                if not _tools.isCommaToken(toky):
                    raise FieldSyntaxError(
                        u"comma (,) must follow choice value %r but found: %r"
                        % (choice, toky[1]))
                # Process next choice after comma.
                toky = tokens.next()
                if _tools.isEofToken(toky):
                    raise FieldSyntaxError(
                        u"trailing comma (,) must be removed")
        if not self.isAllowedToBeEmpty and not self.choices:
            raise FieldSyntaxError(
                u"choice field without any choices must be allowed to be empty"
            )
Ejemplo n.º 3
0
def validatedFieldName(supposedFieldName, location=None):
    """
    Same as ``supposedFieldName`` except with surrounding white space removed, provided that it
    describes a valid field name. Otherwise, raise a `FieldSyntaxError` pointing to ``location``.
    """
    tokens = _tools.tokenizeWithoutSpace(supposedFieldName)
    tokenType, result, _, _, _ = tokens.next()
    if tokenType != token.NAME:
        message = (
            u"field name must be a valid Python name consisting of ASCII letters, underscore (%r) and digits but is: %r"
            % ("_", result)
        )
        raise FieldSyntaxError(message, location)
    if keyword.iskeyword(result):
        raise FieldSyntaxError(u"field name must not be a Python keyword but is: %r" % result, location)
    toky = tokens.next()
    if not _tools.isEofToken(toky):
        raise FieldSyntaxError(u"field name must be a single word but is: %r" % supposedFieldName, location)
    return result
Ejemplo n.º 4
0
def validatedFieldName(supposedFieldName, location=None):
    """
    Same as ``supposedFieldName`` except with surrounding white space removed, provided that it
    describes a valid field name. Otherwise, raise a `FieldSyntaxError` pointing to ``location``.
    """
    tokens = _tools.tokenizeWithoutSpace(supposedFieldName)
    tokenType, result, _, _, _ = tokens.next()
    if tokenType != token.NAME:
        message = u"field name must be a valid Python name consisting of ASCII letters, underscore (%r) and digits but is: %r" % (
            "_", result)
        raise FieldSyntaxError(message, location)
    if keyword.iskeyword(result):
        raise FieldSyntaxError(
            u"field name must not be a Python keyword but is: %r" % result,
            location)
    toky = tokens.next()
    if not _tools.isEofToken(toky):
        raise FieldSyntaxError(
            u"field name must be a single word but is: %r" % supposedFieldName,
            location)
    return result