Skip to content

Commit 82c2800

Browse files
Add interface resolver
1 parent d3d6db8 commit 82c2800

11 files changed

Lines changed: 259 additions & 24 deletions

File tree

.gitignore

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,4 @@ dmypy.json
153153
# Cython debug symbols
154154
cython_debug/
155155

156-
# PyCharm
157-
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
158-
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
159-
# and can be added to the global gitignore or merged into this file. For a more nuclear
160-
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
161-
#.idea/
156+
tests/test_root

.idea/.gitignore

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/python-common-utility.iml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common_utility/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
from .fileDownloader import *
55
from .configLoader import *
66
from .rateLimiter import *
7+
from .interfaceResolver import *
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from enum import Enum
2+
3+
import netifaces
4+
from context_logger import get_logger
5+
6+
log = get_logger('InterfaceResolver')
7+
8+
9+
class AddressFamily(Enum):
10+
IPv4 = netifaces.AF_INET
11+
IPv6 = netifaces.AF_INET6
12+
MAC = netifaces.AF_LINK
13+
14+
15+
class IInterfaceResolver:
16+
17+
def resolve(self, interface: str, family: AddressFamily = AddressFamily.IPv4) -> str | None:
18+
raise NotImplementedError()
19+
20+
21+
class InterfaceResolver(IInterfaceResolver):
22+
23+
def resolve(self, interface: str, family: AddressFamily = AddressFamily.IPv4) -> str | None:
24+
interfaces = netifaces.interfaces()
25+
if interface in interfaces:
26+
inet_address = netifaces.ifaddresses(interface).get(family.value)
27+
if inet_address and len(inet_address) > 0:
28+
if address := inet_address[0].get('addr'):
29+
return address
30+
else:
31+
log.error('Address not found for interface', interface=interface, family=family.name)
32+
else:
33+
log.error('Address family not found for interface', interface=interface, family=family.name)
34+
else:
35+
log.error('Selected interface not found', interface=interface, interfaces=interfaces)
36+
37+
return None

pyproject.toml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[project]
2+
name = "python-common-utility"
3+
description = "Common utility packages for Python projects"
4+
authors = [
5+
{ name = "Ferenc Nandor Janky & Attila Gombos", email = "info@effective-range.com" }
6+
]
7+
maintainers = [
8+
{ name = "Ferenc Nandor Janky & Attila Gombos", email = "info@effective-range.com" }
9+
]
10+
dependencies = [
11+
"requests",
12+
"pydantic",
13+
"jinja2",
14+
"netifaces",
15+
"python-context-logger @ git+https://github.com/EffectiveRange/python-context-logger.git@latest",
16+
]
17+
dynamic = ["version"]
18+
19+
[tool.setuptools]
20+
package-dir = {"" = "."}
21+
packages = ["common_utility", "test_utility"]
22+
23+
[tool.setuptools.package-data]
24+
hello = ["py.typed"]
25+
26+
[build-system]
27+
requires = ["setuptools>=61", "setuptools_scm"]
28+
build-backend = "setuptools.build_meta"
29+
30+
[tool.setuptools_scm]
31+
version_scheme = "guess-next-dev"
32+
local_scheme = "node-and-date"
33+
34+
[tool.pytest]
35+
addopts = ["--verbose", "--capture=no"]
36+
python_files = ["*Test.py"]
37+
python_classes = ["*Test"]

setup.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)