Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0b3c8fc774 | ||
|
|
7da18e0f00 | ||
|
|
49e38081ad | ||
|
|
a14f993a31 |
@@ -1,7 +1,7 @@
|
|||||||
# MARK: Project info
|
# MARK: Project info
|
||||||
[project]
|
[project]
|
||||||
name = "corelibs"
|
name = "corelibs"
|
||||||
version = "0.44.0"
|
version = "0.44.2"
|
||||||
description = "Collection of utils for Python scripts"
|
description = "Collection of utils for Python scripts"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.13"
|
requires-python = ">=3.13"
|
||||||
|
|||||||
23
src/corelibs/check_handling/regex_constants_compiled.py
Normal file
23
src/corelibs/check_handling/regex_constants_compiled.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
"""
|
||||||
|
List of regex compiled strings that can be used
|
||||||
|
"""
|
||||||
|
|
||||||
|
from corelibs.check_handling.regex_constants import (
|
||||||
|
compile_re,
|
||||||
|
EMAIL_BASIC_REGEX,
|
||||||
|
NAME_EMAIL_SIMPLE_REGEX,
|
||||||
|
NAME_EMAIL_BASIC_REGEX,
|
||||||
|
DOMAIN_WITH_LOCALHOST_REGEX,
|
||||||
|
DOMAIN_WITH_LOCALHOST_PORT_REGEX,
|
||||||
|
DOMAIN_REGEX
|
||||||
|
)
|
||||||
|
|
||||||
|
# all above in compiled form
|
||||||
|
COMPILED_EMAIL_BASIC_REGEX = compile_re(EMAIL_BASIC_REGEX)
|
||||||
|
COMPILED_NAME_EMAIL_SIMPLE_REGEX = compile_re(NAME_EMAIL_SIMPLE_REGEX)
|
||||||
|
COMPILED_NAME_EMAIL_BASIC_REGEX = compile_re(NAME_EMAIL_BASIC_REGEX)
|
||||||
|
COMPILED_DOMAIN_WITH_LOCALHOST_REGEX = compile_re(DOMAIN_WITH_LOCALHOST_REGEX)
|
||||||
|
COMPILED_DOMAIN_WITH_LOCALHOST_PORT_REGEX = compile_re(DOMAIN_WITH_LOCALHOST_PORT_REGEX)
|
||||||
|
COMPILED_DOMAIN_REGEX = compile_re(DOMAIN_REGEX)
|
||||||
|
|
||||||
|
# __END__
|
||||||
@@ -6,6 +6,10 @@ from corelibs_text_colors.text_colors import Colors
|
|||||||
from corelibs.check_handling.regex_constants import (
|
from corelibs.check_handling.regex_constants import (
|
||||||
compile_re, DOMAIN_WITH_LOCALHOST_REGEX, EMAIL_BASIC_REGEX, NAME_EMAIL_BASIC_REGEX, SUB_EMAIL_BASIC_REGEX
|
compile_re, DOMAIN_WITH_LOCALHOST_REGEX, EMAIL_BASIC_REGEX, NAME_EMAIL_BASIC_REGEX, SUB_EMAIL_BASIC_REGEX
|
||||||
)
|
)
|
||||||
|
from corelibs.check_handling.regex_constants_compiled import (
|
||||||
|
COMPILED_DOMAIN_WITH_LOCALHOST_REGEX, COMPILED_EMAIL_BASIC_REGEX,
|
||||||
|
COMPILED_NAME_EMAIL_SIMPLE_REGEX, COMPILED_NAME_EMAIL_BASIC_REGEX
|
||||||
|
)
|
||||||
|
|
||||||
NAME_EMAIL_SIMPLE_REGEX = r"""
|
NAME_EMAIL_SIMPLE_REGEX = r"""
|
||||||
^\s*(?:"(?P<name1>[^"]+)"\s*<(?P<email1>[^>]+)>|
|
^\s*(?:"(?P<name1>[^"]+)"\s*<(?P<email1>[^>]+)>|
|
||||||
@@ -28,7 +32,7 @@ def domain_test():
|
|||||||
"some-domain.org"
|
"some-domain.org"
|
||||||
]
|
]
|
||||||
|
|
||||||
regex_domain_check = compile_re(DOMAIN_WITH_LOCALHOST_REGEX)
|
regex_domain_check = COMPILED_DOMAIN_WITH_LOCALHOST_REGEX
|
||||||
print(f"REGEX: {DOMAIN_WITH_LOCALHOST_REGEX}")
|
print(f"REGEX: {DOMAIN_WITH_LOCALHOST_REGEX}")
|
||||||
print(f"Check regex: {regex_domain_check.search('localhost')}")
|
print(f"Check regex: {regex_domain_check.search('localhost')}")
|
||||||
|
|
||||||
@@ -59,10 +63,15 @@ def email_test():
|
|||||||
test open <open@open.com>
|
test open <open@open.com>
|
||||||
"""
|
"""
|
||||||
|
|
||||||
basic_email = compile_re(EMAIL_BASIC_REGEX)
|
print(f"REGEX: SUB_EMAIL_BASIC_REGEX: {SUB_EMAIL_BASIC_REGEX}")
|
||||||
|
print(f"REGEX: EMAIL_BASIC_REGEX: {EMAIL_BASIC_REGEX}")
|
||||||
|
print(f"REGEX: COMPILED_NAME_EMAIL_SIMPLE_REGEX: {COMPILED_NAME_EMAIL_SIMPLE_REGEX}")
|
||||||
|
print(f"REGEX: NAME_EMAIL_BASIC_REGEX: {NAME_EMAIL_BASIC_REGEX}")
|
||||||
|
|
||||||
|
basic_email = COMPILED_EMAIL_BASIC_REGEX
|
||||||
sub_basic_email = compile_re(SUB_EMAIL_BASIC_REGEX)
|
sub_basic_email = compile_re(SUB_EMAIL_BASIC_REGEX)
|
||||||
simple_name_email_regex = compile_re(NAME_EMAIL_SIMPLE_REGEX)
|
simple_name_email_regex = COMPILED_NAME_EMAIL_SIMPLE_REGEX
|
||||||
full_name_email_regex = compile_re(NAME_EMAIL_BASIC_REGEX)
|
full_name_email_regex = COMPILED_NAME_EMAIL_BASIC_REGEX
|
||||||
for email in email_list.splitlines():
|
for email in email_list.splitlines():
|
||||||
email = email.strip()
|
email = email.strip()
|
||||||
if not email:
|
if not email:
|
||||||
|
|||||||
@@ -14,7 +14,15 @@ from corelibs.check_handling.regex_constants import (
|
|||||||
NAME_EMAIL_BASIC_REGEX,
|
NAME_EMAIL_BASIC_REGEX,
|
||||||
DOMAIN_WITH_LOCALHOST_REGEX,
|
DOMAIN_WITH_LOCALHOST_REGEX,
|
||||||
DOMAIN_WITH_LOCALHOST_PORT_REGEX,
|
DOMAIN_WITH_LOCALHOST_PORT_REGEX,
|
||||||
DOMAIN_REGEX,
|
DOMAIN_REGEX
|
||||||
|
)
|
||||||
|
from corelibs.check_handling.regex_constants_compiled import (
|
||||||
|
COMPILED_EMAIL_BASIC_REGEX,
|
||||||
|
COMPILED_NAME_EMAIL_SIMPLE_REGEX,
|
||||||
|
COMPILED_NAME_EMAIL_BASIC_REGEX,
|
||||||
|
COMPILED_DOMAIN_WITH_LOCALHOST_REGEX,
|
||||||
|
COMPILED_DOMAIN_WITH_LOCALHOST_PORT_REGEX,
|
||||||
|
COMPILED_DOMAIN_REGEX,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -51,7 +59,7 @@ class TestEmailBasicRegex:
|
|||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def email_pattern(self) -> re.Pattern[str]:
|
def email_pattern(self) -> re.Pattern[str]:
|
||||||
"""Fixture that returns compiled email regex pattern."""
|
"""Fixture that returns compiled email regex pattern."""
|
||||||
return compile_re(EMAIL_BASIC_REGEX)
|
return COMPILED_EMAIL_BASIC_REGEX
|
||||||
|
|
||||||
@pytest.mark.parametrize("valid_email", [
|
@pytest.mark.parametrize("valid_email", [
|
||||||
"user@example.com",
|
"user@example.com",
|
||||||
@@ -192,7 +200,7 @@ class TestNameEmailSimpleRegex:
|
|||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def name_email_simple_pattern(self) -> re.Pattern[str]:
|
def name_email_simple_pattern(self) -> re.Pattern[str]:
|
||||||
"""Fixture that returns compiled name+email simple regex pattern."""
|
"""Fixture that returns compiled name+email simple regex pattern."""
|
||||||
return compile_re(NAME_EMAIL_SIMPLE_REGEX)
|
return COMPILED_NAME_EMAIL_SIMPLE_REGEX
|
||||||
|
|
||||||
@pytest.mark.parametrize("test_input,expected_groups", [
|
@pytest.mark.parametrize("test_input,expected_groups", [
|
||||||
('"John Doe" <john@example.com>', {'name1': 'John Doe', 'email1': 'john@example.com'}),
|
('"John Doe" <john@example.com>', {'name1': 'John Doe', 'email1': 'john@example.com'}),
|
||||||
@@ -284,7 +292,7 @@ class TestNameEmailBasicRegex:
|
|||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def name_email_basic_pattern(self) -> re.Pattern[str]:
|
def name_email_basic_pattern(self) -> re.Pattern[str]:
|
||||||
"""Fixture that returns compiled name+email basic regex pattern."""
|
"""Fixture that returns compiled name+email basic regex pattern."""
|
||||||
return compile_re(NAME_EMAIL_BASIC_REGEX)
|
return COMPILED_NAME_EMAIL_BASIC_REGEX
|
||||||
|
|
||||||
@pytest.mark.parametrize("test_input,expected_name,expected_email", [
|
@pytest.mark.parametrize("test_input,expected_name,expected_email", [
|
||||||
('"John Doe" <john@example.com>', 'John Doe', 'john@example.com'),
|
('"John Doe" <john@example.com>', 'John Doe', 'john@example.com'),
|
||||||
@@ -391,7 +399,7 @@ class TestDomainWithLocalhostRegex:
|
|||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def domain_localhost_pattern(self) -> re.Pattern[str]:
|
def domain_localhost_pattern(self) -> re.Pattern[str]:
|
||||||
"""Fixture that returns compiled domain with localhost regex pattern."""
|
"""Fixture that returns compiled domain with localhost regex pattern."""
|
||||||
return compile_re(DOMAIN_WITH_LOCALHOST_REGEX)
|
return COMPILED_DOMAIN_WITH_LOCALHOST_REGEX
|
||||||
|
|
||||||
@pytest.mark.parametrize("valid_domain", [
|
@pytest.mark.parametrize("valid_domain", [
|
||||||
"localhost",
|
"localhost",
|
||||||
@@ -443,7 +451,7 @@ class TestDomainWithLocalhostPortRegex:
|
|||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def domain_localhost_port_pattern(self) -> re.Pattern[str]:
|
def domain_localhost_port_pattern(self) -> re.Pattern[str]:
|
||||||
"""Fixture that returns compiled domain and localhost with port pattern."""
|
"""Fixture that returns compiled domain and localhost with port pattern."""
|
||||||
return compile_re(DOMAIN_WITH_LOCALHOST_PORT_REGEX)
|
return COMPILED_DOMAIN_WITH_LOCALHOST_PORT_REGEX
|
||||||
|
|
||||||
@pytest.mark.parametrize("valid_domain", [
|
@pytest.mark.parametrize("valid_domain", [
|
||||||
"localhost",
|
"localhost",
|
||||||
@@ -509,7 +517,7 @@ class TestDomainRegex:
|
|||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def domain_pattern(self) -> re.Pattern[str]:
|
def domain_pattern(self) -> re.Pattern[str]:
|
||||||
"""Fixture that returns compiled domain regex pattern."""
|
"""Fixture that returns compiled domain regex pattern."""
|
||||||
return compile_re(DOMAIN_REGEX)
|
return COMPILED_DOMAIN_REGEX
|
||||||
|
|
||||||
@pytest.mark.parametrize("valid_domain", [
|
@pytest.mark.parametrize("valid_domain", [
|
||||||
"example.com",
|
"example.com",
|
||||||
@@ -568,6 +576,8 @@ class TestRegexPatternConsistency:
|
|||||||
"""Test that all regex patterns can be compiled without errors."""
|
"""Test that all regex patterns can be compiled without errors."""
|
||||||
patterns = [
|
patterns = [
|
||||||
EMAIL_BASIC_REGEX,
|
EMAIL_BASIC_REGEX,
|
||||||
|
NAME_EMAIL_SIMPLE_REGEX,
|
||||||
|
NAME_EMAIL_BASIC_REGEX,
|
||||||
DOMAIN_WITH_LOCALHOST_REGEX,
|
DOMAIN_WITH_LOCALHOST_REGEX,
|
||||||
DOMAIN_WITH_LOCALHOST_PORT_REGEX,
|
DOMAIN_WITH_LOCALHOST_PORT_REGEX,
|
||||||
DOMAIN_REGEX,
|
DOMAIN_REGEX,
|
||||||
@@ -576,9 +586,24 @@ class TestRegexPatternConsistency:
|
|||||||
compiled = compile_re(pattern)
|
compiled = compile_re(pattern)
|
||||||
assert isinstance(compiled, re.Pattern)
|
assert isinstance(compiled, re.Pattern)
|
||||||
|
|
||||||
|
def test_compiled_patterns_are_patterns(self) -> None:
|
||||||
|
"""Test that all COMPILED_ constants are Pattern objects."""
|
||||||
|
compiled_patterns = [
|
||||||
|
COMPILED_EMAIL_BASIC_REGEX,
|
||||||
|
COMPILED_NAME_EMAIL_SIMPLE_REGEX,
|
||||||
|
COMPILED_NAME_EMAIL_BASIC_REGEX,
|
||||||
|
COMPILED_DOMAIN_WITH_LOCALHOST_REGEX,
|
||||||
|
COMPILED_DOMAIN_WITH_LOCALHOST_PORT_REGEX,
|
||||||
|
COMPILED_DOMAIN_REGEX,
|
||||||
|
]
|
||||||
|
for pattern in compiled_patterns:
|
||||||
|
assert isinstance(pattern, re.Pattern)
|
||||||
|
|
||||||
def test_domain_patterns_are_strings(self) -> None:
|
def test_domain_patterns_are_strings(self) -> None:
|
||||||
"""Test that all regex constants are strings."""
|
"""Test that all regex constants are strings."""
|
||||||
assert isinstance(EMAIL_BASIC_REGEX, str)
|
assert isinstance(EMAIL_BASIC_REGEX, str)
|
||||||
|
assert isinstance(NAME_EMAIL_SIMPLE_REGEX, str)
|
||||||
|
assert isinstance(NAME_EMAIL_BASIC_REGEX, str)
|
||||||
assert isinstance(DOMAIN_WITH_LOCALHOST_REGEX, str)
|
assert isinstance(DOMAIN_WITH_LOCALHOST_REGEX, str)
|
||||||
assert isinstance(DOMAIN_WITH_LOCALHOST_PORT_REGEX, str)
|
assert isinstance(DOMAIN_WITH_LOCALHOST_PORT_REGEX, str)
|
||||||
assert isinstance(DOMAIN_REGEX, str)
|
assert isinstance(DOMAIN_REGEX, str)
|
||||||
@@ -587,8 +612,8 @@ class TestRegexPatternConsistency:
|
|||||||
"""Test that domain patterns follow expected hierarchy."""
|
"""Test that domain patterns follow expected hierarchy."""
|
||||||
# DOMAIN_WITH_LOCALHOST_PORT_REGEX should accept everything
|
# DOMAIN_WITH_LOCALHOST_PORT_REGEX should accept everything
|
||||||
# DOMAIN_WITH_LOCALHOST_REGEX accepts
|
# DOMAIN_WITH_LOCALHOST_REGEX accepts
|
||||||
domain_localhost = compile_re(DOMAIN_WITH_LOCALHOST_REGEX)
|
domain_localhost = COMPILED_DOMAIN_WITH_LOCALHOST_REGEX
|
||||||
domain_localhost_port = compile_re(DOMAIN_WITH_LOCALHOST_PORT_REGEX)
|
domain_localhost_port = COMPILED_DOMAIN_WITH_LOCALHOST_PORT_REGEX
|
||||||
|
|
||||||
test_cases = ["example.com", "subdomain.example.com", "localhost"]
|
test_cases = ["example.com", "subdomain.example.com", "localhost"]
|
||||||
for test_case in test_cases:
|
for test_case in test_cases:
|
||||||
|
|||||||
Reference in New Issue
Block a user