mickael9  committed b7f96ff

Allow files, directories and patterns to be used instead of just files.

  • Participants
  • Parent commits 45cf415
  • Branches default

Comments (0)

Files changed (1)


 import signal
 import re
+from itertools import chain
+from time import sleep
 from gett import *
-from time import sleep
 url_re = re.compile(r'^(?:http://ge\.tt/|/)?(\w+)(?:/(?:v/(\d+)/?)?)?$')
     except APIError as ex:
         print('API error : %s' % ex)
+def pattern(string):
+    import glob
+    ret = []
+    if os.path.isfile(string):
+        ret.append(open(string, 'rb'))
+    else:
+        if os.path.isdir(string):
+            pattern = string + '/*'
+        else:
+            pattern = string
+        for item in glob.iglob(pattern):
+            if os.path.isfile(item):
+                ret.append(open(item, 'rb'))
+    if not ret:
+        raise argparse.ArgumentTypeError("Invalid pattern")
+    return ret
 def main():
     signal.signal(signal.SIGINT, signal.SIG_DFL)
     parser.add_argument('-D', dest='debug', action='store_true', help='Debug API calls')
     upload_group = parser.add_argument_group('Upload options')
-    upload_group.add_argument('file', nargs='*', type=argparse.FileType('rb'), help="Name of a file to upload")
+    upload_group.add_argument('file', nargs='*', type=pattern, help="Name of a file or a directory to upload. Patterns are allowed. This is not recursive.")
     upload_group.add_argument('-t', dest='title', help='Title of the share')
     upload_group.add_argument('-s', dest='share', help='Name or URL of the share to upload to (defaults to a newly created one)')
     upload_group.add_argument('-P', dest='parallel_upload', action='store_true', help='Upload files in parallel rather than sequentially, the progress bars are displayed in ascending file size order')
         # Create the file URLs
-        for fp in args.file:
+        for fp in chain.from_iterable(args.file):
             name = os.path.basename(
             file = share.create_file(name)