Skip to content

Commit

Permalink
0.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirs-git committed Oct 23, 2023
1 parent da3c450 commit 2292b76
Show file tree
Hide file tree
Showing 15 changed files with 232 additions and 155 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,4 @@ dmypy.json
# Temp
tmp/
temp/
notes/
_notes/
9 changes: 8 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@
CHANGELOG
=========

0.9.0 (2023-10-23)
------------------
* [new] intf_map.py ALL_SHORT
* [change] platform > device_type
* [change] Intf._init_device_type() ValueError > NetportsValueError
* [new] Intf.name_short(replace=[("Fa", "Eth")])

0.8.2 (2023-04-04)
------------------
Intf(platform="hp_procurve").all_names() -> ["interface Trk1", ...]
* [change] Intf(platform="hp_procurve").all_names() -> ["interface Trk1", ...]

0.8.1 (2023-04-04)
------------------
Expand Down
50 changes: 30 additions & 20 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.8.2.tar.gz
pip install https://github.com/vladimirs-git/netports/archive/refs/tags/0.9.0.tar.gz
or install the package from github.com repository

.. code:: bash
pip install git+https://github.com/vladimirs-git/netports@0.8.2
pip install git+https://github.com/vladimirs-git/netports@0.9.0
Expand Down Expand Up @@ -389,13 +389,13 @@ Return

long_to_short()
...............
**long_to_short(platform, key_lower, value_lower)**
Returns Interfaces map long-to-short, platform specific
**long_to_short(device_type, key_lower, value_lower)**
Returns Interfaces map long-to-short, device_type specific

=============== =========================== ============================================================================
Parameter Type Description
=============== =========================== ============================================================================
platform *str* Platform, increase priority of platform specific keys. "", "cisco_asr", "cisco_ios", "cisco_nxos", "hp_comware"
device_type *str* Netmiko device type, increase priority of device_type specific keys. "", "cisco_asr", "cisco_ios", "cisco_nxos", "hp_comware"
key_lower *bool* True - keys lower-case, False - keys upper-case
value_lower *bool* True - values lower-case, False - values upper-case
=============== =========================== ============================================================================
Expand All @@ -406,13 +406,13 @@ Return

long_to_long()
..............
**long_to_long(platform, key_lower, value_lower)**
Returns Interfaces map long-to-long, platform specific
**long_to_long(device_type, key_lower, value_lower)**
Returns Interfaces map long-to-long, device_type specific

=============== =========================== ============================================================================
Parameter Type Description
=============== =========================== ============================================================================
platform *str* Platform, increase priority of platform specific keys. "", "cisco_asr", "cisco_ios", "cisco_nxos", "hp_comware"
device_type *str* Netmiko device type, increase priority of device_type specific keys. "", "cisco_asr", "cisco_ios", "cisco_nxos", "hp_comware"
key_lower *bool* True - keys lower-case, False - keys upper-case
value_lower *bool* True - values lower-case, False - values upper-case
=============== =========================== ============================================================================
Expand All @@ -423,13 +423,13 @@ Return

short_to_long()
...............
**short_to_long(platform, key_lower, value_lower)**
Returns Interfaces map short-to-long, platform specific
**short_to_long(device_type, key_lower, value_lower)**
Returns Interfaces map short-to-long, device_type specific

=============== =========================== ============================================================================
Parameter Type Description
=============== =========================== ============================================================================
platform *str* Platform, increase priority of platform specific keys. "", "cisco_asr", "cisco_ios", "cisco_nxos", "hp_comware"
device_type *str* Netmiko device type, increase priority of device_type specific keys. "", "cisco_asr", "cisco_ios", "cisco_nxos", "hp_comware"
key_lower *bool* True - keys lower-case, False - keys upper-case
value_lower *bool* True - values lower-case, False - values upper-case
=============== =========================== ============================================================================
Expand All @@ -440,13 +440,13 @@ Return

short_to_short()
................
**short_to_short(platform, key_lower, value_lower)**
Returns Interfaces map short-to-short, platform specific
**short_to_short(device_type, key_lower, value_lower)**
Returns Interfaces map short-to-short, device_type specific

=============== =========================== ============================================================================
Parameter Type Description
=============== =========================== ============================================================================
platform *str* Platform, increase priority of platform specific keys. "", "cisco_asr", "cisco_ios", "cisco_nxos", "hp_comware"
device_type *str* Netmiko device type, increase priority of device_type specific keys. "", "cisco_asr", "cisco_ios", "cisco_nxos", "hp_comware"
key_lower *bool* True - keys lower-case, False - keys upper-case
value_lower *bool* True - values lower-case, False - values upper-case
=============== =========================== ============================================================================
Expand All @@ -457,15 +457,15 @@ Return

Intf()
......
**Intf(line, platform, splitter)**
**Intf(line, device_type, splitter)**
An object of interface name, that can contain up to 4 indexes.
Sorts the interfaces by indexes (not by alphabetic).

=============== ======= ============================================================================
Parameter Type Description
=============== ======= ============================================================================
line *str* Interface name that can contain up to 4 indexes
platform *str* Platform like in Netmiko (default "")
device_type *str* Netmiko device_type (default "")
splitter *str* Separator of characters between indexes (default ",./:")
=============== ======= ============================================================================

Expand All @@ -486,7 +486,7 @@ ids Interface all IDs
line *str* Interface line
name *str* Interface name with IDs
splitter *str* Separator of characters between indexes
platform *str* Platform
device_type *str* Netmiko device_type
=============== ============ =======================================================================


Expand All @@ -499,7 +499,7 @@ Index of last ID in interface line
all_names()
...........
**all_names()**
All variants of names: long, short, upper-case, lover-case. Platform specific
All variants of names: long, short, upper-case, lover-case. Device type specific


name_full()
Expand All @@ -516,8 +516,18 @@ Interface long name with IDs and without interface keyword

name_short()
............
**name_short()**
Interface short name with IDs, platform specific
**name_short(replace)**
Interface short name with IDs, Device type specific

=========== =========================== ============================================================
Parameter Type Description
=========== =========================== ============================================================
replace *List[Tuple[str, str]]* Replace the default short name with the first one
that matches in the list of the 'replace' argument.
=========== =========================== ============================================================

Return
*str* Interface short name.


part_after()
Expand Down
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from netports.exceptions import NetportsValueError
from netports.intf import Intf
from netports.intf_gm import IntfGM, intfrange
from netports.intf_map import short_to_long, short_to_short, long_to_short, long_to_long
from netports.ip import IP_NAMES, IP_NUMBERS, iip, sip, ip_pairs
from netports.item import Item
from netports.ports import inumbers, parse_range, snumbers
from netports.range import Range
from netports.tcp import stcp, itcp
from netports.vlan import ivlan, svlan
from netports.intf_map import short_to_long, short_to_short, long_to_short, long_to_long
2 changes: 2 additions & 0 deletions examples/intf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# Methods
print("last_idx", intf.last_idx())
print("name_short", intf.name_short())
print("name_short", intf.name_short(replace=[("Eth", "Fa")]))
print("name_long", intf.name_long())
print("name_full", intf.name_full())
print("part before id", intf.part_before(idx=3))
Expand All @@ -29,6 +30,7 @@
print()
# last_idx 4
# name_short Eth1/2/3.4
# name_short Fa1/2/3.4
# name_long Ethernet1/2/3.4
# name_full interface Ethernet1/2/3.4
# part before id interface Ethernet1/2/
Expand Down
3 changes: 2 additions & 1 deletion netports/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
from netports.exceptions import NetportsValueError
from netports.intf import Intf
from netports.intf_gm import IntfGM, intfrange
from netports.intf_map import short_to_long, short_to_short, long_to_short, long_to_long
from netports.ip import IP_NAMES, IP_NUMBERS, iip, sip, ip_pairs
from netports.item import Item
from netports.ports import inumbers, parse_range, snumbers
from netports.range import Range
from netports.tcp import stcp, itcp
from netports.vlan import ivlan, svlan
from netports.intf_map import short_to_long, short_to_short, long_to_short, long_to_long

__all__ = [
"IP_NAMES",
"IP_NUMBERS",
Expand Down
65 changes: 39 additions & 26 deletions netports/intf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
from typing import List, Optional, Set, Tuple, Union

from netports import intf_map, helpers as h
from netports.static import PLATFORMS
from netports.types_ import T3Str, T5Str, LStr, SStr, DStr
from netports.exceptions import NetportsValueError
from netports.static import DEVICE_TYPES
from netports.types_ import T3Str, T5Str, LStr, SStr, DStr, LT2Str

SPLITTER = ",./:"

Expand All @@ -23,12 +24,12 @@ def __init__(self, line: str = "", **kwargs):
::
:param line: Interface name that can contain up to 4 indexes
:type line: str
:param platform: Platform like in Netmiko (default "")
:type platform: str
:param device_type: Netmiko device type (default "")
:type device_type: str
:param splitter: Separator of characters between indexes (default ",./:")
:type splitter: str
"""
self._platform = self._init_platform(**kwargs)
self._device_type = self._init_device_type(**kwargs)
self._splitter = str(kwargs.get("splitter") or SPLITTER)
self._line = self._init_line(line)

Expand Down Expand Up @@ -85,13 +86,17 @@ def _init_line(self, line: str) -> str:
return "".join(items)

@staticmethod
def _init_platform(**kwargs) -> str:
"""Init platform"""
platform = str(kwargs.get("platform") or "")
expected = ["", *PLATFORMS]
if platform not in expected:
raise ValueError(f"{platform=} {expected=}")
return platform
def _init_device_type(**kwargs) -> str:
"""Init Netmiko device type
::
:return: Netmiko device type
:raise: NetportsValueError if the device type is unknown
"""
device_type = str(kwargs.get("device_type") or "")
expected = ["", *DEVICE_TYPES]
if device_type not in expected:
raise NetportsValueError(f"{device_type=} {expected=}")
return device_type

# =========================== property ===========================

Expand Down Expand Up @@ -181,9 +186,9 @@ def name(self) -> str:
return re.sub(r"^interface\s+", "", self.line)

@property
def platform(self) -> str:
"""Platform"""
return self._platform
def device_type(self) -> str:
"""Netmiko device type"""
return self._device_type

@property
def splitter(self) -> str:
Expand Down Expand Up @@ -216,12 +221,12 @@ def all_names(self) -> LStr:
name_ = f"interface {name_}"
results.add(name_)

intf_map_upper: DStr = intf_map.short_to_long(self._platform)
intf_map_lower: DStr = intf_map.short_to_long(self._platform, key_lower=True)
intf_map_upper: DStr = intf_map.short_to_long(self._device_type)
intf_map_lower: DStr = intf_map.short_to_long(self._device_type, key_lower=True)
names: LStr = [self.name, self.name_short()]
names = h.no_dupl(names)
for name in names:
intf_o = Intf(line=name, platform=self.platform)
intf_o = Intf(line=name, device_type=self.device_type)
for id0_short, intf_map_d in [
(intf_o.id0, intf_map_upper),
(intf_o.id0.lower(), intf_map_lower),
Expand All @@ -236,7 +241,7 @@ def all_names(self) -> LStr:
results_: LStr = sorted(results)
results_.sort(key=len, reverse=True)

if self.platform == "hp_procurve":
if self.device_type == "hp_procurve":
if digits := [s for s in results if s.isdigit()]:
names = [f"interface 1/{s}" for s in digits]
results_.extend(names)
Expand All @@ -262,7 +267,6 @@ def name_full(self) -> str:
name = self.name_long()
return f"interface {name}"

# noinspection DuplicatedCode
def name_long(self) -> str:
"""Interface long name with IDs and without interface keyword
::
Expand All @@ -274,13 +278,13 @@ def name_long(self) -> str:
if id0.startswith("interface "):
id0 = id0.replace("interface ", "", 1)

intf_map_short: DStr = intf_map.short_to_long(self._platform, key_lower=True)
intf_map_short: DStr = intf_map.short_to_long(self._device_type, key_lower=True)
for short_lower, long_upper in intf_map_short.items():
if id0 == short_lower:
id0 = long_upper
break
else:
intf_map_long: DStr = intf_map.long_to_long(self._platform, key_lower=True)
intf_map_long: DStr = intf_map.long_to_long(self._device_type, key_lower=True)
for long_lower, long_upper in intf_map_long.items():
if id0 == long_lower:
id0 = long_upper
Expand All @@ -290,30 +294,39 @@ def name_long(self) -> str:
name = f"{id0}{id1}"
return name

# noinspection DuplicatedCode
def name_short(self) -> str:
def name_short(self, replace: LT2Str = None) -> str:
"""Interface short name with IDs
::
:param replace: Replace the default short name with the first one
that matches in the list of the 'replace' argument.
:return: Interface short name.
:example:
intf = Intf("interface FastEthernet1/2")
intf.name_short() -> "Fa1/2"
intf.name_short(replace=[("Fa", "Eth")]) -> "Eth1/2"
"""
id0 = self.id0.lower()
if id0.startswith("interface "):
id0 = id0.replace("interface ", "", 1)

intf_map_l2s: DStr = intf_map.long_to_short(self._platform, key_lower=True)
intf_map_l2s: DStr = intf_map.long_to_short(self._device_type, key_lower=True)
for long_lower, short_upper in intf_map_l2s.items():
if id0 == long_lower:
id0 = short_upper
break
else:
intf_map_s2s: DStr = intf_map.short_to_short(self._platform, key_lower=True)
intf_map_s2s: DStr = intf_map.short_to_short(self._device_type, key_lower=True)
for short_lower, short_upper in intf_map_s2s.items():
if id0 == short_lower:
id0 = short_upper
break

if replace:
for before, after in replace:
if id0 == before:
id0 = after
break

id1 = self.part_after(idx=0)
name = f"{id0}{id1}"
return name
Expand Down
Loading

0 comments on commit 2292b76

Please sign in to comment.