Skip to content

Commit

Permalink
0.7.3 (2023-01-24)
Browse files Browse the repository at this point in the history
------------------
* [fix] Intf.all_names() > mgmt0
* [new] intf_name_map.py long_to_shorts
  • Loading branch information
vprusakovs committed Jan 24, 2023
1 parent 3c1c672 commit b64e864
Show file tree
Hide file tree
Showing 14 changed files with 323 additions and 184 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
CHANGELOG
=========

0.7.3 (2023-01-24)
------------------
* [fix] Intf.all_names() > mgmt0


0.7.2 (2022-12-17)
------------------
* [fix] Intf.all_names() for HP Procurve
Expand Down
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ or install the package from github.com release

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

.. code:: bash
pip install git+https://github.com/vladimirs-git/[email protected].2
pip install git+https://github.com/vladimirs-git/[email protected].3
Expand Down
1 change: 0 additions & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@
from netports.range import Range
from netports.tcp import stcp, itcp
from netports.vlan import ivlan, svlan

27 changes: 16 additions & 11 deletions netports/intf.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,24 +209,29 @@ def all_names(self) -> LStr:
"eth1/2",
]
"""
results: SStr = {self.line}
results.add(self.name)
results.add(self.name_short)
results: SStr = set()
for name_ in [self.line, self.name]:
results.add(name_)
if not name_.startswith("interface "):
name_ = f"interface {name_}"
results.add(name_)
# results.add(self.name_short)

for name in [self.name, self.name_short]:
short_o = Intf(name)
intf_o = Intf(name)
for id0_short, map_d in [
(short_o.id0, short_to_long),
(short_o.id0.lower(), short_to_long_lower),
(intf_o.id0, short_to_long),
(intf_o.id0.lower(), short_to_long_lower),
]:
id0_long = map_d.get(id0_short) or ""
name_long = short_o.line.replace(id0_short, id0_long, 1)
results.add(name_long)
results.add(f"interface {name_long}")
if id0_long := map_d.get(id0_short) or "":
name_long = intf_o.line.replace(id0_short, id0_long, 1)
results.add(name_long)
if not name_long.startswith("interface "):
results.add(f"interface {name_long}")

results.update([s.lower() for s in results])
results_: LStr = sorted(results)
results_.sort(key=lambda s: len(s), reverse=True)
results_.sort(key=len, reverse=True)
return results_

def part(self, idx: int) -> str:
Expand Down
60 changes: 30 additions & 30 deletions netports/intf_name_map.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
"""Interface name mapping"""
from netports.types_ import DStr
from netports.types_ import DStr, DLStr

long_to_short: DStr = {
# sorted ethernet
"FastEthernet": "Fa",
"FortyGigabitEthernet": "Fo",
"GigabitEthernet": "Gi",
"HundredGigabitEthernet": "Hu",
"TenGigabitEthernet": "Te",
"TwoGigabitEthernet": "Two",
"Wlan-GigabitEthernet": "Wl-Gi",
"Ethernet": "Eth",
# sorted tunnel
"Tunnel-ip": "Tu",
"Tunnel": "Tu",
# frequently used
"Port-channel": "Po",
"Loopback": "Lo",
"Vlan": "Vl",
# other
"ATM": "At",
"EOBC": "EO",
"Fddi": "FD",
"MFR": "MFR",
"Management": "Ma",
"Multilink": "Mu",
"POS": "PO",
"Serial": "Se",
"TwentyFiveGigE": "Twe",
"Virtual-Access": "Vi",
"Virtual-Template": "Vt",
long_to_shorts: DLStr = {
# ethernets
"FastEthernet": ["Fa"], # Cisco IOS
"FortyGigabitEthernet": ["Fo"],
"GigabitEthernet": ["Gi"], # Cisco IOS
"HundredGigabitEthernet": ["Hu"],
"TenGigabitEthernet": ["Te"], # Cisco IOS
"TwoGigabitEthernet": ["Two"],
"Wlan-GigabitEthernet": ["Wl-Gi"],
"Ethernet": ["Eth"], # Cisco NX-OS
# other frequently used
"Tunnel-ip": ["Tu"], # Cisco ASR9000
"Tunnel": ["Tu"], # Cisco IOS
"Port-channel": ["Po"], # Cisco IOS
"Loopback": ["Lo"], # Cisco IOS
"Vlan": ["Vl", "V"], # Cisco IOS
# other rear
"ATM": ["At"],
"EOBC": ["EO"],
"Fddi": ["FD"],
"MFR": ["MFR"],
"Management": ["Ma"],
"Multilink": ["Mu"],
# "POS": ["PO"], # conflict with Port-channel
"Serial": ["Se"],
"TwentyFiveGigE": ["Twe"],
"Virtual-Access": ["Vi"],
"Virtual-Template": ["Vt"],
}
long_to_short = {k: ls[0] for k, ls in long_to_shorts.items()}
long_to_short_lower: DStr = {k.lower(): v for k, v in long_to_short.items()}
short_to_long: DStr = {v: k for k, v in long_to_short.items()}
short_to_long_lower: DStr = {k.lower(): v for k, v in short_to_long.items()}
32 changes: 16 additions & 16 deletions netports/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,25 +475,25 @@ def iip(items: Any = "", **kwargs) -> LInt:
:param items: Range of IP protocol numbers, can be unsorted and with duplicates,
"ip" - Return all IP protocol numbers: [0, 1, ..., 255]
:type items: str, List[int], List[str]
:param verbose: True - all protocol numbers in verbose mode: [0, 1, ..., 255],
False - all protocol numbers in brief mode: [-1] to save RAM (default)
:type verbose: bool
:param strict: True - Raises ValueError, if the protocol is unknown (default)
False - Skips unknown protocols
:param all: True - Return all IP protocol numbers: [0, 1, ..., 255]
:type all: bool
:return: *List[int]* of unique sorted IP protocol numbers
:rtype: List[int]
:raises ValueError: If IP protocol numbers are outside valid range 0...255
:example: Converts mix of ip protocol names and numbers to the numbers
iip("icmp,tcp,7,255") -> [1, 6, 7, 255]
:example: Converts ip to full range of ip protocol numbers
iip("ip") -> [0, 1, ... 254, 255]
"""
Expand Down Expand Up @@ -535,23 +535,23 @@ def sip(items: Any = "", **kwargs) -> str:
:param items: Range of IP protocol numbers, can be unsorted and with duplicates,
"ip" - mean all numbers in range 0...255
:type items: str, List[int], List[str]
:param verbose: True - all protocol numbers in verbose mode: [0, 1, ..., 255],
False - all protocol numbers in brief mode: [-1], to save RAM (default)
:type verbose: bool
:param strict: True - Raises ValueError, if the protocol is unknown (default),
False - Skips unknown protocols
:type strict: bool
:param all: True - Return all IP protocol numbers: "0-255"
:type all: bool
:return: *str* of unique sorted IP protocol numbers
:rtype: str
:raises ValueError: If IP protocol numbers are outside valid range 0...255
:example:
sip(["icmp", "tcp", "7", 255]) -> "1,6-7,255"
"""
Expand All @@ -574,15 +574,15 @@ def ip_pairs(items: Any, **kwargs) -> Tuple[LTIntStr, LStr]:
::
:param items: Range of IP protocols, can be unsorted and with duplicates
:type items: str, List[int], List[str]
:param verbose: True - all protocol numbers in verbose mode: [0, 1, ..., 255],
False - all protocol numbers in brief mode: [-1], to save RAM (default)
:type verbose: bool
:return: *List[Tuple[int, str]]* Pairs of IP protocol number and name,
*List[str]* Undefined protocol names and invalid numbers
:rtype: Tuple[List[Tuple[int, str]], List[str]]
:example:
ip_pairs("tcp,1,typo,256") -> [(1, "icmp"), (6, "tcp")], ["256", "typo"]
ip_pairs(["tcp", 1, "typo", 256]) -> [(1, "icmp"), (6, "tcp")], ["256", "typo"]
Expand Down
1 change: 1 addition & 0 deletions netports/types_.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
StrInt = Union[str, int]
TIntStr = Tuple[int, str]

DLStr = Dict[str, LStr]
DSStr = Dict[str, SStr]
IStrInt = Union[IStr, IInt]
LTIntStr = List[TIntStr]
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.7.2"
version = "0.7.3"
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.7.2.tar.gz"
"Download URL" = "https://github.com/vladimirs-git/netports/archive/refs/tags/0.7.3.tar.gz"
[tool.setuptools.packages.find]
include = ["netports"]
[tool.setuptools.package-data]
Expand Down
26 changes: 13 additions & 13 deletions tests/helpers_.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
class Helpers(unittest.TestCase):
"""Address"""

def _test_attrs(self, obj, req_d, msg: str):
def _test_attrs(self, obj, exp_d, msg: str):
"""Test obj.line and attributes in req_d
:param obj: Tested object
:param req_d: Valid attributes and values
:param exp_d: Valid attributes and values
:param msg: Message
"""
result = obj.line
req = req_d["line"]
self.assertEqual(result, req, msg=f"{msg} line")
result = str(obj)
self.assertEqual(result, req, msg=f"{msg} str")
for attr, req in req_d.items():
actual = obj.line
expected = exp_d["line"]
self.assertEqual(expected, actual, msg=f"{msg} line")
actual = str(obj)
self.assertEqual(expected, actual, msg=f"{msg} str")
for attr, expected in exp_d.items():
if attr == "numbers":
method: Callable = getattr(obj, attr)
result = method()
actual = method()
else:
result = getattr(obj, attr)
if hasattr(result, "line"):
result = str(result)
self.assertEqual(result, req, msg=f"{msg} {attr=}")
actual = getattr(obj, attr)
if hasattr(actual, "line"):
actual = str(actual)
self.assertEqual(expected, actual, msg=f"{msg} {attr=}")
Loading

0 comments on commit b64e864

Please sign in to comment.