Skip to content

Commit 88253a1

Browse files
authored
Version 2.6.3
Signed-off-by: mctinker <mikrubin@gmail.com>
1 parent 9f91bd4 commit 88253a1

File tree

15 files changed

+634
-303
lines changed

15 files changed

+634
-303
lines changed

maptasker/src/config.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,29 @@
2828
DARK_MODE = True
2929

3030
#
31-
# Set up to fetch the backup file from Android device running the Tasker server
31+
# Set up to fetch the backup file from Android device running the Tasker server.
3232
#
33-
# The following two parameters must both be filled in for the fetch to work:
34-
# BACKUP_FILE_HTTP is the Tasker server ip address and port number on the Android
35-
# device from which to fetch the file
36-
# BACKUP_FILE_LOCATION is the location of the backup file on the Android device
37-
#
38-
# In addition, the Tasker HTTP sample Project must be active on the Android device
39-
# (https://taskernet.com/shares/?user=AS35m8ne7oO4s%2BaDx%2FwlzjdFTfVMWstg1ay5AkpiNdrLoSXEZdFfw1IpXiyJCVLNW0yn&id=Project%3AHttp+Server+Example),
40-
# and the server must be active.
33+
# In addition, the Tasker HTTP sample Project must be installed on the Android device,
34+
# found at...
35+
# (https://shorturl.at/bwCD4),
36+
# and the server must be active on the Android device.
37+
38+
# This is the HTTP IP address of the Android device from which to fetch the backup.
39+
# Example: ANDROID_IPADDR = "192.168.0.210"
40+
41+
ANDROID_IPADDR = ""
4142

42-
# This is the HTTP IP address and port number (e.g. 1821) that Tasker assigns to the
43-
# server on the device containing the backup file to get.
44-
# Example: BACKUP_FILE_HTTP = "http://192.168.0.210:1821" (note: not "https")
43+
# This is the port number for the Android device from which to fetch the backup,
44+
# and is specified in the Tasker HTTP Server Example project notification.
45+
# From notification: HTTP Server Info {"device_name":"http://192.168.0.49:1821"}
46+
# Example: ANDROID_PORT = "1821"
4547

46-
BACKUP_FILE_HTTP = ""
48+
ANDROID_PORT = ""
4749

4850
# This is the location on the Android device from which to pull the backup file
49-
# Example: BACKUP_FILE_LOCATION = "/Tasker/configs/user/backup.xml"
51+
# Example: ANDROID_FILE = "/Tasker/configs/user/backup.xml"
5052

51-
BACKUP_FILE_LOCATION = ""
53+
ANDROID_FILE = ""
5254

5355
# ##################################################################################
5456
# END User-modifiable global constants

maptasker/src/frontmtr.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,13 @@ def output_the_heading() -> None:
8383
if PrimeItems.program_arguments["fetched_backup_from_android"]:
8484
source_file = (
8585
"From Android device"
86-
f' {PrimeItems.program_arguments["backup_file_http"]} at'
87-
f' {PrimeItems.program_arguments["backup_file_location"]}'
86+
f' TCP IP address:{PrimeItems.program_arguments["android_ipaddr"]}'
87+
f' on port:{PrimeItems.program_arguments["android_port"]}'
88+
f' with file location: {PrimeItems.program_arguments["android_file"]}'
8889
)
8990
elif PrimeItems.program_arguments["debug"] or not PrimeItems.program_arguments["file"]:
9091
filename = isinstance(PrimeItems.file_to_get, str)
91-
if not filename:
92-
filename = PrimeItems.file_to_get.name
93-
else:
94-
filename = PrimeItems.file_to_get
92+
filename = PrimeItems.file_to_get.name if not filename else PrimeItems.file_to_get
9593
source_file = filename
9694
else:
9795
source_file = PrimeItems.program_arguments["file"]

maptasker/src/getbakup.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ def write_out_backup_file(file_contents: bin) -> None:
4343

4444
# We are going to save the file as...
4545
# Get position of the last "/" in path/file
46-
name_location = PrimeItems.program_arguments["backup_file_location"].rfind("/") + 1
46+
name_location = PrimeItems.program_arguments["android_file"].rfind("/") + 1
4747
# Get the name of the file
48-
my_file_name = PrimeItems.program_arguments["backup_file_location"][name_location:]
48+
my_file_name = PrimeItems.program_arguments["android_file"][name_location:]
4949

5050
# Convert the binary code to string
5151
output_lines = file_contents.decode("utf-8")
5252

5353
# Set up the backup file full path
54-
the_backup_file = PrimeItems.program_arguments["backup_file_location"]
54+
the_backup_file = PrimeItems.program_arguments["android_file"]
5555
put_message = f"Fetching backup file {my_file_name}: {the_backup_file}"
5656
logger.debug(put_message)
5757

@@ -69,13 +69,11 @@ def write_out_backup_file(file_contents: bin) -> None:
6969
# Set flag to identify that backup file was fetched from Android device
7070
PrimeItems.program_arguments["fetched_backup_from_android"] = True
7171

72-
return
73-
7472

7573
# ##################################################################################
7674
# Issue HTTP Request to get the backup xml file from the Android device.
7775
# ##################################################################################
78-
def request_file(backup_file_http: str, backup_file_location: str) -> tuple[int, object]:
76+
def request_file(ip_addr: str, port_number: str, file_location: str) -> tuple[int, object]:
7977
"""
8078
Issue HTTP Request to get the backup xml file from the Android device.
8179
Tasker's HTTP Server Example must be installed for this to work:
@@ -88,8 +86,8 @@ def request_file(backup_file_http: str, backup_file_location: str) -> tuple[int,
8886
# Create the URL to request the backup xml file from the Android device running the
8987
# Tasker server.
9088
# Something like: 192.168.0.210:1821/file/path/to/backup.xml?download=1
91-
http = "http://" if "http://" not in backup_file_http else ""
92-
url = f"{http}{backup_file_http}/file{backup_file_location}?download=1"
89+
http = "http://" if "http://" not in ip_addr else ""
90+
url = f"{http}{ip_addr}:{port_number}/file{file_location}?download=1"
9391

9492
# Make the request.
9593
try:
@@ -115,7 +113,7 @@ def request_file(backup_file_http: str, backup_file_location: str) -> tuple[int,
115113
# Return the contents of the file.
116114
return 0, response.content
117115
elif response.status_code == 404:
118-
return 6, f"File '{backup_file_location}' not found."
116+
return 6, f"File '{file_location}' not found."
119117
else:
120118
return (
121119
8,
@@ -152,17 +150,18 @@ def get_backup_file() -> str:
152150
"""
153151
# Get the contents of the file.
154152
return_code, file_contents = request_file(
155-
PrimeItems.program_arguments["backup_file_http"],
156-
PrimeItems.program_arguments["backup_file_location"],
153+
PrimeItems.program_arguments["android_ipaddr"],
154+
PrimeItems.program_arguments["android_port"],
155+
PrimeItems.program_arguments["android_file"],
157156
)
158157

159158
if return_code != 0:
160159
if PrimeItems.program_arguments["gui"]:
161160
PrimeItems.error_code = return_code
162-
return
161+
return None
163162
error_handler(str(file_contents), 8)
164163

165164
# Process the backup file
166165
write_out_backup_file(file_contents)
167166

168-
return substring_after_last(PrimeItems.program_arguments["backup_file_location"], "/")
167+
return substring_after_last(PrimeItems.program_arguments["android_file"], "/")

maptasker/src/getputer.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,29 @@
1111
# preserved. Contributors provide an express grant of patent rights. #
1212
# #
1313
# #################################################################################### #
14+
from __future__ import annotations
15+
16+
import contextlib
1417
import json
1518
from pathlib import Path
1619

1720
from maptasker.src.error import error_handler
1821
from maptasker.src.sysconst import ARGUMENTS_FILE
1922

2023

21-
def corrupted_file(program_arguments, colors_to_use):
24+
def corrupted_file(program_arguments, colors_to_use) -> None:
25+
"""
26+
Checks for corrupted settings file and handles error
27+
Args:
28+
program_arguments: Command line arguments
29+
colors_to_use: Color settings
30+
Returns:
31+
None: Returns None
32+
Processing Logic:
33+
1. Checks settings file for corruption
34+
2. Generates error message if corrupted
35+
3. Returns error message and settings as dictionaries for GUI display
36+
4. Does not restore corrupted settings, asks user to re-save"""
2237
error_handler(
2338
(
2439
f"'-restore' option... The settings file, {ARGUMENTS_FILE} is"
@@ -33,8 +48,8 @@ def corrupted_file(program_arguments, colors_to_use):
3348
"msg": (
3449
"The settings file is corrupt or not compatible with the new verison of \
3550
MapTasker!"
36-
"The old settings can not be restored. Re-save your settings."
37-
)
51+
"The old settings can not be restored. Re-save your settings.",
52+
),
3853
}
3954
return program_arguments, colors_to_use
4055

@@ -94,4 +109,14 @@ def save_restore_args(
94109
except json.decoder.JSONDecodeError: # no saved file
95110
corrupted_file(program_arguments, colors_to_use)
96111

112+
# Convert old android device settings to new settings.
113+
with contextlib.suppress(KeyError):
114+
if program_arguments["backup_file_http"]:
115+
temp_args = program_arguments["backup_file_http"].split(":")
116+
program_arguments["android_ipaddr"] = temp_args[1][2:]
117+
program_arguments["android_port"] = temp_args[2]
118+
program_arguments["android_file"] = program_arguments["backup_file_location"]
119+
del program_arguments["backup_file_http"]
120+
del program_arguments["backup_file_location"]
121+
97122
return program_arguments, colors_to_use

maptasker/src/guiutils.py

Lines changed: 62 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
# #################################################################################### #
1515
from __future__ import annotations
1616

17+
import contextlib
1718
import os
1819
from tkinter import font
1920

2021
from maptasker.src.colrmode import set_color_mode
2122
from maptasker.src.lineout import LineOut
23+
from maptasker.src.maputils import validate_ip_address, validate_port
2224
from maptasker.src.nameattr import get_tk
2325
from maptasker.src.primitem import PrimeItems
2426
from maptasker.src.proginit import get_data_and_output_intro
@@ -126,68 +128,87 @@ def get_monospace_fonts() -> dict:
126128
# ##################################################################################
127129
# Ping the Android evice to make sure it is reachable.
128130
# ##################################################################################
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
130132
# The following should return a list: [ip_address:port_number, file_location]
131133
"""
132134
Pings an Android device
133135
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.
138141
Processing Logic:
139142
- Splits the backup_info string into ip_address, port_number, and file_location
140143
- Validates the IP address, port number, and file location
141144
- Pings the IP address to check connectivity
142145
- Returns a tuple indicating success/failure and any error message
143146
"""
144-
temp_info = backup_info.split(",")
145-
temp_ip = temp_info[0].split(":")
146-
147147
# 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:
154160
self.backup_error(
155-
f"Invalid IP Address: {temp_ip[0]}. Try again.",
161+
f"{ip_address} is not reachable (error {response}). Try again.",
156162
)
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:
167169
self.backup_error(
168-
f"{temp_ip[0]} is not reachable (error {response}). Try again.",
170+
f"Invalid IP address: {ip_address}. Try again.",
169171
)
170-
return True, "", ""
171-
self.display_message_box(
172-
"Ping successful.",
173-
False,
174-
)
172+
return True
173+
175174
# Validate port number
176-
if len(temp_ip) == 1 or not temp_ip[1].isdigit:
175+
if validate_port(ip_address, port_number) != 0:
177176
self.backup_error(
178-
f"Invalid port number: {temp_ipaddr[1]}. Try again.",
177+
f"Invalid Port number: {port_number}. Try again.",
179178
)
180-
return True, "", ""
179+
return True
181180

182181
# Validate file location
183-
if len(temp_info) < 2 or temp_info[1] == "":
182+
if len(file_location) < 2 or file_location == "":
184183
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
190185

191186
# Empty message = good to go...no error.
192187
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)

maptasker/src/initparg.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# preserved. Contributors provide an express grant of patent rights. #
1313
# #
1414
# #################################################################################### #
15-
from maptasker.src.config import BACKUP_FILE_HTTP, BACKUP_FILE_LOCATION, OUTPUT_FONT
15+
from maptasker.src.config import ANDROID_IPADDR, ANDROID_PORT, ANDROID_FILE, OUTPUT_FONT
1616

1717

1818
#######################################################################################
@@ -26,9 +26,10 @@ def initialize_runtime_arguments() -> dict:
2626
:return: runtime arguments in dictionary
2727
"""
2828
return {
29+
"android_ipaddr": ANDROID_IPADDR, # IP address of Android device
30+
"android_port": ANDROID_PORT, # Port of Android device
31+
"android_file": ANDROID_FILE,
2932
"appearance_mode": "system", # Appearance mode: "system", "dark", or "light"
30-
"backup_file_http": BACKUP_FILE_HTTP, # Port for Android-based Tasker server, to get backup file from
31-
"backup_file_location": BACKUP_FILE_LOCATION, # Location of the backup file to grab from Android device
3233
"bold": False, # Display Project/Profile?Task/Scene names in bold text
3334
"debug": False, # Run in debug mode (create log file)
3435
"directory": False, # Display directory

0 commit comments

Comments
 (0)