Skip to content

Commit

Permalink
Merge 02cb775 into a801697
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex L. Urban committed Apr 7, 2019
2 parents a801697 + 02cb775 commit d564b79
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
71 changes: 68 additions & 3 deletions gwdetchar/io/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,66 @@ def new_bootstrap_page(base=os.path.curdir, lang='en', refresh=False,
return page


def about_this_page(config, packagelist=True):
"""Write a blurb documenting how a page was generated, including the
command-line arguments and configuration files used
Parameters
----------
config : `str`, `list`, optional
the absolute path(s) to one or a number of INI files used in this
process
packagelist : `bool`, optional
boolean switch to include (`True`) or exclude (`False`) a
comprehensive list of system packages
Returns
-------
page : :class:`~MarkupPy.markup.page`
the HTML page to be inserted into the #main <div>.
"""
page = markup.page()
page.div(class_='row')
page.div(class_='col-md-12')
page.h2('On the command-line')
page.p('This page was generated with the following command-line call:')
page.add(get_command_line())
# render config file(s)
page.h2('Configuration files')
page.p('The following INI-format configuration file(s) were passed '
'on the comand-line and are reproduced in full:')
if isinstance(config, str):
with open(config, 'r') as fobj:
contents = fobj.read()
page.add(htmlio.render_code(contents, 'ini'))
elif isinstance(config, list):
page.div(class_='panel-group', id="accordion")
for i, cpfile in enumerate(config):
page.div(class_='panel panel-default')
page.a(href='#file%d' % i, **{'data-toggle': 'collapse',
'data-parent': '#accordion'})
page.div(class_='panel-heading')
page.h4(os.path.basename(cpfile), class_='panel-title')
page.div.close()
page.a.close()
page.div(id_='file%d' % i, class_='panel-collapse collapse')
page.div(class_='panel-body')
with open(cpfile, 'r') as fobj:
contents = fobj.read()
page.add(render_code(contents, 'ini'))
page.div.close()
page.div.close()
page.div.close()
page.div.close()
# render package list
if packagelist:
page.add(package_table())
page.div.close()
page.div.close()
return page()


def write_param(param, value):
"""Format a parameter value with HTML
"""
Expand Down Expand Up @@ -279,10 +339,15 @@ def get_command_line(language='bash'):
Parameters
----------
language : `str`, optional
language the code is written in, default: `'bash'`
type of environment the code is run in, default: `'bash'`
"""
commandline = ' '.join(sys.argv)
return render_code(commandline, language)
if sys.argv[0].endswith('__main__.py'):
package = sys.argv[0].rsplit(os.path.sep, 2)[1]
commandline = '$ python -m {0} {1}'.format(
package, ' '.join(sys.argv[1:]))
else:
commandline = '$ ' + ' '.join(sys.argv)
return render_code(commandline.replace(' --html-only', ''), language)


def html_link(href, txt, target="_blank", **params):
Expand Down
19 changes: 19 additions & 0 deletions gwdetchar/io/tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,25 @@ def test_write_param():
'<p>\n<strong>test: </strong>\ntest\n</p>')


def test_get_command_line():
testargs = ['gwdetchar-conlog', '-i', 'X1']
with mock.patch.object(sys, 'argv', testargs):
cmdline = html.get_command_line()
assert parse_html(cmdline) == parse_html(
'<div class="highlight" style="background: #f8f8f8">'
'<pre style="line-height: 125%"><span></span>'
'$ gwdetchar-conlog -i X1\n</pre></div>\n')

def test_get_command_line_module():
testargs = ['gwdetchar/omega/__main__.py', '--html-only']
with mock.patch.object(sys, 'argv', testargs):
cmdline = html.get_command_line()
assert parse_html(cmdline) == parse_html(
'<div class="highlight" style="background: #f8f8f8">'
'<pre style="line-height: 125%"><span></span>'
'$ python -m omega\n</pre></div>\n')


@pytest.mark.parametrize('args, kwargs, result', [
(('test.html', 'Test link'), {},
'<a href="test.html" target="_blank">Test link</a>'),
Expand Down

0 comments on commit d564b79

Please sign in to comment.