Exemplo n.º 1
0
    def __init__(self, field_name, is_allowed_to_be_empty, length, rule, data_format):
        super(ConstantFieldFormat, self).__init__(
            field_name, is_allowed_to_be_empty, length, rule, data_format, empty_value='')

        # Extract constant from rule tokens.
        tokens = _tools.tokenize_without_space(rule)
        toky = next(tokens)
        if _tools.is_eof_token(toky):
            # No rule means that the field must always be empty.
            self._constant = ''
        else:
            self._constant = _tools.token_text(toky)
            toky = next(tokens)
            if not _tools.is_eof_token(toky):
                raise errors.InterfaceError(
                    'constant rule must be a single Python token but also found: %s'
                    % _compat.text_repr(_tools.token_text(toky)))
        has_empty_rule = (rule == '')
        if self.is_allowed_to_be_empty and not has_empty_rule:
            raise errors.InterfaceError(
                'to describe a Constant that can be empty, use a Choice field with a single choice')
        if not self.is_allowed_to_be_empty and has_empty_rule:
            raise errors.InterfaceError(
                'field must be marked as empty to describe a constant empty value')
        try:
            self.length.validate(
                'rule of constant field %s' % _compat.text_repr(self.field_name), len(self._constant))
        except errors.RangeValueError:
            raise errors.InterfaceError(
                'length is %s but must be %d to match constant %s'
                % (self.length, len(self._constant), _compat.text_repr(self._constant)))
Exemplo n.º 2
0
    def __init__(self, field_name, is_allowed_to_be_empty, length, rule, data_format):
        super(ConstantFieldFormat, self).__init__(
            field_name, is_allowed_to_be_empty, length, rule, data_format, empty_value='')

        # Extract constant from rule tokens.
        tokens = _tools.tokenize_without_space(rule)
        toky = next(tokens)
        if _tools.is_eof_token(toky):
            # No rule means that the field must always be empty.
            self._constant = ''
        else:
            self._constant = _tools.token_text(toky)
            toky = next(tokens)
            if not _tools.is_eof_token(toky):
                raise errors.InterfaceError(
                    _('constant rule must be a single Python token but also found: %s')
                    % _compat.text_repr(_tools.token_text(toky)))
        has_empty_rule = (rule == '')
        if self.is_allowed_to_be_empty and not has_empty_rule:
            raise errors.InterfaceError(
                _('to describe a Constant that can be empty, use a Choice field with a single choice'))
        if not self.is_allowed_to_be_empty and has_empty_rule:
            raise errors.InterfaceError(
                _('field must be marked as empty to describe a constant empty value'))
        try:
            self.length.validate(
                _('rule of constant field %s') % _compat.text_repr(self.field_name), len(self._constant))
        except errors.RangeValueError:
            raise errors.InterfaceError(
                _('length is %s but must be %d to match constant %s')
                % (self.length, len(self._constant), _compat.text_repr(self._constant)))
Exemplo n.º 3
0
def generate_choices(rule):
    choices = []

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

    # Extract choices from rule tokens.
    # TODO: Handle comma after comma without choice.
    # previous_toky = None
    toky = next(tokens)
    while not _tools.is_eof_token(toky):
        if _tools.is_comma_token(toky):
            # TODO: Handle comma after comma without choice.
            # if previous_toky:
            #     previous_toky_text = previous_toky[1]
            # else:
            #     previous_toky_text = None
            pass
        choice = _tools.token_text(toky)
        choices.append(choice)
        toky = next(tokens)
        if not _tools.is_eof_token(toky):
            # Process next choice after comma.
            toky = next(tokens)

    return choices
Exemplo n.º 4
0
    def __init__(self, field_name, is_allowed_to_be_empty, length, rule,
                 data_format):
        super(ChoiceFieldFormat, self).__init__(field_name,
                                                is_allowed_to_be_empty,
                                                length,
                                                rule,
                                                data_format,
                                                empty_value='')
        self.choices = []

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

        # Extract choices from rule tokens.
        previous_toky = None
        toky = next(tokens)
        while not _tools.is_eof_token(toky):
            if _tools.is_comma_token(toky):
                # Handle comma after comma without choice.
                if previous_toky:
                    previous_toky_text = previous_toky[1]
                else:
                    previous_toky_text = None
                raise errors.InterfaceError(
                    "choice value must precede a comma (,) but found: %s" %
                    _compat.text_repr(previous_toky_text))
            choice = _tools.token_text(toky)
            if not choice:
                raise errors.InterfaceError(
                    "choice field must be allowed to be empty instead of containing an empty choice"
                )
            self.choices.append(choice)
            toky = next(tokens)
            if not _tools.is_eof_token(toky):
                if not _tools.is_comma_token(toky):
                    raise errors.InterfaceError(
                        "comma (,) must follow choice value %s but found: %s" %
                        (_compat.text_repr(choice), _compat.text_repr(
                            toky[1])))
                # Process next choice after comma.
                toky = next(tokens)
                if _tools.is_eof_token(toky):
                    raise errors.InterfaceError(
                        "trailing comma (,) must be removed")
        if not self.is_allowed_to_be_empty and not self.choices:
            raise errors.InterfaceError(
                "choice field without any choices must be allowed to be empty")
Exemplo n.º 5
0
    def __init__(self, field_name, is_allowed_to_be_empty, length, rule, data_format):
        super(ChoiceFieldFormat, self).__init__(
            field_name, is_allowed_to_be_empty, length, rule, data_format, empty_value='')
        self.choices = []

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

        # Extract choices from rule tokens.
        previous_toky = None
        toky = next(tokens)
        while not _tools.is_eof_token(toky):
            if _tools.is_comma_token(toky):
                # Handle comma after comma without choice.
                if previous_toky:
                    previous_toky_text = previous_toky[1]
                else:
                    previous_toky_text = None
                raise errors.InterfaceError(
                    "choice value must precede a comma (,) but found: %s" % _compat.text_repr(previous_toky_text))
            choice = _tools.token_text(toky)
            if not choice:
                raise errors.InterfaceError(
                    "choice field must be allowed to be empty instead of containing an empty choice")
            self.choices.append(choice)
            toky = next(tokens)
            if not _tools.is_eof_token(toky):
                if not _tools.is_comma_token(toky):
                    raise errors.InterfaceError(
                        "comma (,) must follow choice value %s but found: %s"
                        % (_compat.text_repr(choice), _compat.text_repr(toky[1])))
                # Process next choice after comma.
                toky = next(tokens)
                if _tools.is_eof_token(toky):
                    raise errors.InterfaceError("trailing comma (,) must be removed")
        if not self.is_allowed_to_be_empty and not self.choices:
            raise errors.InterfaceError("choice field without any choices must be allowed to be empty")