use argparse and allow sort into other dir

This commit is contained in:
matthias@arch 2023-12-01 20:55:57 +01:00
parent 4dcda19614
commit 56f5423766
2 changed files with 27 additions and 21 deletions

View File

@ -1,9 +1,8 @@
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): if not path.isfile(filepath): return False
raise FileNotFoundError(f"read_config: Invalid filepath {filepath}")
file = open(filepath, 'r') file = open(filepath, 'r')
keys = {} keys = {}
@ -12,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

@ -17,6 +17,8 @@ if __name__ == "__main__": # make relative imports work as described here: http
from .configs import read_config, write_config, select_config, create_config from .configs import read_config, write_config, select_config, create_config
import argparse
settings = { settings = {
"q": "quit", "q": "quit",
"s": "skip", "s": "skip",
@ -219,6 +221,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:
@ -239,12 +243,15 @@ class Sorter:
return new_path return new_path
def quit(self, message = ""): def quit(self, message = ""):
<<<<<<< HEAD
print(message) print(message)
print(f"Quitting imgsort {version}") print(f"Quitting imgsort {version}")
exit(0) exit(0)
def __del__(self): def __del__(self):
=======
>>>>>>> 1fd5e82 (use argparse and allow sort into other dir)
self.window.clear() self.window.clear()
self.window.refresh() self.window.refresh()
c.endwin() c.endwin()
@ -253,27 +260,26 @@ class Sorter:
def main(): def main():
# set working directory # set working directory
print(f"""=================================================================================================== print("""
imgsort {version} - Image Sorter ===================================================================================================
===================================================================================================""") Image Sorter
# set directories ===================================================================================================
config_dir = os.path.join(os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config")), "regina") """)
config_dir = os.path.join(os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config")), "imgsort")
# check if environment variables are set and use them if they are # check if environment variables are set and use them if they are
if 'IMGSOSRT_CONFIG_DIR' in os.environ: config_dir = os.environ['IMGSORT_CONFIG_DIR'] if 'IMGSOSRT_CONFIG_DIR' in os.environ: config_dir = os.environ['IMGSORT_CONFIG_DIR']
parser = argparse.ArgumentParser(prog="imgsort") parser = argparse.ArgumentParser("imgsort")
parser.add_argument("--config", "-c", action="store", help="name or path of config file", metavar="config file") parser.add_argument("-c", "--config", action="store", help="name of the config file in ~/.config/imgsort")
parser.add_argument("--dir", "-d", action="store", help="working directory", metavar="working directory") parser.add_argument("-i", "--sort-dir", action="store", help="the directory where the folders from the config will be created")
args = parser.parse_args() args = parser.parse_args()
# working directory wd = getcwd();
if args.dir:
if not path.isdir(args.dir): if args.sort_dir:
parser.error(f"invalid working directory: {args.wdir}") args.sort_dir = path.abspath(args.sort_dir)
wd = path.abspath(args.dir)
os.chdir(wd)
else: else:
wd = getcwd() args.sort_dir = getcwd()
# configuration # configuration
if args.config: if args.config:
@ -284,8 +290,9 @@ imgsort {version} - Image Sorter
if not path.isfile(config_path): if not path.isfile(config_path):
parser.error(f"invalid configuration path/name:'{config_path}'/'{args.config}'") parser.error(f"invalid configuration path/name:'{config_path}'/'{args.config}'")
else: else:
config_path = select_config() args.config = select_config()
print(config_path) print(config_path)
config = read_config(args.config, root_directory=args.sort_dir)
if config_path is not None: if config_path is not None:
config = read_config(config_path) config = read_config(config_path)
else: else:
@ -293,7 +300,7 @@ imgsort {version} - Image Sorter
if not config: if not config:
print("Error reading the config:") print("Error reading the config:")
print(" Config Name:", config_path) print(" Config Name:", args.config)
print(" Config:", config) print(" Config:", config)
exit(1) exit(1)