Skip to content

Commit

Permalink
Allow to use pathlib.Path as implicit converter (#47)
Browse files Browse the repository at this point in the history
* Allow to use pathlib.Path as implicit converter
* Check for subclass of implicit converters
* Deal with Python 2 compatibility for pathlib
  • Loading branch information
Shir0kamii authored and epsy committed Jun 7, 2018
1 parent 6029fa2 commit 2885fc1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
25 changes: 22 additions & 3 deletions clize/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@

from clize import errors, util

try:
import pathlib
except ImportError:
try:
import pathlib2 as pathlib
except ImportError:
pathlib = None


class ParameterFlag(object):
def __init__(self, name, prefix='clize.Parameter'):
Expand Down Expand Up @@ -232,15 +240,26 @@ def is_true(arg):
six.binary_type: identity,
}

if pathlib:
@value_converter(name='PATH')
def path_converter(arg):
return pathlib.Path(arg)

_implicit_converters[pathlib.PurePath] = path_converter


def get_value_converter(annotation):
try:
return _implicit_converters[annotation]
except KeyError:
pass
if not getattr(annotation, '_clize__value_converter', False):
raise ValueError('{0!r} is not a value converter'.format(annotation))
return annotation
if getattr(annotation, '_clize__value_converter', False):
return annotation
if isinstance(annotation, type):
for ic in _implicit_converters:
if issubclass(annotation, ic):
return _implicit_converters[ic]
raise ValueError('{0!r} is not a value converter'.format(annotation))


class ParameterWithValue(Parameter):
Expand Down
17 changes: 16 additions & 1 deletion clize/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@
from clize import parser, errors, util
from clize.tests.util import Fixtures

try:
import pathlib
pathlib_name = "pathlib"
except ImportError:
import pathlib2 as pathlib
pathlib_name = "pathlib2 as pathlib"


_ic = parser._implicit_converters


class FromSigTests(Fixtures):
def _test(self, sig_str, typ, str_rep, attrs):
sig = support.s(sig_str, pre='from clize import Parameter')
pre_code = ("import {}; from clize import"
" Parameter".format(pathlib_name))
sig = support.s(sig_str, pre=pre_code)
return self._do_test(sig, typ, str_rep, attrs)

def _do_test(self, sig, typ, str_rep, attrs):
Expand Down Expand Up @@ -44,6 +53,12 @@ def _do_test(self, sig, typ, str_rep, attrs):
'conv': _ic[int], 'default': 3, 'required': False,
'argument_name': 'one', 'display_name': 'one',
'undocumented': False, 'last_option': None}
pos_default_path = (
'file=pathlib.Path(\'/tmp\')', parser.PositionalParameter, '[file]', {
'conv': _ic[pathlib.PurePath],
'default': pathlib.Path('/tmp'), 'argument_name': 'file',
'required': False, 'undocumented': False,
'last_option': None, 'display_name': 'file'})
pos_default_but_required = (
'one:Parameter.REQUIRED=3', parser.PositionalParameter, 'one', {
'conv': _ic[int], 'default': util.UNSET, 'required': True,
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ usedevelop=True
deps=
unittest2
repeated_test
pathlib2
python-dateutil
Pygments
docs: sphinx
Expand Down

0 comments on commit 2885fc1

Please sign in to comment.