8000 Enable saving of fixture files without git clone (#1375) · python-kasa/python-kasa@5918e4d · GitHub
[go: up one dir, main page]

Skip to content

Commit 5918e4d

Browse files
authored
Enable saving of fixture files without git clone (#1375)
Allows `dump_devinfo` to be run without fixture subfolders present from cloned repository
1 parent d03a387 commit 5918e4d

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

devtools/dump_devinfo.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,20 @@
5757
from kasa.smartcam import SmartCamDevice
5858

5959
Call = namedtuple("Call", "module method")
60-
FixtureResult = namedtuple("FixtureResult", "filename, folder, data")
60+
FixtureResult = namedtuple("FixtureResult", "filename, folder, data, protocol_suffix")
6161

6262
SMART_FOLDER = "tests/fixtures/smart/"
6363
SMARTCAM_FOLDER = "tests/fixtures/smartcam/"
6464
SMART_CHILD_FOLDER = "tests/fixtures/smart/child/"
6565
IOT_FOLDER = "tests/fixtures/iot/"
6666

67+
SMART_PROTOCOL_SUFFIX = "SMART"
68+
SMARTCAM_SUFFIX = "SMARTCAM"
69+
SMART_CHILD_SUFFIX = "SMART.CHILD"
70+
IOT_SUFFIX = "IOT"
71+
72+
NO_GIT_FIXTURE_FOLDER = "kasa-fixtures"
73+
6774
ENCRYPT_TYPES = [encrypt_type.value for encrypt_type in DeviceEncryptionType]
6875

6976
_LOGGER = logging.getLogger(__name__)
@@ -140,7 +147,17 @@ async def handle_device(
140147
]
141148

142149
for fixture_result in fixture_results:
143-
save_filename = Path(basedir) / fixture_result.folder / fixture_result.filename
150+
save_folder = Path(basedir) / fixture_result.folder
151+
if save_folder.exists():
152+
save_filename = save_folder / f"{fixture_result.filename}.json"
153+
else:
154+
# If being run without git clone
155+
save_folder = Path(basedir) / NO_GIT_FIXTURE_FOLDER
156+
save_folder.mkdir(exist_ok=True)
157+
save_filename = (
158+
save_folder
159+
/ f"{fixture_result.filename}-{fixture_result.protocol_suffix}.json"
160+
)
144161

145162
pprint(fixture_result.data)
146163
if autosave:
@@ -459,9 +476,14 @@ async def get_legacy_fixture(
459476
hw_version = sysinfo["hw_ver"]
460477
sw_version = sysinfo["sw_ver"]
461478
sw_version = sw_version.split(" ", maxsplit=1)[0]
462-
save_filename = f"{model}_{hw_version}_{sw_version}.json"
479+
save_filename = f"{model}_{hw_version}_{sw_version}"
463480
copy_folder = IOT_FOLDER
464-
return FixtureResult(filename=save_filename, folder=copy_folder, data=final)
481+
return FixtureResult(
482+
filename=save_filename,
483+
folder=copy_folder,
484+
data=final,
485+
protocol_suffix=IOT_SUFFIX,
486+
)
465487

466488

467489
def _echo_error(msg: str):
@@ -830,9 +852,12 @@ def get_smart_child_fixture(response):
830852
model = model_info.long_name
831853
if model_info.region is not None:
832854
model = f"{model}({model_info.region})"
833-
save_filename = f"{model}_{hw_version}_{fw_version}.json"
855+
save_filename = f"{model}_{hw_version}_{fw_version}"
834856
return FixtureResult(
835-
filename=save_filename, folder=SMART_CHILD_FOLDER, data=response
857+
filename=save_filename,
858+
folder=SMART_CHILD_FOLDER,
859+
data=response,
860+
protocol_suffix=SMART_CHILD_SUFFIX,
836861
)
837862

838863

@@ -980,20 +1005,28 @@ async def get_smart_fixtures(
9801005
# smart protocol
9811006
model_info = SmartDevice._get_device_info(final, discovery_result)
9821007
copy_folder = SMART_FOLDER
1008+
protocol_suffix = SMART_PROTOCOL_SUFFIX
9831009
else:
9841010
# smart camera protocol
9851011
model_info = SmartCamDevice._get_device_info(final, discovery_result)
9861012
copy_folder = SMARTCAM_FOLDER
1013+
protocol_suffix = SMARTCAM_SUFFIX
9871014
hw_version = model_info.hardware_version
9881015
sw_version = model_info.firmware_version
9891016
model = model_info.long_name
9901017
if model_info.region is not None:
9911018
model = f"{model}({model_info.region})"
9921019

993-
save_filename = f"{model}_{hw_version}_{sw_version}.json"
1020+
save_filename = f"{model}_{hw_version}_{sw_version}"
9941021

9951022
fixture_results.insert(
996-
0, FixtureResult(filename=save_filename, folder=copy_folder, data=final)
1023+
0,
1024+
FixtureResult(
1025+
filename=save_filename,
1026+
folder=copy_folder,
1027+
data=final,
1028+
protocol_suffix=protocol_suffix,
1029+
),
9971030
)
9981031
return fixture_results
9991032

0 commit comments

Comments
 (0)
0