|
1 | 1 | import os |
2 | 2 |
|
3 | 3 | # Supported image extensions |
4 | | -imageExtensions = ( |
| 4 | +imageExtensions = [ |
5 | 5 | # bmp: |
6 | 6 | '.bmp', |
7 | 7 | # cineon: |
|
57 | 57 | '.zfile', |
58 | 58 | # osl: |
59 | 59 | '.osl', '.oso', '.oslgroup', '.oslbody', |
60 | | - ) |
61 | | -videoExtensions = ( |
| 60 | + ] |
| 61 | +videoExtensions = [ |
62 | 62 | '.avi', '.mov', '.qt', |
63 | 63 | '.mkv', '.webm', |
64 | 64 | '.mp4', '.mpg', '.mpeg', '.m2v', '.m4v', |
65 | 65 | '.wmv', |
66 | 66 | '.ogv', '.ogg', |
67 | 67 | '.mxf', |
68 | | - ) |
69 | | -panoramaInfoExtensions = ('.xml') |
70 | | -meshroomSceneExtensions = ('.mg') |
| 68 | + ] |
| 69 | +panoramaInfoExtensions = ['.xml'] |
| 70 | +meshroomSceneExtensions = ['.mg'] |
71 | 71 |
|
72 | 72 |
|
73 | 73 | def hasExtension(filepath, extensions): |
74 | 74 | """ Return whether filepath is one of the following extensions. """ |
| 75 | + if os.path.isdir(filepath): |
| 76 | + return False |
75 | 77 | return os.path.splitext(filepath)[1].lower() in extensions |
76 | 78 |
|
77 | 79 |
|
@@ -134,13 +136,29 @@ def findFilesByTypeInFolder(folder, recursive=False): |
134 | 136 | continue |
135 | 137 | elif os.path.isdir(currentFolder): |
136 | 138 | if recursive: |
| 139 | + # Get through all of the depth levels |
137 | 140 | for root, directories, files in os.walk(currentFolder): |
138 | 141 | for filename in files: |
139 | 142 | output.addFile(os.path.join(root, filename)) |
140 | 143 | else: |
141 | | - output.addFiles([os.path.join(currentFolder, filename) for filename in os.listdir(currentFolder)]) |
| 144 | + # Only get the first level of depth, so top-level folders' |
| 145 | + # files will be added, if they exist. |
| 146 | + # This may prevent from importing nothing at all when files |
| 147 | + # are nested a level down |
| 148 | + try: |
| 149 | + root, directories, files = next(os.walk(currentFolder)) |
| 150 | + output.addFiles([os.path.join(root, file) for file in files]) |
| 151 | + for directory in directories: |
| 152 | + for file in os.listdir(os.path.join(root, directory)): |
| 153 | + filepath = os.path.join(root, directory, file) |
| 154 | + if os.path.isfile(filepath): |
| 155 | + output.addFile(filepath) |
| 156 | + except (StopIteration, OSError): |
| 157 | + # Directory empty or inaccessible, skip processing |
| 158 | + pass |
| 159 | + |
142 | 160 | else: |
143 | | - # if not a directory or a file, it may be an expression |
| 161 | + # If not a directory or a file, it may be an expression |
144 | 162 | import glob |
145 | 163 | paths = glob.glob(currentFolder) |
146 | 164 | filesByType = findFilesByTypeInFolder(paths, recursive=recursive) |
|
0 commit comments