diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 589fe61..2fa86f0 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -22,20 +22,20 @@ jobs: with: go-version: 1.21 - - name: get toml-test + - name: install toml-test run: | - git clone https://github.com/BurntSushi/toml-test - # cd toml-test/cmd/toml-test - # go build + go install github.com/toml-lang/toml-test/cmd/toml-test@v1.4.0 - name: Run python tests run: | pip -q install "tox>=4" tox run-parallel --parallel-no-spinner - # TODO(warchant): this cmd is old, need to update it + # TODO(warchant): many tests fail # - name: Run go tests # run: | - # pip -q install setuptools - # python setup.py install - # ./toml-test/cmd/toml-test/toml-test -i ./tests -g ./tests/gotests.sh + # export PYTHONPATH=. + # e=0 + # toml-test ./tests/decoding_test.py || e=1 + # toml-test -encoder ./tests/encoding_test.py || e=1 + # exit $e diff --git a/CONTRIBUTING b/CONTRIBUTING index ea61841..19167e2 100644 --- a/CONTRIBUTING +++ b/CONTRIBUTING @@ -42,7 +42,7 @@ Directions ---------- 1. Install `Go `_ (AKA golang) -2. Get the toml-test suite from `here `_ +2. Get the toml-test suite from `here `_ and follow the instructions under the **Try it out** section of the README. 3. Test your changes for both versions of Python: diff --git a/README.rst b/README.rst index b65ae72..c6e692d 100644 --- a/README.rst +++ b/README.rst @@ -14,7 +14,7 @@ TOML A Python library for parsing and creating `TOML `_. -The module passes `the TOML test suite `_. +The module passes `the TOML test suite `_. See also: diff --git a/tests/decoding_test.py b/tests/decoding_test.py index cd4040d..51c8334 100755 --- a/tests/decoding_test.py +++ b/tests/decoding_test.py @@ -1,56 +1,35 @@ -"""Decodes toml and outputs it as tagged JSON""" +#!/usr/bin/env python + +"""Decodes TOML and outputs it as tagged JSON""" import datetime import json import sys import toml -if sys.version_info < (3,): - _range = xrange # noqa: F821 - iteritems = dict.iteritems -else: - unicode = str - _range = range - basestring = str - unichr = chr - iteritems = dict.items - long = int - def tag(value): if isinstance(value, dict): - d = {} - for k, v in iteritems(value): - d[k] = tag(v) - return d + return {k: tag(v) for (k, v) in value.items()} elif isinstance(value, list): - a = [] - for v in value: - a.append(tag(v)) - try: - a[0]["value"] - except KeyError: - return a - except IndexError: - pass - return {'type': 'array', 'value': a} - elif isinstance(value, basestring): + return [tag(v) for v in value] + elif isinstance(value, str): return {'type': 'string', 'value': value} elif isinstance(value, bool): return {'type': 'bool', 'value': str(value).lower()} elif isinstance(value, int): return {'type': 'integer', 'value': str(value)} - elif isinstance(value, long): - return {'type': 'integer', 'value': str(value)} elif isinstance(value, float): return {'type': 'float', 'value': repr(value)} elif isinstance(value, datetime.datetime): - return {'type': 'datetime', 'value': value.isoformat() - .replace('+00:00', 'Z')} + return { + 'type': 'datetime-local' if value.tzinfo is None else 'datetime', + 'value': value.isoformat().replace('+00:00', 'Z'), + } elif isinstance(value, datetime.date): - return {'type': 'date', 'value': value.isoformat()} + return {'type': 'date-local', 'value': value.isoformat()} elif isinstance(value, datetime.time): - return {'type': 'time', 'value': value.strftime('%H:%M:%S.%f')} + return {'type': 'time-local', 'value': value.strftime('%H:%M:%S.%f')} assert False, 'Unknown type: %s' % type(value) diff --git a/tests/encoding_test.py b/tests/encoding_test.py new file mode 100755 index 0000000..54f2a80 --- /dev/null +++ b/tests/encoding_test.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python + +"""Reads tagged JSON and encodes it as TOML""" + +import json +import sys +import toml + +def convert(v): + if type(v) == list: + return [convert(vv) for vv in v] + elif v.get('type', None) is None or v.get('value', None) is None: + return {k: convert(vv) for (k, vv) in v.items()} + elif v['type'] == 'string': + return v['value'] + elif v['type'] == 'integer': + return int(v['value']) + elif v['type'] == 'float': + return float(v['value']) + elif v['type'] == 'bool': + return True if v['value'] == 'true' else False + elif v['type'] in ['datetime', 'datetime-local', 'date-local', 'time-local']: + return toml.loads('a=' + v['value'])['a'] + else: + raise Exception(f'unknown type: {v}') + +j = json.loads(sys.stdin.read()) +print(toml.dumps({k: convert(v) for (k, v) in j.items()}))