diff configmix/json.py @ 122:21d92ff8cf31

Begin the handling of JSON-style configuration files
author Franz Glasner <hg@dom66.de>
date Wed, 04 Apr 2018 09:45:29 +0200
parents
children 4218c66b9281
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/configmix/json.py	Wed Apr 04 09:45:29 2018 +0200
@@ -0,0 +1,34 @@
+# -*- coding: utf-8 -*-
+#-
+# :Copyright: (c) 2018, Franz Glasner. All rights reserved.
+# :License:   3-clause BSD. See LICENSE.txt for details.
+#-
+"""Read JSON-style configuration files.
+
+"""
+
+from __future__ import division, absolute_import, print_function
+
+import io
+import json.decoder
+
+
+__all__ = ["load"]
+
+
+def load(filename, encoding="utf-8"):
+    """Load a single JSON file with name `filename` and encoding `encoding`.
+
+    .. todo:: Allow comments in JSON files
+
+    .. todo:: Allow all Python string literals
+
+    .. todo:: Use OrderedDict as default mapping implementation (Python 2.7+)
+
+    """
+    with io.open(filename, "rt", encoding=encoding) as jsfp:
+        decoder = json.decoder.JSONDecoder(
+            encoding=encoding,
+            parse_int=lambda n: int(n, 0),
+            strict=False)
+        return decoder.decode(jsfp.read())