Skip to content
Myr

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

4 files+75 added−4 removed

modified:click/__init__.py+4 added-3 removed
26 unmodified lines
27 password_option, version_option, help_option2829# Types30from .types import ParamType, File, Path, Choice, IntRange, Tuple, \31 STRING, INT, FLOAT, BOOL, UUID, UNPROCESSED, FloatRange31 DateTime, STRING, INT, FLOAT, BOOL, UUID, UNPROCESSED, FloatRange3233# Utilities34from .utils import echo, get_binary_stream, get_text_stream, open_file, \35 format_filename, get_app_dir, get_os_args
28 unmodified lines
64 'argument', 'option', 'confirmation_option', 'password_option',65 'version_option', 'help_option',6667 # 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',7172 # Utilities73 'echo', 'get_binary_stream', 'get_text_stream', 'open_file',74 'format_filename', 'get_app_dir', 'get_os_args',
25 unmodified lines
modified:click/types.py+34 added
1import os2import stat3from datetime import datetime45from ._compat import open_stream, text_type, filename_to_ui, \6 get_filesystem_encoding, get_streerror, _get_argv_encoding, PY27from .exceptions import BadParameter
174 unmodified lines
182 def __repr__(self):183 return 'Choice(%r)' % list(self.choices)184185186class DateTime(ParamType):187 name = 'datetime'188189 def __init__(self, formats=None):190 self.formats = formats or [191 '%Y-%m-%d',192 '%Y-%m-%dT%H:%M:%S'193 ]194195 def get_metavar(self, param):196 return '[{}]'.format('|'.join(self.formats))197198 def _try_to_convert_date(self, value, format):199 try:200 return datetime.strptime(value, format)201 except ValueError:202 return None203204 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 dtime210211 self.fail(212 'invalid datetime format: {}. (choose from {})'.format(213 value, ', '.join(self.formats)))214215 def __repr__(self):216 return 'DateTime'217218219class IntParamType(ParamType):220 name = 'integer'221222 def convert(self, value, param, ctx):
426 unmodified lines
modified:tests/test_basic.py+36 added
330 unmodified lines
331 result = runner.invoke(cli, ['--help'])332 assert '--method [foo|bar|baz]' in result.output333334335def test_datetime_option_default(runner):336337 @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'))341342 result = runner.invoke(cli, ['--start_date=2015-09-29'])343 assert not result.exception344 assert result.output == '2015-09-29T00:00:00\n'345346 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'349350 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.output354355 result = runner.invoke(cli, ['--help'])356 assert '--start_date [%Y-%m-%d|%Y-%m-%dT%H:%M:%S]' in result.output357358359def 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'))365366 result = runner.invoke(cli, ['--start_date=Wednesday June 05, 2010'])367 assert not result.exception368 assert result.output == '2010-06-05T00:00:00\n'369370371def test_int_range_option(runner):372 @click.command()373 @click.option('--x', type=click.IntRange(0, 5))374 def cli(x):
160 unmodified lines
modified:tests/test_imports.py+1 added-1 removed
30 unmodified lines
3132ALLOWED_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])3738if WIN:39 ALLOWED_IMPORTS.update(['ctypes', 'ctypes.wintypes', 'msvcrt', 'time',
16 unmodified lines