Skip to content

Commit

Permalink
Deal with Python 2 compatibility for pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
Shir0kamii committed May 29, 2018
1 parent abb3846 commit 5738191
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
22 changes: 15 additions & 7 deletions clize/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import itertools
from functools import partial, wraps
import pathlib
import warnings

import six
Expand All @@ -17,6 +16,14 @@

from clize import errors, util

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


class ParameterFlag(object):
def __init__(self, name, prefix='clize.Parameter'):
Expand Down Expand Up @@ -225,20 +232,21 @@ def is_true(arg):
return arg.lower() not in ('', '0', 'n', 'no', 'f', 'false')


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


_implicit_converters = {
int: int,
float: float,
bool: is_true,
six.text_type: identity,
six.binary_type: identity,
pathlib.PurePath: path_converter,
}

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:
Expand Down
13 changes: 10 additions & 3 deletions clize/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@
# Copyright (C) 2011-2016 by Yann Kaiser and contributors. See AUTHORS and
# COPYING for details.

import pathlib
from sigtools import support, modifiers, specifiers

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='import pathlib;'
'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
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 5738191

Please sign in to comment.