Skip to content

Commit 7e4fadf

Browse files
committed
lint and test improvements
1 parent 8db0383 commit 7e4fadf

File tree

8 files changed

+78
-45
lines changed

8 files changed

+78
-45
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
- If you pass it some weird bytes that aren't decodable as utf-8, it's obviously going to break. Don't be dumb :)
2020

2121
### Changed
22-
* Reduce size of wheel file (`.whl`)
22+
* BREAKING CHANGE: refactored how settings are handled. Instead of module-level globals, they're implemented in a Settings singleton in `getmac.settings`. For example, `getmac.getmac.PORT` should now be `getmac.settings.PORT`.
23+
* Reduce size of wheel distribution (`.whl` file)
2324

2425
### Removed
2526
* Removed support for Python 2.7. It's time. Supporting 2.7 has become an onerous burden on the project, and has prevented fully supporting 3.10 and 3.11 due to having to stay on an ancient version of pytest. I'm done supporting a version of the language that hasn't been updated in nearly 15 years, and has been wholly unsupported for over 5 years.
@@ -36,6 +37,8 @@
3637
* Add `pyproject.toml`, consolidated most tool configurations here
3738
* Removed `setup.py`, `MANIFEST.in`, `requirements*.txt`, `tox.ini`
3839
* Switched to CodeCov from Coveralls
40+
* Use [isort](https://pycqa.github.io/isort/index.html) to sort imports
41+
* Refactored source code documentation and added an API reference to the docs
3942

4043
## 0.9.5 (07/15/2024)
4144

docs/misc_docs/TODO.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
- [ ] Re-add Man pages (and auto-build them in CI and include in releases and the distributions)
1111
- [x] Document `get_by_method()`
1212
- [x] Document `initialize_method_cache()`
13-
- [ ] Auto-generated API docs
13+
- [x] Auto-generated API docs
1414
- [x] Add docstrings to all util methods
1515
- [x] Furo, sphinx-autodoc-typehints, sphinx-argparse-cli, sphinx-automodapi, sphinx-copybutton, recommonmark
1616

@@ -19,7 +19,7 @@
1919
- [ ] >90% test coverage
2020
- refactor tests to use the new system and structure
2121
- directly test methods via a `Method.parse()` function
22-
- add `Method.parse()` that handles the parsing of command
22+
- add `Method.parse()` that handles the parsing of command
2323
- [ ] Improve CLI tests to ensure output is what's expected (e.g. ensure `--override-port` logs a warning and the value actually gets overridden)
2424

2525
## Features
@@ -43,7 +43,7 @@
4343
- Passing an instance of a subclass of `getmac.Method`
4444
- Add a CLI argument to reference class by name
4545
- Add ability to exclude methods. Just remove them from METHODS list so they never get used. Useful for testing specific methods or working around buggy methods.
46-
- Document these features in the README/docs, including the CLI arguments
46+
- Document these features in the README/docs, including the CLI arguments
4747

4848
## Enhancements/fixes/misc.
4949
- [ ] Properly support WSL2
@@ -92,9 +92,9 @@ Removing Tox, maybe. Can do version enumeration in GitHub Actions, for all platf
9292

9393
# Etc
9494
- [ ] Add to Conda Forge ([example here](https://github.com/conda-forge/staged-recipes/pull/26828/files))
95-
- [ ] Add [isort](https://pycqa.github.io/isort/) (requires python 3.8+)
95+
- [x] Add [isort](https://pycqa.github.io/isort/) (requires python 3.8+)
9696
- [ ] cache the result of executable checks in `getmac.utils.popen()`
97-
- [ ] Refactor the default interface code. Combine the functions into
97+
- [ ] Refactor the default interface code. Combine the functions into
9898
one, move the default fallback logic into the function.
9999
- TODO: MAC -> IP. "to_find='mac'"? (create GitHub issue?)
100100
- [ ] Move method classes into a separate file

getmac/getmac.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,35 +1763,35 @@ def get_mac_address(
17631763
mac = get_by_method("ip4", ip)
17641764
elif interface:
17651765
mac = get_by_method("iface", interface)
1766-
else: # Default to searching for interface
1766+
# === Default to searching for interface ===
1767+
elif consts.WINDOWS and network_request:
17671768
# Default to finding MAC of the interface with the default route
1768-
if consts.WINDOWS and network_request:
1769-
default_iface_ip = utils.fetch_ip_using_dns()
1770-
mac = get_by_method("ip4", default_iface_ip)
1771-
elif consts.WINDOWS:
1772-
# TODO: implement proper default interface detection on windows
1773-
# (add a Method subclass to implement DefaultIface on Windows)
1774-
mac = get_by_method("iface", "Ethernet")
1775-
else:
1776-
if not gvars.DEFAULT_IFACE:
1777-
gvars.DEFAULT_IFACE = get_by_method("default_iface") # type: ignore
1769+
default_iface_ip = utils.fetch_ip_using_dns()
1770+
mac = get_by_method("ip4", default_iface_ip)
1771+
elif consts.WINDOWS:
1772+
# TODO: implement proper default interface detection on windows
1773+
# (add a Method subclass to implement DefaultIface on Windows)
1774+
mac = get_by_method("iface", "Ethernet")
1775+
else:
1776+
if not gvars.DEFAULT_IFACE:
1777+
gvars.DEFAULT_IFACE = get_by_method("default_iface") # type: ignore
17781778

1779-
if gvars.DEFAULT_IFACE:
1780-
gvars.DEFAULT_IFACE = str(gvars.DEFAULT_IFACE).strip()
1779+
if gvars.DEFAULT_IFACE:
1780+
gvars.DEFAULT_IFACE = str(gvars.DEFAULT_IFACE).strip()
17811781

1782-
# TODO: better fallback if default iface lookup fails
1783-
if not gvars.DEFAULT_IFACE and consts.BSD:
1784-
gvars.DEFAULT_IFACE = "em0"
1785-
elif not gvars.DEFAULT_IFACE and consts.DARWIN: # OSX, maybe?
1786-
gvars.DEFAULT_IFACE = "en0"
1787-
elif not gvars.DEFAULT_IFACE:
1788-
gvars.DEFAULT_IFACE = "eth0"
1782+
# TODO: better fallback if default iface lookup fails
1783+
if not gvars.DEFAULT_IFACE and consts.BSD:
1784+
gvars.DEFAULT_IFACE = "em0"
1785+
elif not gvars.DEFAULT_IFACE and consts.DARWIN: # OSX, maybe?
1786+
gvars.DEFAULT_IFACE = "en0"
1787+
elif not gvars.DEFAULT_IFACE:
1788+
gvars.DEFAULT_IFACE = "eth0"
17891789

1790-
mac = get_by_method("iface", gvars.DEFAULT_IFACE)
1790+
mac = get_by_method("iface", gvars.DEFAULT_IFACE)
17911791

1792-
# TODO: hack to fallback to loopback if lookup fails
1793-
if not mac:
1794-
mac = get_by_method("iface", "lo")
1792+
# TODO: hack to fallback to loopback if lookup fails
1793+
if not mac:
1794+
mac = get_by_method("iface", "lo")
17951795

17961796
gvars.log.debug(f"Raw MAC found: {mac}")
17971797

getmac/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def uuid_convert(mac: int) -> str:
246246

247247
def fetch_ip_using_dns() -> str:
248248
"""
249-
Determines the IP address of the default network interface.
249+
Determine the IP address of the default network interface.
250250
251251
Sends a UDP packet to Cloudflare's DNS (``1.1.1.1``), which should go through
252252
the default interface. This populates the source address of the socket,

getmac/variables.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ class Constants(VarsClass):
156156
class Variables(VarsClass):
157157
"""
158158
Things that can change.
159+
159160
Essentially most of the global variables in getmac.
160161
"""
161162

pdm.lock

Lines changed: 27 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ test = [
8686
"pytest-benchmark",
8787
"pytest-cov",
8888
"pytest-xdist",
89+
"pytest-sugar",
8990
"psutil",
9091
"pygments<3.0,>=2.13",
91-
# TODO: pytest-sugar
9292
]
9393
lint = [
9494
"codespell>=2.2.4",
@@ -234,10 +234,6 @@ extend-exclude = [
234234
# If you're still confused, ask a fellow developer for assistance.
235235
# You can also run "ruff rule <rule>" to explain a rule on the command line, without a browser or internet access.
236236
select = [
237-
"F", # Pyflakes
238-
"E", # pycodestyle
239-
"W", # Warning
240-
"N", # pep8-naming
241237
"ANN", # flake8-annotations
242238
"S", # flake8-bandit
243239
"B", # flake8-bugbear
@@ -254,11 +250,18 @@ select = [
254250
"RET", # flake8-return
255251
"TID", # flake8-tidy-imports
256252
"ARG", # flake8-unused-arguments
253+
"N", # pep8-naming
254+
"E", # pycodestyle - Error
255+
"W", # pycodestyle - Warning
256+
"F", # Pyflakes
257257
"PGH", # pygrep-hooks
258-
"PLC", # Pylint Convention
259-
"PLE", # Pylint Errors
260-
"PLW", # Pylint Warnings
258+
"PLC", # Pylint - Convention
259+
"PLE", # Pylint - Errors
260+
"PLR", # Pylint - Refactor
261+
"PLW", # Pylint - Warnings
262+
# "UP", # pyupgrade
261263
"RUF", # Ruff-specific rules
264+
262265
]
263266

264267
# Linting error codes to ignore
@@ -273,10 +276,13 @@ ignore = [
273276
"RUF012", # Mutable class attributes should be annotated with typing.ClassVar
274277
"S603", # subprocess-without-shell-equals-true
275278
"PLC0415", # import should be at the top-level of a file (getmac uses imports in functions)
279+
"PLR0915",
280+
"PLR0912",
281+
"PLR2004",
276282
]
277283

278284
[tool.ruff.lint.per-file-ignores]
279-
"tests/*.py" = ["ANN001", "ANN002", "ANN003", "ANN201", "ANN202", "S101", "S104", "S603"]
285+
"tests/*.py" = ["ANN001", "ANN002", "ANN003", "ANN201", "ANN202", "S101", "S104", "S603", "D", "PLR"]
280286

281287
[tool.isort]
282288
profile = "black"

tests/conftest.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import io
21
from os import path
32

43
import pytest
@@ -12,9 +11,7 @@
1211
def get_sample():
1312
def _get_sample(sample_path):
1413
sdir = path.realpath(path.join(path.dirname(__file__), "samples"))
15-
with io.open(
16-
path.join(sdir, sample_path), "rt", newline="", encoding="utf-8"
17-
) as f:
14+
with open(path.join(sdir, sample_path), newline="", encoding="utf-8") as f:
1815
return f.read()
1916

2017
return _get_sample

0 commit comments

Comments
 (0)