annotate configmix/json.py @ 148:be352645871c

Work around the deprecation of SafeConfigParser.readfp in Python 3.2+
author Franz Glasner <hg@dom66.de>
date Sat, 14 Apr 2018 12:44:42 +0200
parents 7e6ec99d5ff5
children e2e8d21b4122
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
127
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
14 try:
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
15 from collections import OrderedDict as DictImpl
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
16 except ImportError:
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
17 try:
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
18 from ordereddict import OrderedDict as DictImpl
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
19 except ImportError:
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
20 DictImpl = dict
122
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
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
23 __all__ = ["load"]
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
24
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
25
127
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
26 #
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
27 # Determine whether the JSONDecoder has the "object_pairs_hook"
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
28 # parameter once
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
29 #
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
30 try:
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
31 json.decoder.JSONDecoder(object_pairs_hook=DictImpl)
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
32 except TypeError:
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
33 _with_object_pairs_hook = False
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
34 else:
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
35 _with_object_pairs_hook = True
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
36
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
37
122
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
38 def load(filename, encoding="utf-8"):
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
39 """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
40
21d92ff8cf31 Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff changeset
41 """
124
be6cdc9cb79c Use keyword "mode" for file file mode
Franz Glasner <f.glasner@feldmann-mg.com>
parents: 123
diff changeset
42 with io.open(filename, mode="rt", encoding=encoding) as jsfp:
129
df60417d7665 The JSON scanner allows only decimal integers
Franz Glasner <hg@dom66.de>
parents: 128
diff changeset
43 #
df60417d7665 The JSON scanner allows only decimal integers
Franz Glasner <hg@dom66.de>
parents: 128
diff changeset
44 # The scanner (not to be changed yet) does only recognize decimal
df60417d7665 The JSON scanner allows only decimal integers
Franz Glasner <hg@dom66.de>
parents: 128
diff changeset
45 # integers yet.
df60417d7665 The JSON scanner allows only decimal integers
Franz Glasner <hg@dom66.de>
parents: 128
diff changeset
46 #
127
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
47 kwds = {
128
95ad65c69561 FIX: todo comment: allow **numbers** -- not strings
Franz Glasner <hg@dom66.de>
parents: 127
diff changeset
48 "parse_int": lambda s: int(s, 0),
127
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
49 "strict": False
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
50 }
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
51 if _with_object_pairs_hook:
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
52 kwds["object_pairs_hook"] = DictImpl
5b62d2c0e5a8 Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents: 124
diff changeset
53 decoder = json.decoder.JSONDecoder(**kwds)
134
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 129
diff changeset
54 data = decoder.decode(jsfp.read())
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 129
diff changeset
55 if not isinstance(data, DictImpl):
137
e84870359e1a Enhance docu
Franz Glasner <hg@dom66.de>
parents: 134
diff changeset
56 raise TypeError("JSON root must be an object (i.e. dict)")
134
2f2e819e8d17 Check the return type of the JSON and YAML loading functions: they must be a dict alike
Franz Glasner <hg@dom66.de>
parents: 129
diff changeset
57 return data