Mercurial > hgrepos > Python > libs > ConfigMix
annotate configmix/json.py @ 134:2f2e819e8d17
Check the return type of the JSON and YAML loading functions: they must be a dict alike
| author | Franz Glasner <hg@dom66.de> |
|---|---|
| date | Thu, 05 Apr 2018 09:12:29 +0200 |
| parents | df60417d7665 |
| children | e84870359e1a |
| 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 .. todo:: Allow comments in JSON files |
|
21d92ff8cf31
Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
42 |
|
21d92ff8cf31
Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
43 """ |
|
124
be6cdc9cb79c
Use keyword "mode" for file file mode
Franz Glasner <f.glasner@feldmann-mg.com>
parents:
123
diff
changeset
|
44 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
|
45 # |
|
df60417d7665
The JSON scanner allows only decimal integers
Franz Glasner <hg@dom66.de>
parents:
128
diff
changeset
|
46 # 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
|
47 # integers yet. |
|
df60417d7665
The JSON scanner allows only decimal integers
Franz Glasner <hg@dom66.de>
parents:
128
diff
changeset
|
48 # |
|
127
5b62d2c0e5a8
Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents:
124
diff
changeset
|
49 kwds = { |
|
128
95ad65c69561
FIX: todo comment: allow **numbers** -- not strings
Franz Glasner <hg@dom66.de>
parents:
127
diff
changeset
|
50 "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
|
51 "strict": False |
|
5b62d2c0e5a8
Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents:
124
diff
changeset
|
52 } |
|
5b62d2c0e5a8
Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents:
124
diff
changeset
|
53 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
|
54 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
|
55 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
|
56 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
|
57 if not isinstance(data, DictImpl): |
|
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
|
58 raise TypeError("JSON root must be an object") |
|
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
|
59 return data |
