[ramdisk] add cvitek pre-built ramdisk

Change-Id: Ic7d2046a23358129eaf621b5558984a64fa7361d
This commit is contained in:
sam.xiang
2023-02-23 09:56:47 +08:00
parent 4f810186ab
commit 1cf39ecdd5
12481 changed files with 1478086 additions and 0 deletions

View File

@ -0,0 +1,49 @@
# encoding: utf-8
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""
from __future__ import absolute_import
from ._app import (
validate_excel_sheet_name,
sanitize_excel_sheet_name
)
from ._error import (
NullNameError,
InvalidNameError,
InvalidCharError,
InvalidCharWindowsError,
InvalidLengthError,
ReservedNameError,
ValidReservedNameError,
InvalidReservedNameError,
)
from ._file import (
validate_filename,
validate_file_path,
sanitize_filename,
sanitize_file_path,
)
from ._ltsv import (
validate_ltsv_label,
sanitize_ltsv_label,
)
from ._sqlite import (
validate_sqlite_table_name,
validate_sqlite_attr_name,
)
from ._symbol import (
validate_symbol,
replace_symbol,
)
from .variable import (
validate_js_var_name,
sanitize_js_var_name,
validate_python_var_name,
sanitize_python_var_name,
ElasticsearchIndexNameSanitizer,
JavaScriptVarNameSanitizer,
PythonVarNameSanitizer,
)

View File

@ -0,0 +1,80 @@
# encoding: utf-8
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""
from __future__ import absolute_import
from __future__ import unicode_literals
import re
from ._common import (
_validate_null_string,
_preprocess,
)
from ._error import (
InvalidCharError,
InvalidLengthError
)
__MAX_SHEET_NAME_LEN = 31
__INVALID_EXCEL_CHARS = "[]:*?/\\"
__RE_INVALID_EXCEL_SHEET_NAME = re.compile(
"[{:s}]".format(re.escape(__INVALID_EXCEL_CHARS)), re.UNICODE)
def validate_excel_sheet_name(sheet_name):
"""
:param str sheet_name: Excel sheet name to validate.
:raises pathvalidate.NullNameError: If the ``sheet_name`` is empty.
:raises pathvalidate.InvalidCharError:
If the ``sheet_name`` includes invalid char(s):
|invalid_excel_sheet_chars|.
:raises pathvalidate.InvalidLengthError:
If the ``sheet_name`` is longer than 31 characters.
"""
_validate_null_string(sheet_name)
if len(sheet_name) > __MAX_SHEET_NAME_LEN:
raise InvalidLengthError(
"sheet name is too long: expected<={:d}, actual={:d}".format(
__MAX_SHEET_NAME_LEN, len(sheet_name)))
unicode_sheet_name = _preprocess(sheet_name)
match = __RE_INVALID_EXCEL_SHEET_NAME.search(unicode_sheet_name)
if match is not None:
raise InvalidCharError(
"invalid char found in the sheet name: '{:s}'".format(
re.escape(match.group())))
def sanitize_excel_sheet_name(sheet_name, replacement_text=""):
"""
Replace invalid characters for a Excel sheet name within the ``sheet_name``
with the ``replacement_text``. Invalid characters are as follows:
|invalid_excel_sheet_chars|.
The ``sheet_name`` will truncated to 31 characters
(max sheet name length of Excel) from the head, if the length
of the name is exceed 31 characters.
:param str sheet_name: Excel sheet name to sanitize.
:param str replacement_text: Replacement text.
:return: A replacement string.
:rtype: str
:raises ValueError: If the ``sheet_name`` is a invalid sheet name.
"""
try:
unicode_sheet_name = _preprocess(sheet_name)
except AttributeError as e:
raise ValueError(e)
modify_sheet_name = __RE_INVALID_EXCEL_SHEET_NAME.sub(
replacement_text, unicode_sheet_name)
return modify_sheet_name[:__MAX_SHEET_NAME_LEN]

View File

@ -0,0 +1,41 @@
# encoding: utf-8
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""
from __future__ import absolute_import
from __future__ import unicode_literals
from ._error import NullNameError
def _validate_null_string(text, error_msg="null name"):
if is_not_null_string(text):
return
if is_null_string(text):
raise NullNameError(error_msg)
raise TypeError("text must be a string")
def _preprocess(name):
return name.strip()
def is_null_string(value):
if value is None:
return True
try:
return len(value.strip()) == 0
except AttributeError:
return False
def is_not_null_string(value):
try:
return len(value.strip()) > 0
except AttributeError:
return False

View File

@ -0,0 +1,59 @@
# encoding: utf-8
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""
from __future__ import absolute_import
from __future__ import unicode_literals
class InvalidNameError(ValueError):
"""
Base exception class that indicates invalid name errors.
"""
class NullNameError(InvalidNameError):
"""
Exception raised when a name is empty.
"""
class InvalidCharError(InvalidNameError):
"""
Exception raised when includes invalid character(s) within a string.
"""
class InvalidCharWindowsError(InvalidCharError):
"""
Exception raised when includes Windows specific invalid character(s)
within a string.
"""
class InvalidLengthError(InvalidNameError):
"""
Exception raised when a string too long/short.
"""
class ReservedNameError(InvalidNameError):
"""
Exception raised when a string is matched a reserved name.
"""
class ValidReservedNameError(ReservedNameError):
"""
Exception raised when a string is matched a reserved name.
However, it can be used as a name.
"""
class InvalidReservedNameError(ReservedNameError):
"""
Exception raised when a string is matched a reserved name.
And the reserved name is invalid as a name.
"""

View File

@ -0,0 +1,347 @@
# encoding: utf-8
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""
from __future__ import absolute_import
from __future__ import unicode_literals
import itertools
import os.path
import platform
import re
from ._common import _preprocess
from ._error import (
InvalidCharError,
InvalidCharWindowsError,
InvalidLengthError,
InvalidReservedNameError
)
from ._interface import NameSanitizer
_DEFAULT_MAX_FILENAME_LEN = 255
class FileSanitizer(NameSanitizer):
_VALID_WIN_PLATFORM_NAME_LIST = ["windows", "win"]
_INVALID_PATH_CHARS = "\0"
_INVALID_FILENAME_CHARS = _INVALID_PATH_CHARS + "/"
_INVALID_WIN_PATH_CHARS = _INVALID_PATH_CHARS + ':*?"<>|'
_INVALID_WIN_FILENAME_CHARS = (
_INVALID_FILENAME_CHARS +
_INVALID_WIN_PATH_CHARS +
"\\"
)
_ERROR_MSG_TEMPLATE = "invalid char found : invalid-char='{}', value='{}'"
@property
def platform_name(self):
return self.__platform_name
@property
def max_len(self):
return self._max_len
def __init__(self, filename, max_len, platform_name=None):
super(FileSanitizer, self).__init__(filename)
self._max_len = max_len
if platform_name is None:
platform_name = platform.system()
self.__platform_name = platform_name.lower()
class FileNameSanitizer(FileSanitizer):
__WINDOWS_RESERVED_FILE_NAME_LIST = [
"CON", "PRN", "AUX", "NUL",
] + [
"{:s}{:d}".format(name, num)
for name, num in itertools.product(["COM", "LPT"], range(1, 10))
]
__RE_INVALID_FILENAME = re.compile("[{:s}]".format(
re.escape(FileSanitizer._INVALID_FILENAME_CHARS)), re.UNICODE)
__RE_INVALID_WIN_FILENAME = re.compile("[{:s}]".format(
re.escape(FileSanitizer._INVALID_WIN_FILENAME_CHARS)), re.UNICODE)
@property
def reserved_keywords(self):
return self.__WINDOWS_RESERVED_FILE_NAME_LIST
def __init__(
self, filename, max_filename_len=_DEFAULT_MAX_FILENAME_LEN,
platform_name=None
):
super(FileNameSanitizer, self).__init__(
filename, max_len=max_filename_len, platform_name=platform_name)
def validate(self):
self._validate(self._value)
def sanitize(self, replacement_text=""):
sanitize_file_name = self.__RE_INVALID_WIN_FILENAME.sub(
replacement_text, self._str)
sanitize_file_name = sanitize_file_name[:self.max_len]
try:
self._validate(sanitize_file_name)
except InvalidReservedNameError:
sanitize_file_name += "_"
return sanitize_file_name
def _validate(self, value):
self._validate_null_string(value)
if len(value) > self.max_len:
raise InvalidLengthError(
"filename is too long: expected<={:d}, actual={:d}".format(
self.max_len, len(value)))
error_message_template = "invalid char found in the filename: '{:s}'"
unicode_filename = _preprocess(value)
if self.platform_name in self._VALID_WIN_PLATFORM_NAME_LIST:
self.__validate_win_filename(unicode_filename)
else:
match = self.__RE_INVALID_FILENAME.search(unicode_filename)
if match is not None:
raise InvalidCharError(
error_message_template.format(re.escape(match.group())))
def __validate_win_filename(self, unicode_filename):
match = self.__RE_INVALID_WIN_FILENAME.search(unicode_filename)
if match is not None:
raise InvalidCharWindowsError(self._ERROR_MSG_TEMPLATE.format(
unicode_filename, re.escape(match.group())))
if self._is_reserved_keyword(unicode_filename.upper()):
raise InvalidReservedNameError(
"{} is a reserved name by Windows".format(unicode_filename))
class FilePathSanitizer(FileSanitizer):
__RE_INVALID_PATH = re.compile("[{:s}]".format(
re.escape(FileSanitizer._INVALID_PATH_CHARS)), re.UNICODE)
__RE_INVALID_WIN_PATH = re.compile("[{:s}]".format(
re.escape(FileSanitizer._INVALID_WIN_PATH_CHARS)), re.UNICODE)
@property
def reserved_keywords(self):
return []
def __init__(
self, filename, platform_name=None, max_path_len=None):
super(FilePathSanitizer, self).__init__(
filename, max_len=max_path_len, platform_name=platform_name)
if self.max_len is None:
self._max_len = self.__get_default_max_path_len()
def validate(self):
self._validate(self._value)
def sanitize(self, replacement_text=""):
try:
unicode_file_path = _preprocess(self._value)
except AttributeError as e:
raise ValueError(e)
return self.__RE_INVALID_WIN_PATH.sub(
replacement_text, unicode_file_path)
def _validate(self, value):
self._validate_null_string(value)
file_path = os.path.normpath(os.path.splitdrive(value)[1])
unicode_file_path = _preprocess(file_path)
if self.platform_name in self._VALID_WIN_PLATFORM_NAME_LIST:
self.__validate_win_file_path(unicode_file_path)
else:
match = self.__RE_INVALID_PATH.search(unicode_file_path)
if match is not None:
raise InvalidCharError(self._ERROR_MSG_TEMPLATE.format(
re.escape(match.group()), unicode_file_path))
if len(unicode_file_path) > self.max_len:
raise InvalidLengthError(
"file path is too long: expected<={:d}, actual={:d}".format(
self.max_len, len(unicode_file_path)))
def __get_default_max_path_len(self):
if self.platform_name == "linux":
return 4096
if platform.system() == "windows":
return 260
if platform.system() == "mac":
return 1024
return 260
def __validate_win_file_path(self, unicode_file_path):
match = self.__RE_INVALID_WIN_PATH.search(unicode_file_path)
if match is not None:
raise InvalidCharWindowsError(self._ERROR_MSG_TEMPLATE.format(
re.escape(match.group()), unicode_file_path))
def validate_filename(
filename, platform_name=None,
max_filename_len=_DEFAULT_MAX_FILENAME_LEN):
"""
Verifying whether the ``filename`` is a valid file name or not.
:param str filename: Filename to validate.
:param str platform_name: |platform_name|
:param int max_filename_len:
Upper limit of the ``filename`` length. Defaults to 255.
:raises pathvalidate.NullNameError: If the ``filename`` is empty.
:raises pathvalidate.InvalidLengthError:
If the ``filename`` is longer than ``max_filename_len`` characters.
:raises pathvalidate.InvalidCharError:
If the ``filename`` includes invalid character(s) for a filename:
|invalid_filename_chars|.
:raises pathvalidate.InvalidCharWindowsError:
If the ``filename`` includes invalid character(s) for a Windows
filename: |invalid_win_filename_chars|.
:raises pathvalidate.InvalidReservedNameError:
If the ``filename`` equals reserved name by OS.
Windows reserved name is as follows:
``"CON"``, ``"PRN"``, ``"AUX"``, ``"NUL"``,
``"COM[1-9]"``, ``"LPT[1-9]"``
:Examples:
:ref:`example-validate-filename`
.. seealso::
`Naming Files, Paths, and Namespaces (Windows)
<https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx>`__
"""
FileNameSanitizer(
filename,
platform_name=platform_name,
max_filename_len=max_filename_len).validate()
def validate_file_path(file_path, platform_name=None, max_path_len=None):
"""
Verifying whether the ``file_path`` is a valid file path or not.
:param str file_path: File path to validate.
:param str platform_name: |platform_name|
:param int max_filename_len:
Upper limit of the ``file_path`` length. If the value is |None|,
the default value will be automatically determined by the execution
environment: **(1)** 4096 (``Linux``) **(2)** 260 (``Windows``).
:raises pathvalidate.NullNameError: If the ``file_path`` is empty.
:raises pathvalidate.InvalidCharError:
If the ``file_path`` includes invalid char(s):
|invalid_file_path_chars|.
:raises pathvalidate.InvalidCharWindowsError:
If the ``file_path`` includes invalid character(s) for a Windows
file path: |invalid_win_file_path_chars|
:raises pathvalidate.InvalidLengthError:
If the ``file_path`` is longer than 1024 characters.
:Examples:
:ref:`example-validate-file-path`
.. seealso::
`Naming Files, Paths, and Namespaces (Windows)
<https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx>`__
"""
FilePathSanitizer(
file_path,
platform_name=platform_name,
max_path_len=max_path_len).validate()
def sanitize_filename(
filename, replacement_text="", platform_name=None,
max_filename_len=_DEFAULT_MAX_FILENAME_LEN):
"""
Make a valid filename for both Windows and Linux.
To make a valid filename:
- Replace invalid characters for a filename within the ``filename``
with the ``replacement_text``. Invalid characters are as follows:
|invalid_filename_chars|, |invalid_win_filename_chars|.
- Append under bar (``"_"``) at the tail of the name if sanitized name
is one of the reserved names by the OS.
:param str filename: Filename to sanitize.
:param str replacement_text: Replacement text.
:param str platform_name: |platform_name|
:param int max_filename_len:
Upper limit of the ``filename`` length. Truncate the name length if
the ``filename`` length exceed this value.
Defaults to 255.
:return: A replacement string.
:rtype: str
:raises ValueError: If the ``filename`` is a invalid filename.
:Examples:
:ref:`example-sanitize-filename`
.. note::
Reserved names by OS will not be replaced.
"""
return FileNameSanitizer(
filename,
platform_name=platform_name,
max_filename_len=max_filename_len).sanitize(replacement_text)
def sanitize_file_path(
file_path, replacement_text="", platform_name=None, max_path_len=None):
"""
Make a valid file path for both Windows and Linux.
Replace invalid characters for a file path within the ``file_path``
with the ``replacement_text``. Invalid characters are as follows:
|invalid_file_path_chars|, |invalid_win_file_path_chars|.
:param str file_path: File path to sanitize.
:param str replacement_text: Replacement text.
:param str platform_name: |platform_name|
:param int max_path_len:
Upper limit of the ``file_path`` length. Truncate the name length if
the ``file_path`` length exceed this value.
If the value is |None|, the default value will be automatically
determined by the execution environment:
**(1)** 4096 (``Linux``) **(2)** 260 (``Windows``).
:return: A replacement string.
:rtype: str
:raises ValueError: If the ``file_path`` is a invalid file path.
:Examples:
:ref:`example-sanitize-file-path`
"""
return FilePathSanitizer(
file_path,
platform_name=platform_name,
max_path_len=max_path_len).sanitize(replacement_text)

View File

@ -0,0 +1,45 @@
# encoding: utf-8
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""
from __future__ import absolute_import
from __future__ import unicode_literals
import abc
from ._common import _validate_null_string
from ._six import add_metaclass
@add_metaclass(abc.ABCMeta)
class NameSanitizer(object):
@abc.abstractproperty
def reserved_keywords(self): # pragma: no cover
pass
@abc.abstractmethod
def validate(self): # pragma: no cover
pass
@abc.abstractmethod
def sanitize(self, replacement_text=""): # pragma: no cover
pass
@property
def _str(self):
return self._value
def __init__(self, value):
self._validate_null_string(value)
self._value = value.strip()
def _is_reserved_keyword(self, value):
return value in self.reserved_keywords
@staticmethod
def _validate_null_string(text, error_msg="null name"):
_validate_null_string(text)

View File

@ -0,0 +1,55 @@
# encoding: utf-8
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""
from __future__ import absolute_import
from __future__ import unicode_literals
import re
from ._common import (
_validate_null_string,
_preprocess,
)
from ._error import InvalidCharError
__RE_INVALID_LTSV_LABEL = re.compile("[^0-9A-Za-z_.-]", re.UNICODE)
def validate_ltsv_label(label):
"""
Verifying whether ``label`` is a valid
`Labeled Tab-separated Values (LTSV) <http://ltsv.org/>`__ label or not.
:param str label: Label to validate.
:raises pathvalidate.NullNameError: If the ``label`` is empty.
:raises pathvalidate.InvalidCharError:
If invalid character(s) found in the ``label`` for a LTSV format label.
"""
_validate_null_string(label, error_msg="label is empty")
match_list = __RE_INVALID_LTSV_LABEL.findall(_preprocess(label))
if match_list:
raise InvalidCharError(
"invalid character found for a LTSV format label: {}".format(
match_list))
def sanitize_ltsv_label(label, replacement_text=""):
"""
Replace all of the symbols in text.
:param str label: Input text.
:param str replacement_text: Replacement text.
:return: A replacement string.
:rtype: str
"""
_validate_null_string(label, error_msg="label is empty")
return __RE_INVALID_LTSV_LABEL.sub(
replacement_text, _preprocess(label))

View File

@ -0,0 +1,22 @@
"""
Code from six:
https://github.com/benjaminp/six/blob/master/LICENSE
"""
from __future__ import absolute_import
def add_metaclass(metaclass):
"""Class decorator for creating a class with a metaclass."""
def wrapper(cls):
orig_vars = cls.__dict__.copy()
slots = orig_vars.get('__slots__')
if slots is not None:
if isinstance(slots, str):
slots = [slots]
for slots_var in slots:
orig_vars.pop(slots_var)
orig_vars.pop('__dict__', None)
orig_vars.pop('__weakref__', None)
return metaclass(cls.__name__, cls.__bases__, orig_vars)
return wrapper

View File

@ -0,0 +1,168 @@
# encoding: utf-8
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""
from __future__ import absolute_import
from __future__ import unicode_literals
import re
from ._common import (
_validate_null_string,
_preprocess,
)
from ._error import (
InvalidCharError,
ValidReservedNameError,
InvalidReservedNameError
)
__SQLITE_VALID_RESERVED_KEYWORDS = [
'ABORT', 'ACTION', 'AFTER', 'ANALYZE', 'ASC', 'ATTACH',
'BEFORE', 'BEGIN', 'BY',
'CASCADE', 'CAST', 'COLUMN', 'CONFLICT', 'CROSS', 'CURRENT_DATE',
'CURRENT_TIME', 'CURRENT_TIMESTAMP',
'DATABASE', 'DEFERRED', 'DESC', 'DETACH',
'EACH', 'END', 'EXCLUSIVE', 'EXPLAIN',
'FAIL', 'FOR', 'FULL', 'GLOB',
'IGNORE', 'IMMEDIATE', 'INDEXED', 'INITIALLY', 'INNER', 'INSTEAD',
'KEY',
'LEFT', 'LIKE',
'MATCH',
'NATURAL',
'NO',
'OF', 'OFFSET', 'OUTER',
'PLAN', 'PRAGMA',
'QUERY',
'RAISE', 'RECURSIVE', 'REGEXP', 'REINDEX', 'RELEASE', 'RENAME', 'REPLACE',
'RESTRICT', 'RIGHT', 'ROLLBACK', 'ROW',
'SAVEPOINT',
'TEMP', 'TEMPORARY', 'TRIGGER',
'VACUUM', 'VIEW', 'VIRTUAL',
'WITH', 'WITHOUT',
]
__SQLITE_INVALID_RESERVED_KEYWORDS = [
'ADD', 'ALL', 'ALTER', 'AND', 'AS', 'AUTOINCREMENT',
'BETWEEN',
'CASE', 'CHECK', 'COLLATE', 'COMMIT', 'CONSTRAINT', 'CREATE',
'DEFAULT', 'DEFERRABLE', 'DELETE', 'DISTINCT', 'DROP',
'ELSE', 'ESCAPE', 'EXCEPT', 'EXISTS', 'FOREIGN',
'FROM',
'GROUP',
'HAVING',
'IN', 'INDEX', 'INSERT', 'INTERSECT', 'INTO', 'IS', 'ISNULL',
'JOIN',
'LIMIT',
'NOT', 'NOTNULL', 'NULL',
'ON', 'OR', 'ORDER',
'PRIMARY',
'REFERENCES',
'SELECT', 'SET',
'TABLE', 'THEN', 'TO', 'TRANSACTION',
'UNION', 'UNIQUE', 'UPDATE', 'USING',
'VALUES',
'WHEN', 'WHERE',
]
__SQLITE_VALID_RESERVED_KEYWORDS_TABLE = (
__SQLITE_VALID_RESERVED_KEYWORDS)
__SQLITE_INVALID_RESERVED_KEYWORDS_TABLE = (
__SQLITE_INVALID_RESERVED_KEYWORDS + ['IF'])
__SQLITE_VALID_RESERVED_KEYWORDS_ATTR = (
__SQLITE_VALID_RESERVED_KEYWORDS + ['IF'])
__SQLITE_INVALID_RESERVED_KEYWORDS_ATTR = (
__SQLITE_INVALID_RESERVED_KEYWORDS)
__RE_INVALID_SQLITE_NAME_HEAD = re.compile("^[^a-zA-Z]+", re.UNICODE)
def validate_sqlite_table_name(name):
"""
:param str name: Name to validate.
:raises pathvalidate.NullNameError: If the ``name`` is empty.
:raises pathvalidate.InvalidCharError:
If the first character of the ``name`` is invalid
(not an alphabetic character).
:raises pathvalidate.InvalidReservedNameError:
|raises_sqlite_keywords|
And invalid as a table name.
:raises pathvalidate.ValidReservedNameError:
|raises_sqlite_keywords|
However, valid as a table name.
"""
_validate_null_string(name)
if name.upper() in __SQLITE_INVALID_RESERVED_KEYWORDS_TABLE:
raise InvalidReservedNameError(
"'{:s}' is a reserved keyword by sqlite".format(name))
if name.upper() in __SQLITE_VALID_RESERVED_KEYWORDS_TABLE:
raise ValidReservedNameError(
"'{:s}' is a reserved keyword by sqlite".format(name))
match = __RE_INVALID_SQLITE_NAME_HEAD.search(_preprocess(name))
if match is not None:
name = match.group()
try:
name.encode("ascii")
except UnicodeEncodeError:
try:
name.encode("utf8")
except Exception:
raise
else:
return
raise InvalidCharError(
"the first character of the sqlite name is invalid: '{}'".format(
re.escape(name)))
def validate_sqlite_attr_name(name):
"""
:param str name: Name to validate.
:raises pathvalidate.NullNameError: If the ``name`` is empty.
:raises pathvalidate.InvalidCharError:
If the first character of the ``name`` is invalid
(not an alphabetic character).
:raises pathvalidate.InvalidReservedNameError:
|raises_sqlite_keywords|
And invalid as a attribute name.
:raises pathvalidate.ValidReservedNameError:
|raises_sqlite_keywords|
However, valid as a attribute name.
"""
_validate_null_string(name)
if name.upper() in __SQLITE_INVALID_RESERVED_KEYWORDS_ATTR:
raise InvalidReservedNameError(
"'{}' is a reserved keyword by sqlite".format(name))
if name.upper() in __SQLITE_VALID_RESERVED_KEYWORDS_ATTR:
raise ValidReservedNameError(
"'{}' is a reserved keyword by sqlite".format(name))
match = __RE_INVALID_SQLITE_NAME_HEAD.search(_preprocess(name))
if match is not None:
name = match.group()
try:
name.encode("ascii")
except UnicodeEncodeError:
try:
name.encode("utf8")
except Exception:
raise
else:
return
raise InvalidCharError(
"the first character of the sqlite name is invalid: '{}'".format(
re.escape(match.group())))

View File

@ -0,0 +1,55 @@
# encoding: utf-8
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""
from __future__ import absolute_import
from __future__ import unicode_literals
import re
from ._common import (
_preprocess,
is_not_null_string,
)
from ._error import InvalidCharError
__RE_SYMBOL = re.compile(
"[\0\"\s" + re.escape("\\/:*?<>|!#$&\'=~^@`[]+-;{},.()%_") + "]",
re.UNICODE)
def validate_symbol(text):
"""
Verifying whether symbol(s) included in the ``text`` or not.
:param str text: Input text.
:raises pathvalidate.InvalidCharError:
If symbol(s) included in the ``text``.
"""
match_list = __RE_SYMBOL.findall(_preprocess(text))
if match_list:
raise InvalidCharError("invalid symbols found: {}".format(match_list))
def replace_symbol(text, replacement_text=""):
"""
Replace all of the symbols in the ``text``.
:param str text: Input text.
:param str replacement_text: Replacement text.
:return: A replacement string.
:rtype: str
:Examples:
:ref:`example-sanitize-symbol`
"""
if not is_not_null_string(text):
raise TypeError("text must be a string")
return __RE_SYMBOL.sub(replacement_text, _preprocess(text))

View File

@ -0,0 +1,19 @@
# encoding: utf-8
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""
from __future__ import absolute_import
from ._elasticsearch import ElasticsearchIndexNameSanitizer
from ._javascript import (
validate_js_var_name,
sanitize_js_var_name,
JavaScriptVarNameSanitizer,
)
from ._python import (
validate_python_var_name,
sanitize_python_var_name,
PythonVarNameSanitizer,
)

View File

@ -0,0 +1,88 @@
# encoding: utf-8
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""
from __future__ import absolute_import
from __future__ import unicode_literals
import abc
import re
from .._common import (
_preprocess,
is_null_string,
)
from .._error import (
InvalidCharError,
InvalidReservedNameError,
NullNameError
)
from .._interface import NameSanitizer
class VarNameSanitizer(NameSanitizer):
@abc.abstractproperty
def _invalid_var_name_head_re(self): # pragma: no cover
pass
@abc.abstractproperty
def _invalid_var_name_re(self): # pragma: no cover
pass
def validate(self):
self._validate(self._value)
def sanitize(self, replacement_text=""):
sanitize_var_name = self._invalid_var_name_re.sub(
replacement_text, self._str)
# delete invalid char(s) in the beginning of the variable name
is_require_remove_head = any([
is_null_string(replacement_text),
self._invalid_var_name_head_re.search(
replacement_text) is not None,
])
if is_require_remove_head:
sanitize_var_name = self._invalid_var_name_head_re.sub(
"", sanitize_var_name)
else:
match = self._invalid_var_name_head_re.search(sanitize_var_name)
if match is not None:
sanitize_var_name = (
match.end() * replacement_text +
self._invalid_var_name_head_re.sub("", sanitize_var_name)
)
try:
self._validate(sanitize_var_name)
except InvalidReservedNameError:
sanitize_var_name += "_"
except NullNameError:
pass
return sanitize_var_name
def _validate(self, value):
self._validate_null_string(value)
unicode_var_name = _preprocess(value)
if self._is_reserved_keyword(unicode_var_name):
raise InvalidReservedNameError(
"{:s} is a reserved keyword by pyhon".format(unicode_var_name))
match = self._invalid_var_name_re.search(unicode_var_name)
if match is not None:
raise InvalidCharError(
"invalid char found in the variable name: '{}'".format(
re.escape(match.group())))
match = self._invalid_var_name_head_re.search(unicode_var_name)
if match is not None:
raise InvalidCharError(
"the first character of the variable name is invalid: '{}'".format(
re.escape(match.group())))

View File

@ -0,0 +1,31 @@
# encoding: utf-8
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""
from __future__ import absolute_import
from __future__ import unicode_literals
import re
from ._base import VarNameSanitizer
class ElasticsearchIndexNameSanitizer(VarNameSanitizer):
__RE_INVALID_INDEX_NAME = re.compile(
"[" + re.escape("\\/*?\"<>|,\"") + "\s]+")
__RE_INVALID_INDEX_NAME_HEAD = re.compile("^[_]+")
@property
def reserved_keywords(self):
return []
@property
def _invalid_var_name_head_re(self):
return self.__RE_INVALID_INDEX_NAME_HEAD
@property
def _invalid_var_name_re(self):
return self.__RE_INVALID_INDEX_NAME

View File

@ -0,0 +1,112 @@
# encoding: utf-8
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""
from __future__ import absolute_import
from __future__ import unicode_literals
import re
from ._base import VarNameSanitizer
class JavaScriptVarNameSanitizer(VarNameSanitizer):
__JS_RESERVED_KEYWORDS_ES6 = [
"break",
"case", "catch", "class", "const", "continue",
"debugger", "default", "delete", "do",
"else", "export", "extends",
"finally", "for", "function",
"if", "import", "in", "instanceof",
"new", "return",
"super", "switch",
"this", "throw", "try", "typeof",
"var", "void",
"while", "with",
"yield",
]
__JS_RESERVED_KEYWORDS_FUTURE = [
"enum",
"implements", "interface", "let", "package", "private", "protected", "public", "static",
"await",
"abstract", "boolean", "byte", "char", "double", "final", "float",
"goto", "int", "long", "native", "short", "synchronized", "throws",
"transient", "volatile"
]
__JS_BUILTIN_CONSTANTS = [
"null", "true", "false",
]
__RE_INVALID_VAR_NAME = re.compile("[^a-zA-Z0-9_$]")
__RE_INVALID_VAR_NAME_HEAD = re.compile("^[^a-zA-Z$]+")
@property
def reserved_keywords(self):
return (
self.__JS_RESERVED_KEYWORDS_ES6 +
self.__JS_RESERVED_KEYWORDS_FUTURE +
self.__JS_BUILTIN_CONSTANTS
)
@property
def _invalid_var_name_head_re(self):
return self.__RE_INVALID_VAR_NAME_HEAD
@property
def _invalid_var_name_re(self):
return self.__RE_INVALID_VAR_NAME
def validate_js_var_name(var_name):
"""
:param str var_name: Name to validate.
:raises pathvalidate.NullNameError: If the ``var_name`` is empty.
:raises pathvalidate.InvalidCharError:
If the ``var_name`` is invalid as a JavaScript identifier.
:raises pathvalidate.InvalidReservedNameError:
If the ``var_name`` is equals to
`JavaScript reserved keywords
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords>`__.
.. note::
Currently, not supported unicode variable names.
"""
JavaScriptVarNameSanitizer(var_name).validate()
def sanitize_js_var_name(var_name, replacement_text=""):
"""
Make a valid JavaScript variable name from ``var_name``.
To make a valid name:
- Replace invalid characters for a JavaScript variable name within
the ``var_name`` with the ``replacement_text``
- Delete invalid chars for the beginning of the variable name
- Append under bar (``"_"``) at the tail of the name if sanitized name
is one of the JavaScript reserved names
:JavaScriptstr filename: Name to sanitize.
:param str replacement_text: Replacement text.
:return: A replacement string.
:rtype: str
:raises ValueError: If ``var_name`` or ``replacement_text`` is invalid.
:Examples:
:ref:`example-sanitize-var-name`
.. note::
Currently, not supported unicode variable names.
.. seealso::
:py:func:`.validate_js_var_name`
"""
return JavaScriptVarNameSanitizer(var_name).sanitize(replacement_text)

View File

@ -0,0 +1,96 @@
# encoding: utf-8
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""
from __future__ import absolute_import
from __future__ import unicode_literals
import re
from ._base import VarNameSanitizer
class PythonVarNameSanitizer(VarNameSanitizer):
__PYTHON_RESERVED_KEYWORDS = [
"and", "del", "from", "not", "while",
"as", "elif", "global", "or", "with",
"assert", "else", "if", "pass", "yield",
"break", "except", "import", "print",
"class", "exec", "in", "raise",
"continue", "finally", "is", "return",
"def", "for", "lambda", "try",
]
__PYTHON_BUILTIN_CONSTANTS = [
"False", "True", "None", "NotImplemented", "Ellipsis", "__debug__",
]
__RE_INVALID_VAR_NAME = re.compile("[^a-zA-Z0-9_]")
__RE_INVALID_VAR_NAME_HEAD = re.compile("^[^a-zA-Z]+")
@property
def reserved_keywords(self):
return (
self.__PYTHON_RESERVED_KEYWORDS + self.__PYTHON_BUILTIN_CONSTANTS)
@property
def _invalid_var_name_head_re(self):
return self.__RE_INVALID_VAR_NAME_HEAD
@property
def _invalid_var_name_re(self):
return self.__RE_INVALID_VAR_NAME
def validate_python_var_name(var_name):
"""
:param str var_name: Name to validate.
:raises pathvalidate.NullNameError: If the ``var_name`` is empty.
:raises pathvalidate.InvalidCharError: If the ``var_name`` is invalid as
`Python identifier
<https://docs.python.org/3/reference/lexical_analysis.html#identifiers>`__.
:raises pathvalidate.InvalidReservedNameError:
If the ``var_name`` is equals to
`Python reserved keywords
<https://docs.python.org/3/reference/lexical_analysis.html#keywords>`__
or
`Python built-in constants
<https://docs.python.org/3/library/constants.html>`__.
:Examples:
:ref:`example-validate-var-name`
"""
PythonVarNameSanitizer(var_name).validate()
def sanitize_python_var_name(var_name, replacement_text=""):
"""
Make a valid Python variable name from ``var_name``.
To make a valid name:
- Replace invalid characters for a Python variable name within
the ``var_name`` with the ``replacement_text``
- Delete invalid chars for the beginning of the variable name
- Append under bar (``"_"``) at the tail of the name if sanitized name
is one of the Python reserved names
:param str filename: Name to sanitize.
:param str replacement_text: Replacement text.
:return: A replacement string.
:rtype: str
:raises ValueError: If ``var_name`` or ``replacement_text`` is invalid.
:Examples:
:ref:`example-sanitize-var-name`
.. seealso::
:py:func:`.validate_python_var_name`
"""
return PythonVarNameSanitizer(var_name).sanitize(replacement_text)