Skip to content

Commit

Permalink
bump version, Merge branch 'devel'
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl committed Jan 18, 2018
2 parents 15c7b89 + c38fabb commit 2b14d96
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
language: python
matrix:
include:
- python: 2.6
env: TOXENV=py26
- python: 2.7
env: TOXENV=py27
- python: 3.3
Expand Down
38 changes: 35 additions & 3 deletions argopt/_argopt.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
from __future__ import print_function
import argparse
try:
from argparse import ArgumentParser
except ImportError:
# py26
ArgumentParser = None

import re
import sys
from docopt import docopt # only for py26
from ._docopt import Argument, Option, AnyOptions, \
parse_defaults, parse_pattern, printable_usage, formal_usage
from ._utils import _range, set_nargs
from ._utils import _range, set_nargs, DictAttrWrap, typecast
from ._version import __version__ # NOQA

__author__ = "Casper da Costa-Luis <casper@caspersci.uk.to>"
Expand Down Expand Up @@ -90,7 +97,29 @@ def docopt_parser(doc='', **_kwargs):
return once_args + qest_args + star_args + plus_args, opts


def argopt(doc='', argparser=argparse.ArgumentParser, **_kwargs):
class DocoptArgumentParser(object):
"""Thin wrapper around docopt which behaves like argparse (for py26)
"""
def __init__(self, doc, version=None):
self.doc = doc
self.version = version

def parse_args(self, args=None):
args = docopt(
self.doc, version=self.version,
argv=args if args is not None else sys.argv[1:])
for (k, v) in args.items():
try:
args[k] = typecast(v.rsplit(':', 1))
except:
pass
return DictAttrWrap(args)

def print_help(self, file=sys.stderr):
file.write(self.doc)


def argopt(doc='', argparser=ArgumentParser, **_kwargs):
"""
Note that `docopt` supports neither type specifiers nor default
positional arguments. We support both here.
Expand Down Expand Up @@ -137,6 +166,9 @@ def argopt(doc='', argparser=argparse.ArgumentParser, **_kwargs):
# TEST: metavar
# TEST: version

if argparser is None:
return DocoptArgumentParser(doc, version=_kwargs.get("version"))

pu = printable_usage(doc)
args, opts = docopt_parser(doc, **_kwargs)

Expand Down
2 changes: 1 addition & 1 deletion argopt/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
__all__ = ["__version__"]

# major, minor, patch, -extra
version_info = 0, 3, 2
version_info = 0, 3, 3

# Nice string for the version
__version__ = '.'.join(map(str, version_info))
Expand Down
33 changes: 21 additions & 12 deletions argopt/tests/tests_argopt.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from argopt import argopt
import argparse
try:
from argparse import RawDescriptionHelpFormatter
except ImportError:
# py26
RawDescriptionHelpFormatter = None
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
import sys


def test_basic():
Expand All @@ -23,9 +28,10 @@ def test_basic():
-p PAT, --patts PAT Or [default: '':str].
--bar=<b> Another [default: something] should assume str.
-f, --force Force.
-v, --version Print version and exit.
'''
parser = argopt(doc, version='0.1.2-3.4',
formatter_class=argparse.RawDescriptionHelpFormatter)
formatter_class=RawDescriptionHelpFormatter)
fs = StringIO()
parser.print_help(file=fs)
res = fs.getvalue()
Expand All @@ -49,16 +55,19 @@ def test_basic():
-p PAT, --patts PAT Or [default: '':str].'''.split('\n'))

except AssertionError:
raise AssertionError(res)
if not all([l.strip() in res for l in doc.split('\n')]):
raise AssertionError(res)

args = parser.parse_args(args=' such test much is'.split())
assert (args.x == 'such')
assert (args.x == 'such')
args = parser.parse_args(args='such test much is'.split())
try:
assert (args.x == 'such')
except AssertionError as e:
raise AssertionError("x:" + str(args.x) + str(e))
assert (args.y == 'test much is'.split())
try:
args = parser.parse_args(args=' -v'.split())
parser.parse_args(args=['-v'])
except SystemExit as e:
assert(str(e) == '0')
assert(str(e) in ['0', ''])
else:
raise ValueError('System should have exited with code 0')

Expand All @@ -67,7 +76,7 @@ def test_verbose_and_version():
doc = '''Usage:
test.py [options]
Arguments:
Options:
-v, --verbose Not silent.
'''
parser = argopt(doc, version='4.3.2-1.0')
Expand All @@ -87,13 +96,13 @@ def test_verbose_and_version():
except AssertionError:
raise AssertionError(res)

args = parser.parse_args(args=' -v'.split())
args = parser.parse_args(args=['-v'])
assert args.verbose
args = parser.parse_args(args=[])
assert (not args.verbose)
try:
args = parser.parse_args(args=' --version'.split())
parser.parse_args(args=['--version'])
except SystemExit as e:
assert(str(e) == '0')
assert (str(e) in ['0', ''])
else:
raise ValueError('System should have exited with code 0')
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,5 @@ def execute_makefile_commands(commands, alias, verbose=False):
' console terminal command line CLI UI gui gooey',
test_suite='nose.collector',
tests_require=['nose', 'flake8', 'coverage'],
requires=["docopt"] if sys.version_info[:2] == (2, 6) else [],
)
14 changes: 13 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[tox]
# deprecation warning: py32-4
envlist = py27, py33, py34, py35, py36, py37-dev, pypy, pypy3, flake8, setup.py
envlist = py26, py27, py33, py34, py35, py36, py37-dev, pypy, pypy3, flake8, setup.py

[testenv]
passenv = CI TRAVIS TRAVIS_*
Expand All @@ -20,6 +20,18 @@ commands =
coveralls
codecov

[testenv:py26]
passenv = CI TRAVIS TRAVIS_*
deps =
nose
coverage
coveralls
# codecov
commands =
nosetests --with-coverage --cover-package=argopt -d -v argopt/
coveralls
# codecov

[testenv:flake8]
deps = flake8
commands =
Expand Down

0 comments on commit 2b14d96

Please sign in to comment.