Skip to content

Commit bde69d9

Browse files
author
James William Pye
committed
Be sure to properly quote new settings.
Change pg_dotconf to comment settings when no '=' is present.
1 parent ae6e997 commit bde69d9

3 files changed

Lines changed: 28 additions & 14 deletions

File tree

postgresql/bin/pg_dotconf.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#!/usr/bin/env python
22
import sys
33
from optparse import OptionParser
4-
from postgresql.configfile import pg_cf
4+
from .. import configfile as pg_cf
55

6-
__all__ = ['pg_dotconf']
6+
__all__ = ['command']
77

88
def command(args):
99
"""
1010
pg_dotconf script entry point.
1111
"""
1212
op = OptionParser(
13-
"%prog [--stdout] [-f settings] config_file_src [param=val]+",
13+
"%prog [--stdout] [-f settings] config_file_src ([param=val]|[param])+",
1414
version = '0'
1515
)
1616
op.add_option(
@@ -41,15 +41,9 @@ def command(args):
4141
settings[line[pl[0]]] = unquote(line[pl[1]])
4242

4343
for p in ca[1:]:
44-
if p.startswith('#'):
45-
k = p[1:]
44+
if '=' not in p:
45+
k = p
4646
v = None
47-
elif '=' not in p:
48-
sys.stderr.write(
49-
"ERROR: setting parameter, %r, does not have '=' " \
50-
"to separate setting name from setting value"
51-
)
52-
return 1
5347
else:
5448
k, v = p.split('=', 1)
5549
settings[k] = v

postgresql/configfile.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def alter_config(
125125
if val is not None:
126126
if not lines[-1].endswith(os.linesep):
127127
lines[-1] = lines[-1] + os.linesep
128-
lines.append('%s = %s' %(key, val))
128+
lines.append("%s = '%s'" %(key, val.replace("'", "''")))
129129

130130
# Multiple lines may have the key, so make a decision based on the value.
131131
for ck in candidates.keys():
@@ -211,12 +211,14 @@ def read_config(iter, d = None, selector = None):
211211

212212
class ConfigFile(pg_api.Settings):
213213
"""
214-
Provides a dictionary mapping interface to a configuration file.
214+
Provides a mapping interface to a configuration file.
215215
216-
Every action will cause the file to be wholly read.
216+
Every action will cause the file to be wholly read, so using `update` to make
217+
multiple changes is desirable.
217218
"""
218219
ife_ancestor = None
219220
ife_label = 'CONFIGFILE'
221+
220222
def ife_snapshot_text(self):
221223
s = (os.linesep+' ').join(
222224
k + ' = ' + v for k,v in self.items()

postgresql/test/test_configfile.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,24 @@
108108
{'foo' : 'newval'},
109109
"foo = 'newval'#foo this should be commented"
110110
),
111+
(
112+
# New setting in empty string
113+
"",
114+
{'bar' : 'newvar'},
115+
"bar = 'newvar'",
116+
),
117+
(
118+
# New setting
119+
"foo = 'bar'",
120+
{'bar' : 'newvar'},
121+
"foo = 'bar'\nbar = 'newvar'",
122+
),
123+
(
124+
# New setting with quote escape
125+
"foo = 'bar'",
126+
{'bar' : "new'var"},
127+
"foo = 'bar'\nbar = 'new''var'",
128+
),
111129
]
112130

113131
class test_configfile(unittest.TestCase):

0 commit comments

Comments
 (0)