Skip to content

Commit 98e0fbc

Browse files
committed
Allow loading local configs also
1 parent f4c4a25 commit 98e0fbc

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

openandroidinstaller/installer_config.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
# If not, see <https://www.gnu.org/licenses/>."""
1414
# Author: Tobias Sterbak
1515

16-
from typing import List
16+
from typing import List, Optional
17+
from pathlib import Path
18+
from loguru import logger
1719

1820
import yaml
1921

@@ -54,3 +56,29 @@ def from_file(cls, path):
5456

5557
steps = [Step(**raw_step) for raw_step in raw_steps]
5658
return cls(steps, metadata)
59+
60+
61+
def _load_config(device_code: str, config_path: Path) -> Optional[InstallerConfig]:
62+
"""
63+
Function to load a function from given path and directory path.
64+
65+
Try to load local file in the same directory as the executable first, then load from assets.
66+
"""
67+
# try loading a custom local file first
68+
custom_path = Path.cwd().joinpath(Path(f"{device_code}.yaml"))
69+
try:
70+
config = InstallerConfig.from_file(custom_path)
71+
logger.info(f"Loaded custom device config from {custom_path}.")
72+
logger.info(f"Config metadata: {config.metadata}.")
73+
return config
74+
except FileNotFoundError:
75+
# if no localfile, then try to load a config file from assets
76+
path = config_path.joinpath(Path(f"{device_code}.yaml"))
77+
try:
78+
config = InstallerConfig.from_file(path)
79+
logger.info(f"Loaded device config from {path}.")
80+
logger.info(f"Config metadata: {config.metadata}.")
81+
return config
82+
except FileNotFoundError:
83+
logger.info(f"No device config found for {path}.")
84+
return None

openandroidinstaller/openandroidinstaller.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from subprocess import STDOUT, CalledProcessError, call, check_output
2020
from time import sleep
2121
from typing import Callable, Optional
22+
from loguru import logger
2223

2324
import flet
2425
from flet import (
@@ -47,13 +48,12 @@
4748
colors,
4849
icons,
4950
)
50-
from installer_config import InstallerConfig, Step
51-
from loguru import logger
51+
from installer_config import Step, _load_config
5252
from tool_utils import call_tool_with_command, search_device
5353
from widgets import call_button, confirm_button, get_title
5454

5555
# Toggle to True for development purposes
56-
DEVELOPMENT = False
56+
DEVELOPMENT = False
5757
DEVELOPMENT_CONFIG = "sargo" # "a3y17lte" # "sargo"
5858

5959

@@ -199,8 +199,7 @@ def search_devices(self, e):
199199
if device_code:
200200
self.device_name.value = device_code
201201
# load config from file
202-
path = CONFIG_PATH.joinpath(Path(f"{device_code}.yaml"))
203-
device_name = self.load_config(path)
202+
device_name = self.load_config(device_code)
204203

205204
# display success in the application
206205
if device_name:
@@ -394,17 +393,12 @@ def confirm(self, e):
394393
self.view.controls.append(self.final_view)
395394
self.view.update()
396395

397-
def load_config(self, path: str) -> Optional[str]:
398-
"""Function to load a config file from path."""
399-
try:
400-
self.config = InstallerConfig.from_file(path)
396+
def load_config(self, device_code: str) -> Optional[str]:
397+
"""Function to load a config file from device code."""
398+
self.config = _load_config(device_code, CONFIG_PATH)
399+
if self.config:
401400
self.num_total_steps = len(self.config.steps)
402-
logger.info(f"Loaded device config from {path}.")
403-
logger.info(f"Config metadata: {self.config.metadata}.")
404401
return self.config.metadata.get("devicename", "No device name in config.")
405-
except FileNotFoundError:
406-
logger.info(f"No device config found for {path}.")
407-
return None
408402

409403
def pick_image_result(self, e: FilePickerResultEvent):
410404
self.selected_image.value = (
@@ -496,10 +490,12 @@ def call_to_phone(self, e, command: str):
496490
497491
Some parts of the command are changed by placeholders.
498492
"""
493+
# replace placeholders by the required values
499494
command = command.replace("<recovery>", self.recovery_path)
500495
command = command.replace("<image>", self.image_path)
501496
command = command.replace("<inputtext>", self.inputtext.value)
502497

498+
# display a progress ring to show something is happening
503499
self.right_view.controls.append(
504500
Row(
505501
[ProgressRing(color="#00d886")],

openandroidinstaller/widgets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from functools import partial
1717
from typing import Callable
1818

19-
from flet import Column, Container, ElevatedButton, Row, Text, alignment, icons
19+
from flet import Container, ElevatedButton, Row, Text, alignment, icons
2020

2121

2222
def get_title(title: str):

0 commit comments

Comments
 (0)