6
6
import re
7
7
import sys
8
8
from datetime import timedelta
9
- from importlib .resources import files
10
9
from path import Path
11
10
12
11
from django .conf import settings
13
- from django .core .management .base import BaseCommand , CommandError
14
- from django .test import TestCase
12
+ from django .core .management .base import BaseCommand
15
13
16
14
17
15
SETTING_NAME_REGEX = re .compile (r'^[A-Z][A-Z0-9_]*$' )
@@ -48,14 +46,14 @@ def handle(self, *args, **kwargs):
48
46
Handle the command.
49
47
"""
50
48
settings_json = {
51
- name : _to_json_friendly_repr (getattr (settings , name ))
49
+ name : _to_json_friendly_repr (getattr (settings , name ), f"settings. { name } " )
52
50
for name in dir (settings )
53
51
if SETTING_NAME_REGEX .match (name )
54
52
}
55
53
print (json .dumps (settings_json , indent = 4 ))
56
54
57
55
58
- def _to_json_friendly_repr (value : object ) -> object :
56
+ def _to_json_friendly_repr (value : object , debug_key : str ) -> object :
59
57
"""
60
58
Turn the value into something that we can print to a JSON file (that is: str, bool, None, int, float, list, dict).
61
59
@@ -64,18 +62,20 @@ def _to_json_friendly_repr(value: object) -> object:
64
62
if isinstance (value , (type (None ), bool , int , float , str )):
65
63
# All these types can be printed directly
66
64
return value
67
- if isinstance (value , (list , tuple )):
68
- # Print both lists and tuples as JSON arrays
69
- return [_to_json_friendly_repr (element ) for element in value ]
70
- if isinstance (value , set ):
71
- # Print sets by sorting them (so that order doesn't matter) and printing the result as a JSON array
72
- return [sorted (_to_json_friendly_repr (element ) for element in value )]
65
+ if isinstance (value , (list , tuple , set )):
66
+ if isinstance (value , set ):
67
+ # Print sets by sorting them (so that order doesn't matter) into a JSON array.
68
+ elements = sorted (value )
69
+ else :
70
+ # Print both lists and tuples as JSON arrays.
71
+ elements = value
72
+ return [_to_json_friendly_repr (element , f"{ debug_key } [{ ix } ]" ) for ix , element in enumerate (elements )]
73
73
if isinstance (value , dict ):
74
74
# Print dicts as JSON objects
75
75
for subkey in value .keys ():
76
76
if not isinstance (subkey , (str , int )):
77
77
raise ValueError (f"Unexpected dict key { subkey } of type { type (subkey )} " )
78
- return {subkey : _to_json_friendly_repr (subval ) for subkey , subval in value .items ()}
78
+ return {subkey : _to_json_friendly_repr (subval , f" { debug_key } [ { subkey !r } ]" ) for subkey , subval in value .items ()}
79
79
if isinstance (value , Path ):
80
80
# Print path objects as the string `Path('path/to/something')`.
81
81
return repr (value )
@@ -87,7 +87,7 @@ def _to_json_friendly_repr(value: object) -> object:
87
87
if len (proxy_args ) == 1 :
88
88
if isinstance (proxy_args [0 ], str ):
89
89
return proxy_args [0 ]
90
- raise ValueError (f"Not sure how to dump value { value !r} with proxy args { proxy_args !r} " )
90
+ raise ValueError (f"Not sure how to dump { debug_key } with value { value !r} with proxy args { proxy_args !r} " )
91
91
if value is sys .stderr :
92
92
# Print the stderr object as simply "sys.stderr"
93
93
return "sys.stderr"
@@ -97,7 +97,9 @@ def _to_json_friendly_repr(value: object) -> object:
97
97
qualname = value .__qualname__
98
98
except AttributeError :
99
99
# If that doesn't work, then give up--we don't know how to print this value.
100
- raise ValueError (f"Not sure how to dump value { value !r} of type { type (value )} " )
100
+ raise ValueError ( # pylint: disable=raise-missing-from
101
+ f"Not sure how to dump { debug_key } with value { value !r} of type { type (value )} "
102
+ )
101
103
if qualname == "<lambda>" :
102
104
# Handle lambdas by printing the source lines
103
105
return inspect .getsource (value ).strip ()
0 commit comments