summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Pentchev <roam@ringlet.net>2019-01-09 23:18:33 +0200
committerPeter Pentchev <roam@ringlet.net>2019-01-12 01:48:04 +0200
commit367327ed2e148878c968973fba209ce19b7f64cc (patch)
tree1c7314bf8d559f795de4b420aca6de9a32cc1d13
parentb95970edf658a58ae0b586ec3688a14bb4019546 (diff)
Only use the Python typing module if it is available.
-rwxr-xr-xpython/confget/__main__.py9
-rw-r--r--python/confget/backend/__init__.py8
-rw-r--r--python/confget/backend/abstract.py8
-rw-r--r--python/confget/backend/http_get.py11
-rw-r--r--python/confget/backend/ini.py32
-rw-r--r--python/confget/defs.py10
-rw-r--r--python/confget/format.py34
-rw-r--r--python/tox.ini25
-rw-r--r--python/unit_tests/data/defs.py6
-rw-r--r--python/unit_tests/data/load.py8
10 files changed, 104 insertions, 47 deletions
diff --git a/python/confget/__main__.py b/python/confget/__main__.py
index 26a0279..905877d 100755
--- a/python/confget/__main__.py
+++ b/python/confget/__main__.py
@@ -32,14 +32,17 @@ from __future__ import print_function
import argparse
import sys
-from typing import List, Optional
-
from . import backend
from . import defs
from . import format as fmt
+try:
+ from typing import List, Optional
+
+ _TYPING_USED = (defs, List, Optional)
+except ImportError:
+ pass
-_TYPING_USED = (defs, List, Optional)
VERSION_STRING = '2.2.0'
FEATURES = [
diff --git a/python/confget/backend/__init__.py b/python/confget/backend/__init__.py
index 4cc6ac9..51be726 100644
--- a/python/confget/backend/__init__.py
+++ b/python/confget/backend/__init__.py
@@ -26,12 +26,14 @@
Provide an interface to all the configuration format backends.
"""
-from typing import Dict, Type
-
from . import abstract, http_get, ini
+try:
+ from typing import Dict, Type
-_TYPING_USED = (Dict, Type, abstract)
+ _TYPING_USED = (Dict, Type, abstract)
+except ImportError:
+ pass
BACKENDS = {
diff --git a/python/confget/backend/abstract.py b/python/confget/backend/abstract.py
index 194976e..1ee2c8d 100644
--- a/python/confget/backend/abstract.py
+++ b/python/confget/backend/abstract.py
@@ -28,14 +28,16 @@ An abstract metaclass for confget backends.
import abc
-from typing import Dict
-
import six
from .. import defs
+try:
+ from typing import Dict
-_TYPING_USED = (defs, Dict)
+ _TYPING_USED = (defs, Dict)
+except ImportError:
+ pass
@six.add_metaclass(abc.ABCMeta)
diff --git a/python/confget/backend/http_get.py b/python/confget/backend/http_get.py
index 9c20e5a..a6f1ba4 100644
--- a/python/confget/backend/http_get.py
+++ b/python/confget/backend/http_get.py
@@ -30,12 +30,17 @@ import os
import re
import urllib
-from typing import Dict, List
-
from .. import defs
from . import abstract
+try:
+ from typing import Dict, List
+
+ _TYPING_USED = (defs, Dict, List)
+except ImportError:
+ pass
+
try:
import urllib.parse # pylint: disable=ungrouped-imports
@@ -45,8 +50,6 @@ except ImportError:
urllib_unquote = urllib.unquote # pylint: disable=invalid-name,no-member
-_TYPING_USED = (defs, Dict, List)
-
RE_ENTITY = re.compile(r'^ (?P<full> [a-zA-Z0-9_]+ ; )', re.X)
diff --git a/python/confget/backend/ini.py b/python/confget/backend/ini.py
index bd36fa6..44b9470 100644
--- a/python/confget/backend/ini.py
+++ b/python/confget/backend/ini.py
@@ -29,14 +29,31 @@ A confget backend for reading INI-style files.
import re
import sys
-from typing import Callable, Dict, Match, NamedTuple, Pattern
-
from .. import defs
from . import abstract
+try:
+ from typing import Callable, Dict, Match, NamedTuple, Pattern
+
+ _TYPING_USED = (defs, Dict)
+
+ MatcherType = NamedTuple('MatcherType', [
+ ('regex', Pattern[str]),
+ ('handle', Callable[[
+ Match[str],
+ Dict[str, str],
+ defs.Config,
+ defs.ConfigData,
+ ], None]),
+ ])
+except ImportError:
+ import collections
-_TYPING_USED = (defs, Dict)
+ MatcherType = collections.namedtuple('MatcherType', [ # type: ignore
+ 'regex',
+ 'handle',
+ ])
class INIBackend(abstract.Backend):
@@ -105,15 +122,6 @@ class INIBackend(abstract.Backend):
if not state['cont']:
res[state['section']][state['name']] = state['value']
- MatcherType = NamedTuple('MatcherType', [
- ('regex', Pattern[str]),
- ('handle', Callable[[
- Match[str],
- Dict[str, str],
- defs.Config,
- defs.ConfigData,
- ], None]),
- ])
matches = [
MatcherType(
regex=re.compile(r'^ \s* (?: [#;] .* )? $', re.X),
diff --git a/python/confget/defs.py b/python/confget/defs.py
index f4fa335..7f6a796 100644
--- a/python/confget/defs.py
+++ b/python/confget/defs.py
@@ -26,12 +26,14 @@
Common definitions for the confget configuration parsing library.
"""
-from typing import Dict, List, Optional
+try:
+ from typing import Dict, List, Optional
+ _TYPING_USED = (List, Optional)
-_TYPING_USED = (List, Optional)
-
-ConfigData = Dict[str, Dict[str, str]]
+ ConfigData = Dict[str, Dict[str, str]]
+except ImportError:
+ pass
class Config(object):
diff --git a/python/confget/format.py b/python/confget/format.py
index 1f99aa1..d2265b5 100644
--- a/python/confget/format.py
+++ b/python/confget/format.py
@@ -29,22 +29,32 @@ Filter and format a subset of the configuration variables.
import fnmatch
import re
-from typing import Callable, Dict, Iterable, NamedTuple, List, Optional
+try:
+ from typing import Callable, Dict, Iterable, NamedTuple, List, Optional
+
+ _TYPING_USED = (Callable, Dict, Iterable, List, Optional)
+
+ FormatOutput = NamedTuple('FormatOutput', [
+ ('name', str),
+ ('value', str),
+ ('output_name', str),
+ ('output_value', str),
+ ('output_full', str),
+ ])
+except ImportError:
+ import collections
+
+ FormatOutput = collections.namedtuple('FormatOutput', [ # type: ignore
+ 'name',
+ 'value',
+ 'output_name',
+ 'output_value',
+ 'output_full',
+ ])
from . import defs
-_TYPING_USED = (Callable, Dict, Iterable, List, Optional)
-
-FormatOutput = NamedTuple('FormatOutput', [
- ('name', str),
- ('value', str),
- ('output_name', str),
- ('output_value', str),
- ('output_full', str),
-])
-
-
class FormatConfig(defs.Config):
# pylint: disable=too-few-public-methods,too-many-instance-attributes
"""
diff --git a/python/tox.ini b/python/tox.ini
index 9b0c5ae..757d546 100644
--- a/python/tox.ini
+++ b/python/tox.ini
@@ -23,7 +23,7 @@
# SUCH DAMAGE.
[tox]
-envlist = pep8,mypy2,mypy3,prove_2,prove_3,unit_tests_2,unit_tests_3,pylint
+envlist = pep8,mypy2,mypy3,prove_2,prove_2_nt,prove_3,unit_tests_2,unit_tests_2_nt,unit_tests_3,pylint
skipsdist = True
[testenv:pep8]
@@ -73,6 +73,16 @@ setenv =
commands =
pytest -s -vv unit_tests
+[testenv:unit_tests_2_nt]
+basepython = python2
+deps =
+ ddt
+ pytest
+setenv =
+ TESTDIR={toxinidir}/../t
+commands =
+ pytest -s -vv unit_tests
+
[testenv:unit_tests_3]
basepython = python3
deps =
@@ -106,6 +116,19 @@ whitelist_externals =
commands =
prove ../t
+[testenv:prove_2_nt]
+basepython = python2
+deps =
+ six
+setenv =
+ CONFGET=python -m confget
+ MANPAGE={toxinidir}/../confget.1
+ TESTDIR={toxinidir}/../t
+whitelist_externals =
+ prove
+commands =
+ prove ../t
+
[testenv:prove_3]
basepython = python3
deps =
diff --git a/python/unit_tests/data/defs.py b/python/unit_tests/data/defs.py
index b0e2922..759487a 100644
--- a/python/unit_tests/data/defs.py
+++ b/python/unit_tests/data/defs.py
@@ -88,7 +88,8 @@ class XFormNone(XFormType):
def do_xform(self, res):
# type: (XFormNone, Iterable[cformat.FormatOutput]) -> str
- return '\n'.join([line.output_full for line in res])
+ xform = '\n'.join([line.output_full for line in res]) # type: str
+ return xform
class XFormNewlineToSpace(XFormType):
@@ -101,7 +102,8 @@ class XFormNewlineToSpace(XFormType):
def do_xform(self, res):
# type: (XFormNewlineToSpace, Iterable[cformat.FormatOutput]) -> str
- return ''.join([line.output_full + ' ' for line in res])
+ xform = ''.join([line.output_full + ' ' for line in res]) # type: str
+ return xform
class XFormCountLines(XFormType):
diff --git a/python/unit_tests/data/load.py b/python/unit_tests/data/load.py
index b954379..cf23f54 100644
--- a/python/unit_tests/data/load.py
+++ b/python/unit_tests/data/load.py
@@ -30,12 +30,14 @@ Load a test definition from a JSON file.
import json
import os
-from typing import Any, Dict
-
from . import defs
+try:
+ from typing import Any, Dict
-TESTING_USED = (defs, Any, Dict)
+ TESTING_USED = (defs, Any, Dict)
+except ImportError:
+ pass
def _load_test_v1(data, _version):