reddit-image-wall-getter/reddit_imgs/runner.py

91 lines
3.1 KiB
Python
Executable File

#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
import os
import shutil
import subprocess
import sys
from pathlib import Path
import reddit_imgs.condensate_hashes
import reddit_imgs.download_pruner
import reddit_imgs.fetch2
import reddit_imgs.hashit2
import reddit_imgs.linguisticdictanal
import reddit_imgs.sizebysubreddit
import reddit_imgs.suggest_subreddits_from_links
import reddit_imgs.sync
import reddit_imgs.wallpapers2
wdir = os.path.abspath('.')
def ensureFolderAvailability():
if not os.path.exists(os.path.join(wdir, 'w')):
os.makedirs(os.path.join(wdir, 'w'))
if not os.path.exists(os.path.join(wdir, 'd')):
os.makedirs(os.path.join(wdir, 'd'))
if not os.path.exists(os.path.join(wdir, 'i')):
os.makedirs(os.path.join(wdir, 'i'))
if not os.path.exists(os.path.join(wdir, 'r')):
os.makedirs(os.path.join(wdir, 'r'))
def main():
# ensureFolderAvailability()
if len(sys.argv) > 1:
cmdline()
else:
mainmenu()
def main_unshared():
cmdline(True)
def cmdline(run_each_on_subprocess=False):
cmds = sys.argv[1:]
available_commands = ((
('sync', reddit_imgs.sync.cmdline),
('fetch', reddit_imgs.fetch2.cmdline),
('suggest_subreddits_from_links', reddit_imgs.suggest_subreddits_from_links.cmdline),
('prune_downloads', reddit_imgs.download_pruner.cmdline),
('hashit', reddit_imgs.hashit2.cmdline),
('condensate_hashes', reddit_imgs.condensate_hashes.cmdline),
('size_by_subreddit', reddit_imgs.sizebysubreddit.cmdline),
('wallpapers', reddit_imgs.wallpapers2.cmdline),
('linguistic_dictionary_analysis', reddit_imgs.linguisticdictanal.cmdline),
# ('cachedhash', reddit_imgs.cachedhash.main),
# ('hashit', reddit_imgs.hashit.main),
# ('normalizetobmp', reddit_imgs.normalizetobmp.main),
# ('thumbnailize', reddit_imgs.thumbnailize.main),
# ('reorganize', reddit_imgs.reorganize.main),
))
available_commands_names = tuple(
list(map(lambda a: a[0], available_commands)))
for cmd in cmds:
if cmd.split(':', 1)[0] not in available_commands_names:
print('Usage {0} [{1}]'.format(sys.argv[0],
'/'.join(available_commands_names)))
return
for cmd in cmds:
command_ran = False
for acmd in available_commands:
if cmd.split(':', 1)[0] == acmd[0]:
command_ran = True
if run_each_on_subprocess:
the_other_callable = Path(__file__).parent.parent.joinpath('redditgetter.py').absolute()
handler = subprocess.run([str(the_other_callable), cmd])
handler.check_returncode()
else:
cmd_callable = acmd[1]
cmd_name, fcmd, *_ = *cmd.split(':', 1), ''
cmd_callable(encoded_args=fcmd)
if not command_ran:
print('Usage {0} [{1}]'.format(sys.argv[0],
'/'.join(available_commands_names)))
if __name__ == '__main__':
main()