From d0cf9bced0d1da2d0a6fc3383fdf6aca707ba9e0 Mon Sep 17 00:00:00 2001 From: LecygneNoir Date: Fri, 2 Mar 2018 11:40:15 +0100 Subject: [PATCH] replace argparser with docopt + schema --- ptyt_upload.py | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) mode change 100644 => 100755 ptyt_upload.py diff --git a/ptyt_upload.py b/ptyt_upload.py old mode 100644 new mode 100755 index 7211c2d..343274a --- a/ptyt_upload.py +++ b/ptyt_upload.py @@ -1,3 +1,75 @@ #!/usr/bin/python # coding: utf-8 +""" +ptyt_upload - tool to upload videos to Peertube and Youtube + +Usage: + ptyt_upload --file=/path/to/video [options] + ptyt_upload -h | --help + ptyt_upload --version + +Options: + --file=PATH Path to the video to upload. + --name=STRING Name of the video to upload. (default to video file name) + -d --description=STRING Description of the video. + -t --tags=STRING Tags for the video. (comma separated) + --version Show version. + -h --help Show this help. + + +""" +import argparse +from docopt import docopt +try: + from schema import Schema, And, Or, Use, Optional, SchemaError +except ImportError: + exit('This program requires that the `schema` data-validation library' + ' is installed: \n' + 'see https://github.com/halst/schema\n') +try: + import magic +except ImportError: + exit('This program requires that the `magic` library' + ' is installed, NOT the Python bindings to libmagic API \n' + 'see https://github.com/ahupp/python-magic\n') + +VERSION="ptyt 0.1-alpha" +VALID_PRIVACY_STATUSES = ('public', 'private', 'unlisted') + +def validateVideo(path): + supported_types = ['video/mp4'] + if magic.from_file(path, mime=True) in supported_types: + return path + else: + return False + +if __name__ == '__main__': + + #Allows you to a relative import from the parent folder + import os.path, sys + sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))+"/lib") + + import yt_upload + import pt_upload + + options = docopt(__doc__, version=VERSION) + + schema = Schema({ + '--file': And(str, lambda s: validateVideo(s), error='file is not supported, please use mp4'), + Optional('--name'): Or(None, And(str, lambda x: not x.isdigit(), error="The video name should be a string")), + Optional('--description'): Or(None, And(str, lambda x: not x.isdigit(), error="The video name should be a string")), + Optional('--d'): Or(None, And(str, lambda x: not x.isdigit(), error="The video name should be a string")), + Optional('--tags'): Or(None, And(str, lambda x: not x.isdigit(), error="Tags should be a string")), + Optional('--t'): Or(None, And(str, lambda x: not x.isdigit(), error="Tags should be a string")), + '-h': bool, + '--help': bool, + '--version': bool + }) + + try: + options = schema.validate(options) + except SchemaError as e: + exit(e) + + # yt_upload.run(args) \ No newline at end of file