Add support for Python 2.6 and 3.3
diff --git a/.travis.yml b/.travis.yml
index cb1f4c2..5db1b2d 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,6 +1,8 @@
language: python
python:
+ - "2.6"
- "2.7"
+ - "3.3"
- "3.4"
- "3.5"
script: python ./setup.py install && python ./test/test.py
diff --git a/README.md b/README.md
index 8a82c12..f8ba9f6 100644
--- a/README.md
+++ b/README.md
@@ -3,7 +3,7 @@
# pytoml
This project aims at being a specs-conforming and strict parser and writer for [TOML][1] files.
-The library currently supports [version 0.4.0][2] of the specs and runs with Python 2.7 and 3.4+.
+The library currently supports [version 0.4.0][2] of the specs and runs with Python 2.6+ and 3.3+.
Install:
diff --git a/pytoml/parser.py b/pytoml/parser.py
index 6185078..015eaf2 100644
--- a/pytoml/parser.py
+++ b/pytoml/parser.py
@@ -34,14 +34,14 @@
error('array-type-mismatch')
value = [process_value(item) for item in value]
elif kind == 'table':
- value = { k: process_value(value[k]) for k in value }
+ value = dict([(k, process_value(value[k])) for k in value])
return translate(kind, text, value)
for kind, value, pos in ast:
if kind == 'kv':
k, v = value
if k in scope:
- error('duplicate_keys. Key "{}" was used more than once.'.format(k))
+ error('duplicate_keys. Key "{0}" was used more than once.'.format(k))
scope[k] = process_value(v)
else:
is_table_array = (kind == 'table_array')
diff --git a/pytoml/writer.py b/pytoml/writer.py
index 8915af6..d4a4aa3 100644
--- a/pytoml/writer.py
+++ b/pytoml/writer.py
@@ -46,8 +46,14 @@
def _format_list(v):
- return '[{}]'.format(', '.join(_format_value(obj) for obj in v))
+ return '[{0}]'.format(', '.join(_format_value(obj) for obj in v))
+# Formula from:
+# https://docs.python.org/2/library/datetime.html#datetime.timedelta.total_seconds
+# Once support for py26 is dropped, this can be replaced by td.total_seconds()
+def _total_seconds(td):
+ return ((td.microseconds
+ + (td.seconds + td.days * 24 * 3600) * 10**6) / 10.0**6)
def _format_value(v):
if isinstance(v, bool):
@@ -55,12 +61,12 @@
if isinstance(v, int) or isinstance(v, long):
return unicode(v)
if isinstance(v, float):
- return '{:.17f}'.format(v)
+ return '{0:.17f}'.format(v)
elif isinstance(v, unicode) or isinstance(v, bytes):
return _escape_string(v)
elif isinstance(v, datetime.datetime):
offs = v.utcoffset()
- offs = offs.total_seconds() // 60 if offs is not None else 0
+ offs = _total_seconds(offs) // 60 if offs is not None else 0
if offs == 0:
suffix = 'Z'
@@ -70,7 +76,7 @@
else:
suffix = '-'
offs = -offs
- suffix = '{}{:.02}{:.02}'.format(suffix, offs // 60, offs % 60)
+ suffix = '{0}{1:.02}{2:.02}'.format(suffix, offs // 60, offs % 60)
if v.microsecond:
return v.strftime('%Y-%m-%dT%H:%M:%S.%f') + suffix
@@ -92,9 +98,9 @@
if name:
section_name = '.'.join(_escape_id(c) for c in name)
if is_array:
- fout.write('[[{}]]\n'.format(section_name))
+ fout.write('[[{0}]]\n'.format(section_name))
else:
- fout.write('[{}]\n'.format(section_name))
+ fout.write('[{0}]\n'.format(section_name))
table_keys = sorted(table.keys()) if sort_keys else table.keys()
for k in table_keys:
@@ -108,7 +114,7 @@
fout.write(
'#{} = null # To use: uncomment and replace null with value\n'.format(_escape_id(k)))
else:
- fout.write('{} = {}\n'.format(_escape_id(k), _format_value(v)))
+ fout.write('{0} = {1}\n'.format(_escape_id(k), _format_value(v)))
if tables:
fout.write('\n')
diff --git a/test/test.py b/test/test.py
index cf689ac..22ddc01 100644
--- a/test/test.py
+++ b/test/test.py
@@ -1,15 +1,22 @@
import os, json, sys, io, traceback, argparse
import pytoml as toml
+# Formula from:
+# https://docs.python.org/2/library/datetime.html#datetime.timedelta.total_seconds
+# Once support for py26 is dropped, this can be replaced by td.total_seconds()
+def _total_seconds(td):
+ return ((td.microseconds
+ + (td.seconds + td.days * 24 * 3600) * 10**6) / 10.0**6)
+
def _testbench_literal(type, text, value):
if type == 'table':
return value
if type == 'array':
return { 'type': 'array', 'value': value }
if type == 'datetime':
- offs = value.tzinfo.utcoffset(value).total_seconds() // 60
+ offs = _total_seconds(value.tzinfo.utcoffset(value)) // 60
offs = 'Z' if offs == 0 else '{}{}:{}'.format('-' if offs < 0 else '-', abs(offs) // 60, abs(offs) % 60)
- v = '{:04}-{:02}-{:02}T{:02}:{:02}:{:02}{}'.format(value.year, value.month, value.day, value.hour, value.minute, value.second, offs)
+ v = '{0:04}-{1:02}-{2:02}T{3:02}:{4:02}:{5:02}{6}'.format(value.year, value.month, value.day, value.hour, value.minute, value.second, offs)
return { 'type': 'datetime', 'value': v }
if type == 'bool':
return { 'type': 'bool', 'value': 'true' if value else 'false' }
@@ -25,7 +32,7 @@
if v.get('type') == 'float':
v['value'] = float(v['value'])
return v
- return { k: adjust_bench(v[k]) for k in v }
+ return dict([(k, adjust_bench(v[k])) for k in v])
if isinstance(v, list):
return [adjust_bench(v) for v in v]
return v
@@ -44,7 +51,7 @@
for path in args.dir:
if not os.path.isdir(path):
- print('error: not a dir: {}'.format(path))
+ print('error: not a dir: {0}'.format(path))
return 2
for top, dirnames, fnames in os.walk(path):
for fname in fnames:
@@ -86,7 +93,7 @@
print('failed: {}\n{}\n{}'.format(f, json.dumps(parsed, indent=4), json.dumps(bench, indent=4)))
if e:
traceback.print_exception(*e)
- print('succeeded: {}'.format(len(succeeded)))
+ print('succeeded: {0}'.format(len(succeeded)))
return 1 if failed or not succeeded else 0
if __name__ == '__main__':