Merge pull request #8 from ntrepid8/feature/write-null-values-as-comments
Feature/write null values as comments
diff --git a/.gitignore b/.gitignore
index 21702c8..4347bb8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,6 @@
build/
dist/
pytoml.egg-info/
+venv/
+.idea/
+__pycache__/
diff --git a/README.md b/README.md
index 6804935..8a82c12 100644
--- a/README.md
+++ b/README.md
@@ -27,6 +27,16 @@
>>> print toml.dumps(obj)
a = 1
+
+## tests
+
+To run the tests update the `toml-test` submodule:
+
+ $ git submodule update --init --recursive
+
+Then run the tests:
+
+ $ python test/test.py
[1]: https://github.com/toml-lang/toml
[2]: https://github.com/toml-lang/toml/blob/master/versions/en/toml-v0.4.0.md
diff --git a/pytoml/writer.py b/pytoml/writer.py
index 12f1d57..055ab65 100644
--- a/pytoml/writer.py
+++ b/pytoml/writer.py
@@ -5,16 +5,20 @@
long = int
unicode = str
+
def dumps(obj):
fout = io.StringIO()
dump(fout, obj)
return fout.getvalue()
-_escapes = { '\n': 'n', '\r': 'r', '\\': '\\', '\t': 't', '\b': 'b', '\f': 'f', '"': '"' }
+
+_escapes = {'\n': 'n', '\r': 'r', '\\': '\\', '\t': 't', '\b': 'b', '\f': 'f', '"': '"'}
+
def _escape_string(s):
res = []
start = 0
+
def flush():
if start != i:
res.append(s[start:i])
@@ -34,14 +38,17 @@
flush()
return '"' + ''.join(res) + '"'
+
def _escape_id(s):
if any(not c.isalnum() and c not in '-_' for c in s):
return _escape_string(s)
return s
+
def _format_list(v):
return '[{}]'.format(', '.join(_format_value(obj) for obj in v))
+
def _format_value(v):
if isinstance(v, bool):
return 'true' if v else 'false'
@@ -74,6 +81,7 @@
else:
raise RuntimeError(v)
+
def dump(fout, obj):
tables = [((), obj, False)]
@@ -92,6 +100,10 @@
tables.append((name + (k,), v, False))
elif isinstance(v, list) and v and all(isinstance(o, dict) for o in v):
tables.extend((name + (k,), d, True) for d in reversed(v))
+ elif v is None:
+ # based on mojombo's comment: https://github.com/toml-lang/toml/issues/146#issuecomment-25019344
+ 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)))
diff --git a/setup.py b/setup.py
index 4ab3427..f183416 100644
--- a/setup.py
+++ b/setup.py
@@ -5,7 +5,7 @@
setup(
name='pytoml',
- version='0.1.4',
+ version='0.1.5',
description='A parser for TOML-0.4.0',
author='Martin Vejnár',