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 a45d2af
Showing 1 changed file with 15 additions and 7 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

0 comments on commit a45d2af

Please sign in to comment.