|
14 | 14 | # #################################################################################### # |
15 | 15 | from __future__ import annotations |
16 | 16 |
|
| 17 | +import contextlib |
17 | 18 | import os |
18 | 19 | from tkinter import font |
19 | 20 |
|
20 | 21 | from maptasker.src.colrmode import set_color_mode |
21 | 22 | from maptasker.src.lineout import LineOut |
| 23 | +from maptasker.src.maputils import validate_ip_address, validate_port |
22 | 24 | from maptasker.src.nameattr import get_tk |
23 | 25 | from maptasker.src.primitem import PrimeItems |
24 | 26 | from maptasker.src.proginit import get_data_and_output_intro |
@@ -126,68 +128,87 @@ def get_monospace_fonts() -> dict: |
126 | 128 | # ################################################################################## |
127 | 129 | # Ping the Android evice to make sure it is reachable. |
128 | 130 | # ################################################################################## |
129 | | -def ping_android_device(self, backup_info: str) -> tuple[bool, str]: # noqa: ANN001 |
| 131 | +def ping_android_device(self, ip_address: str, port_number: str, file_location: str) -> bool: # noqa: ANN001 |
130 | 132 | # The following should return a list: [ip_address:port_number, file_location] |
131 | 133 | """ |
132 | 134 | Pings an Android device |
133 | 135 | Args: |
134 | | - backup_info: str - Backup information in ip_address:port_number,file_location format |
135 | | - Returns: |
136 | | - tuple[bool, str] - A tuple containing a bool for success/failure and a error message string |
137 | | - - If failure, returns True and a blank strings, else False and ip addr and location |
| 136 | + ip_address: str - TCP IP address of the Android device |
| 137 | + port_number: str - TCP port number of the Android device |
| 138 | + file_location: str - File location on the Android device |
| 139 | + Return: |
| 140 | + Error: True if error, false if all is good. |
138 | 141 | Processing Logic: |
139 | 142 | - Splits the backup_info string into ip_address, port_number, and file_location |
140 | 143 | - Validates the IP address, port number, and file location |
141 | 144 | - Pings the IP address to check connectivity |
142 | 145 | - Returns a tuple indicating success/failure and any error message |
143 | 146 | """ |
144 | | - temp_info = backup_info.split(",") |
145 | | - temp_ip = temp_info[0].split(":") |
146 | | - |
147 | 147 | # Validate IP Address |
148 | | - temp_ipaddr = temp_ip[0].split(".") |
149 | | - if len(temp_ipaddr) < 4: |
150 | | - self.backup_error(f"Invalid IP Address: {temp_ip[0]}. Try again.") |
151 | | - return True, "", "" |
152 | | - for item in temp_ipaddr[0]: |
153 | | - if not item.isdigit(): |
| 148 | + if validate_ip_address(ip_address): |
| 149 | + |
| 150 | + # Verify that the host IP is reachable: |
| 151 | + self.display_message_box( |
| 152 | + f"Pinging address {ip_address}. Please wait...", |
| 153 | + True, |
| 154 | + ) |
| 155 | + self.update() # Force a window refresh. |
| 156 | + |
| 157 | + # Ping IP address. |
| 158 | + response = os.system("ping -c 1 -t50 > /dev/null " + ip_address) # noqa: S605 |
| 159 | + if response != 0: |
154 | 160 | self.backup_error( |
155 | | - f"Invalid IP Address: {temp_ip[0]}. Try again.", |
| 161 | + f"{ip_address} is not reachable (error {response}). Try again.", |
156 | 162 | ) |
157 | | - return True, "", "" |
158 | | - # Verify that the host IP (temp_ip[0]) is reachable: |
159 | | - self.display_message_box( |
160 | | - f"Pinging address {temp_ip[0]}. Please wait...", |
161 | | - True, |
162 | | - ) |
163 | | - self.update() # Force a window refresh. |
164 | | - # Ping IP address. |
165 | | - response = os.system("ping -c 1 -t50 > /dev/null " + temp_ip[0]) # noqa: S605 |
166 | | - if response != 0: |
| 163 | + return True |
| 164 | + self.display_message_box( |
| 165 | + "Ping successful.", |
| 166 | + True, |
| 167 | + ) |
| 168 | + else: |
167 | 169 | self.backup_error( |
168 | | - f"{temp_ip[0]} is not reachable (error {response}). Try again.", |
| 170 | + f"Invalid IP address: {ip_address}. Try again.", |
169 | 171 | ) |
170 | | - return True, "", "" |
171 | | - self.display_message_box( |
172 | | - "Ping successful.", |
173 | | - False, |
174 | | - ) |
| 172 | + return True |
| 173 | + |
175 | 174 | # Validate port number |
176 | | - if len(temp_ip) == 1 or not temp_ip[1].isdigit: |
| 175 | + if validate_port(ip_address, port_number) != 0: |
177 | 176 | self.backup_error( |
178 | | - f"Invalid port number: {temp_ipaddr[1]}. Try again.", |
| 177 | + f"Invalid Port number: {port_number}. Try again.", |
179 | 178 | ) |
180 | | - return True, "", "" |
| 179 | + return True |
181 | 180 |
|
182 | 181 | # Validate file location |
183 | | - if len(temp_info) < 2 or temp_info[1] == "": |
| 182 | + if len(file_location) < 2 or file_location == "": |
184 | 183 | self.backup_error("File location is missing. Try again.") |
185 | | - return None |
186 | | - |
187 | | - # All is well so far... |
188 | | - self.backup_file_http = temp_info[0] |
189 | | - self.backup_file_location = temp_info[1] |
| 184 | + return True |
190 | 185 |
|
191 | 186 | # Empty message = good to go...no error. |
192 | 187 | self.backup_error("") |
193 | | - return False, self.backup_file_http, self.backup_file_location |
| 188 | + return False |
| 189 | + |
| 190 | + |
| 191 | +# ################################################################################## |
| 192 | +# Clear all buttons associated with fetching the backup file from Android device |
| 193 | +# ################################################################################## |
| 194 | +def clear_android_buttons(self) -> None: # noqa: ANN001 |
| 195 | + """ |
| 196 | + Clears android device configuration buttons and displays backup button |
| 197 | + Args: |
| 198 | + self: The class instance |
| 199 | + Returns: |
| 200 | + None |
| 201 | + - Destroys IP, port, file entry and label widgets |
| 202 | + - Destroys get backup button |
| 203 | + - Displays new backup button with callback to get_backup_event method""" |
| 204 | + with contextlib.suppress(AttributeError): |
| 205 | + self.ip_entry.destroy() |
| 206 | + self.port_entry.destroy() |
| 207 | + self.file_entry.destroy() |
| 208 | + self.ip_label.destroy() |
| 209 | + self.port_label.destroy() |
| 210 | + self.file_label.destroy() |
| 211 | + self.get_backup_button.destroy() |
| 212 | + self.cancel_entry_button.destroy() |
| 213 | + |
| 214 | + self.display_backup_button("Get Backup from Android Device", "#246FB6", "#6563ff", self.get_backup_event) |
0 commit comments