Included click.DateTime type (with tests)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Santiago Basulto · committed by David Lord · parent 3e17a3d · Browse files · 4 files·+75 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+75 added−4 removed
- tests:+37 added−1 removed
- other:+38 added−3 removed
Provenance
- Commit
6e62332- Tree
9011720- Author
Santiago Basulto <santiago.basulto@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
3e17a3d
4 files changed+75 added−4 removed
modified:click/__init__.py+4 added-3 removed
26 unmodified lines27 password_option, version_option, help_option28 29 # Types30 from .types import ParamType, File, Path, Choice, IntRange, Tuple, \31- STRING, INT, FLOAT, BOOL, UUID, UNPROCESSED, FloatRange31+ DateTime, STRING, INT, FLOAT, BOOL, UUID, UNPROCESSED, FloatRange32 33 # Utilities34 from .utils import echo, get_binary_stream, get_text_stream, open_file, \35 format_filename, get_app_dir, get_os_args28 unmodified lines64 'argument', 'option', 'confirmation_option', 'password_option',65 'version_option', 'help_option',66 67 # Types68- 'ParamType', 'File', 'Path', 'Choice', 'IntRange', 'Tuple', 'STRING',69- 'INT', 'FLOAT', 'BOOL', 'UUID', 'UNPROCESSED', 'FloatRange',68+ 'ParamType', 'File', 'Path', 'Choice', 'IntRange', 'Tuple',69+ 'DateTime', 'STRING', 'INT', 'FLOAT', 'BOOL', 'UUID', 'UNPROCESSED',70+ 'FloatRange',71 72 # Utilities73 'echo', 'get_binary_stream', 'get_text_stream', 'open_file',74 'format_filename', 'get_app_dir', 'get_os_args',25 unmodified linesmodified:click/types.py+34 added
1 import os2 import stat3+from datetime import datetime4 5 from ._compat import open_stream, text_type, filename_to_ui, \6 get_filesystem_encoding, get_streerror, _get_argv_encoding, PY27 from .exceptions import BadParameter174 unmodified lines182 def __repr__(self):183 return 'Choice(%r)' % list(self.choices)184 185 186+class DateTime(ParamType):187+ name = 'datetime'188+189+ def __init__(self, formats=None):190+ self.formats = formats or [191+ '%Y-%m-%d',192+ '%Y-%m-%dT%H:%M:%S'193+ ]194+195+ def get_metavar(self, param):196+ return '[{}]'.format('|'.join(self.formats))197+198+ def _try_to_convert_date(self, value, format):199+ try:200+ return datetime.strptime(value, format)201+ except ValueError:202+ return None203+204+ def convert(self, value, param, ctx):205+ # Exact match206+ for format in self.formats:207+ dtime = self._try_to_convert_date(value, format)208+ if dtime:209+ return dtime210+211+ self.fail(212+ 'invalid datetime format: {}. (choose from {})'.format(213+ value, ', '.join(self.formats)))214+215+ def __repr__(self):216+ return 'DateTime'217+218+219 class IntParamType(ParamType):220 name = 'integer'221 222 def convert(self, value, param, ctx):426 unmodified linesmodified:tests/test_basic.py+36 added
330 unmodified lines331 result = runner.invoke(cli, ['--help'])332 assert '--method [foo|bar|baz]' in result.output333 334 335+def test_datetime_option_default(runner):336+337+ @click.command()338+ @click.option('--start_date', type=click.DateTime())339+ def cli(start_date):340+ click.echo(start_date.strftime('%Y-%m-%dT%H:%M:%S'))341+342+ result = runner.invoke(cli, ['--start_date=2015-09-29'])343+ assert not result.exception344+ assert result.output == '2015-09-29T00:00:00\n'345+346+ result = runner.invoke(cli, ['--start_date=2015-09-29T09:11:22'])347+ assert not result.exception348+ assert result.output == '2015-09-29T09:11:22\n'349+350+ result = runner.invoke(cli, ['--start_date=2015-09'])351+ assert result.exit_code == 2352+ assert 'Invalid value for "--start_date": invalid datetime format: 2015-09. ' \353+ '(choose from %Y-%m-%d, %Y-%m-%dT%H:%M:%S)' in result.output354+355+ result = runner.invoke(cli, ['--help'])356+ assert '--start_date [%Y-%m-%d|%Y-%m-%dT%H:%M:%S]' in result.output357+358+359+def test_datetime_option_custom(runner):360+ @click.command()361+ @click.option('--start_date',362+ type=click.DateTime(formats=['%A %B %d, %Y']))363+ def cli(start_date):364+ click.echo(start_date.strftime('%Y-%m-%dT%H:%M:%S'))365+366+ result = runner.invoke(cli, ['--start_date=Wednesday June 05, 2010'])367+ assert not result.exception368+ assert result.output == '2010-06-05T00:00:00\n'369+370+371 def test_int_range_option(runner):372 @click.command()373 @click.option('--x', type=click.IntRange(0, 5))374 def cli(x):160 unmodified linesmodified:tests/test_imports.py+1 added-1 removed
30 unmodified lines31 32 ALLOWED_IMPORTS = set([33 'weakref', 'os', 'struct', 'collections', 'sys', 'contextlib',34 'functools', 'stat', 're', 'codecs', 'inspect', 'itertools', 'io',35- 'threading', 'colorama', 'errno', 'fcntl'35+ 'threading', 'colorama', 'errno', 'fcntl', 'datetime'36 ])37 38 if WIN:39 ALLOWED_IMPORTS.update(['ctypes', 'ctypes.wintypes', 'msvcrt', 'time',16 unmodified lines