Skip to content

Commit 7a43ce9

Browse files
committed
feat: add support for controlling readline history features
You can disable loading or saving the history file and disable loading the readline rc file. This is controlled by pragma history_features which is a bit string. Setting a bit disables the feature. Also cleaned up a python traceback if an invalid pragma was specified on the command line. Just displays an error message and drops into interactive mode. I had a hand when running regression tests with tet-admin.py::testBasicInteractive. It was loading the history file and saving the commands making the file huge. So disable all history_features by default when testing by setting pragma to 7.
1 parent 316a454 commit 7a43ce9

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

roundup/admin.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ def __init__(self):
114114
'display_header': False,
115115
'display_protected': False,
116116
'indexer_backend': "as set in config.ini",
117+
'history_features': 0,
117118
'history_length': -1,
118119
'_reopen_tracker': False,
119120
'savepoint_limit': self._default_savepoint_setting,
@@ -132,6 +133,15 @@ def __init__(self):
132133
_("Have 'display designator' and 'specification class' show\n"
133134
" protected fields: creator, id etc.\n"),
134135

136+
'history_features':
137+
_("Controls history options. It is a bitstring where setting\n"
138+
" the bit disables the feature. A value of 0 (default)\n"
139+
" enables all features. Value 1 disables loading of\n"
140+
" history. Value 2 disables saving history. Value 4\n"
141+
" disables loading init file. Since it is a bitstring a\n"
142+
" value of 6 disables both loading init file and saving\n"
143+
" history.\n"),
144+
135145
'history_length':
136146
_("Set the number of lines of history to keep for this session.\n"
137147
" -1 is infinite.\n"),
@@ -2263,6 +2273,21 @@ def run_command(self, args):
22632273
ret = 1
22642274
return ret
22652275

2276+
def history_features(self, feature):
2277+
""" self.settings['history_features'] = 0: load rc, load/save history
2278+
self.settings['history_features'] = 1: do not load history
2279+
self.settings['history_features'] = 2: do not save history
2280+
self.settings['history_features'] = 4: don't load rc
2281+
"""
2282+
2283+
features = { # bit bashing
2284+
'load_history': 1,
2285+
'save_history': 2,
2286+
'load_rc': 4}
2287+
2288+
# setting the bit disables the feature, so use not.
2289+
return not self.settings['history_features'] & features[feature]
2290+
22662291
def interactive(self):
22672292
"""Run in an interactive mode
22682293
"""
@@ -2277,13 +2302,15 @@ def interactive(self):
22772302
try:
22782303
import readline
22792304
try:
2280-
readline.read_init_file(initfile)
2305+
if self.history_features('load_rc'):
2306+
readline.read_init_file(initfile)
22812307
except IOError: # FileNotFoundError under python3
22822308
# file is optional
22832309
pass
22842310

22852311
try:
2286-
readline.read_history_file(histfile)
2312+
if self.history_features('load_history'):
2313+
readline.read_history_file(histfile)
22872314
except IOError: # FileNotFoundError under python3
22882315
# no history file yet
22892316
pass
@@ -2322,7 +2349,8 @@ def interactive(self):
23222349

23232350
# looks like histfile is saved with mode 600
23242351
if readline:
2325-
readline.write_history_file(histfile)
2352+
if self.history_features('save_history'):
2353+
readline.write_history_file(histfile)
23262354
return 0
23272355

23282356
def main(self): # noqa: PLR0912, PLR0911
@@ -2374,7 +2402,10 @@ def main(self): # noqa: PLR0912, PLR0911
23742402
elif opt == '-d':
23752403
self.print_designator = 1
23762404
elif opt == '-P':
2377-
self.do_pragma([arg])
2405+
try:
2406+
self.do_pragma([arg])
2407+
except UsageError as e:
2408+
print('\n%s\n' % e)
23782409
elif opt == '-u':
23792410
login_opt = arg.split(':')
23802411
self.name = login_opt[0]

test/test_admin.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ def testBasicInteractive(self):
134134

135135
self.install_init()
136136
self.admin=AdminTool()
137-
sys.argv=['main', '-i', self.dirname]
137+
# set history_features to disable loading/saving history
138+
# and loading rc file. Otherwise file gets large and
139+
# breaks testing or overwrites the users history file.
140+
sys.argv=['main', '-i', self.dirname, '-P', 'history_features=7']
138141

139142
with captured_output() as (out, err):
140143
ret = self.admin.main()

0 commit comments

Comments
 (0)