Exemplo n.º 1
0
    def normalize_table(self, table_data, dup_col_handler=None):
        from tabledata import TableData
        from pathvalidate import replace_symbol, replace_unprintable_char
        from simplesqlite import SQLiteTableDataSanitizer

        if dup_col_handler is None:
            dup_col_handler = DEFAULT_DUP_COL_HANDLER

        normalized_table_data = SQLiteTableDataSanitizer(
            table_data, dup_col_handler=dup_col_handler).normalize()

        if self._symbol_replace_value is None:
            return normalized_table_data

        return TableData(
            normalized_table_data.table_name,
            [
                replace_symbol(
                    replace_unprintable_char(header),
                    self._symbol_replace_value,
                    is_replace_consecutive_chars=True,
                    is_strip=True,
                ) for header in normalized_table_data.headers
            ],
            normalized_table_data.rows,
            dp_extractor=normalized_table_data.dp_extractor,
            type_hints=table_data.dp_extractor.column_type_hints,
        )
Exemplo n.º 2
0
    def normalize_table(self, table_data, dup_col_handler=None):
        from tabledata import TableData
        from pathvalidate import replace_symbol, replace_unprintable_char
        from simplesqlite import SQLiteTableDataSanitizer

        if dup_col_handler is None:
            dup_col_handler = DEFAULT_DUP_COL_HANDLER

        normalized_table_data = SQLiteTableDataSanitizer(
            table_data, dup_col_handler=dup_col_handler, is_type_inference=self._is_type_inference
        ).normalize()

        if self._symbol_replace_value is None:
            return normalized_table_data

        return TableData(
            normalized_table_data.table_name,
            [
                replace_symbol(
                    replace_unprintable_char(header),
                    self._symbol_replace_value,
                    is_replace_consecutive_chars=True,
                    is_strip=True,
                )
                for header in normalized_table_data.headers
            ],
            normalized_table_data.rows,
            dp_extractor=normalized_table_data.dp_extractor,
            type_hints=table_data.dp_extractor.column_type_hints,
        )
Exemplo n.º 3
0
    def _preprocess_table_name(self):
        try:
            new_name = pv.sanitize_filename(self._tabledata.table_name, replacement_text="_")
        except TypeError:
            raise NameValidationError(
                "table name must be a string: actual='{}'".format(self._tabledata.table_name)
            )

        new_name = pv.replace_unprintable_char(new_name, replacement_text="")
        new_name = pv.replace_symbol(new_name, replacement_text="_")
        new_name = new_name.replace(" ", "_")
        new_name = re.sub("_+", "_", new_name)
        new_name = new_name.strip("_")

        return new_name
Exemplo n.º 4
0
    def _preprocess_table_name(self):
        try:
            new_name = pv.sanitize_filename(self._tabledata.table_name,
                                            replacement_text="_")
        except TypeError:
            raise NameValidationError(
                "table name must be a string: actual='{}'".format(
                    self._tabledata.table_name))

        new_name = pv.replace_unprintable_char(new_name, replacement_text="")
        new_name = pv.replace_symbol(new_name, replacement_text="_")
        new_name = new_name.replace(" ", "_")
        new_name = re.sub("_+", "_", new_name)
        new_name = new_name.strip("_")

        return new_name
Exemplo n.º 5
0
    def _preprocess_table_name(self) -> str:
        try:
            new_name = str(
                pv.sanitize_filename(self._tabledata.table_name,
                                     replacement_text="_"))
        except TypeError:
            raise NameValidationError(
                f"table name must be a string: actual='{self._tabledata.table_name}'"
            )

        new_name = pv.replace_unprintable_char(new_name, replacement_text="")
        new_name = pv.replace_symbol(
            new_name,
            replacement_text="_",
            is_replace_consecutive_chars=True,
            is_strip=True,
        )

        return new_name
Exemplo n.º 6
0
    def write_table(self, **kwargs) -> None:
        """
        |write_table| with CSS.
        """

        with self._logger:
            try:
                self._verify_property()
            except EmptyTableDataError:
                self._logger.logger.debug("no tabular data found")
                return

            self._preprocess()

            self.__write_css(
                css_class=replace_symbol(self.table_name,
                                         replacement_text="-"),
                write_style_tag=kwargs.get("write_style_tag", False),
            )
Exemplo n.º 7
0
def make_temp_file_path_from_url(temp_dir_path, url):
    try:
        url_path = urlparse(url).path
    except AttributeError:
        raise InvalidFilePathError("url must be a string")

    if typepy.is_null_string(url_path):
        raise InvalidFilePathError("invalid URL path: {}".format(url_path))

    temp_name = os.path.basename(url_path.rstrip("/"))
    if typepy.is_null_string(temp_name):
        temp_name = pathvalidate.replace_symbol(
            temp_name, replacement_text="_")

    if typepy.is_null_string(temp_name):
        raise InvalidFilePathError("invalid URL: {}".format(url))

    try:
        return posixpath.join(temp_dir_path, temp_name)
    except (TypeError, AttributeError):
        raise InvalidFilePathError("temp_dir_path must be a string")
Exemplo n.º 8
0
def normalize_enum(value,
                   enum_class: Type[Enum],
                   validate: bool = True,
                   default: Optional[Enum] = None):
    if value is None:
        return default

    if isinstance(value, enum_class):
        return value

    try:
        return enum_class[replace_symbol(value.strip(), "_").upper()]
    except AttributeError:
        if validate:
            raise TypeError(
                f"value must be a {enum_class} or a str: actual={type(value)}")
    except KeyError:
        if validate:
            raise ValueError(
                "invalid valid found: expected={}, actual={}".format(
                    "/".join(item.name for item in enum_class), value))

    return value
Exemplo n.º 9
0
 def test_abnormal(self, value, expected):
     with pytest.raises(expected):
         replace_symbol(value)
Exemplo n.º 10
0
 def test_normal_consecutive(self, value, replace_text,
                             is_replace_consecutive_chars, is_strip,
                             expected):
     assert (replace_symbol(value, replace_text,
                            is_replace_consecutive_chars,
                            is_strip) == expected)
Exemplo n.º 11
0
 def test_normal(self, value, replace_text, expected):
     assert replace_symbol(value, replace_text) == expected
Exemplo n.º 12
0
    def write_table(self, **kwargs) -> None:
        """
        |write_table| with HTML table format.

        Args:
            write_css (bool):
                If |True|, write CSS corresponding to the specified styles,
                instead of attributes of HTML tags.

        Example:
            :ref:`example-html-table-writer`

        .. note::
            - |None| values will be replaced with an empty value
        """

        tags, raw = _get_tags_module()
        write_css = kwargs.get("write_css", False)

        with self._logger:
            try:
                self._verify_property()
            except EmptyTableDataError:
                self._logger.logger.debug("no tabular data found")
                return

            self._preprocess()

            css_class = None

            if write_css:
                css_class = kwargs.get("css_class")
                css_class = css_class if css_class else "{}-css".format(
                    self.table_name)
                css_class = replace_symbol(self.table_name,
                                           replacement_text="-")

                css_writer = CssTableWriter()
                css_writer.from_writer(self)
                css_writer.table_name = css_class
                css_writer.write_table(write_style_tag=True)

            if typepy.is_not_null_string(self.table_name):
                if css_class:
                    self._table_tag = tags.table(id=sanitize_python_var_name(
                        self.table_name),
                                                 class_name=css_class)
                else:
                    self._table_tag = tags.table(
                        id=sanitize_python_var_name(self.table_name))
                self._table_tag += tags.caption(
                    MultiByteStrDecoder(self.table_name).unicode_str)
            else:
                self._table_tag = tags.table()

            try:
                self._write_header()
            except ValueError:
                pass

            self._write_body(not write_css)
Exemplo n.º 13
0
def tag_path(tag: str) -> Path:
    tag_dir = replace_symbol(tag)
    path = Path() / cfg.META_PATH / tag_dir
    if not path.exists():
        path.mkdir(parents=True)
    return path
Exemplo n.º 14
0
 def test_abnormal(self, value, expected):
     with pytest.raises(expected):
         replace_symbol(value)
Exemplo n.º 15
0
 def test_normal_consecutive(
     self, value, replace_text, is_replace_consecutive_chars, is_strip, expected
 ):
     assert (
         replace_symbol(value, replace_text, is_replace_consecutive_chars, is_strip) == expected
     )
Exemplo n.º 16
0
 def test_normal(self, value, replace_text, expected):
     assert replace_symbol(value, replace_text) == expected