From e37e01f67442a535dbc1c988c9ac27d257cd5855 Mon Sep 17 00:00:00 2001 From: nickdelis Date: Mon, 23 Dec 2024 15:14:14 +0200 Subject: [PATCH] Change navigation logic & restructure --- .gitignore | 5 +++- README.md | 13 ++++++--- utils.py => cli_utils.py | 8 ++++-- main.py | 61 +++++++++++++--------------------------- bdlk.py => remote.py | 0 setup.py | 16 +++++++++++ 6 files changed, 54 insertions(+), 49 deletions(-) rename utils.py => cli_utils.py (91%) rename bdlk.py => remote.py (100%) create mode 100644 setup.py diff --git a/.gitignore b/.gitignore index 3e8ecb4..aa3c548 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ .idea/ -.venv/ \ No newline at end of file +.venv/ +build/ +dist/ +Broadlink_Reader.egg-info/ \ No newline at end of file diff --git a/README.md b/README.md index fa9cf2d..80133a8 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,17 @@ them to a file for future use. To get started with Broadlink Reader, clone the repository from GitHub: -``` +```sh git clone https://github.com/JexSrs/broadlink-reader.git cd broadlink-reader +``` -python -m venv .venv -source .venv/bin/activate +and install the project: +```sh +python3 setup.py install +``` +or +```sh pip install -r requirements.txt ``` @@ -29,7 +34,7 @@ pip install -r requirements.txt You can run the Broadlink Reader using the `main.py` script. The following options are available to customize your interaction with the Broadlink device: -``` +```sh python main.py --ip 192.168.1.100 # Remote's IP address ``` diff --git a/utils.py b/cli_utils.py similarity index 91% rename from utils.py rename to cli_utils.py index 97ca49c..e18767d 100644 --- a/utils.py +++ b/cli_utils.py @@ -60,10 +60,12 @@ def print_keys(actions: dict, max_keys=10): max_length = -1 for index, action in enumerate(keys): color = get_action_color(actions, action) - display_text = color + f"{index}: {action}{' (select to expand)' if isinstance(actions[action], dict) else ''}" + display_text = color + f"{index}: {action}" # Find the largest string when keys >= 10 - if len(keys) >= max_keys and len(keys) >= max_length: - max_length = len(display_text) + if len(keys) >= max_keys: + if len(display_text) >= max_length: + max_length = len(display_text) + display_texts.append(display_text) else: print(display_text) diff --git a/main.py b/main.py index 9cc37d6..2649684 100644 --- a/main.py +++ b/main.py @@ -5,8 +5,8 @@ from broadlink import Device from colorama import Fore, init -import bdlk -from utils import print_keys +import remote +from cli_utils import print_keys init(autoreset=True) @@ -16,23 +16,20 @@ def navigate_actions(device: Device, actions): - """Navigate through the nested actions.""" - global read_timeout - - current_actions = actions - path = [] - display_path = [] - + current_path = [] while True: - if display_path: - print(Fore.LIGHTBLUE_EX + f'\nCurrent path: {' > '.join(display_path)}') + if current_path: + print(Fore.LIGHTBLUE_EX + f'\nCurrent path: {' > '.join(current_path)}') else: - print(Fore.LIGHTBLUE_EX + '\nHome Page') + print(Fore.LIGHTBLUE_EX + '\nCurrent path: Home') print("Choose an action by index to register:") - print(Fore.WHITE + "-1: " + ("Go back" if path else "Exit")) - action_keys = print_keys(current_actions) + print(Fore.WHITE + "-1: " + ("Go back" if current_path else "Exit")) + + curr_actions = actions + for path in current_path: + curr_actions = curr_actions[path] + action_keys = print_keys(curr_actions) - choice = -1 try: choice = int(input("> ")) except ValueError: @@ -40,41 +37,25 @@ def navigate_actions(device: Device, actions): continue if choice == -1: - if path: + if current_path: # Go back one level - current_actions = path.pop() - - # Fix "Go back" bug - for action in reversed(display_path): - if action in current_actions: - if not isinstance(current_actions[action], dict) and path: - current_actions = path.pop() - display_path.pop() - - display_path.pop() + current_path.pop() else: break elif 0 <= choice < len(action_keys): - # Save current level selected_action = action_keys[choice] - if selected_action not in display_path: - display_path.append(selected_action) - path.append(current_actions) - - # Go deeper into the next level - if isinstance(current_actions[selected_action], dict): - current_actions = current_actions[selected_action] + if isinstance(curr_actions[selected_action], dict): + current_path.append(selected_action) else: # If it's a leaf and read the packet try: - packet = bdlk.handle_action(device, read_timeout) + packet = remote.handle_action(device, read_timeout) except Exception as e: print(Fore.RED + 'Failed to read from remote: ' + str(e)) continue # Save the packet in the nested structure - parent_action = path[-1] if path else actions - parent_action[selected_action] = packet + curr_actions[selected_action] = packet # Save the updated data back to the output file with open(mFile, 'w') as file: @@ -99,10 +80,8 @@ def main(): args = parser.parse_args() read_timeout = args.read - data = {} - - # Load existing data from the input file if it exists mFile = args.file + # Load existing data from the input file if it exists if os.path.exists(mFile): with open(mFile, 'r') as f: try: @@ -111,7 +90,7 @@ def main(): print(Fore.WHITE + "Could not read input file. Starting with empty data.") # Setup code - device = bdlk.get_device(ip_address=args.ip, port=args.port, timeout=args.timeout) + device = remote.get_device(ip_address=args.ip, port=args.port, timeout=args.timeout) if not device: exit(1) diff --git a/bdlk.py b/remote.py similarity index 100% rename from bdlk.py rename to remote.py diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..8cc2bfe --- /dev/null +++ b/setup.py @@ -0,0 +1,16 @@ +from setuptools import setup, find_packages + +with open("requirements.txt") as f: + requirements = f.read().splitlines() + +setup( + name="Broadlink Reader", + version="0.1.0", + description="A Python CLI tool for learning and capturing IR commands from Broadlink devices, with options for customizable connection settings and command saving.", + long_description=open("README.md").read(), + long_description_content_type="text/markdown", + url="https://github.com/JexSrs/broadlink-reader", + packages=find_packages(), + install_requires=requirements, + python_requires=">=3.11", +)