Mercurial > hgrepos > Python > libs > ConfigMix
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 121:0d378dcc018b | 122:21d92ff8cf31 |
|---|---|
| 1 # -*- coding: utf-8 -*- | |
| 2 #- | |
| 3 # :Copyright: (c) 2018, Franz Glasner. All rights reserved. | |
| 4 # :License: 3-clause BSD. See LICENSE.txt for details. | |
| 5 #- | |
| 6 """Read JSON-style configuration files. | |
| 7 | |
| 8 """ | |
| 9 | |
| 10 from __future__ import division, absolute_import, print_function | |
| 11 | |
| 12 import io | |
| 13 import json.decoder | |
| 14 | |
| 15 | |
| 16 __all__ = ["load"] | |
| 17 | |
| 18 | |
| 19 def load(filename, encoding="utf-8"): | |
| 20 """Load a single JSON file with name `filename` and encoding `encoding`. | |
| 21 | |
| 22 .. todo:: Allow comments in JSON files | |
| 23 | |
| 24 .. todo:: Allow all Python string literals | |
| 25 | |
| 26 .. todo:: Use OrderedDict as default mapping implementation (Python 2.7+) | |
| 27 | |
| 28 """ | |
| 29 with io.open(filename, "rt", encoding=encoding) as jsfp: | |
| 30 decoder = json.decoder.JSONDecoder( | |
| 31 encoding=encoding, | |
| 32 parse_int=lambda n: int(n, 0), | |
| 33 strict=False) | |
| 34 return decoder.decode(jsfp.read()) |
