Skip to content
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

Return help text #913

Merged
merged 6 commits into from
Apr 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 45 additions & 30 deletions pynest/nest/lib/hl_api_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,41 @@ def __show_help_in_modal_window(objname, hlptxt):
display(Javascript(s.substitute(jstitle=objname, jstext=hlptxt)))


def load_help(hlpobj, return_filename=False):
"""Returns documentation of the object

Parameters
----------
hlpobj : object
Object to display help for
return_filename : bool
Option for returning filename

Returns
-------
string:
The documentation of the object.
string:
Filename for the object
"""

helpdir = os.path.join(os.environ['NEST_INSTALL_DIR'], "share", "doc",
"nest", "help")
objname = hlpobj + '.hlp'

# Searching the given object in all helpfiles
for dirpath, dirnames, files in os.walk(helpdir):
for hlp in files:
if hlp == objname:
objf = os.path.join(dirpath, objname)
with open(objf, 'r') as fhlp:
hlptxt = fhlp.read()
if return_filename:
return hlptxt, objf
else:
return hlptxt


def show_help_with_pager(hlpobj, pager):
"""Output of doc in python with pager or print

Expand Down Expand Up @@ -500,37 +535,17 @@ def show_help_with_pager(hlpobj, pager):
rc.close()
else:
pager = 'more'
hlperror = True
# Searching the given object in all helpfiles, check the environment

# Load the helptext, check the environment
# and display the helptext in the pager.
for dirpath, dirnames, files in os.walk(helpdir):
for hlp in files:
if hlp == objname:
hlperror = False
objf = os.path.join(dirpath, objname)
fhlp = open(objf, 'r')
hlptxt = fhlp.read()
# only for notebook
if __check_nb():
if pager in consolepager:
# only in notebook open modal window
__show_help_in_modal_window(objname, hlptxt)
fhlp.close()
break
else:
subprocess.call([pager, objf])
fhlp.close()
break
else:
if pager in consolepager:
subprocess.call([pager, objf])
fhlp.close()
break
else:
subprocess.call([pager, objf])
fhlp.close()
break
if hlperror:
hlptxt, objf = load_help(hlpobj, return_filename=True)
if hlptxt:
if pager in consolepager and __check_nb():
# only in notebook open modal window
__show_help_in_modal_window(objname, hlptxt)
else:
subprocess.call([pager, objf])
else:
print("Sorry, there is no help for '" + hlpobj + "'!")


Expand Down
9 changes: 7 additions & 2 deletions pynest/nest/lib/hl_api_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def helpdesk():


@check_stack
def help(obj=None, pager=None):
def help(obj=None, pager=None, return_text=False):
"""Show the help page for the given object using the given pager.

The default pager is more.
Expand All @@ -102,10 +102,15 @@ def help(obj=None, pager=None):
Object to display help for
pager : str, optional
Pager to use
return_text : bool, optional
Option for returning the help text
"""
hlpobj = obj
if hlpobj is not None:
show_help_with_pager(hlpobj, pager)
if return_text:
return load_help(hlpobj)
else:
show_help_with_pager(hlpobj, pager)

else:
print("Type 'nest.helpdesk()' to access the online documentation "
Expand Down