Compare commits

..

No commits in common. "466af2ccf09be1c15a564b21b51be99b01e15bc4" and "59159e4ad224024240f5dc68809714efc2a6f8d5" have entirely different histories.

2 changed files with 35 additions and 73 deletions

View File

@ -2,8 +2,7 @@ from os import path, getcwd, listdir, mkdir, makedirs, rename
import re
def read_config(filepath):
if not path.isfile(filepath):
raise FileNotFoundError(f"read_config: Invalid filepath {filepath}")
if not path.isfile(filepath): return False
file = open(filepath, 'r')
keys = {}
@ -27,12 +26,13 @@ def create_config():
"""
===================================================================================================
Creating a new config
===================================================================================================
Please enter at least one key and one directory.
The key must be one single letter, a single digit number or some other keyboard key like .-#+&/ ...
The key can not be 'q', 's', 'o' or 'u'.
The key can not be 'q', 's' or 'u'.
The directory must be a valid path to a directory, but is does not have to exist.
You can use an absolute path (starting with '/', not '~') or a relative path (from here).
===================================================================================================
You can use an absolute path (starting with '/') or a relative path (from here).
Do not use '~'!
"""
)
@ -93,9 +93,9 @@ def select_config():
print(f"{n}: {conf}")
choice = input("Please select a config: ")
if choice == "0": return None
if choice == "0": return False
elif choice in configs:
return path.normpath(config_path + "/" + configs[choice])
else:
print("Invalid choice - creating new config")
return None
return False

View File

@ -1,27 +1,17 @@
#!/bin/python3
from imgsort.ueberzug import UeberzugLayer
from imgsort.configs import read_config, write_config, select_config, create_config
import curses as c
import argparse
import os
from os import path, getcwd, listdir, mkdir, makedirs, rename
import subprocess
if __name__ == "__main__": # make relative imports work as described here: https://peps.python.org/pep-0366/#proposed-change
if __package__ is None:
__package__ = "imgsort"
import sys
filepath = path.realpath(path.abspath(__file__))
sys.path.insert(0, path.dirname(path.dirname(filepath)))
from .ueberzug import UeberzugLayer
from .configs import read_config, write_config, select_config, create_config
from sys import argv
settings = {
"q": "quit",
"s": "skip",
"u": "undo",
"o": "open"
}
# Size settings
@ -35,8 +25,6 @@ CURSOR_Y = 2
KEYS_BEGIN = 5
version = "1.2"
class Sorter:
def __init__(self, wdir, config):
self.wd = wdir
@ -75,6 +63,9 @@ class Sorter:
self._img_height = self.win_y - FOOTER_HEIGHT - 2
self._img_identifier = "imgsort_preview"
# version
self.version = "Image Sorter 1.1"
def validate_dirs(self):
"""
Create the directories that dont exist.
@ -140,12 +131,6 @@ class Sorter:
else:
self.message = "Nothing to undo!"
continue
elif settings[self.pressed_key] == "open":
try:
subprocess.run(['xdg-open', self.image])
self.message = "Opening with xdg-open"
except Exception as e:
print(f"open: Error: {e}")
# move to folder
elif self.pressed_key in self.keys:
@ -154,7 +139,7 @@ class Sorter:
self.images_new[self.image_iter] = new_filepath
self.message = f"Moved image to {self.keys[self.pressed_key]}"
else:
self.message = f"ERROR: Failed to move '{self.image}' to '{self.keys[self.pressed_key]}'."
self.message = f"ERROR: Failed to move '{self.image}' to '{keys[self.pressed_key]}'."
self.image_iter += 1
self.quit("All done!")
@ -170,9 +155,8 @@ class Sorter:
self.window.vline(0, SIDEBAR_WIDTH, '|', self.win_y - FOOTER_HEIGHT + 1)
# version
version_str = f"imgsort {version}"
x = self.win_x - len(version_str) - 1
self.window.addstr(self.win_y - 1, x, version_str)
x = self.win_x - len(self.version) - 1
self.window.addstr(self.win_y - 1, x, self.version)
# wd
wdstring = f"Sorting {self.wd} - {len(self.images)} files - {len(self.images) - self.image_iter} remaining."
@ -238,54 +222,32 @@ class Sorter:
self.window.refresh()
c.endwin()
print(message)
print(f"Quitting imgsort {version}")
print("Quitting " + self.version)
exit(0)
def main():
# set working directory
print(f"""===================================================================================================
imgsort {version} - Image Sorter
===================================================================================================""")
# set directories
config_dir = os.path.join(os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config")), "regina")
# 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']
parser = argparse.ArgumentParser(prog="imgsort")
parser.add_argument("--config", "-c", action="store", help="name or path of config file", metavar="config file")
parser.add_argument("--dir", "-d", action="store", help="working directory", metavar="working directory")
args = parser.parse_args()
# working directory
if args.dir:
if not path.isdir(args.dir):
parser.error(f"invalid working directory: {args.wdir}")
wd = path.abspath(args.dir)
os.chdir(wd)
print("""
===================================================================================================
Image Sorter
===================================================================================================
""")
if len(argv) > 1:
wd = path.abspath(argv[1])
else:
wd = getcwd()
wd = getcwd();
# configuration
if args.config:
config_path = args.config_file
# search locally
if not path.isfile(config_path):
config_path = path.join(config_dir, args.config)
if not path.isfile(config_path):
parser.error(f"invalid configuration path/name:'{config_path}'/'{args.config}'")
else:
config_path = select_config()
print(config_path)
if config_path is not None:
config = read_config(config_path)
config_name = select_config()
if type(config_name) == str:
config = read_config(config_name)
else:
config = create_config()
if not config:
print("Error reading the config:")
print(" Config Name:", config_path)
print(" Config Name:", config_name)
print(" Config:", config)
exit(1)