Validates user input per oei flags. This function will print to stderr in case user-provided values are invalid (and return False). Returns: True if the input is valid, False if otherwise.
(only, exclude, include, curr_b)
| 195 | |
| 196 | |
| 197 | def _oei_validate(only, exclude, include, curr_b): |
| 198 | """Validates user input per oei flags. |
| 199 | |
| 200 | This function will print to stderr in case user-provided values are invalid |
| 201 | (and return False). |
| 202 | |
| 203 | Returns: |
| 204 | True if the input is valid, False if otherwise. |
| 205 | """ |
| 206 | if only and (exclude or include): |
| 207 | pprint.err( |
| 208 | 'You provided a list of filenames to be committed but also ' |
| 209 | 'provided a list of files to be excluded (-e) or included (-i)') |
| 210 | return False |
| 211 | |
| 212 | err = [] |
| 213 | |
| 214 | def validate(fps, check_fn, msg): |
| 215 | ''' fps: files |
| 216 | check_fn: lambda(file) -> boolean |
| 217 | msg: string-format of pre-defined constant string. |
| 218 | ''' |
| 219 | ret = True |
| 220 | if not fps: |
| 221 | return ret |
| 222 | for fp in fps: |
| 223 | try: |
| 224 | f = curr_b.status_file(fp) |
| 225 | except KeyError: |
| 226 | err.append('File {0} doesn\'t exist'.format(fp)) |
| 227 | ret = False # set error flag, but keep assessing other files |
| 228 | else: # executed after "try", exception will be ignored here |
| 229 | if not check_fn(f): |
| 230 | err.append(msg(fp)) # dynamic string formatting |
| 231 | ret = False |
| 232 | return ret |
| 233 | |
| 234 | only_valid = validate( |
| 235 | only, lambda f: f.type == core.GL_STATUS_UNTRACKED or ( |
| 236 | f.type == core.GL_STATUS_TRACKED and f.modified), |
| 237 | 'File {0} is not a tracked modified or untracked file'.format) |
| 238 | exclude_valid = validate( |
| 239 | exclude, lambda f: f.type == core.GL_STATUS_TRACKED and f.modified, |
| 240 | 'File {0} is not a tracked modified file'.format) |
| 241 | include_valid = validate( |
| 242 | include, lambda f: f.type == core.GL_STATUS_UNTRACKED, |
| 243 | 'File {0} is not an untracked file'.format) |
| 244 | |
| 245 | if only_valid and exclude_valid and include_valid: |
| 246 | return True |
| 247 | |
| 248 | for e in err: |
| 249 | pprint.err(e) |
| 250 | return False |