Skip to content

Commit 0c1dcc3

Browse files
committed
feat: change comment in dictConfig json file to // from #
Emacs json mode at least will properly indent when using // as a comment character and not #.
1 parent 98ef003 commit 0c1dcc3

File tree

3 files changed

+53
-47
lines changed

3 files changed

+53
-47
lines changed

doc/admin_guide.txt

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,18 @@ dictConfigs are specified in JSON format with support for comments.
110110
The file name in the tracker's config for the ``logging`` -> ``config``
111111
setting must end with ``.json`` to choose the correct processing.
112112

113-
Comments have to be in one of two forms:
113+
Comments have to be in one of two forms based on javascript line
114+
comments:
114115

115-
1. A ``#`` with preceding white space is considered a comment and is
116-
stripped from the file before being passed to the json parser. This
117-
is a "block comment".
116+
1. A ``//`` possibly indented with whitespace on a line is considereda
117+
a comment and is stripped from the file before being passed to the
118+
json parser. This is a "line comment".
118119

119-
2. A ``#`` preceded by at least three
120-
white space characters is stripped from the end of the line before
121-
begin passed to the json parser. This is an "inline comment".
120+
2. A ``//` with at least three white space characters before it is
121+
stripped from the end of the line before begin passed to the json
122+
parser. This is an "inline comment".
123+
124+
Block style comments are not supported.
122125

123126
Other than this the file is a standard json file that matches the
124127
`Configuration dictionary schema
@@ -129,9 +132,12 @@ defined in the Python documentation.
129132
Example dictConfig Logging Config
130133
.................................
131134

132-
Note that this file is not actually JSON format as it include comments.
133-
So you can not use tools that expect JSON (linters, formatters) to
134-
work with it.
135+
Note that this file is not actually JSON format as it include
136+
comments. However by using javascript style comments, some tools that
137+
expect JSON (editors, linters, formatters) might work with it. A
138+
command like ``sed -e 's#^\s*//.*##' -e 's#\s*\s\s\s//.*##'
139+
logging.json`` can be used to strip comments for programs that need
140+
it.
135141

136142
The config below works with the `Waitress wsgi server
137143
<https://github.com/Pylons/waitress>`_ configured to use the
@@ -143,35 +149,35 @@ the tracker home. The tracker home is the subdirectory demo under the
143149
current working directory. The commented config is::
144150

145151
{
146-
"version": 1, # only supported version
147-
"disable_existing_loggers": false, # keep the wsgi loggers
152+
"version": 1, // only supported version
153+
"disable_existing_loggers": false, // keep the wsgi loggers
148154

149155
"formatters": {
150-
# standard format for Roundup messages
156+
// standard format for Roundup messages
151157
"standard": {
152-
"format": "%(asctime)s %(levelname)s %(name)s:%(module)s %(msg)s"
158+
"format": "%(asctime)s %(ctx_id)s %(levelname)s %(name)s:%(module)s %(msg)s"
153159
},
154-
# used for waitress wsgi server to produce httpd style logs
160+
// used for waitress wsgi server to produce httpd style logs
155161
"http": {
156162
"format": "%(message)s"
157163
}
158164
},
159165
"handlers": {
160-
# create an access.log style http log file
166+
// create an access.log style http log file
161167
"access": {
162168
"level": "INFO",
163169
"formatter": "http",
164170
"class": "logging.FileHandler",
165171
"filename": "demo/access.log"
166172
},
167-
# logging for roundup.* loggers
173+
// logging for roundup.* loggers
168174
"roundup": {
169175
"level": "DEBUG",
170176
"formatter": "standard",
171177
"class": "logging.FileHandler",
172178
"filename": "demo/roundup.log"
173179
},
174-
# print to stdout - fall through for other logging
180+
// print to stdout - fall through for other logging
175181
"default": {
176182
"level": "DEBUG",
177183
"formatter": "standard",
@@ -187,30 +193,30 @@ current working directory. The commented config is::
187193
"level": "DEBUG",
188194
"propagate": false
189195
},
190-
# used by roundup.* loggers
196+
// used by roundup.* loggers
191197
"roundup": {
192198
"handlers": [
193199
"roundup"
194200
],
195201
"level": "DEBUG",
196-
"propagate": false # note pytest testing with caplog requires
197-
# this to be true
202+
"propagate": false // note pytest testing with caplog requires
203+
// this to be true
198204
},
199205
"roundup.hyperdb": {
200206
"handlers": [
201207
"roundup"
202208
],
203-
"level": "INFO", # can be a little noisy use INFO for production
209+
"level": "INFO", // can be a little noisy use INFO for production
204210
"propagate": false
205211
},
206-
"roundup.wsgi": { # using the waitress framework
212+
"roundup.wsgi": { // using the waitress framework
207213
"handlers": [
208214
"roundup"
209215
],
210216
"level": "DEBUG",
211217
"propagate": false
212218
},
213-
"roundup.wsgi.translogger": { # httpd style logging
219+
"roundup.wsgi.translogger": { // httpd style logging
214220
"handlers": [
215221
"access"
216222
],

roundup/configuration.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2343,9 +2343,9 @@ def reset(self):
23432343
def load_config_dict_from_json_file(self, filename):
23442344
import json
23452345
comment_re = re.compile(
2346-
r"""^\s*\#.* # comment at beginning of line possibly indented.
2346+
r"""^\s*//#.* # comment at beginning of line possibly indented.
23472347
| # or
2348-
^(.*)\s\s\s\#.* # comment char preceeded by at least three spaces.
2348+
^(.*)\s\s\s\//.* # comment char preceeded by at least three spaces.
23492349
""", re.VERBOSE)
23502350

23512351
config_list = []
@@ -2371,8 +2371,8 @@ def load_config_dict_from_json_file(self, filename):
23712371
line = config_list[error_at_doc_line - 1][:-1]
23722372

23732373
hint = ""
2374-
if line.find('#') != -1:
2375-
hint = "\nMaybe bad inline comment, 3 spaces needed before #."
2374+
if line.find('//') != -1:
2375+
hint = "\nMaybe bad inline comment, 3 spaces needed before //."
23762376

23772377
raise LoggingConfigError(
23782378
'Error parsing json logging dict (%(file)s) '

test/test_config.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,35 +1117,35 @@ def testDictLoggerConfigViaJson(self):
11171117
# good base test case
11181118
config1 = dedent("""
11191119
{
1120-
"version": 1, # only supported version
1121-
"disable_existing_loggers": false, # keep the wsgi loggers
1120+
"version": 1, // only supported version
1121+
"disable_existing_loggers": false, // keep the wsgi loggers
11221122
11231123
"formatters": {
1124-
# standard Roundup formatter including context id.
1124+
// standard Roundup formatter including context id.
11251125
"standard": {
11261126
"format": "%(asctime)s %(levelname)s %(name)s:%(module)s %(msg)s"
11271127
},
1128-
# used for waitress wsgi server to produce httpd style logs
1128+
// used for waitress wsgi server to produce httpd style logs
11291129
"http": {
11301130
"format": "%(message)s"
11311131
}
11321132
},
11331133
"handlers": {
1134-
# create an access.log style http log file
1134+
// create an access.log style http log file
11351135
"access": {
11361136
"level": "INFO",
11371137
"formatter": "http",
11381138
"class": "logging.FileHandler",
11391139
"filename": "_test_instance/access.log"
11401140
},
1141-
# logging for roundup.* loggers
1141+
// logging for roundup.* loggers
11421142
"roundup": {
11431143
"level": "DEBUG",
11441144
"formatter": "standard",
11451145
"class": "logging.FileHandler",
11461146
"filename": "_test_instance/roundup.log"
11471147
},
1148-
# print to stdout - fall through for other logging
1148+
// print to stdout - fall through for other logging
11491149
"default": {
11501150
"level": "DEBUG",
11511151
"formatter": "standard",
@@ -1156,35 +1156,35 @@ def testDictLoggerConfigViaJson(self):
11561156
"loggers": {
11571157
"": {
11581158
"handlers": [
1159-
"default" # used by wsgi/usgi
1159+
"default" // used by wsgi/usgi
11601160
],
11611161
"level": "DEBUG",
11621162
"propagate": false
11631163
},
1164-
# used by roundup.* loggers
1164+
// used by roundup.* loggers
11651165
"roundup": {
11661166
"handlers": [
11671167
"roundup"
11681168
],
11691169
"level": "DEBUG",
1170-
"propagate": false # note pytest testing with caplog requires
1171-
# this to be true
1170+
"propagate": false // note pytest testing with caplog requires
1171+
// this to be true
11721172
},
11731173
"roundup.hyperdb": {
11741174
"handlers": [
11751175
"roundup"
11761176
],
1177-
"level": "INFO", # can be a little noisy INFO for production
1177+
"level": "INFO", // can be a little noisy INFO for production
11781178
"propagate": false
11791179
},
1180-
"roundup.wsgi": { # using the waitress framework
1180+
"roundup.wsgi": { // using the waitress framework
11811181
"handlers": [
11821182
"roundup"
11831183
],
11841184
"level": "DEBUG",
11851185
"propagate": false
11861186
},
1187-
"roundup.wsgi.translogger": { # httpd style logging
1187+
"roundup.wsgi.translogger": { // httpd style logging
11881188
"handlers": [
11891189
"access"
11901190
],
@@ -1215,7 +1215,7 @@ def testDictLoggerConfigViaJson(self):
12151215
self.assertEqual(config['version'], 1)
12161216

12171217
# broken inline comment misformatted
1218-
test_config = config1.replace(": 1, #", ": 1, #")
1218+
test_config = config1.replace(": 1, //", ": 1, //")
12191219
with open(log_config_filename, "w") as log_config_file:
12201220
log_config_file.write(test_config)
12211221

@@ -1226,9 +1226,9 @@ def testDictLoggerConfigViaJson(self):
12261226
cm.exception.args[0],
12271227
('Error parsing json logging dict '
12281228
'(%s) near \n\n '
1229-
'"version": 1, # only supported version\n\nExpecting '
1229+
'"version": 1, // only supported version\n\nExpecting '
12301230
'property name enclosed in double quotes: line 3 column 18.\n'
1231-
'Maybe bad inline comment, 3 spaces needed before #.' %
1231+
'Maybe bad inline comment, 3 spaces needed before //.' %
12321232
log_config_filename)
12331233
)
12341234

@@ -1318,8 +1318,8 @@ def testDictLoggerConfigViaJson(self):
13181318

13191319
# broken invalid level MANGO
13201320
test_config = config1.replace(
1321-
': "INFO", # can',
1322-
': "MANGO", # can')
1321+
': "INFO", // can',
1322+
': "MANGO", // can')
13231323
with open(log_config_filename, "w") as log_config_file:
13241324
log_config_file.write(test_config)
13251325

0 commit comments

Comments
 (0)