diff --git a/functions.py b/functions.py index 2c9b054..da7b14c 100644 --- a/functions.py +++ b/functions.py @@ -94,6 +94,15 @@ def clean_dir(dirfullpath): if os.path.isfile(f): os.remove(f) +def print_colormsg(msg, level): + if level == 'fail': + print(Colors.FAIL) + elif level == 'warning': + print(Colors.WARNING) + elif level == 'ok': + print(Colors.BLUE) + print(msg) + print(Colors.ENDC) class Colors: diff --git a/sandbox/upload_imgs_dir.py b/upload_imgs_dir.py similarity index 79% rename from sandbox/upload_imgs_dir.py rename to upload_imgs_dir.py index d43e46f..0ec4fe2 100644 --- a/sandbox/upload_imgs_dir.py +++ b/upload_imgs_dir.py @@ -1,6 +1,6 @@ import os, argparse, sys, re from mwclient import Site - +from functions import print_colormsg p = argparse.ArgumentParser(description='Upload files from a directory, with metadata values to the wiki.\n' 'Note that any value containing spaces should be between quotation marks', @@ -23,7 +23,7 @@ args = p.parse_args() site = Site(host=args.host, path=args.path, scheme=args.scheme) -wd = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # parent working directory +wd =os.path.dirname(os.path.abspath(__file__)) # parent working directory with open(os.path.join(wd, 'login.txt'), 'r') as login: # read login user & pwd loginlines = login.read() user, pwd = loginlines.split('\n') @@ -36,22 +36,25 @@ with open(os.path.join(wd, 'login.txt'), 'r') as login: # read login user & pwd user, pwd = loginlines.split('\n') site.login(username=user, password=pwd) # login to wiki -# todo: loop though images in folder (.jpg, .jpeg, .png) ensuring correct order 1 before 11 if os.path.isdir(args.dir) is False: - print(f'Error: --dir {args.dir} absolute path cannot be found') + print_colormsg(f'Error: --dir {args.dir} absolute path cannot be found', level='fail') sys.exit() elif not re.match(r'\d{4}\/\d{2}\/\d{2}', args.date): - print(f'Error: --date {args.date} format should be --date "yyyy/mm/dd"') + print_colormsg(f'Error: --date {args.date} format should be --date "yyyy/mm/dd"', level='fail') sys.exit() elif len(list(site.ask(f'[[Title::{args.title}]]'))) > 0: - print(f'Error: --title "{args.title}" already exists in wiki. Please provide a different one') + print_colormsg(f'Error: --title "{args.title}" already exists in wiki. Provide a different one', level='fail') sys.exit() - +# TODO os.listdir(args.dir) ORDER! +# TODO PartOf from os.listdir(args.dir) that fit on to condition +# TODO Part base on os.lisdir order! for _file in os.listdir(args.dir): - print(_file) - + if (os.path.splitext(_file)[-1]).lower() not in ['.jpg', '.jpeg', '.png']: + print_colormsg(f'File: {_file} is not a .jpg .jpeg or .png hence will Not be upload', level='warning') + else: + print_colormsg(_file, level='ok') if args.dry == True: print(args)