Skip to content

Commit

Permalink
feat: get_region (#32)
Browse files Browse the repository at this point in the history
Port logic from establishing a device's region from hm-pktfwd.
  • Loading branch information
marvinmarnold authored Oct 18, 2021
1 parent 9240c3b commit 2029e19
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ logger = get_logger(__name__)
logger.debug("message to log")
```

## miner_param

### get_region
Return the region from envvar REGION_OVERRIDE or
from the contents of /var/pktfwd/region

```python
from hm_pyhelper.miner_param import get_region
print(get_region())

> US9155
```

## Testing

To run tests:
Expand Down
37 changes: 37 additions & 0 deletions hm_pyhelper/miner_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
import subprocess
import logging
import json
from retry import retry
from hm_pyhelper.utils.logger import get_logger

logger = get_logger(__name__)


def log_stdout_stderr(sp_result):
Expand Down Expand Up @@ -173,3 +177,36 @@ def get_mac_address(path):
except PermissionError as e:
raise e
return file.readline().strip().upper()


REGION_OVERRIDE_KEY = 'REGION_OVERRIDE'
REGION_FILEPATH = '/var/pktfwd/region'
REGION_INVALID_SLEEP_SECONDS = 30
REGION_FILE_MISSING_SLEEP_SECONDS = 60


class MalformedRegionException(Exception):
pass


@retry(MalformedRegionException, delay=REGION_INVALID_SLEEP_SECONDS, logger=logger) # noqa
@retry(FileNotFoundError, delay=REGION_FILE_MISSING_SLEEP_SECONDS, logger=logger) # noqa
def get_region():
"""
Return the region from the environment or parse file created by hm-miner.
Retry if region in file is malformed or not found.
"""
region = os.getenv(REGION_OVERRIDE_KEY, False)
if region:
return region

logger.debug("No REGION_OVERRIDE defined, will retrieve from miner.")
with open(REGION_FILEPATH) as region_file:
region = region_file.read().rstrip('\n')
logger.debug("Region %s parsed from %s " % (region, REGION_FILEPATH))

is_region_valid = len(region) > 3
if is_region_valid:
return region

raise MalformedRegionException("Region %s is invalid" % region)
15 changes: 15 additions & 0 deletions hm_pyhelper/tests/test_get_region.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from hm_pyhelper.miner_param import get_region, REGION_OVERRIDE_KEY
import unittest
from unittest.mock import mock_open, patch
import os


class TestGetRegion(unittest.TestCase):
def test_get_region_from_override(self):
os.environ[REGION_OVERRIDE_KEY] = 'foo'
self.assertEqual(get_region(), 'foo')

@patch("builtins.open", new_callable=mock_open, read_data="ZZ111\n")
def test_get_region_from_miner(self, _):
os.environ[REGION_OVERRIDE_KEY] = ''
self.assertEqual(get_region(), 'ZZ111')
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
jsonrpcclient==3.3.6
requests==2.26.0
retry==0.9.2
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='hm_pyhelper',
version='0.8.1',
version='0.8.2',
author="Nebra Ltd",
author_email="[email protected]",
description="Helium Python Helper",
Expand Down

0 comments on commit 2029e19

Please sign in to comment.