Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stricto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@
from .selector import Selector
from .kparse import Kparse
from .event import EVENT_MANAGER
from .extended.email import Email
2 changes: 1 addition & 1 deletion stricto/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ def get_selectors(
return self

# The index_or_slice is actually ignored.
(key, sub_index_or_slice) = sel.pop()
key, sub_index_or_slice = sel.pop()

if key in self._keys:
v = self.__dict__[key]
Expand Down
33 changes: 33 additions & 0 deletions stricto/extended/email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
Module email verify if valid
"""
import re
from stricto import Extend, SConstraintError


class Email(Extend):
"""Extend type that validates and stores an email address."""
def __init__(self, **kwargs):
super().__init__(str, **kwargs)

def __json_encode__(self):

va = self.get_value()
if va is None:
return None
return str(self.get_value())

def __json_decode__(self, value: str):
return value

def check_constraints(self, value: str):

pattern1 = r"[A-Za-z0-9-.+]+.[A-Za-z0-9-.+]*"
pattern2 = r"[A-Za-z0-9-.+]+.[A-Za-z]+"
result = pattern1 + r"@" + pattern2
verify = re.match(result, value)
if verify is None:
raise SConstraintError(
'{0}:Must be a email (value="{value}")', self.path_name(), value=value
)
return True
1 change: 1 addition & 0 deletions stricto/extended/ip_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
Module for ip adresses
"""

import ipaddress
from stricto.extend import Extend
from stricto import STypeError
Expand Down
1 change: 1 addition & 0 deletions stricto/extended/ip_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
Module for ip network addresses in CIDR notation
"""

import ipaddress
from stricto.extend import Extend
from stricto import STypeError
Expand Down
2 changes: 1 addition & 1 deletion stricto/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ def select(self, selector_as_string: str) -> Self | None:
if sel.empty():
return self

(key, sub_index_or_slice) = sel.pop()
key, sub_index_or_slice = sel.pop()

if key == "$":
return self.get_root().get_selectors(sub_index_or_slice, sel)
Expand Down
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
from .test_selectors import TestSelectors
from .test_toolbox import TestToolbox
from .test_kparse import TestKparse
from .test_email import TestEmail
54 changes: 54 additions & 0 deletions tests/test_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""module for a test_email"""
import unittest
import json
from stricto import Email, SConstraintError, StrictoEncoder


class TestEmail(unittest.TestCase):
"""test unitaire for a module mail"""
def __init__(self, m):
unittest.TestCase.__init__(self, m)
self.on_change_bool = False

def test_error_constraint(self):
"""test error constraint"""
a = Email()
with self.assertRaises(SConstraintError) as e:
a.set("emelie.com")
self.assertEqual(
e.exception.to_string(), '$:Must be a email (value="emelie.com")'
)

def test_json(self):
"""test a function json"""
a = Email()
b = Email()
a.set("papo@pupo_toro.titi.net")
sa = json.dumps(a, cls=StrictoEncoder)
b.set(json.loads(sa))
self.assertEqual(b, a)

def test_get_value(self):
"test value "
a = Email()
b = Email()
a.set("emelie@network.fr")
v = a.get_value()
b.set(v)
self.assertEqual(b, a)

def test_get_encoded(self):
"""test encoded"""
a = Email()
b = Email()
a.set("fifi.titi@toto23.too.fr")
v = a.get_encoded()
b.set(v)
self.assertEqual(b, a)

def test_default(self):
"""test value default"""
a = Email()
self.assertEqual(a.get_encoded(), None)
a = Email(default="toto.tota@testpass_ok.net")
self.assertEqual(a, "toto.tota@testpass_ok.net")
1 change: 1 addition & 0 deletions tests/test_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
test for Errors()
"""

import unittest

from stricto import StrictoError, STypeError, SError
Expand Down
1 change: 1 addition & 0 deletions tests/test_kparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
test for Kparse()
"""

import unittest
from typing import Callable

Expand Down
1 change: 0 additions & 1 deletion tests/test_rights.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from stricto import String, Int, Dict, SRightError, SAttributeError


can_modify_flag = True # pylint: disable=invalid-name


Expand Down
1 change: 1 addition & 0 deletions tests/test_toolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
test for toolbox()
"""

import unittest
from typing import Any, Callable, Self

Expand Down
Loading