diff options
author | Leonard Kugis <leonard@kug.is> | 2023-03-31 03:27:21 +0200 |
---|---|---|
committer | Leonard Kugis <leonard@kug.is> | 2023-03-31 03:27:21 +0200 |
commit | 1433cd6f2dccb6b981209a2cc2a3ac6a45156387 (patch) | |
tree | 4ccdcf7aa650b746437fcb551f4e5d1806b655c2 | |
parent | a76c7a1839573052ea7694ea63aea31a6cd137f7 (diff) |
TMSU: Classified and added option to override default command.
-rw-r--r-- | file-tagger.py | 14 | ||||
-rw-r--r-- | tmsu.py | 126 |
2 files changed, 74 insertions, 66 deletions
diff --git a/file-tagger.py b/file-tagger.py index 36953ec..9708dfa 100644 --- a/file-tagger.py +++ b/file-tagger.py @@ -16,7 +16,7 @@ Walk over all files for the given base directory and all subdirectories recursiv Parameters: args: Argument dict. ''' -def walk(args): +def walk(tmsu, args): logger = logging.getLogger(__name__) logger.info("Walking files ...") @@ -43,7 +43,7 @@ def walk(args): for i in range(args["index"], len(files)): file_path = files[i] logger.info("Handling file {}, {}".format(i, file_path)) - tags = tmsu_tags(args["base"], file_path) + tags = tmsu.tags(file_path) not_empty = bool(tags) logger.info("Existing tags: {}".format(tags)) @@ -97,7 +97,7 @@ def walk(args): tags = set(input_with_prefill("\nTags for file {}:\n".format(file_path), ','.join(tags)).split(",")) logger.info("Tagging {}".format(tags)) - tmsu_tag(args["base"], file_path, tags, untag=not_empty) + tmsu.tag(file_path, tags, untag=not_empty) if __name__ == "__main__": @@ -105,6 +105,7 @@ if __name__ == "__main__": parser.add_argument('-b', '--base', nargs='?', default='.', type=dir_path, help='Base directory with database (default: %(default)s)') parser.add_argument('-f', '--file-dir', nargs='?', default='.', type=dir_path, help='File directory for walking (default: %(default)s)') parser.add_argument('-g', '--gui', nargs='?', const=1, default=False, type=bool, help='Show main GUI (default: %(default)s)') + parser.add_argument('--tmsu-command', nargs='?', const=1, default="tmsu", type=str, help='TMSU command override (default: %(default)s)') parser.add_argument('--predict-images', nargs='?', const=1, default=False, type=bool, help='Use prediction for image tagging (default: %(default)s)') parser.add_argument('--predict-images-backend', nargs='?', const=1, choices=["torch", "tensorflow", "keras"], default="torch", type=str.lower, help='Determines which backend should be used for keyword prediction (default: %(default)s)') 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)') @@ -133,6 +134,7 @@ if __name__ == "__main__": "base": args.base, "file_dir": args.file_dir, "gui": args.gui, + "tmsu_command": args.tmsu_command, "predict_images": args.predict_images, "predict_images_backend": args.predict_images_backend, "predict_images_top": args.predict_images_top, @@ -153,5 +155,7 @@ if __name__ == "__main__": logger.debug("Starting main GUI ...") args = GuiMain(args).loop() - if tmsu_init(args["base"]): - walk(args) + tmsu = TMSU(args["base"], args["tmsu_command"]) + + if tmsu.status: + walk(tmsu, args) @@ -3,69 +3,73 @@ import re import logging import os -''' -Initializes TMSU in a given directory. -Does nothing, if it is already initialized. +class TMSU(object): -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 + ''' + Initializes TMSU in a given directory. + Does nothing, if it is already initialized. -''' -Reads the tags for the specified file. + Parameters: + base: Directory to initialize. + ''' + def __init__(self, base, command): + self.__base = base + self.__command = command + 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([self.__command, "init"], cwd=self.__base) + proc.wait() + logger.debug("TMSU returncode: {}".format(proc.returncode)) + if proc.returncode != 0: + logger.error("Could not initialize TMSU database.") + self.status = False + self.status = True -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 + ''' + Reads the tags for the specified file. -''' -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) + Parameters: + base: Base directory for the database. + file: File to get the tags for. + ''' + def tags(self, file): + logger = logging.getLogger(__name__) + logger.debug("Getting existing tags for file {}".format(file)) + tags = set() + proc = subprocess.Popen([self.__command, "tags", os.path.relpath(file, self.__base)], cwd=self.__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")
\ No newline at end of file + 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 tag(self, file, tags, untag=True): + logger = logging.getLogger(__name__) + if untag: + logger.debug("Untagging file") + proc = subprocess.Popen([self.__command, "untag", "--all", os.path.relpath(file, self.__base)], cwd=self.__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([self.__command, "tag", os.path.relpath(file, self.__base)] + list(tags), cwd=self.__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")
\ No newline at end of file |