_re = re.compile(r"^([0-9]+)\s{0,1}([BKMG]){0,1}$")
_units = {"B": 1, "K": 1024, "M": 1024 ** 2, "G": 1024 ** 3}
+ def bytes(self) -> int:
+ return self._value
+
class TimeUnit(Unit):
_re = re.compile(r"^(\d+)\s{0,1}([smhd]s?){0,1}$")
_PREVIOUS_SCHEMA = Raw
typ: ListenType
- ip: Optional[Union[ipaddress.IPv4Address, ipaddress.IPv6Address]] = None
- port: Optional[int] = None
- unix_socket: Optional[AnyPath] = None
- interface: Optional[str] = None
+ ip: Optional[Union[ipaddress.IPv4Address, ipaddress.IPv6Address]]
+ port: Optional[int]
+ unix_socket: Optional[AnyPath]
+ interface: Optional[str]
def _typ(self, origin: Raw):
present = {
def test_parsing_units():
- class TestClass(SchemaNode):
+ class TestSchema(SchemaNode):
size: SizeUnit
time: TimeUnit
- class TestClassStrict(SchemaNode):
- size: int
- time: int
-
- def _validate(self) -> None:
- pass
-
- obj = TestClass({"size": "3K", "time": "10m"})
- assert obj.size == SizeUnit("3072B")
- assert obj.time == TimeUnit("10m")
-
- strict = TestClassStrict(obj)
- assert strict.size == 3 * 1024
- assert strict.time == 10 * 60 * 1000
+ o = TestSchema({"size": "3K", "time": "10m"})
+ assert o.size == SizeUnit("3072B")
+ assert o.time == TimeUnit("10m")
+ assert o.size.bytes() == 3072
+ assert o.time.seconds() == 10 * 60
def test_anypath():