From 1fd5e820f4e133246e9e89f92c1028017ba4d597 Mon Sep 17 00:00:00 2001 From: "matthias@arch" Date: Fri, 1 Dec 2023 20:55:57 +0100 Subject: [PATCH] use argparse and allow sort into other dir --- imgsort/configs.py | 6 +++--- imgsort/sorter.py | 29 +++++++++++++++++++---------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/imgsort/configs.py b/imgsort/configs.py index ac94b83..3bf3146 100755 --- a/imgsort/configs.py +++ b/imgsort/configs.py @@ -1,9 +1,9 @@ from os import path, getcwd, listdir, mkdir, makedirs, rename import re -def read_config(filepath): +def read_config(filepath, root_directory="."): if not path.isfile(filepath): return False - + file = open(filepath, 'r') keys = {} for line in file.readlines(): @@ -11,7 +11,7 @@ def read_config(filepath): match = re.match(r". = /?([a-z-A-ZöÖäÄüÜ0-9/: _-]+/)*[a-zA-ZöÖäÄüÜ0-9/: _-]+/?", line) if match: key, value = line.split(" = ") - keys[key] = value + keys[key] = root_directory + "/" + value return keys def write_config(filepath, keys): diff --git a/imgsort/sorter.py b/imgsort/sorter.py index 775c8b6..375212c 100755 --- a/imgsort/sorter.py +++ b/imgsort/sorter.py @@ -8,6 +8,8 @@ import curses as c from os import path, getcwd, listdir, mkdir, makedirs, rename from sys import argv +import argparse + settings = { "q": "quit", "s": "skip", @@ -198,6 +200,8 @@ class Sorter: for k, v in self.keys.items(): if i >= self.win_y - KEYS_BEGIN - FOOTER_HEIGHT: # dont write into footer break + # show only last part + v = v.split("/")[-1] if k == self.pressed_key: self.window.addnstr(KEYS_BEGIN + i, 0, f" {k}: {v}", SIDEBAR_WIDTH, c.A_STANDOUT) else: @@ -217,7 +221,7 @@ class Sorter: rename(file, new_path) return new_path - def quit(self, message = ""): + def quit(self, message = ""): self.window.clear() self.window.refresh() c.endwin() @@ -234,20 +238,25 @@ def main(): Image Sorter =================================================================================================== """) - if len(argv) > 1: - wd = path.abspath(argv[1]) - else: - wd = getcwd(); + parser = argparse.ArgumentParser("imgsort") + parser.add_argument("-c", "--config", action="store", help="name of the config file in ~/.config/imgsort") + parser.add_argument("-i", "--sort-dir", action="store", help="the directory where the folders from the config will be created") + args = parser.parse_args() - config_name = select_config() - if type(config_name) == str: - config = read_config(config_name) + wd = getcwd(); + + if args.sort_dir: + args.sort_dir = path.abspath(args.sort_dir) else: - config = create_config() + args.sort_dir = getcwd() + + if not args.config: + args.config = select_config() + config = read_config(args.config, root_directory=args.sort_dir) if not config: print("Error reading the config:") - print(" Config Name:", config_name) + print(" Config Name:", args.config) print(" Config:", config) exit(1)