Skip to content

Commit

Permalink
Restored 2.6-2.7 compatibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
epsy committed Dec 13, 2013
1 parent 0af9037 commit 0677b12
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 51 deletions.
3 changes: 1 addition & 2 deletions clize/help.py
Expand Up @@ -9,7 +9,6 @@
from functools import partial
import inspect
import re
from collections import OrderedDict

import six
from sigtools.modifiers import annotate, kwoargs
Expand Down Expand Up @@ -115,7 +114,7 @@ def split_docstring(cls, s):
argdoc_re = re.compile('^([a-zA-Z_]+): ?(.+)$')
def parse_docstring(self, s):
header = []
arghelp = OrderedDict()
arghelp = util.OrderedDict()
before = {}
after = {}
last_arghelp = None
Expand Down
9 changes: 6 additions & 3 deletions clize/parser.py
Expand Up @@ -162,7 +162,10 @@ def coerce_value(self, arg):
raise exc

def format_type(self):
if self.typ is not util.identity and self.typ not in six.string_types:
if (
self.typ is not util.identity
and not issubclass(self.typ, six.string_types)
):
return '=' + util.name_type2cli(self.typ)
return ''

Expand Down Expand Up @@ -261,11 +264,11 @@ def read_argument(self, args, i, ba):
raise errors.DuplicateNamedArgument()
arg = args[i]
if arg.startswith('--'):
return super().read_argument(args, i, ba)
return super(IntOptionParameter, self).read_argument(args, i, ba)

arg = arg.lstrip('-')[1:]
if not arg:
return super().read_argument(args, i, ba)
return super(IntOptionParameter, self).read_argument(args, i, ba)

val, rest = split_int_rest(arg)
ba.kwargs[self.argument_name] = self.coerce_value(val)
Expand Down
9 changes: 6 additions & 3 deletions clize/runner.py
Expand Up @@ -2,9 +2,11 @@
# Copyright (C) 2013 by Yann Kaiser <kaiser.yann@gmail.com>
# See COPYING for details.

from __future__ import print_function

import sys
from functools import partial, update_wrapper
from collections import OrderedDict
import operator

import six
from sigtools.modifiers import annotate, autokwoargs
Expand All @@ -26,7 +28,7 @@ def cli(self):
RequireInspect = partial(util.DefinedBy, lambda s: s.inspect())

def cli_commands(obj, namef, clizer):
cmds = OrderedDict()
cmds = util.OrderedDict()
cmd_by_name = {}
for key, val in util.dict_from_names(obj).items():
if not key:
Expand Down Expand Up @@ -225,7 +227,8 @@ def __init__(self, commands=()):
commands, namef=util.name_py2cli, clizer=self.clizer)

@Clize(pass_name=True, helper_class=make_dispatcher_helper)
@annotate(command=(str.lower, parser.Parameter.LAST_OPTION),
@annotate(command=(operator.methodcaller('lower'),
parser.Parameter.LAST_OPTION),
args=parser.Parameter.EAT_REST)
def cli(self, name, command, *args):
try:
Expand Down
20 changes: 0 additions & 20 deletions clize/tests/test_legacy.py
@@ -1,8 +1,6 @@
#!/usr/bin/python
# encoding: utf-8

from __future__ import unicode_literals

import unittest
import warnings

Expand Down Expand Up @@ -371,21 +369,3 @@ def fn(a):
"""
)

class UnicodeTests(OldInterfaceTests):
try:
unicode
except NameError:
def as_argv(self, string):
return string
else:
def as_argv(self, string):
return string.encode('utf8')

def test_unicode(self):
@clize
def fn(one):
return one
self.assertEqual(
fn('fn', self.as_argv('ಠ')),
'ಠ'
)
54 changes: 35 additions & 19 deletions clize/tests/test_legacy_py3k.py
Expand Up @@ -2,14 +2,16 @@

import unittest

from sigtools import modifiers
from clize import clize, errors

#from tests import HelpTester

class AnnotationParams(unittest.TestCase):
def test_alias(self):
@clize
def fn(one: 'o' = 1):
@modifiers.annotate(one='o')
def fn(one=1):
return one
self.assertEqual(
fn('fn', '-o', '2'),
Expand All @@ -18,7 +20,8 @@ def fn(one: 'o' = 1):

def test_position(self):
@clize
def fn(one: clize.POSITIONAL = 1):
@modifiers.annotate(one=clize.POSITIONAL)
def fn(one=1):
return one
self.assertEqual(
fn('fn', '2'),
Expand All @@ -27,7 +30,8 @@ def fn(one: clize.POSITIONAL = 1):

def test_coerce(self):
@clize
def fn(one: float):
@modifiers.annotate(one=float)
def fn(one):
return one
self.assertEqual(
fn('fn', '2.1'),
Expand All @@ -36,7 +40,8 @@ def fn(one: float):

def test_coerce_and_default(self):
@clize
def fn(one: float = 1):
@modifiers.annotate(one=float)
def fn(one=1):
return one
self.assertEqual(
fn('fn'),
Expand All @@ -49,7 +54,8 @@ def fn(one: float = 1):

def test_multiple(self):
@clize
def fn(one: (float, clize.POSITIONAL) = 1):
@modifiers.annotate(one=(float, clize.POSITIONAL))
def fn(one=1):
return one
self.assertEqual(
fn('fn', '2.1'),
Expand All @@ -62,30 +68,37 @@ def fn(one: (float, clize.POSITIONAL) = 1):

class AnnotationFailures(unittest.TestCase):
def test_coerce_twice(self):
with self.assertRaises(ValueError):
def test():
@clize
def fn(one: (float, int)):
@modifiers.annotate(one=(float, int))
def fn(one):
return one
fn.signature
self.assertRaises(ValueError, test)

def test_alias_space(self):
with self.assertRaises(ValueError):
def test():
@clize
def fn(one: 'a b'=1):
@modifiers.annotate(one='a b')
def fn(one=1):
return one
fn.signature
self.assertRaises(ValueError, test)

def test_unknown(self):
with self.assertRaises(ValueError):
def test():
@clize
def fn(one: 1.0):
@modifiers.annotate(one=1.0)
def fn(one):
return one
fn.signature
self.assertRaises(ValueError, test)

class KwoargsParams(unittest.TestCase):
def test_kwoparam(self):
@clize
def fn(*, one):
@modifiers.kwoargs('one')
def fn(one):
return one

self.assertEqual(
Expand All @@ -95,15 +108,16 @@ def fn(*, one):

def test_kwoparam_required(self):
@clize
def fn(*, one):
@modifiers.kwoargs('one')
def fn(one):
return one

with self.assertRaises(errors.MissingRequiredArguments):
fn('fn')
self.assertRaises(errors.MissingRequiredArguments, fn, 'fn')

def test_kwoparam_optional(self):
@clize
def fn(*, one=1):
@modifiers.kwoargs('one')
def fn(one=1):
return one
self.assertEqual(
fn('fn'),
Expand Down Expand Up @@ -133,7 +147,8 @@ def fn(one, two=2):

class KwoargsHelpTests(object):
def test_kwoparam(self):
def fn(*, one='1'):
@modifiers.kwoargs('one')
def fn(one='1'):
"""
one: one!
Expand All @@ -148,7 +163,8 @@ def fn(*, one='1'):
""")

def test_kwoparam_required(self):
def fn(*, one):
@modifiers.kwoargs('one')
def fn(one):
"""
one: one!
"""
Expand All @@ -159,4 +175,4 @@ def fn(*, one):
Options:
--one=STR one!(required)
""") # ಠ_ಠ
""")
9 changes: 7 additions & 2 deletions clize/tests/test_parser.py
@@ -1,5 +1,5 @@
from sigtools import test, modifiers
import inspect
from clize.util import funcsigs

from clize import parser, errors, util
from clize.tests.util import testfunc
Expand Down Expand Up @@ -174,6 +174,11 @@ class ExtraParamsTests(object):
'', [parser.FallbackCommandParameter(func=_func, aliases=['--alt'])],
('a', '--alt', 'a', 'b'), [], {}, _func
)
flb_cmd_invalid_valid = (
'a: int, b',
[parser.FallbackCommandParameter(func=_func, aliases=['--alt'])],
('xyz', 'abc', '--alt', 'def'), [], {}, _func
)

def test_alt_middle(self):
_func = test.f('')
Expand Down Expand Up @@ -217,7 +222,7 @@ def test_not_enough_pos_collect(self):
@modifiers.annotate(args=parser.Parameter.REQUIRED)
def func(*args):
raise NotImplementedError
csig = parser.CliSignature.from_signature(inspect.signature(func))
csig = parser.CliSignature.from_signature(funcsigs.signature(func))
try:
csig.read_arguments(())
except errors.MissingRequiredArguments:
Expand Down
6 changes: 5 additions & 1 deletion clize/util.py
Expand Up @@ -5,7 +5,6 @@
"""various"""

import os
from collections import OrderedDict
from functools import update_wrapper

import inspect
Expand All @@ -16,6 +15,11 @@
else:
funcsigs = inspect

try:
from collections import OrderedDict
except ImportError:
from ordereddict import OrderedDict

import six
from sigtools.wrappers import wrapper_decorator

Expand Down
9 changes: 8 additions & 1 deletion setup.py
@@ -1,7 +1,14 @@
#!/usr/bin/env python

import sys

from setuptools import setup

deps = ['sigtools', 'six']

if sys.version_info < (2, 7):
deps.append('ordereddict')

setup(
name='clize',
version='3.0a',
Expand All @@ -10,7 +17,7 @@
url='https://github.com/epsy/clize',
author='Yann Kaiser',
author_email='kaiser.yann@gmail.com',
install_requires=['sigtools', 'six'],
install_requires=deps,
packages=('clize',),
test_suite='clize.tests',
keywords=[
Expand Down

0 comments on commit 0677b12

Please sign in to comment.