use argparse and allow sort into other dir

This commit is contained in:
matthias@arch 2023-12-01 20:55:57 +01:00
parent 429a7be16a
commit 1fd5e820f4
2 changed files with 22 additions and 13 deletions

View File

@ -1,7 +1,7 @@
from os import path, getcwd, listdir, mkdir, makedirs, rename from os import path, getcwd, listdir, mkdir, makedirs, rename
import re import re
def read_config(filepath): def read_config(filepath, root_directory="."):
if not path.isfile(filepath): return False if not path.isfile(filepath): return False
file = open(filepath, 'r') file = open(filepath, 'r')
@ -11,7 +11,7 @@ def read_config(filepath):
match = re.match(r". = /?([a-z-A-ZöÖäÄüÜ0-9/: _-]+/)*[a-zA-ZöÖäÄüÜ0-9/: _-]+/?", line) match = re.match(r". = /?([a-z-A-ZöÖäÄüÜ0-9/: _-]+/)*[a-zA-ZöÖäÄüÜ0-9/: _-]+/?", line)
if match: if match:
key, value = line.split(" = ") key, value = line.split(" = ")
keys[key] = value keys[key] = root_directory + "/" + value
return keys return keys
def write_config(filepath, keys): def write_config(filepath, keys):

View File

@ -8,6 +8,8 @@ import curses as c
from os import path, getcwd, listdir, mkdir, makedirs, rename from os import path, getcwd, listdir, mkdir, makedirs, rename
from sys import argv from sys import argv
import argparse
settings = { settings = {
"q": "quit", "q": "quit",
"s": "skip", "s": "skip",
@ -198,6 +200,8 @@ class Sorter:
for k, v in self.keys.items(): for k, v in self.keys.items():
if i >= self.win_y - KEYS_BEGIN - FOOTER_HEIGHT: # dont write into footer if i >= self.win_y - KEYS_BEGIN - FOOTER_HEIGHT: # dont write into footer
break break
# show only last part
v = v.split("/")[-1]
if k == self.pressed_key: if k == self.pressed_key:
self.window.addnstr(KEYS_BEGIN + i, 0, f" {k}: {v}", SIDEBAR_WIDTH, c.A_STANDOUT) self.window.addnstr(KEYS_BEGIN + i, 0, f" {k}: {v}", SIDEBAR_WIDTH, c.A_STANDOUT)
else: else:
@ -234,20 +238,25 @@ def main():
Image Sorter Image Sorter
=================================================================================================== ===================================================================================================
""") """)
if len(argv) > 1: parser = argparse.ArgumentParser("imgsort")
wd = path.abspath(argv[1]) parser.add_argument("-c", "--config", action="store", help="name of the config file in ~/.config/imgsort")
else: parser.add_argument("-i", "--sort-dir", action="store", help="the directory where the folders from the config will be created")
args = parser.parse_args()
wd = getcwd(); wd = getcwd();
config_name = select_config() if args.sort_dir:
if type(config_name) == str: args.sort_dir = path.abspath(args.sort_dir)
config = read_config(config_name)
else: 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: if not config:
print("Error reading the config:") print("Error reading the config:")
print(" Config Name:", config_name) print(" Config Name:", args.config)
print(" Config:", config) print(" Config:", config)
exit(1) exit(1)