Mercurial > hgrepos > Python > libs > ConfigMix
annotate configmix/json.py @ 208:bbe8513ea649
Handle flake8 E265 "block comment should start with '# ': use '# :-' instead of '#-' to mark copyright and license comments
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Sun, 05 May 2019 18:40:03 +0200 |
| parents | b3b5ed34d180 |
| children | 13711ba8e81e |
| 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 -*- |
|
208
bbe8513ea649
Handle flake8 E265 "block comment should start with '# ': use '# :-' instead of '#-' to mark copyright and license comments
Franz Glasner <fzglas.hg@dom66.de>
parents:
207
diff
changeset
|
2 # :- |
|
156
e2e8d21b4122
Adjust copyright to year 2019
Franz Glasner <fzglas.hg@dom66.de>
parents:
144
diff
changeset
|
3 # :Copyright: (c) 2018-2019, Franz Glasner. All rights reserved. |
|
122
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. |
|
208
bbe8513ea649
Handle flake8 E265 "block comment should start with '# ': use '# :-' instead of '#-' to mark copyright and license comments
Franz Glasner <fzglas.hg@dom66.de>
parents:
207
diff
changeset
|
5 # :- |
|
122
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 |
|
207
b3b5ed34d180
Handle most flake8 errors and warnings.
Franz Glasner <fzglas.hg@dom66.de>
parents:
166
diff
changeset
|
22 from .compat import u2fs |
|
166
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
23 |
|
122
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 __all__ = ["load"] |
|
21d92ff8cf31
Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
26 |
|
21d92ff8cf31
Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
27 |
|
127
5b62d2c0e5a8
Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents:
124
diff
changeset
|
28 # |
|
5b62d2c0e5a8
Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents:
124
diff
changeset
|
29 # 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
|
30 # parameter once |
|
5b62d2c0e5a8
Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents:
124
diff
changeset
|
31 # |
|
5b62d2c0e5a8
Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents:
124
diff
changeset
|
32 try: |
|
5b62d2c0e5a8
Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents:
124
diff
changeset
|
33 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
|
34 except TypeError: |
|
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 = False |
|
5b62d2c0e5a8
Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents:
124
diff
changeset
|
36 else: |
|
5b62d2c0e5a8
Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents:
124
diff
changeset
|
37 _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
|
38 |
|
5b62d2c0e5a8
Use the available "OrderedDict" class as dict for the JSON parser
Franz Glasner <hg@dom66.de>
parents:
124
diff
changeset
|
39 |
|
122
21d92ff8cf31
Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
40 def load(filename, encoding="utf-8"): |
|
21d92ff8cf31
Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
41 """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
|
42 |
|
21d92ff8cf31
Begin the handling of JSON-style configuration files
Franz Glasner <hg@dom66.de>
parents:
diff
changeset
|
43 """ |
|
166
b5ce9a8461bf
Use the filesystem encoding explicitely where appropriate.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
44 with io.open(u2fs(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): |
| 137 | 58 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
|
59 return data |
