Exemplo n.º 1
0
    def __init__(self,
                 description,
                 rule,
                 available_field_names,
                 location_of_definition=None):
        r"""
        Create a check.

        :param str description: human readable description of the check
        :param str rule: the check conditions to validate
        :param list available_field_names: the names of the fields available for the check (typically referring \
            to :py:attr:`cutplace.interface.Cid.field_names`)
        :param location_of_definition: location in the CID where the check was declared to be (used by error \
            messages); if ``None``, use ``cutplace.errors.create_caller_location(['checks'])``
        :type location_of_definition: :py:class:`~cutplace.errors.Location` or None
        """
        assert description
        assert rule is not None
        assert available_field_names is not None

        if not available_field_names:
            raise errors.InterfaceError(
                _("field names must be specified before check"),
                location_of_definition)
        self._description = description
        self._rule = rule
        self._field_names = available_field_names
        if location_of_definition is None:
            self._location = errors.create_caller_location(['checks'])
            self._location_of_rule = self._location
        else:
            self._location = copy.copy(location_of_definition)
            self._location.set_cell(1)
            self._location_of_rule = copy.copy(location_of_definition)
            self._location_of_rule.set_cell(3)
Exemplo n.º 2
0
    def __init__(self, description, rule, available_field_names, location_of_definition=None):
        r"""
        Create a check.

        :param str description: human readable description of the check
        :param str rule: the check conditions to validate
        :param list available_field_names: the names of the fields available for the check (typically referring \
            to :py:attr:`cutplace.interface.Cid.field_names`)
        :param location_of_definition: location in the CID where the check was declared to be (used by error \
            messages); if ``None``, use ``cutplace.errors.create_caller_location(['checks'])``
        :type location_of_definition: :py:class:`~cutplace.errors.Location` or None
        """
        assert description
        assert rule is not None
        assert available_field_names is not None

        if not available_field_names:
            raise errors.InterfaceError("field names must be specified before check", location_of_definition)
        self._description = description
        self._rule = rule
        self._field_names = available_field_names
        if location_of_definition is None:
            self._location = errors.create_caller_location(['checks'])
            self._location_of_rule = self._location
        else:
            self._location = copy.copy(location_of_definition)
            self._location.set_cell(1)
            self._location_of_rule = copy.copy(location_of_definition)
            self._location_of_rule.set_cell(3)
Exemplo n.º 3
0
    def set_location_to_caller(self):
        """
        Set the internal :py:attr:`_location` to the caller function. This is
        useful for CID's that are created programmatically to provide at
        least some reference where possible errors in the CID originated from.

        It can be called repeatedly, for instance before each
        :py:meth:`add_check()` and :py:meth:`add_field_format()`.
        """
        self._location = errors.create_caller_location(['interface'], has_cell=True)
Exemplo n.º 4
0
    def set_location_to_caller(self):
        """
        Set the internal :py:attr:`_location` to the caller function. This is
        useful for CID's that are created programmatically to provide at
        least some reference where possible errors in the CID originated from.

        It can be called repeatedly, for instance before each
        :py:meth:`add_check()` and :py:meth:`add_field_format()`.
        """
        self._location = errors.create_caller_location(['interface'], has_cell=True)
Exemplo n.º 5
0
    def __init__(self, format_name, location=None):
        r"""
        Create a new data format.

        :param str format_name: the data format, which must be one of \
            :py:const:`FORMAT_DELIMITED`, :py:const:`FORMAT_EXCEL`,
            :py:const:`FORMAT_FIXED` or :py:const:`FORMAT_ODS`.
        :param cutplace.errors.Location location: location where the data format was declared
        """
        assert format_name == format_name.lower(
        ), 'format_name must be lower case: %r' % format_name

        if format_name not in (_VALID_FORMATS + ['csv']):
            raise errors.InterfaceError(
                _('format is %s but must be on of: %s') %
                (format_name, _VALID_FORMATS), location if location is not None
                else errors.create_caller_location(['data']))
        # HACK: Treat ``format_name`` 'csv' as synonym for ``FORMAT_DELIMITED``.
        self._format = format_name if format_name != 'csv' else FORMAT_DELIMITED
        self._header = 0
        self._validate_header_row_against_field_names = False
        self._is_valid = False
        self._allowed_characters = None
        self._encoding = 'cp1252'
        self._quoting = csv.QUOTE_MINIMAL
        self._strict_field_names = True
        if self.format == FORMAT_DELIMITED:
            self._escape_character = '"'
            self._item_delimiter = ','
            self._quote_character = '"'
            self._skip_initial_space = False
        if self.format in (FORMAT_DELIMITED, FORMAT_FIXED):
            self._decimal_separator = '.'
            self._line_delimiter = ANY
            self._thousands_separator = ''
        elif self.format in (FORMAT_EXCEL, FORMAT_ODS):
            self._sheet = 1
        if self.format in (FORMAT_DELIMITED, FORMAT_FIXED):
            # Valid values for property 'line delimiter', which is only available for delimited and fixed data
            # with no line delimiter only allowed for fixed data.
            self._VALID_LINE_DELIMITER_TEXTS = sorted([
                line_delimiter_text for line_delimiter, line_delimiter_text in
                LINE_DELIMITER_TO_TEXT_MAP.items()
                if (line_delimiter is not None) or (
                    self.format == FORMAT_FIXED)
            ])
Exemplo n.º 6
0
    def __init__(self, format_name, location=None):
        r"""
        Create a new data format.

        :param str format_name: the data format, which must be one of \
            :py:const:`FORMAT_DELIMITED`, :py:const:`FORMAT_EXCEL`,
            :py:const:`FORMAT_FIXED` or :py:const:`FORMAT_ODS`.
        :param cutplace.errors.Location location: location where the data format was declared
        """
        assert format_name == format_name.lower(), 'format_name must be lower case: %r' % format_name

        if format_name not in (_VALID_FORMATS + ['csv']):
            raise errors.InterfaceError(
                'format is %s but must be on of: %s' % (format_name, _VALID_FORMATS),
                location if location is not None else errors.create_caller_location(['data']))
        # HACK: Treat ``format_name`` 'csv' as synonym for ``FORMAT_DELIMITED``.
        self._format = format_name if format_name != 'csv' else FORMAT_DELIMITED
        self._header = 0
        self._is_valid = False
        self._allowed_characters = None
        self._encoding = 'cp1252'
        if self.format == FORMAT_DELIMITED:
            self._escape_character = '"'
            self._item_delimiter = ','
            self._quote_character = '"'
            self._skip_initial_space = False
        if self.format in (FORMAT_DELIMITED, FORMAT_FIXED):
            self._decimal_separator = '.'
            self._line_delimiter = ANY
            self._thousands_separator = ''
        elif self.format in (FORMAT_EXCEL, FORMAT_ODS):
            self._sheet = 1
        if self.format in (FORMAT_DELIMITED, FORMAT_FIXED):
            # Valid values for property 'line delimiter', which is only available for delimited and fixed data
            # with no line delimiter only allowed for fixed data.
            self._VALID_LINE_DELIMITER_TEXTS = sorted([
                line_delimiter_text
                for line_delimiter, line_delimiter_text in LINE_DELIMITER_TO_TEXT_MAP.items()
                if (line_delimiter is not None) or (self.format == FORMAT_FIXED)
            ])
Exemplo n.º 7
0
 def _location(self):
     return errors.create_caller_location(["test_data"])
Exemplo n.º 8
0
 def _location(self):
     return errors.create_caller_location(['test_data'])
Exemplo n.º 9
0
 def test_can_create_caller_location(self):
     location = errors.create_caller_location()
     dev_test.assert_fnmatches(self, str(location),
                               'test_errors.py ([1-9]*)')
Exemplo n.º 10
0
 def test_can_create_caller_location(self):
     location = errors.create_caller_location()
     dev_test.assert_fnmatches(self, str(location), "test_errors.py ([1-9]*)")