From b749e08366c2f44d7490b723a2d17747972cd7d3 Mon Sep 17 00:00:00 2001 From: isaacyang Date: Fri, 12 Apr 2024 12:07:30 +0800 Subject: [PATCH] fix format --- providers/resource/bin/WIFI_phy.py | 34 ++-- providers/resource/tests/test_WIFI_phy.py | 193 +++++++++++----------- 2 files changed, 122 insertions(+), 105 deletions(-) diff --git a/providers/resource/bin/WIFI_phy.py b/providers/resource/bin/WIFI_phy.py index 4b5d822d8..226338812 100755 --- a/providers/resource/bin/WIFI_phy.py +++ b/providers/resource/bin/WIFI_phy.py @@ -6,7 +6,7 @@ def parse_iw_dev_output(): """ - Parses the output of 'iw dev' to extract PHY and interface mappings. + Parses the output of "iw dev" to extract PHY and interface mappings. """ cmd = "iw dev" output = check_output(cmd, shell=True, universal_newlines=True) @@ -17,7 +17,7 @@ def parse_iw_dev_output(): def parse_phy_info_output(output): """ - Parses the output of 'iw phy info' to extract bands and STA support. + Parses the output of "iw phy info" to extract bands and STA support. """ bands_data = output.split("Supported commands:")[0] bands = {} @@ -32,8 +32,11 @@ def parse_phy_info_output(output): freqs_raw = band_content.split("Frequencies:")[1] freq_ptn = r"(\d+ MHz.*)" freq_compile = re.compile(freq_ptn) - freqs = [freq for freq in freq_compile.findall(freqs_raw) - if "disabled" not in freq] + freqs = [ + freq + for freq in freq_compile.findall(freqs_raw) + if "disabled" not in freq + ] bands[band_num] = freqs return bands @@ -46,9 +49,10 @@ def check_sta_support(phy_info_output): # Supported STAs and their keywords supported_stas = {"BE": "EHT", "AX": "HE RX MCS", "AC": "VHT RX MCS"} - sta_supported = {sta: "supported" if sta_keyword in phy_info_output - else "unsupported" - for sta, sta_keyword in supported_stas.items()} + sta_supported = { + sta: "supported" if sta_keyword in phy_info_output else "unsupported" + for sta, sta_keyword in supported_stas.items() + } return sta_supported @@ -60,8 +64,11 @@ def check_freq_support(bands): supported_freqs = {"2.4GHz": "1", "5GHz": "2", "6GHz": "4"} freq_supported = { - freq: "supported" - if band in bands and len(bands[band]) > 0 else "unsupported" + freq: ( + "supported" + if band in bands and len(bands[band]) > 0 + else "unsupported" + ) for freq, band in supported_freqs.items() } return freq_supported @@ -74,8 +81,9 @@ def create_phy_interface_mapping(phy_interface): phy_interface_mapping = {} for phy, interface in phy_interface: cmd = "iw {} info".format(phy) - phy_info_output = check_output(cmd, shell=True, - universal_newlines=True) + phy_info_output = check_output( + cmd, shell=True, universal_newlines=True + ) bands = parse_phy_info_output(phy_info_output) freq_supported = check_freq_support(bands) sta_supported = check_sta_support(phy_info_output) @@ -83,13 +91,13 @@ def create_phy_interface_mapping(phy_interface): "PHY": phy, "Bands": bands, "FREQ_Supported": freq_supported, - "STA_Supported": sta_supported + "STA_Supported": sta_supported, } return phy_interface_mapping def main(): - # Read and parse 'iw dev' output + # Read and parse "iw dev" output phy_interface = parse_iw_dev_output() # Create mapping with interface, PHY, bands, and supported STAs diff --git a/providers/resource/tests/test_WIFI_phy.py b/providers/resource/tests/test_WIFI_phy.py index a9a24088c..c46abea04 100644 --- a/providers/resource/tests/test_WIFI_phy.py +++ b/providers/resource/tests/test_WIFI_phy.py @@ -1,19 +1,25 @@ import os from unittest.mock import patch import unittest -from WIFI_phy import (parse_iw_dev_output, parse_phy_info_output, - check_sta_support, check_freq_support, - create_phy_interface_mapping, main) +from WIFI_phy import ( + parse_iw_dev_output, + parse_phy_info_output, + check_sta_support, + check_freq_support, + create_phy_interface_mapping, + main, +) -class WIFIphyData(): + +class WIFIphyData: @staticmethod def get_text(filenmae): full_path = os.path.join( os.path.dirname(os.path.realpath(__file__)), "test_WIFI_phy_data", - filenmae + filenmae, ) - with open(full_path, 'r', encoding='UTF-8') as stream: + with open(full_path, "r", encoding="UTF-8") as stream: return stream.read() @@ -21,15 +27,18 @@ class WIFITest(unittest.TestCase, WIFIphyData): def setUp(self): self.iw_dev_file_list = ["iw_dev.log", "iw_dev2.log"] - self.phy_info_file_list = ["RTL8821CE_AC.log", "QCNFA765_AX_6G.log", - "BE200_AX.log", "BE200_BE.log"] + self.phy_info_file_list = [ + "RTL8821CE_AC.log", + "QCNFA765_AX_6G.log", + "BE200_AX.log", + "BE200_BE.log", + ] - @patch('WIFI_phy.check_output') + @patch("WIFI_phy.check_output") def test_parse_iw_dev_output(self, mock_check_output): expected_result = { - "iw_dev.log": [('phy#0', 'wlp2s0')], - "iw_dev2.log": [('phy#0', 'wlp3s0f0'), - ('phy#1', 'wlp3s0f1')] + "iw_dev.log": [("phy#0", "wlp2s0")], + "iw_dev2.log": [("phy#0", "wlp3s0f0"), ("phy#1", "wlp3s0f1")] } for file in self.iw_dev_file_list: @@ -37,90 +46,90 @@ def test_parse_iw_dev_output(self, mock_check_output): result = parse_iw_dev_output() self.assertEqual(result, expected_result[file]) - @patch('WIFI_phy.check_output') + @patch("WIFI_phy.check_output") def test_create_phy_interface_mapping(self, mock_check_output): iw_dev_file = self.iw_dev_file_list[0] iw_dev_output = WIFIphyData.get_text(iw_dev_file) excvected_result = { "RTL8821CE_AC.log": { - 'wlp2s0': { - 'PHY': 'phy#0', - 'Bands': { - '1': ['2412 MHz [1] (30.0 dBm)'], - '2': ['5180 MHz [36] (23.0 dBm)'] + "wlp2s0": { + "PHY": "phy#0", + "Bands": { + "1": ["2412 MHz [1] (30.0 dBm)"], + "2": ["5180 MHz [36] (23.0 dBm)"], }, - 'FREQ_Supported': { - '2.4GHz': 'supported', - '5GHz': 'supported', - '6GHz': 'unsupported' + "FREQ_Supported": { + "2.4GHz": "supported", + "5GHz": "supported", + "6GHz": "unsupported", + }, + "STA_Supported": { + "BE": "unsupported", + "AX": "unsupported", + "AC": "supported", }, - 'STA_Supported': { - 'BE': 'unsupported', - 'AX': 'unsupported', - 'AC': 'supported' - } } }, "QCNFA765_AX_6G.log": { - 'wlp2s0': { - 'PHY': 'phy#0', - 'Bands': { - '1': ['2412 MHz [1] (30.0 dBm)'], - '2': ['5180 MHz [36] (24.0 dBm)'], - '4': ['5955 MHz [1] (30.0 dBm)'] + "wlp2s0": { + "PHY": "phy#0", + "Bands": { + "1": ["2412 MHz [1] (30.0 dBm)"], + "2": ["5180 MHz [36] (24.0 dBm)"], + "4": ["5955 MHz [1] (30.0 dBm)"], + }, + "FREQ_Supported": { + "2.4GHz": "supported", + "5GHz": "supported", + "6GHz": "supported", }, - 'FREQ_Supported': { - '2.4GHz': 'supported', - '5GHz': 'supported', - '6GHz': 'supported' + "STA_Supported": { + "BE": "unsupported", + "AX": "supported", + "AC": "supported", }, - 'STA_Supported': { - 'BE': 'unsupported', - 'AX': 'supported', - 'AC': 'supported' - } } }, "BE200_AX.log": { - 'wlp2s0': { - 'PHY': 'phy#0', - 'Bands': { - '1': ['2412 MHz [1] (22.0 dBm)'], - '2': ['5180 MHz [36] (22.0 dBm)'], - '4': [] + "wlp2s0": { + "PHY": "phy#0", + "Bands": { + "1": ["2412 MHz [1] (22.0 dBm)"], + "2": ["5180 MHz [36] (22.0 dBm)"], + "4": [], }, - 'FREQ_Supported': { - '2.4GHz': 'supported', - '5GHz': 'supported', - '6GHz': 'unsupported' + "FREQ_Supported": { + "2.4GHz": "supported", + "5GHz": "supported", + "6GHz": "unsupported", + }, + "STA_Supported": { + "BE": "unsupported", + "AX": "supported", + "AC": "supported", }, - 'STA_Supported': { - 'BE': 'unsupported', - 'AX': 'supported', - 'AC': 'supported' - } } }, "BE200_BE.log": { - 'wlp2s0': { - 'PHY': 'phy#0', - 'Bands': { - '1': ['2412 MHz [1] (22.0 dBm)'], - '2': ['5180 MHz [36] (22.0 dBm)'], - '4': [] + "wlp2s0": { + "PHY": "phy#0", + "Bands": { + "1": ["2412 MHz [1] (22.0 dBm)"], + "2": ["5180 MHz [36] (22.0 dBm)"], + "4": [], + }, + "FREQ_Supported": { + "2.4GHz": "supported", + "5GHz": "supported", + "6GHz": "unsupported", }, - 'FREQ_Supported': { - '2.4GHz': 'supported', - '5GHz': 'supported', - '6GHz': 'unsupported' + "STA_Supported": { + "BE": "supported", + "AX": "supported", + "AC": "supported", }, - 'STA_Supported': { - 'BE': 'supported', - 'AX': 'supported', - 'AC': 'supported' - } } - } + }, } for file in self.phy_info_file_list: phy_info_output = WIFIphyData.get_text(file) @@ -134,8 +143,8 @@ def test_parse_phy_info_output(self): result = parse_phy_info_output(phy_info_output) expected_result = { - '1': ['2412 MHz [1] (30.0 dBm)'], - '2': ['5180 MHz [36] (23.0 dBm)'] + "1": ["2412 MHz [1] (30.0 dBm)"], + "2": ["5180 MHz [36] (23.0 dBm)"], } self.assertDictEqual(result, expected_result) @@ -144,42 +153,42 @@ def test_check_sta_support(self): result = check_sta_support(phy_info_output) expected_result = { - 'BE': 'unsupported', - 'AX': 'unsupported', - 'AC': 'supported' + "BE": "unsupported", + "AX": "unsupported", + "AC": "supported", } self.assertDictEqual(result, expected_result) def test_check_freq_support(self): bands = { - '1': ['2412 MHz [1] (30.0 dBm)'], - '2': ['5180 MHz [36] (23.0 dBm)'] + "1": ["2412 MHz [1] (30.0 dBm)"], + "2": ["5180 MHz [36] (23.0 dBm)"], } result = check_freq_support(bands) expected_result = { - '2.4GHz': 'supported', - '5GHz': 'supported', - '6GHz': 'unsupported' + "2.4GHz": "supported", + "5GHz": "supported", + "6GHz": "unsupported", } self.assertDictEqual(result, expected_result) - @patch('WIFI_phy.print') - @patch('WIFI_phy.check_output') + @patch("WIFI_phy.print") + @patch("WIFI_phy.check_output") def test_main(self, mock_check_output, mock_print): iw_dev_output = WIFIphyData.get_text(self.iw_dev_file_list[0]) phy_info_output = WIFIphyData.get_text(self.phy_info_file_list[0]) mock_check_output.side_effect = [iw_dev_output, phy_info_output] main() expected_calls = [ - unittest.mock.call('wlp2s0_2.4GHz: supported'), - unittest.mock.call('wlp2s0_5GHz: supported'), - unittest.mock.call('wlp2s0_6GHz: unsupported'), - unittest.mock.call('wlp2s0_be: unsupported'), - unittest.mock.call('wlp2s0_ax: unsupported'), - unittest.mock.call('wlp2s0_ac: supported') + unittest.mock.call("wlp2s0_2.4GHz: supported"), + unittest.mock.call("wlp2s0_5GHz: supported"), + unittest.mock.call("wlp2s0_6GHz: unsupported"), + unittest.mock.call("wlp2s0_be: unsupported"), + unittest.mock.call("wlp2s0_ax: unsupported"), + unittest.mock.call("wlp2s0_ac: supported"), ] mock_print.assert_has_calls(expected_calls, any_order=True) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main()