|
1 | | -"""Generate mock airos fixture for testing.""" |
| 1 | +"""Generate mock airos fixtures for testing.""" |
2 | 2 |
|
3 | 3 | import json |
4 | 4 | import logging |
|
13 | 13 | if project_root_dir not in sys.path: |
14 | 14 | sys.path.append(project_root_dir) |
15 | 15 |
|
| 16 | +# NOTE: This assumes the airos module is correctly installed or available in the project path. |
| 17 | +# If not, you might need to adjust the import statement. |
16 | 18 | from airos.airos8 import AirOS, AirOSData # noqa: E402 |
17 | 19 |
|
18 | | -# Define the path to save the fixture |
19 | | -fixture_dir = os.path.join(os.path.dirname(__file__), "../fixtures") |
20 | | -userdata_dir = os.path.join(os.path.dirname(__file__), "../fixtures/userdata") |
21 | | -new_fixture_path = os.path.join(fixture_dir, "airos_loco5ac_ap-ptp.json") |
22 | | -base_fixture_path = os.path.join(userdata_dir, "loco5ac_ap-ptp.json") |
23 | | - |
24 | | -with open(base_fixture_path) as source, open(new_fixture_path, "w") as new: |
25 | | - source_data = json.loads(source.read()) |
26 | | - derived_data = AirOS.derived_data(None, source_data) |
27 | | - new_data = AirOSData.from_dict(derived_data) |
28 | | - json.dump(new_data.to_dict(), new, indent=2, sort_keys=True) |
29 | | - |
30 | | -new_fixture_path = os.path.join(fixture_dir, "airos_loco5ac_sta-ptp.json") |
31 | | -base_fixture_path = os.path.join(userdata_dir, "loco5ac_sta-ptp.json") |
32 | | - |
33 | | -with open(base_fixture_path) as source, open(new_fixture_path, "w") as new: |
34 | | - source_data = json.loads(source.read()) |
35 | | - derived_data = AirOS.derived_data(None, source_data) |
36 | | - new_data = AirOSData.from_dict(derived_data) |
37 | | - json.dump(new_data.to_dict(), new, indent=2, sort_keys=True) |
38 | | - |
39 | | -new_fixture_path = os.path.join(fixture_dir, "airos_mocked_sta-ptmp.json") |
40 | | -base_fixture_path = os.path.join(userdata_dir, "mocked_sta-ptmp.json") |
41 | | - |
42 | | -with open(base_fixture_path) as source, open(new_fixture_path, "w") as new: |
43 | | - source_data = json.loads(source.read()) |
44 | | - derived_data = AirOS.derived_data(None, source_data) |
45 | | - new_data = AirOSData.from_dict(derived_data) |
46 | | - json.dump(new_data.to_dict(), new, indent=2, sort_keys=True) |
47 | | - |
48 | | -new_fixture_path = os.path.join(fixture_dir, "airos_liteapgps_ap_ptmp_40mhz.json") |
49 | | -base_fixture_path = os.path.join(userdata_dir, "liteapgps_ap_ptmp_40mhz.json") |
50 | | - |
51 | | -with open(base_fixture_path) as source, open(new_fixture_path, "w") as new: |
52 | | - source_data = json.loads(source.read()) |
53 | | - derived_data = AirOS.derived_data(None, source_data) |
54 | | - new_data = AirOSData.from_dict(derived_data) |
55 | | - json.dump(new_data.to_dict(), new, indent=2, sort_keys=True) |
56 | | - |
57 | | -new_fixture_path = os.path.join(fixture_dir, "airos_nanobeam5ac_sta_ptmp_40mhz.json") |
58 | | -base_fixture_path = os.path.join(userdata_dir, "nanobeam5ac_sta_ptmp_40mhz.json") |
59 | | - |
60 | | -with open(base_fixture_path) as source, open(new_fixture_path, "w") as new: |
61 | | - source_data = json.loads(source.read()) |
62 | | - derived_data = AirOS.derived_data(None, source_data) |
63 | | - new_data = AirOSData.from_dict(derived_data) |
64 | | - json.dump(new_data.to_dict(), new, indent=2, sort_keys=True) |
| 20 | + |
| 21 | +def generate_airos_fixtures(): |
| 22 | + """Process all (intended) JSON files from the userdata directory to potential fixtures.""" |
| 23 | + |
| 24 | + # Define the paths to the directories |
| 25 | + fixture_dir = os.path.join(os.path.dirname(__file__), "../fixtures") |
| 26 | + userdata_dir = os.path.join(os.path.dirname(__file__), "../fixtures/userdata") |
| 27 | + |
| 28 | + # Ensure the fixture directory exists |
| 29 | + os.makedirs(fixture_dir, exist_ok=True) |
| 30 | + |
| 31 | + # Iterate over all files in the userdata_dir |
| 32 | + for filename in os.listdir(userdata_dir): |
| 33 | + if "mocked" in filename: |
| 34 | + continue |
| 35 | + if filename.endswith(".json"): |
| 36 | + # Construct the full paths for the base and new fixtures |
| 37 | + base_fixture_path = os.path.join(userdata_dir, filename) |
| 38 | + new_filename = f"airos_{filename}" |
| 39 | + new_fixture_path = os.path.join(fixture_dir, new_filename) |
| 40 | + |
| 41 | + _LOGGER.info("Processing '%s'...", filename) |
| 42 | + |
| 43 | + try: |
| 44 | + with open(base_fixture_path) as source: |
| 45 | + source_data = json.loads(source.read()) |
| 46 | + |
| 47 | + derived_data = AirOS.derived_data(None, source_data) |
| 48 | + new_data = AirOSData.from_dict(derived_data) |
| 49 | + |
| 50 | + with open(new_fixture_path, "w") as new: |
| 51 | + json.dump(new_data.to_dict(), new, indent=2, sort_keys=True) |
| 52 | + |
| 53 | + _LOGGER.info("Successfully created '%s'", new_filename) |
| 54 | + |
| 55 | + except json.JSONDecodeError: |
| 56 | + _LOGGER.error("Skipping '%s': Not a valid JSON file.", filename) |
| 57 | + except Exception as e: |
| 58 | + _LOGGER.error("Error processing '%s': %s", filename, e) |
| 59 | + |
| 60 | + |
| 61 | +if __name__ == "__main__": |
| 62 | + logging.basicConfig( |
| 63 | + level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" |
| 64 | + ) |
| 65 | + generate_airos_fixtures() |
0 commit comments