Skip to content

Commit

Permalink
move get_mac() to util
Browse files Browse the repository at this point in the history
  • Loading branch information
bimac committed Aug 8, 2024
1 parent 8343532 commit 9a8d57a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 23 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.12.0]
## [Latest](https://github.com/int-brain-lab/iblutil/commits/main) [1.12.1]

### Modified

- Moved get_mac() to util

## [1.12.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.12.0'
__version__ = '1.12.1'
15 changes: 0 additions & 15 deletions iblutil/io/net/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import re
import json
import socket
import uuid
import warnings
import logging
from asyncio import isfuture
Expand Down Expand Up @@ -37,20 +36,6 @@ 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
15 changes: 15 additions & 0 deletions iblutil/util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import uuid
from itertools import takewhile
from os import scandir
from pathlib import Path
Expand Down Expand Up @@ -283,3 +284,17 @@ def dir_size(directory: Union[str, Path], follow_symlinks: bool = False) -> int:
elif entry.is_file():
total_bytes += entry.stat().st_size
return total_bytes


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()
6 changes: 0 additions & 6 deletions tests/test_net.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from unittest.mock import patch
import sys
import asyncio
import logging
Expand All @@ -10,7 +9,6 @@
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 @@ -51,10 +49,6 @@ 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
8 changes: 8 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path
import tempfile
import logging
from unittest.mock import patch

import numpy as np

Expand Down Expand Up @@ -202,5 +203,12 @@ def test_dir_size(self):
self.assertEqual(util.dir_size(dir1, True), expected + file1.stat().st_size)


class TestGetMac(unittest.TestCase):

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


if __name__ == '__main__':
unittest.main(exit=False)

0 comments on commit 9a8d57a

Please sign in to comment.