Optionally allow dashes to Path. This fixes #444
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 2be872b · Browse files · 2 files·+33 added−19 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+33 added−19 removed
- other:+33 added−19 removed
Provenance
- Commit
ed978a6- Tree
fce10ce- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
2be872b
2 files changed+33 added−19 removed
modified:click/types.py+31 added-19 removed
341 unmodified lines342 different checks. First of all, instead of returning an open file343 handle it returns just the filename. Secondly, it can perform various344 basic checks about what the file or directory should be.345 346+ .. versionchanged:: 6.0347+ `allow_dash` was added.348+349 :param exists: if set to true, the file or directory needs to exist for350 this value to be valid. If this is not required and a351 file does indeed not exist, then all further checks are352 silently skipped.3 unmodified lines356 :param readable: if true, a readable check is performed.357 :param resolve_path: if this is true, then the path is fully resolved358 before the value is passed onwards. This means359 that it's absolute and symlinks are resolved.360+ :param allow_dash: If this is set to `True`, a single dash to indicate361+ standard streams is permitted.362 :param type: optionally a string type that should be used to363 represent the path. The default is `None` which364 means the return value will be either bytes or365 unicode depending on what makes most sense given the2 unmodified lines368 envvar_list_splitter = os.path.pathsep369 370 def __init__(self, exists=False, file_okay=True, dir_okay=True,371 writable=False, readable=True, resolve_path=False,367- path_type=None):372+ allow_dash=False, path_type=None):373 self.exists = exists374 self.file_okay = file_okay375 self.dir_okay = dir_okay376 self.writable = writable377 self.readable = readable378 self.resolve_path = resolve_path379+ self.allow_dash = allow_dash380 self.type = path_type381 382 if self.file_okay and not self.dir_okay:383 self.name = 'file'4 unmodified lines388 else:389 self.name = 'path'390 self.path_type = 'Path'391 392+ def coerce_path_result(self, rv):393+ if self.type is not None and not isinstance(rv, self.type):394+ if self.type is text_type:395+ rv = rv.decode(get_filesystem_encoding())396+ else:397+ rv = rv.encode(get_filesystem_encoding())398+ return rv399+400 def convert(self, value, param, ctx):401 rv = value388- if self.resolve_path:389- rv = os.path.realpath(rv)402 391- try:392- st = os.stat(rv)393- except OSError:394- if not self.exists:395- return rv396- self.fail('%s "%s" does not exist.' % (397- self.path_type,398- filename_to_ui(value)399- ), param, ctx)403+ is_dash = self.file_okay and self.allow_dash and rv in (b'-', '-')404 405+ if not is_dash:406+ if self.resolve_path:407+ rv = os.path.realpath(rv)408+409+ try:410+ st = os.stat(rv)411+ except OSError:412+ if not self.exists:413+ return self.coerce_path_result(rv)414+ self.fail('%s "%s" does not exist.' % (415+ self.path_type,416+ filename_to_ui(value)417+ ), param, ctx)418+419 if not self.file_okay and stat.S_ISREG(st.st_mode):420 self.fail('%s "%s" is a file.' % (421 self.path_type,422 filename_to_ui(value)13 unmodified lines436 self.path_type,437 filename_to_ui(value)438 ), param, ctx)439 422- if self.type is not None and not isinstance(rv, self.type):423- if self.type is text_type:424- rv = rv.decode(get_filesystem_encoding())425- else:426- rv = rv.encode(get_filesystem_encoding())440+ return self.coerce_path_result(rv)441 428- return rv442 430-443 class Tuple(CompositeParamType):444 """The default behavior of Click is to apply a type on a value directly.445 This works well in most cases, except for when `nargs` is set to a fixed446 count and different types should be used for different items. In this102 unmodified linesmodified:CHANGES+2 added
32 unmodified lines33 now work with full unicode functionality in the Python windows console34 by emulating an output stream. This also applies to getting the35 virtual output and input streams via `click.get_text_stream(...)`.36 - Unittests now always force a certain virtual terminal width.37+- Added support for allowing dashes to indicate standard streams to the38+ `Path` type.39 40 Version 5.141 -----------42 251 unmodified lines