Skip to content

Commit

Permalink
Merge pull request #30 from int-brain-lab/feat/mac
Browse files Browse the repository at this point in the history
add get_mac()
  • Loading branch information
bimac authored Aug 8, 2024
2 parents aa60cbe + 8343532 commit fba64dd
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
* Minor releases (X.1.X) are new features such as added functions or small changes that don't cause major compatibility issues.
* Major releases (1.X.X) are major new features or changes that break backward compatibility in a big way.

## [Latest](https://github.com/int-brain-lab/iblutil/commits/main) [1.11.0]
## [Latest](https://github.com/int-brain-lab/iblutil/commits/main) [1.12.0]

### Added

- io.net.base.get_mac: function returning the machine's unique MAC address formatted according to IEEE 802 specifications.

## [1.11.0]

### Added

Expand Down
2 changes: 1 addition & 1 deletion iblutil/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.11.0'
__version__ = '1.12.0'
15 changes: 15 additions & 0 deletions iblutil/io/net/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re
import json
import socket
import uuid
import warnings
import logging
from asyncio import isfuture
Expand Down Expand Up @@ -36,6 +37,20 @@ def external_ip():
return ipaddress.ip_address(urllib.request.urlopen('https://ident.me').read().decode('utf8'))


def get_mac() -> str:
"""
Fetch the machine's unique MAC address formatted according to IEEE 802 specifications.
Returns
-------
str
The MAC address of the device formatted in six groups of two
hexadecimal digits separated by hyphens in transmission order
(e.g., 'BA-DB-AD-C0-FF-EE').
"""
return uuid.getnode().to_bytes(6, 'big').hex('-').upper()


def is_valid_ip(ip_address) -> bool:
"""
Test whether IP address is valid.
Expand Down
6 changes: 6 additions & 0 deletions tests/test_net.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from unittest.mock import patch
import sys
import asyncio
import logging
Expand All @@ -9,6 +10,7 @@
from datetime import date

from iblutil.io.net import base, app
from iblutil.io.net.base import get_mac

ver = (getattr(sys.version_info, v) for v in ('major', 'minor', 'micro'))
ver = Version('.'.join(map(str, ver)))
Expand Down Expand Up @@ -49,6 +51,10 @@ def test_external_ip(self):
"""Test for external_ip"""
self.assertFalse(ipaddress.ip_address(base.external_ip()).is_private)

def test_get_mac(self):
with patch('iblutil.io.net.base.uuid.getnode', return_value=205452675710958):
self.assertEqual(get_mac(), 'BA-DB-AD-C0-FF-EE')

def test_ExpMessage(self):
"""Test for ExpMessage.validate method."""
# Check identity
Expand Down

0 comments on commit fba64dd

Please sign in to comment.