aboutsummaryrefslogtreecommitdiff
path: root/file-tagger.py
diff options
context:
space:
mode:
Diffstat (limited to 'file-tagger.py')
-rw-r--r--file-tagger.py119
1 files changed, 5 insertions, 114 deletions
diff --git a/file-tagger.py b/file-tagger.py
index 9812f62..82aacf7 100644
--- a/file-tagger.py
+++ b/file-tagger.py
@@ -5,119 +5,8 @@ from gui import GuiMain, GuiImage, GuiTag
import cv2
import logging
import magic
-import subprocess
-import re
-import platform
-import readline
-
-'''
-Fetch input prompt with prefilled text.
-
-Parameters:
-prompt: Prompt message.
-text: Prefilled input.
-'''
-def input_with_prefill(prompt, text):
- def hook():
- readline.insert_text(text)
- readline.redisplay()
- readline.set_pre_input_hook(hook)
- result = input(prompt)
- readline.set_pre_input_hook()
- return result
-
-'''
-Checks if the given string is a valid path.
-
-Parameters:
-string: String to be checked.
-'''
-def dir_path(string):
- if os.path.isdir(string):
- return string
- else:
- raise NotADirectoryError(string)
-
-'''
-Opens the given file with the platform default handler.
-
-Parameters:
-file: Path to the file.
-'''
-def open_system(file):
- if platform.system() == 'Darwin': # macOS
- subprocess.call(('open', file))
- elif platform.system() == 'Windows': # Windows
- os.startfile(file)
- else: # linux variants
- subprocess.call(('xdg-open', file))
-
-'''
-Initializes TMSU in a given directory.
-Does nothing, if it is already initialized.
-
-Parameters:
-base: Directory to initialize.
-'''
-def tmsu_init(base):
- logger = logging.getLogger(__name__)
- if not os.path.exists(os.path.join(base, ".tmsu")):
- logger.info("TMSU database does not exist, creating ...")
- proc = subprocess.Popen(["tmsu", "init"], cwd=base)
- proc.wait()
- logger.debug("TMSU returncode: {}".format(proc.returncode))
- if proc.returncode != 0:
- logger.error("Could not initialize TMSU database.")
- return False
- return True
-
-'''
-Reads the tags for the specified file.
-
-Parameters:
-base: Base directory for the database.
-file: File to get the tags for.
-'''
-def tmsu_tags(base, file):
- logger = logging.getLogger(__name__)
- logger.debug("Getting existing tags for file {}".format(file))
- tags = set()
- proc = subprocess.Popen(["tmsu", "tags", os.path.relpath(file, base)], cwd=base, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- proc.wait()
- logger.debug("TMSU returncode: {}".format(proc.returncode))
- if proc.returncode == 0:
- ret = proc.stdout.read().decode()
- logger.debug("Raw TMSU tags: {}".format(ret))
- tags.update(re.split("\s", ret.split(":")[1])[1:-1])
- else:
- logger.error("Could not get tags for file {}".format(file))
- return tags
-
-'''
-Sets tags for the specified file.
-
-Parameters:
-base: Base directory for the database.
-file: File to set the tags for.
-tags: Tags to set.
-untag: If True, it will remove all existing tags first. If False, it will just append new tags.
-'''
-def tmsu_tag(base, file, tags, untag=True):
- logger = logging.getLogger(__name__)
- if untag:
- logger.debug("Untagging file")
- proc = subprocess.Popen(["tmsu", "untag", "--all", os.path.relpath(file, base)], cwd=base, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- proc.wait()
- if proc.returncode != 0:
- logger.error("Could not untag file {}".format(file))
- if tags:
- logger.debug("Writing tags {}".format(tags))
- proc = subprocess.Popen(["tmsu", "tag", os.path.relpath(file, base)] + list(tags), cwd=base, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- proc.wait()
- if proc.returncode != 0:
- logger.error("Could not write tags to file {}".format(file))
- else:
- logger.info("Tags are empty, ignoring")
+from tmsu import *
+from util import *
'''
Walk over all files for the given base directory and all subdirectories recursively.
@@ -199,7 +88,7 @@ def walk(args):
elif ret[0] == GuiTag.RETURN_ABORT:
return
- if not args["gui_tag"]:
+ if ((not args["gui_tag"]) and (not args["skip_prompt"])):
tags = set(input_with_prefill("\nTags for file {}:\n".format(file_path), ','.join(tags)).split(","))
logger.info("Tagging {}".format(tags))
@@ -215,6 +104,7 @@ if __name__ == "__main__":
parser.add_argument('--predict-images-top', nargs='?', const=1, default=10, type=int, help='Defines how many top prediction keywords should be used (default: %(default)s)')
parser.add_argument('--gui-tag', nargs='?', const=1, default=False, type=bool, help='Show GUI for tagging (default: %(default)s)')
parser.add_argument('--open-system', nargs='?', const=1, default=False, type=bool, help='Open all files with system default (default: %(default)s)')
+ parser.add_argument('-s', '--skip-prompt', nargs='?', const=1, default=False, type=bool, help='Skip prompt for file tags (default: %(default)s)')
parser.add_argument('-i', '--index', nargs='?', const=1, default=0, type=int, help='Start tagging at the given file index (default: %(default)s)')
parser.add_argument('-v', '--verbose', action="count", default=0, help="Verbosity level")
args = parser.parse_args()
@@ -237,6 +127,7 @@ if __name__ == "__main__":
"predict_images_top": args.predict_images_top,
"gui_tag": args.gui_tag,
"open_system": args.open_system,
+ "skip_prompt": args.skip_prompt,
"index": args.index,
"verbosity": args.verbose
}