Commit f1fcb31ecd5710d1bbf0dd0c4a580dad7452f772
1 parent
aede664b
created PathNotFoundException in xglob; yield as 3rd arg instead of raising it
Showing
1 changed file
with
15 additions
and
3 deletions
oletools/thirdparty/xglob/xglob.py
| ... | ... | @@ -60,6 +60,15 @@ __version__ = '0.05' |
| 60 | 60 | |
| 61 | 61 | import os, fnmatch, glob, zipfile |
| 62 | 62 | |
| 63 | +#=== EXCEPTIONS ============================================================== | |
| 64 | + | |
| 65 | +class PathNotFoundException(Exception): | |
| 66 | + """ raised if given a fixed file/dir (not a glob) that does not exist """ | |
| 67 | + def __init__(self, path): | |
| 68 | + super(PathNotFoundException, self).__init__( | |
| 69 | + 'Given path does not exist: %r' % path) | |
| 70 | + | |
| 71 | + | |
| 63 | 72 | #=== FUNCTIONS =============================================================== |
| 64 | 73 | |
| 65 | 74 | # recursive glob function to find files in any subfolder: |
| ... | ... | @@ -118,8 +127,11 @@ def iter_files(files, recursive=False, zip_password=None, zip_fname='*'): |
| 118 | 127 | - then files matching zip_fname are opened from the zip archive |
| 119 | 128 | |
| 120 | 129 | Iterator: yields (container, filename, data) for each file. If zip_password is None, then |
| 121 | - only the filename is returned, container and data=None. Otherwise container si the | |
| 122 | - filename of the container (zip file), and data is the file content. | |
| 130 | + only the filename is returned, container and data=None. Otherwise container is the | |
| 131 | + filename of the container (zip file), and data is the file content (or an exception). | |
| 132 | + If a given filename is not a glob and does not exist, the triplet | |
| 133 | + (None, filename, PathNotFoundException) is yielded. (Globs matching nothing | |
| 134 | + do not trigger exceptions) | |
| 123 | 135 | """ |
| 124 | 136 | #TODO: catch exceptions and yield them for the caller (no file found, file is not zip, wrong password, etc) |
| 125 | 137 | #TODO: use logging instead of printing |
| ... | ... | @@ -132,7 +144,7 @@ def iter_files(files, recursive=False, zip_password=None, zip_fname='*'): |
| 132 | 144 | iglob = glob.iglob |
| 133 | 145 | for filespec in files: |
| 134 | 146 | if not is_glob(filespec) and not os.path.exists(filespec): |
| 135 | - raise ValueError('given path {} does not exist!'.format(filespec)) | |
| 147 | + yield None, PathNotFoundException(filespec), None | |
| 136 | 148 | for filename in iglob(filespec): |
| 137 | 149 | if zip_password is not None: |
| 138 | 150 | # Each file is expected to be a zip archive: | ... | ... |