annotate configmix/json.py @ 124:be6cdc9cb79c

Use keyword "mode" for file file mode
author Franz Glasner <f.glasner@feldmann-mg.com>
date Wed, 04 Apr 2018 10:12:04 +0200
parents 4218c66b9281
children 5b62d2c0e5a8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
122
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
2 #-
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
3 # :Copyright: (c) 2018, Franz Glasner. All rights reserved.
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
4 # :License: 3-clause BSD. See LICENSE.txt for details.
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
5 #-
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
6 """Read JSON-style configuration files.
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
7
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
8 """
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
9
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
10 from __future__ import division, absolute_import, print_function
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
11
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
12 import io
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
13 import json.decoder
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
14
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
15
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
16 __all__ = ["load"]
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
17
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
18
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
19 def load(filename, encoding="utf-8"):
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
20 """Load a single JSON file with name `filename` and encoding `encoding`.
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
21
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
22 .. todo:: Allow comments in JSON files
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
23
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
24 .. todo:: Allow all Python string literals
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
25
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
26 .. todo:: Use OrderedDict as default mapping implementation (Python 2.7+)
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
27
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
28 """
124
be6cdc9cb79c Use keyword "mode" for file file mode
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 123
diff changeset
29 with io.open(filename, mode="rt", encoding=encoding) as jsfp:
122
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
30 decoder = json.decoder.JSONDecoder(
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
31 parse_int=lambda n: int(n, 0),
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
32 strict=False)
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
33 return decoder.decode(jsfp.read())