mickael9 avatar mickael9 committed b7f96ff

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

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(fp.name)
             file = share.create_file(name)
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.