-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement report-from-jinja2-file functionality for the 'report' outp…
…ut module * Try to render a report from default settings (defaults.output.report) or a Jinja2 file in the 'reports' subdirectory of the search path * Rename 'format' output module to 'report' retaining a stub in case anyone every used 'format' output * Add sample 'node addressing' report * Document the 'report' output module Also: * Refactor 'templates.template' into 'templates.render_template' * Remove 'get_moddir' from common imports and fix any code using it to point to 'utils.files' * Cleanup all calls to 'templates.template' and add error reporting * Replace incidental calls to 'common.x' with calls to actual utils function
- Loading branch information
Showing
15 changed files
with
281 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Creating Reports with Custom Output Formats | ||
|
||
The *report* output module uses its parameter as the name of a Jinja2 formatting template that is used to create a custom report. For example, `netlab create -o report:addressing` creates an IP addressing report. | ||
|
||
The *report* output module tries to use the **defaults.outputs.report.*rname*** topology setting (*rname* is the report name). If that fails, it tries to read the Jinja2 template from **_rname_.j2** file in **reports** subdirectory of current directory, user _netlab_ directory (`~/.netlab`), system _netlab_ directory (`/etc/netlab`) and _netlab_ package directory. | ||
|
||
_netlab_ ships with the following built-in reports: | ||
|
||
* **addressing** -- Node/interface addressing report |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,9 @@ | ||
# | ||
# Create YAML or JSON output | ||
# This is just a dummy output module that was replaced by the new 'report' | ||
# output module. We kept the old module (pointing to the new one) in case | ||
# someone already used it in their workflow. | ||
# | ||
import typing | ||
from .report import REPORT | ||
|
||
import yaml | ||
import os | ||
from box import Box,BoxList | ||
from jinja2 import Environment, BaseLoader, FileSystemLoader, StrictUndefined, make_logging_undefined | ||
|
||
from .. import common | ||
from .. import data | ||
from ..augment import topology | ||
|
||
from . import _TopologyOutput | ||
|
||
class FORMAT(_TopologyOutput): | ||
|
||
def write(self, topo: Box) -> None: | ||
outfile = self.settings.filename or '-' | ||
modname = type(self).__name__ | ||
|
||
if hasattr(self,'filenames'): | ||
outfile = self.filenames[0] | ||
if len(self.filenames) > 1: | ||
common.error('Extra output filename(s) ignored: %s' % str(self.filenames[1:]),common.IncorrectValue,modname) | ||
|
||
cleantopo: typing.Any = topology.cleanup_topology(topo) | ||
output = common.open_output_file(outfile) | ||
|
||
for fmt in self.format: | ||
if not fmt in self.settings: | ||
common.error(f'Unknown template format {fmt}',common.IncorrectValue,modname) | ||
print(topo.defaults.outputs.format) | ||
continue | ||
|
||
template = Environment(loader=BaseLoader(), \ | ||
trim_blocks=True,lstrip_blocks=True, \ | ||
undefined=make_logging_undefined(base=StrictUndefined)).from_string(self.settings[fmt]) | ||
output.write(template.render(**topo)) | ||
output.write("\n") | ||
class FORMAT(REPORT): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# | ||
# Create YAML or JSON output | ||
# | ||
import typing | ||
import pathlib | ||
|
||
from box import Box | ||
import jinja2 | ||
|
||
from .. import common | ||
from .. import data | ||
from ..augment import topology | ||
from ..utils import files as _files | ||
from ..utils import log,templates,strings | ||
|
||
from . import _TopologyOutput | ||
|
||
SEARCH_PATH: list = [] | ||
|
||
def get_format_text(settings: Box, fname: str) -> typing.Optional[str]: | ||
global SEARCH_PATH | ||
if fname in settings: | ||
return settings[fname] | ||
|
||
if not SEARCH_PATH: | ||
SEARCH_PATH = [ pc + "/reports" for pc in _files.get_search_path() ] | ||
|
||
tfile = _files.find_file(fname+".j2",SEARCH_PATH) | ||
if tfile is None: | ||
return None | ||
|
||
return pathlib.Path(tfile).read_text() | ||
|
||
class REPORT(_TopologyOutput): | ||
|
||
def write(self, topo: Box) -> None: | ||
outfile = self.settings.filename or '-' | ||
modname = type(self).__name__ | ||
|
||
if hasattr(self,'filenames'): | ||
outfile = self.filenames[0] | ||
if len(self.filenames) > 1: | ||
common.error('Extra output filename(s) ignored: %s' % str(self.filenames[1:]),common.IncorrectValue,modname) | ||
|
||
output = common.open_output_file(outfile) | ||
|
||
extra_path = _files.get_search_path("reports") | ||
for fmt in self.format: | ||
if fmt in self.settings: | ||
try: | ||
output.write( | ||
templates.render_template( | ||
data=topo.to_dict(), | ||
j2_text = self.settings[fmt], | ||
path="reports", | ||
extra_path=extra_path)+"\n") | ||
output.write("\n") | ||
except Exception as ex: | ||
log.error( | ||
text=f"Error rendering topology format {fmt}\n{strings.extra_data_printout(str(ex))}", | ||
category=log.IncorrectValue) | ||
continue | ||
|
||
try: | ||
output.write( | ||
templates.render_template( | ||
data=topo.to_dict(), | ||
j2_file=fmt+".j2", | ||
path="reports", | ||
extra_path=extra_path)+"\n") | ||
except jinja2.exceptions.TemplateNotFound: | ||
log.error( | ||
text=f'Cannot find "{fmt}" in any of the report directories', | ||
category=common.IncorrectValue, | ||
module='report', | ||
hint='source') | ||
except Exception as ex: | ||
log.error( | ||
text=f"Error rendering {fmt}\n{strings.extra_data_printout(str(ex))}", | ||
category=log.IncorrectValue) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.