Commit d776ac3a authored by Michele Carignani's avatar Michele Carignani
Browse files

align docx formatting to edr

parent d200cfb2
Loading
Loading
Loading
Loading
−3.77 KiB (115 KiB)

File changed.

No diff preview for this file type.

+12 −3
Original line number Diff line number Diff line
#!/env/python

import docx
import config
import testspec

@@ -28,6 +29,14 @@ class TP():
        self.tp_id = None

        for field in self.tp_fields:
            if field.value and not field.value[0].isupper():
                raise Exception("Found field starting with lowercase character: " + field.value)
            if field.key.startswith("Reference"):
                if "ETSI GS NFV-SOL" not in field.value:
                    raise Exception(
                        "Reference possibly incorrect ('ETSI GS NFV-SOL' not found): " + 
                        field.value
                    )
            if field.key == "Test ID":
                self.tp_id = field.value
        
+71 −15
Original line number Diff line number Diff line
#!/env/python

import os
from docx import Document
import docx
from docx.shared import Pt
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT, WD_BREAK_TYPE

@@ -22,13 +22,58 @@ TC_TITLE = 'Test Case'

TABLE_STYLE = 'TestTable'

def add_hyperlink(paragraph, url, text, color, underline):
    """
    A function that places a hyperlink within a paragraph object.

    :param paragraph: The paragraph we are adding the hyperlink to.
    :param url: A string containing the required url
    :param text: The text displayed for the url
    :return: The hyperlink object
    """

    # This gets access to the document.xml.rels file and gets a new relation id value
    part = paragraph.part
    r_id = part.relate_to(url, docx.opc.constants.RELATIONSHIP_TYPE.HYPERLINK, is_external=True)

    # Create the w:hyperlink tag and add needed values
    hyperlink = docx.oxml.shared.OxmlElement('w:hyperlink')
    hyperlink.set(docx.oxml.shared.qn('r:id'), r_id, )

    # Create a w:r element
    new_run = docx.oxml.shared.OxmlElement('w:r')

    # Create a new w:rPr element
    rPr = docx.oxml.shared.OxmlElement('w:rPr')

    # Add color if it is given
    if not color is None:
        c = docx.oxml.shared.OxmlElement('w:color')
        c.set(docx.oxml.shared.qn('w:val'), color)
        rPr.append(c)

    # Remove underlining if it is requested
    if not underline:
        u = docx.oxml.shared.OxmlElement('w:u')
        u.set(docx.oxml.shared.qn('w:val'), 'none')
        rPr.append(u)

    # Join all the xml elements together add add the required text to the w:r element
    new_run.append(rPr)
    new_run.text = text
    hyperlink.append(new_run)

    paragraph._p.append(hyperlink)

    return hyperlink

class TestSpec():

    def __init__(self, path=None):
        if path:
            print("Opening doc: "+path)
        print("Current dir: "+os.getcwd())
        self.doc = Document(path)
        self.doc = docx.Document(path)

    @staticmethod
    def cell_text_bold(cell):
@@ -50,21 +95,23 @@ class TestSpec():

    @staticmethod
    def mk_heading(txt, sec, subsec1="", subsec2="", subsec3=""):
        return TestSpec.mk_ref(sec, subsec1, subsec2, subsec3) + " " + txt
        return TestSpec.mk_ref(sec, subsec1, subsec2, subsec3) + "\t" + txt

    @staticmethod
    def mk_tp_hdr(table):
    def mk_tp_hdr(table, doc):
        hdr_cells = table.rows[0].cells
        hdr_cells[0].merge(hdr_cells[1])
        hdr_cells[0].text = TP_TITLE
        hdr_cells[0].paragraphs[0].style = doc.styles["TAH"]
        TestSpec.cell_text_bold(hdr_cells[0])
        TestSpec.cell_text_centered(hdr_cells[0])

    @staticmethod
    def mk_tc_hdr(table):
    def mk_tc_hdr(table, doc):
        hdr_cells = table.add_row().cells
        hdr_cells[0].merge(hdr_cells[1])
        hdr_cells[0].text = TC_TITLE
        hdr_cells[0].paragraphs[0].style = doc.styles["TAH"]
        TestSpec.cell_text_bold(hdr_cells[0])
        TestSpec.cell_text_centered(hdr_cells[0])

@@ -73,24 +120,34 @@ class TestSpec():
        Adds a note to the document containing the URL to the location of the
        file, according to the configured URL prefix configured.
        '''
        self.doc.add_paragraph("Note: Robot code can be found at " + commit + robot_file)
        empty_par = self.doc.add_paragraph("")
        note_par = self.doc.add_paragraph("NOTE: Robot code can be found at ")

        hyperlink = add_hyperlink(note_par, commit + robot_file, commit + robot_file, None, True)

        note_par.style = self.doc.styles["NO"]

    def add_tp(self, fields, testbehaviour):
        table = self.doc.add_table(cols=2, rows=1)
        table.style = TABLE_STYLE
        table.style = self.doc.styles[TABLE_STYLE]
        table.autofit = True
        TestSpec.mk_tp_hdr(table)
        TestSpec.mk_tp_hdr(table, self.doc)
        for tpf in fields:
            row_cells = table.add_row().cells
            new_row = table.add_row()
            row_cells = new_row.cells
            row_cells[0].width = 2
            row_cells[0].text = tpf.key
            TestSpec.cell_text_bold(row_cells[0])
            row_cells[1].text = tpf.value
        TestSpec.mk_tc_hdr(table)
            row_cells[0].paragraphs[0].style = self.doc.styles["TAL"]
            row_cells[0].paragraphs[0].text = tpf.key.strip()
            row_cells[0].paragraphs[0].runs[0].font.bold = True
            row_cells[1].paragraphs[0].style = self.doc.styles["TAL"]
            row_cells[1].paragraphs[0].text = tpf.value.strip()
        TestSpec.mk_tc_hdr(table, self.doc)
        row_cells = table.add_row().cells
        row_cells[0].merge(row_cells[1])
        row_cells[0].text = testbehaviour
        TestSpec.cell_text_mono(row_cells[0])
        for par in row_cells[0].paragraphs:
            par.style = self.doc.styles["PL"]
        # TestSpec.cell_text_mono(row_cells[0])

    def add_heading(self, txt, lvl, sec="1", subsec1="", subsec2="", subsec3=""):
        self.doc.add_heading(TestSpec.mk_heading(txt, sec, subsec1, subsec2, subsec3), lvl)
@@ -99,7 +156,6 @@ class TestSpec():
        self.doc.save(path)

    def add_main_heading(self, txt):
        self.doc.add_page_break()
        self.doc.add_heading(txt, ANNEX_TITLE_LEVEL)

    def add_sub_heading(self, txt, sec, subsec1, subsec2, subsec3):