From: Christian Heller Date: Mon, 9 Sep 2024 14:00:19 +0000 (+0200) Subject: To browser.py, add optional command line argument of start directory. X-Git-Url: https://plomlompom.com/repos/calendar_export?a=commitdiff_plain;h=181640060c0206f1db359707ff157c5fdc441e24;p=stable_plom To browser.py, add optional command line argument of start directory. --- diff --git a/browser.py b/browser.py index 18a1cfa..b1bcc3a 100755 --- a/browser.py +++ b/browser.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 """Browser for image files.""" +from sys import argv as sys_argv from json import dump as json_dump, load as json_load from os.path import exists as path_exists, join as path_join, abspath from exiftool import ExifToolHelper # type: ignore @@ -14,7 +15,7 @@ from gi.repository import GObject, GLib # type: ignore # noqa: E402 from stable.gen_params import (GenParams, # noqa: E402 GEN_PARAMS, GEN_PARAMS_STR) # noqa: E402 -IMG_DIR = '.' +IMG_DIR_DEFAULT = '.' UPPER_DIR = '..' CACHE_PATH = 'cache.json' @@ -102,8 +103,9 @@ class MainWindow(Gtk.Window): sort_selection: Gtk.SingleSelection prev_key: list - def __init__(self, _app, **kwargs): + def __init__(self, app, **kwargs): super().__init__(**kwargs) + self.app = app def init_navbar(): def add_button(label_, on_click, parent_box): @@ -190,7 +192,6 @@ class MainWindow(Gtk.Window): self.add_controller(key_ctl) self.prev_key = [0] - self.img_dir_absolute = abspath(IMG_DIR) self.block_once_hit_file_selection = False self.block_file_selection_updates = False self.force_width, self.force_height = 0, 0 @@ -362,7 +363,7 @@ class MainWindow(Gtk.Window): return selected = self.gallery_selection.props.selected_item if isinstance(selected, DirItem): - self.img_dir_absolute = selected.full_path + self.app.img_dir_absolute = selected.full_path self.load_directory() def update_file_selection(self): @@ -407,7 +408,7 @@ class MainWindow(Gtk.Window): # navbar callables def load_directory(self, update_gallery=True): - """Load into gallery directory at self.img_dir_absolute.""" + """Load into gallery directory at .app.img_dir_absolute.""" def read_directory_into_gallery_items(cache, dir_path, make_parent=False): @@ -451,7 +452,8 @@ class MainWindow(Gtk.Window): json_dump({}, f) with open(CACHE_PATH, 'r', encoding='utf8') as f: cache = json_load(f) - read_directory_into_gallery_items(cache, self.img_dir_absolute, True) + read_directory_into_gallery_items(cache, self.app.img_dir_absolute, + True) with open(CACHE_PATH, 'w', encoding='utf8') as f: json_dump(cache, f) if update_gallery: @@ -558,15 +560,21 @@ class Application(Gtk.Application): """Image browser application class.""" def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.connect('activate', self.on_activate) - - def on_activate(self, app_): - """Start window and keep it open.""" - win = MainWindow(app_) + super().__init__(flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE, + *args, **kwargs) + self.img_dir_absolute = abspath(IMG_DIR_DEFAULT) + + def do_command_line(self, *args, **kwargs): + """Parse directory from command_line, start window and keep it open.""" + command_line = args[0] + argv = command_line.get_arguments() + if len(argv) > 1: + self.img_dir_absolute = abspath(argv[1]) + win = MainWindow(self) win.present() self.hold() + return 0 -app = Application(application_id='plomlompom.com.StablePixBrowser.App') -app.run(None) +main_app = Application(application_id='plomlompom.com.StablePixBrowser.App') +main_app.run(sys_argv)