Source code for dorie.parscraper.writers.rst

from __future__ import unicode_literals

import datetime
import os
import re

try:
    str = unicode
except NameError:
    pass

from dorie.utilities.check_path import check_path
from dorie.parscraper.parameter import Parameter

[docs]def write(parameters,out,path_base,*args,**kwargs): """ Writes the contents of the ``parameters`` input dict to a .rst file. The output is structured as **category** ============ =============== =============== =============== ================= Parameter Definition Possible values Default Queried at ============ =============== =============== =============== ================= p.key p.definition p.values p.suggestion all sources of p ============ =============== =============== =============== ================= :param dict parameters: dict with categories as keys and iterables of :class:`dorie.parscraper.parameter.Parameter` \ instances as values. :param str out: Path to the output file. Must be writable. :param str path_base: Base path to the source files. Needed for creation of working links \ to the files. """ # DEFINE OUTPUT PROPERTIES headings = ["Parameter","Definition","Possible values","Default","Queried at"] widths = ["15","45","20","10","10"] # relative column widths table_content = lambda p: (p.key,p.definition,p.values,p.suggestion,_sources(p,path_base)) check_path(out) with open(out,"wb") as output: rst_document = _format_rst(parameters, headings, widths, table_content) output.write(rst_document.encode('utf-8'))
def _format_rst(parameters,headers,widths,content_function): rst = u"" for category in parameters: rst += _format_heading(category) table_content = [content_function(p) for p in parameters[category]] rst += _format_table(table_content,headers,widths) return rst def _format_table(content,headers,widths): tab = " " delimiter = "," table = u"" table += ".. csv-table::\n" table += tab + ":header: {}\n".format(", ".join(headers)) table += tab + ":delim: {}\n".format(delimiter) table += tab + ":widths: {}\n".format(", ".join(widths)) table += tab + "\n" for row in content: row = ['"{}"'.format(str(s).strip()) for s in row] row_csv = "{} ".format(delimiter).join(row) row_csv = re.sub(r"\n","\n" + tab,row_csv) table += tab + row_csv table += tab + "\n" table += "\n\n" return table def _format_title(text): sep = "="*len(text) return sep + "\n" + text + "\n" + sep + "\n\n" def _format_heading(text): sep = "+"*len(text) return text + "\n" + sep + "\n\n" def _sources(p,path_base): """ Assembles the cell text for the sources of a parameter. Since the Parameter._sources attribute is a list of tuples, we need to parse this into a printable RST format. Prints a link to each source file, the corresponding line number, and concatenates them with newlines. Returns a unicode string containing the RST code. :param p: Parameter object. """ out = u"" for source_file, line_num, var_type in p._sources: full_path = os.path.join(path_base,source_file) # truncate long file names if len(source_file) > 20: link_text = "..." + source_file[-20:] else: link_text = source_file out += "`{0} <file://{2}>`_:{1}\n\n"\ .format(link_text,line_num,full_path,source_file) return out