Skip to content

Commit

Permalink
0.11.1 (2023-12-03)
Browse files Browse the repository at this point in the history
-------------------
* [new] check_port() check_ports()
  • Loading branch information
vladimirs-git committed Dec 3, 2023
1 parent ad1db1b commit 8c3eda0
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 23 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
CHANGELOG
=========

0.11.1 (2023-12-03)
-------------------
* [new] check_port() check_ports()


0.10.0 (2023-11-03)
------------------
-------------------
* [new] SwVersion()


Expand Down
41 changes: 39 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ or install the package from github.com release

.. code:: bash
pip install https://github.com/vladimirs-git/netports/archive/refs/tags/0.10.0.tar.gz
pip install https://github.com/vladimirs-git/netports/archive/refs/tags/0.11.1.tar.gz
or install the package from github.com repository

.. code:: bash
pip install git+https://github.com/vladimirs-git/netports@0.10.0
pip install git+https://github.com/vladimirs-git/netports@0.11.1
.. contents:: **Contents**
Expand All @@ -47,6 +47,43 @@ or install the package from github.com repository
TCP/UDP ports
-------------

check_port()
............
**check_port(port, strict)**
Check TCP/UDP port in the range 1 to 65535.

=============== =========================== ============================================================================
Parameter Type Description
=============== =========================== ============================================================================
port *int* The TCP/UDP port that needs to be checked.
strict *bool* True - raise NetportsValueError if the port is invalid, False - return False if the port is invalid. Default is `False`.
=============== =========================== ============================================================================

Return
*bool* True - If the port is in the valid range of 1 to 65535, False - otherwise.
Raises
*TypeError* If the port is not integer.
*NetportsValueError* If strict=True and the port is outside the valid range.


check_port()
............
**check_ports(ports, strict)**
heck TCP/UDP ports in the range 1 to 65535.

=============== =========================== ============================================================================
Parameter Type Description
=============== =========================== ============================================================================
ports *List[int]* The TCP/UDP ports that needs to be checked
strict *bool* True - raise NetportsValueError if any in the ports is invalid, False - return False if the port is invalid. Default is `False`.
=============== =========================== ============================================================================

Return
*bool* True - if all ports is in the valid range of 1 to 65535, False - otherwise.
Raises
*TypeError* If any in the ports is not integer.
*NetportsValueError* If strict=True and any in the ports is outside the valid range.


itcp()
......
Expand Down
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
from netports.ports import inumbers, parse_range, snumbers
from netports.range import Range
from netports.swversion import SwVersion
from netports.tcp import stcp, itcp
from netports.tcp import stcp, itcp, check_port, check_ports
from netports.vlan import ivlan, svlan
4 changes: 3 additions & 1 deletion netports/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from netports.ports import inumbers, parse_range, snumbers
from netports.range import Range
from netports.swversion import SwVersion
from netports.tcp import stcp, itcp
from netports.tcp import stcp, itcp, check_port, check_ports
from netports.vlan import ivlan, svlan

__all__ = [
Expand All @@ -21,6 +21,8 @@
"NetportsValueError",
"Range",
"SwVersion",
"check_port",
"check_ports",
"iip",
"intfrange",
"inumbers",
Expand Down
52 changes: 38 additions & 14 deletions netports/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def itcp(items: Any = "", **kwargs) -> LInt:
return [BRIEF_ALL_I]

ports: LInt = inumbers(items)
check_tcp_ports(ports)
check_ports(ports=ports, strict=True)

if h.is_brief(**kwargs):
if ports == ALL_PORTS_L:
Expand Down Expand Up @@ -82,27 +82,51 @@ def stcp(items: Any = "", **kwargs) -> str:
if h.is_brief_in_items(items):
items_ = ",".join(h.lstr(h.remove_brief_items(items)))
range_o: Range = parse_range(items_)
check_tcp_ports(range_o.numbers())
check_ports(ports=range_o.numbers(), strict=True)
return ALL_PORTS_S

range_o = parse_range(items)
check_tcp_ports(range_o.numbers())
check_ports(ports=range_o.numbers(), strict=True)
return str(range_o)


# ============================= helpers ==============================
def check_port(port: int, strict: bool = False) -> bool:
"""Check TCP/UDP port in the range 1 to 65535.
def check_tcp_ports(items: LInt) -> bool:
"""Checks TCP/UDP ports
::
:param items: TCP/UDP ports
:type items: List[int]
:param int port: The TCP/UDP port that needs to be checked.
:param bool strict: True - raise NetportsValueError if the port is invalid,
False - return False if the port is invalid. Default is `False`.
:return: True - If the port is in the valid range of 1 to 65535, False - otherwise.
:rtype: bool
:raises TypeError: If the port is not integer.
:raises NetportsValueError: If strict=True and the port is outside the valid range.
"""
if not isinstance(port, int):
raise TypeError(f"{port=} {int} expected.")
if MIN_PORT <= port <= MAX_PORT:
return True
if strict:
raise NetportsValueError(f"{port=}, expected in the range {MIN_PORT} to {MAX_PORT}")
return False


def check_ports(ports: LInt, strict: bool = False) -> bool:
"""Check TCP/UDP ports in the range 1 to 65535.
:param List[int] ports: The TCP/UDP ports that needs to be checked.
:param bool strict: True - raise NetportsValueError if any in the ports is invalid,
False - return False if the port is invalid. Default is `False`.
:return: True if all items are in the valid TCP/UDP range 1...65535
:rtype: bool
:return: True - if all ports is in the valid range of 1 to 65535, False - otherwise.
:rtype: bool
:raises NetportsValueError: If on of item is outside valid range
:raises TypeError: If any in the ports is not integer.
:raises NetportsValueError: If strict=True and any in the ports is outside the valid range.
"""
if invalid_port := [i for i in items if i < MIN_PORT or i > MAX_PORT]:
raise NetportsValueError(f"{invalid_port=}, expected in range 1...65535")
for port in ports:
if not check_port(port=port, strict=strict):
return False
return True
3 changes: 2 additions & 1 deletion netports/vlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

from typing import Any

from netports import Range, NetportsValueError
from netports import Range
from netports import helpers as h
from netports.exceptions import NetportsValueError
from netports.ports import inumbers, parse_range
from netports.static import BRIEF_ALL_I, RANGE_SPLITTER, RANGE_SPLITTER_HPE, SPLITTER, SPLITTER_HPE
from netports.types_ import LInt
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "netports"
version = "0.10.0"
version = "0.11.1"
authors = [{ name="Vladimir Prusakov", email="[email protected]" }]
description = "Python tools for managing ranges of VLANs, TCP/UDP ports, IP protocols, Interfaces"
readme = "README.rst"
Expand All @@ -21,7 +21,7 @@ classifiers = [
"Homepage" = "https://github.com/vladimirs-git/netports"
"Repository" = "https://github.com/vladimirs-git/netports"
"Bug Tracker" = "https://github.com/vladimirs-git/netports/issues"
"Download URL" = "https://github.com/vladimirs-git/netports/archive/refs/tags/0.10.0.tar.gz"
"Download URL" = "https://github.com/vladimirs-git/netports/archive/refs/tags/0.11.1.tar.gz"
[tool.setuptools.packages.find]
include = ["netports"]
[tool.setuptools.package-data]
Expand Down
54 changes: 54 additions & 0 deletions tests/test__tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,60 @@ def test_invalid__itcp__stcp(self):
with self.assertRaises(error, msg=f"{kwargs=}"):
tcp.stcp(**kwargs)

def test__check_port(self):
"""check_port()"""
for kwargs, req in [
({"port": "", "strict": True}, TypeError),
({"port": "", "strict": False}, TypeError),
({"port": {}, "strict": True}, TypeError),
({"port": {}, "strict": False}, TypeError),
({"port": "-1", "strict": True}, TypeError),
({"port": "-1", "strict": False}, TypeError),
({"port": -1, "strict": True}, NetportsValueError),
({"port": -1, "strict": False}, False),
({"port": "0", "strict": True}, TypeError),
({"port": "0", "strict": False}, TypeError),
({"port": 0, "strict": True}, NetportsValueError),
({"port": 0, "strict": False}, False),
({"port": "1", "strict": True}, TypeError),
({"port": "1", "strict": False}, TypeError),
({"port": 1, "strict": True}, True),
({"port": 1, "strict": False}, True),
({"port": "65535", "strict": True}, TypeError),
({"port": "65535", "strict": False}, TypeError),
({"port": 65535, "strict": True}, True),
({"port": 65535, "strict": False}, True),
({"port": "65536", "strict": True}, TypeError),
({"port": "65536", "strict": False}, TypeError),
({"port": 65536, "strict": True}, NetportsValueError),
({"port": 65536, "strict": False}, False),
]:
if isinstance(req, bool):
result = tcp.check_port(**kwargs)
self.assertEqual(result, req, msg=f"{kwargs=}")
else:
with self.assertRaises(req, msg=f"{kwargs=}"):
tcp.check_port(**kwargs)

def test__check_ports(self):
"""check_ports()"""
for kwargs, req in [
({"ports": [], "strict": True}, True),
({"ports": [], "strict": False}, True),
({"ports": ["1"], "strict": True}, TypeError),
({"ports": ["1"], "strict": False}, TypeError),
({"ports": [1, 65535], "strict": True}, True),
({"ports": [1, 65535], "strict": False}, True),
({"ports": ["0"], "strict": True}, TypeError),
({"ports": ["0"], "strict": False}, TypeError),
]:
if isinstance(req, bool):
result = tcp.check_ports(**kwargs)
self.assertEqual(result, req, msg=f"{kwargs=}")
else:
with self.assertRaises(req, msg=f"{kwargs=}"):
tcp.check_ports(**kwargs)


if __name__ == "__main__":
unittest.main()
3 changes: 2 additions & 1 deletion tests/test__vlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import dictdiffer # type: ignore

from netports import vlan, NetportsValueError
from netports import vlan
from netports.exceptions import NetportsValueError

ALL = list(range(1, 4095))

Expand Down

0 comments on commit 8c3eda0

Please sign in to comment.