-
Notifications
You must be signed in to change notification settings - Fork 371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix parsing of ~/.nestrc in show_help_with_pager() #931
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -504,37 +504,27 @@ def show_help_with_pager(hlpobj, pager): | |
consolepager = ['less', 'more', 'vi', 'vim', 'nano', 'emacs -nw', | ||
'ed', 'editor'] | ||
|
||
# reading ~/.nestrc lookink for pager to use. | ||
# check for pager command in ~/.nestrc if none provided in function call | ||
if pager is None: | ||
# check if .netsrc exist | ||
rc_file = os.path.join(os.environ['HOME'], '.nestrc') | ||
if os.path.isfile(rc_file): | ||
# open ~/.nestrc | ||
rc = open(rc_file, 'r') | ||
# The loop goes through the .nestrc line by line and checks | ||
# it for the definition of a pager. Whether a pager is | ||
# found or not, this pager is used or the standard pager 'more'. | ||
for line in rc: | ||
# the re checks if there are lines beginning with '%' | ||
rctst = re.match(r'^\s?%', line) | ||
if rctst is None: | ||
# the next re checks for a sth. like | ||
# '/page << /command (more)' | ||
# and returns the given pager. | ||
pypagers = re.findall( | ||
r'^\s?/page\s?<<\s?/command\s?\((\w*)', line) | ||
if pypagers: | ||
for pa in pypagers: | ||
if pa: | ||
pager = pa | ||
else: | ||
pager = 'more' | ||
break | ||
else: | ||
pager = 'more' | ||
rc.close() | ||
else: | ||
pager = 'more' | ||
# default to `more` | ||
pager = 'more' | ||
|
||
# Try to open ~/.nestrc safely | ||
try: | ||
with open(os.path.join(os.environ['HOME'], '.nestrc'), 'r') as rc: | ||
for line in rc: | ||
# Go through the .nestrc line by line to check for the | ||
# definition of a pager using a regular expression | ||
# which matches lines of the format | ||
# ' /page << /command (more)' | ||
# but ignores comments (via the `(?!%)` part) | ||
for pa in re.findall( | ||
r'^\s?(?!%)\s?\/page\s?<<\s?\/command\s?\((\w*)', | ||
line): | ||
pager = pa | ||
except: | ||
print('Failed to open ~/.nestrc, using default pager') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would rather write "parse" than "open", since in principle an exception could occur during reading as well. |
||
pass | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you parsing the
to get the command in a much safer way. To have a default pager available in case
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jougs The parsing code was introduced over a year ago in bdc87b7 and merged with #688. I reviewed then, and as now entirely overlooked the fact that we know the parser once NEST is loaded. So @physicalist, the solution proposed by @jougs is the way to go. |
||
# Load the helptext, check the environment | ||
# and display the helptext in the pager. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PEP8 advises against bare except clauses.
except IOError
seems appropriate here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, I meant to look up the most general but still appropriate exception class but forgot to do that :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the above solution the exception class would simply be
NESTError
.