| 1 |
"""Generate mock airos fixtures for testing.""" |
| 2 |
|
| 3 |
import inspect |
| 4 |
import json |
| 5 |
import logging |
| 6 |
import os |
| 7 |
import sys |
| 8 |
|
| 9 |
_LOGGER = logging.getLogger(__name__) |
| 10 |
|
| 11 |
current_script_dir = os.path.dirname(os.path.abspath(__file__)) # noqa: PTH100, PTH120 |
| 12 |
project_root_dir = os.path.abspath(os.path.join(current_script_dir, os.pardir)) # noqa: PTH100, PTH118 |
| 13 |
|
| 14 |
if project_root_dir not in sys.path: |
| 15 |
sys.path.append(project_root_dir) |
| 16 |
|
| 17 |
# NOTE: This assumes the airos module is correctly installed or available in the project path. |
| 18 |
# If not, you might need to adjust the import statement. |
| 19 |
from airos.airos6 import AirOS6 # noqa: E402 |
| 20 |
from airos.airos8 import AirOS8 # noqa: E402 |
| 21 |
from airos.data import AirOS6Data, AirOS8Data # noqa: E402 |
| 22 |
|
| 23 |
|
| 24 |
def generate_airos_fixtures() -> None: |
| 25 |
"""Process all (intended) JSON files from the userdata directory to potential fixtures.""" |
| 26 |
print(f"Loading AirOS6 from: {inspect.getfile(AirOS6)}") |
| 27 |
|
| 28 |
# Define the paths to the directories |
| 29 |
fixture_dir = os.path.join(os.path.dirname(__file__), "../fixtures") # noqa: PTH118, PTH120 |
| 30 |
userdata_dir = os.path.join(os.path.dirname(__file__), "../fixtures/userdata") # noqa: PTH118, PTH120 |
| 31 |
|
| 32 |
# Ensure the fixture directory exists |
| 33 |
os.makedirs(fixture_dir, exist_ok=True) # noqa: PTH103 |
| 34 |
|
| 35 |
# Iterate over all files in the userdata_dir |
| 36 |
for filename in os.listdir(userdata_dir): # noqa: PTH208 |
| 37 |
if filename != "mocked_sta-ptmp.json" and "mocked" in filename: |
| 38 |
continue |
| 39 |
if filename.endswith(".json"): |
| 40 |
# Construct the full paths for the base and new fixtures |
| 41 |
base_fixture_path = os.path.join(userdata_dir, filename) # noqa: PTH118 |
| 42 |
new_filename = f"airos_{filename}" |
| 43 |
new_fixture_path = os.path.join(fixture_dir, new_filename) # noqa: PTH118 |
| 44 |
|
| 45 |
_LOGGER.info("Processing '%s'...", filename) |
| 46 |
|
| 47 |
try: |
| 48 |
with open(base_fixture_path, encoding="utf-8") as source: # noqa: PTH123 |
| 49 |
source_data = json.loads(source.read()) |
| 50 |
|
| 51 |
fwversion = (source_data.get("host") or {}).get("fwversion") |
| 52 |
if not fwversion: |
| 53 |
_LOGGER.error( |
| 54 |
"Unable to determine firmware version in '%s' (missing host.fwversion)", |
| 55 |
filename, |
| 56 |
) |
| 57 |
raise ValueError("fwversion missing") from None # noqa: TRY301 |
| 58 |
|
| 59 |
try: |
| 60 |
fw_major = int(fwversion.lstrip("v").split(".", 1)[0]) |
| 61 |
except (ValueError, AttributeError) as exc: |
| 62 |
_LOGGER.error( |
| 63 |
"Invalid firmware version '%s' in '%s'", fwversion, filename |
| 64 |
) |
| 65 |
raise ValueError("invalid fwversion") from exc |
| 66 |
|
| 67 |
new_data: AirOS6Data | AirOS8Data |
| 68 |
|
| 69 |
if fw_major == 6: |
| 70 |
derived_data = AirOS6.derived_data(source_data) |
| 71 |
new_data = AirOS6Data.from_dict(derived_data) |
| 72 |
else: |
| 73 |
derived_data = AirOS8.derived_data(source_data) |
| 74 |
new_data = AirOS8Data.from_dict(derived_data) |
| 75 |
|
| 76 |
with open(new_fixture_path, "w", encoding="utf-8") as new: # noqa: PTH123 |
| 77 |
json.dump(new_data.to_dict(), new, indent=2, sort_keys=True) |
| 78 |
|
| 79 |
_LOGGER.info("Successfully created '%s'", new_filename) |
| 80 |
|
| 81 |
except json.JSONDecodeError: |
| 82 |
_LOGGER.error("Skipping '%s': Not a valid JSON file.", filename) |
| 83 |
raise |
| 84 |
except Exception as e: |
| 85 |
_LOGGER.error("Error processing '%s': %s", filename, e) |
| 86 |
raise |
| 87 |
|
| 88 |
|
| 89 |
if __name__ == "__main__": |
| 90 |
logging.basicConfig( |
| 91 |
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" |
| 92 |
) |
| 93 |
generate_airos_fixtures() |