Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: first version #1

Merged
merged 7 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,12 @@ jobs:
fail-fast: false
matrix:
python-version:
- "3.8"
- "3.9"
- "3.10"
- "3.11"
- "3.12"
os:
- ubuntu-latest
- windows-latest
- macOS-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
Expand Down
4 changes: 0 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ repos:
rev: 24.2.0
hooks:
- id: black
- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
hooks:
- id: codespell
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
hooks:
Expand Down
12 changes: 12 additions & 0 deletions build_oui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""Build oui table."""

from __future__ import annotations

from typing import Any

from generate_oui_data import generate


def build(setup_kwargs: dict[str, Any]) -> None:
"""Build the OUI data."""
generate()
25 changes: 25 additions & 0 deletions generate_oui_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Build oui table."""

import pathlib

import requests # type: ignore


def generate() -> None:
"""Generate the OUI data."""
resp = requests.get("https://standards-oui.ieee.org/oui.txt", timeout=10)
resp.raise_for_status()
oui_bytes = resp.content
oui_to_vendor = {}
for line in oui_bytes.splitlines():
if b"(base 16)" in line:
oui, _, vendor = line.partition(b"(base 16)")
oui_to_vendor[oui.strip()] = vendor.strip()
file = pathlib.Path(__file__)
target_file = file.parent.joinpath("src").joinpath("aiooui").joinpath("oui.data")
with open(target_file, "wb") as f:
f.write(b"\n".join(b"=".join((o, v)) for o, v in oui_to_vendor.items()))


if __name__ == "__main__":
generate()
181 changes: 179 additions & 2 deletions poetry.lock

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ packages = [
"Changelog" = "https://github.com/bluetooth-devices/aiooui/blob/main/CHANGELOG.md"

[tool.poetry.dependencies]
python = "^3.8"
python = "^3.9"

[tool.poetry.group.dev.dependencies]
pytest = "^7.0"
pytest-cov = "^3.0"
pytest-asyncio = "^0.23.5"
requests = "^2.31.0"

[tool.semantic_release]
version_toml = ["pyproject.toml:tool.poetry.version"]
Expand Down Expand Up @@ -125,5 +127,9 @@ module = "tests.*"
allow_untyped_defs = true

[build-system]
requires = ["poetry-core>=1.0.0"]
requires = ["setuptools>=65.4.1", "poetry-core>=1.0.0", 'requests']
build-backend = "poetry.core.masonry.api"

[tool.poetry.build]
generate-setup-file = true
script = "build_oui.py"
9 changes: 0 additions & 9 deletions setup.py

This file was deleted.

72 changes: 72 additions & 0 deletions src/aiooui/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,73 @@
from __future__ import annotations

__version__ = "0.0.0"

import asyncio
import pathlib

_OUI_DATA_FILE = pathlib.Path(__file__).parent.joinpath("oui.data")


class OUIManager:
"""Manages the OUI data."""

def __init__(self) -> None:
"""Initialize the OUIManager."""
self._oui_to_vendor: dict[str, str] = {}
self._load_future: asyncio.Future[None] | None = None

def get_vendor(self, mac: str) -> str | None:
"""Get the vendor for a MAC address."""
if not self._oui_to_vendor:
raise RuntimeError("OUI data not loaded, call async_load first")
return self._oui_to_vendor.get(mac.replace(":", "")[:6].upper())

async def async_load(self) -> None:
"""Load the OUI data."""
if self._oui_to_vendor:
return
if self._load_future:
await self._load_future
return
loop = asyncio.get_running_loop()
self._load_future = loop.create_future()
try:
self._oui_to_vendor = await loop.run_in_executor(None, self._load_oui_data)
except Exception as err:
self._load_future.set_exception(err)
raise
else:
self._load_future.set_result(None)
finally:
self._load_future = None

def _load_oui_data(self) -> dict[str, str]:
"""Load the OUI data."""
with open(_OUI_DATA_FILE) as f:
oui_to_vendor: dict[str, str] = {}
for line in f.read().splitlines():
oui, _, vendor = line.partition("=")
oui_to_vendor[oui] = vendor

return oui_to_vendor


_OUI_MANAGER = OUIManager()


def is_loaded() -> bool:
"""Return if the OUI data is loaded."""
return bool(_OUI_MANAGER._oui_to_vendor)


def get_vendor(mac: str) -> str | None:
"""Get the vendor for a MAC address."""
return _OUI_MANAGER.get_vendor(mac)


async def async_load() -> None:
"""Load the OUI data."""
await _OUI_MANAGER.async_load()


__all__ = ["async_load", "get_vendor", "is_loaded"]
3 changes: 0 additions & 3 deletions src/aiooui/main.py

This file was deleted.

Loading