[ovs-dev] [PATCH] nroff: Fix style of names.

Ben Pfaff blp at nicira.com
Mon Jul 6 21:42:02 UTC 2015


The recommended Google Python style is multi_word_names, not
multiWordNames.

There are lots of other places where the style could be improved.
I started here because I was working in this code anyway and because
this code is only used at build time and not installed, so that it
can't break any third-party code.

Signed-off-by: Ben Pfaff <blp at nicira.com>
---
 build-aux/xml2nroff   |   5 +-
 ovsdb/ovsdb-doc       |  24 +++++-----
 python/build/nroff.py | 129 +++++++++++++++++++++++++++++++++++++++-----------
 3 files changed, 116 insertions(+), 42 deletions(-)

diff --git a/build-aux/xml2nroff b/build-aux/xml2nroff
index 1df195e..1f8519a 100755
--- a/build-aux/xml2nroff
+++ b/build-aux/xml2nroff
@@ -73,9 +73,10 @@ def manpage_to_nroff(xml_file, subst, version=None):
 .  I "\\$1"
 .  RE
 ..
-''' % (textToNroff(program), textToNroff(section), textToNroff(title), textToNroff(version))
+''' % (text_to_nroff(program), text_to_nroff(section),
+       text_to_nroff(title), text_to_nroff(version))
 
-    s += blockXmlToNroff(doc.childNodes) + "\n"
+    s += block_xml_to_nroff(doc.childNodes) + "\n"
 
     return s
 
diff --git a/ovsdb/ovsdb-doc b/ovsdb/ovsdb-doc
index 51b022b..5cf26ee 100755
--- a/ovsdb/ovsdb-doc
+++ b/ovsdb/ovsdb-doc
@@ -29,9 +29,9 @@ from build.nroff import *
 argv0 = sys.argv[0]
 
 def typeAndConstraintsToNroff(column):
-    type = column.type.toEnglish(escapeNroffLiteral)
-    constraints = column.type.constraintsToEnglish(escapeNroffLiteral,
-                                                   textToNroff)
+    type = column.type.toEnglish(escape_nroff_literal)
+    constraints = column.type.constraintsToEnglish(escape_nroff_literal,
+                                                   text_to_nroff)
     if constraints:
         type += ", " + constraints
     if column.unique:
@@ -53,7 +53,7 @@ def columnGroupToNroff(table, groupXml, documented_columns):
             introNodes += [node]
 
     summary = []
-    intro = blockXmlToNroff(introNodes)
+    intro = block_xml_to_nroff(introNodes)
     body = ''
     for node in columnNodes:
         if node.tagName == 'column':
@@ -76,7 +76,7 @@ def columnGroupToNroff(table, groupXml, documented_columns):
 
                 if column.type.value:
                     typeNroff = "optional %s" % column.type.value.toEnglish(
-                        escapeNroffLiteral)
+                        escape_nroff_literal)
                     if (column.type.value.type == ovs.db.types.StringType and
                         type_.type == ovs.db.types.BooleanType):
                         # This is a little more explicit and helpful than
@@ -90,8 +90,8 @@ def columnGroupToNroff(table, groupXml, documented_columns):
                             else:
                                 typeNroff += ", containing a %s" % type_english
                         constraints = (
-                            type_.constraintsToEnglish(escapeNroffLiteral,
-                                                       textToNroff))
+                            type_.constraintsToEnglish(escape_nroff_literal,
+                                                       text_to_nroff))
                         if constraints:
                             typeNroff += ", %s" % constraints
                 else:
@@ -102,14 +102,14 @@ def columnGroupToNroff(table, groupXml, documented_columns):
             if not column.mutable:
                 typeNroff = "immutable %s" % typeNroff
             body += '.IP "\\fB%s\\fR: %s"\n' % (nameNroff, typeNroff)
-            body += blockXmlToNroff(node.childNodes, '.IP') + "\n"
+            body += block_xml_to_nroff(node.childNodes, '.IP') + "\n"
             summary += [('column', nameNroff, typeNroff)]
         elif node.tagName == 'group':
             title = node.attributes["title"].nodeValue
             subSummary, subIntro, subBody = columnGroupToNroff(
                 table, node, documented_columns)
             summary += [('group', title, subSummary)]
-            body += '.ST "%s:"\n' % textToNroff(title)
+            body += '.ST "%s:"\n' % text_to_nroff(title)
             body += subIntro + subBody
         else:
             raise error.Error("unknown element %s in <table>" % node.tagName)
@@ -186,7 +186,7 @@ def docsToNroff(schemaFile, xmlFile, erFile, version=None):
 .SH NAME
 %s \- %s database schema
 .PP
-''' % (manpage, schema.version, version, textToNroff(manpage), schema.name)
+''' % (manpage, schema.version, version, text_to_nroff(manpage), schema.name)
 
     tables = ""
     introNodes = []
@@ -212,7 +212,7 @@ def docsToNroff(schemaFile, xmlFile, erFile, version=None):
     for table in undocumented_tables:
         raise error.Error("undocumented table %s" % table)
 
-    s += blockXmlToNroff(introNodes) + "\n"
+    s += block_xml_to_nroff(introNodes) + "\n"
 
     s += r"""
 .SH "TABLE SUMMARY"
@@ -228,7 +228,7 @@ Purpose
 .TQ 1in
 \fB%s\fR
 %s
-""" % (name, textToNroff(title))
+""" % (name, text_to_nroff(title))
 
     if erFile:
         s += """
diff --git a/python/build/nroff.py b/python/build/nroff.py
index 078ec4b..03d2b67 100644
--- a/python/build/nroff.py
+++ b/python/build/nroff.py
@@ -16,7 +16,7 @@ import re
 
 from ovs.db import error
 
-def textToNroff(s, font=r'\fR'):
+def text_to_nroff(s, font=r'\fR'):
     def escape(match):
         c = match.group(0)
 
@@ -55,20 +55,20 @@ def textToNroff(s, font=r'\fR'):
     s = re.sub('(-[0-9]|--|[-"\'\\\\.])', escape, s)
     return s
 
-def escapeNroffLiteral(s, font=r'\fB'):
-    return font + r'%s\fR' % textToNroff(s, font)
+def escape_nroff_literal(s, font=r'\fB'):
+    return font + r'%s\fR' % text_to_nroff(s, font)
 
-def inlineXmlToNroff(node, font, to_upper=False):
+def inline_xml_to_nroff(node, font, to_upper=False):
     if node.nodeType == node.TEXT_NODE:
         if to_upper:
-            return textToNroff(node.data.upper(), font)
+            return text_to_nroff(node.data.upper(), font)
         else:
-            return textToNroff(node.data, font)
+            return text_to_nroff(node.data, font)
     elif node.nodeType == node.ELEMENT_NODE:
         if node.tagName in ['code', 'em', 'option', 'env']:
             s = r'\fB'
             for child in node.childNodes:
-                s += inlineXmlToNroff(child, r'\fB')
+                s += inline_xml_to_nroff(child, r'\fB')
             return s + font
         elif node.tagName == 'ref':
             s = r'\fB'
@@ -88,7 +88,7 @@ def inlineXmlToNroff(node, font, to_upper=False):
         elif node.tagName == 'var' or node.tagName == 'dfn':
             s = r'\fI'
             for child in node.childNodes:
-                s += inlineXmlToNroff(child, r'\fI')
+                s += inline_xml_to_nroff(child, r'\fI')
             return s + font
         else:
             raise error.Error("element <%s> unknown or invalid here" % node.tagName)
@@ -101,15 +101,86 @@ def pre_to_nroff(nodes, para, font):
         if node.nodeType != node.TEXT_NODE:
             fatal("<pre> element may only have text children")
         for line in node.data.split('\n'):
-            s += escapeNroffLiteral(line, font) + '\n.br\n'
+            s += escape_nroff_literal(line, font) + '\n.br\n'
     s += '.fi\n'
     return s
 
-def blockXmlToNroff(nodes, para='.PP'):
+def diagram_header_to_nroff(header_node):
+    header_fields = []
+    i = 0
+    for node in header_node.childNodes:
+        if node.nodeType == node.ELEMENT_NODE and node.tagName == 'bits':
+            name = node.attributes['name'].nodeValue
+            width = node.attributes['width'].nodeValue
+            above = node.getAttribute('above')
+            below = node.getAttribute('below')
+            fill = node.getAttribute('fill')
+            header_fields += [{"name": name,
+                              "tag": "B%d" % i,
+                              "width": width,
+                              "above": above,
+                              "below": below,
+                              "fill": fill}]
+            i += 1
+        elif node.nodeType == node.COMMENT_NODE:
+            pass
+        elif node.nodeType == node.TEXT_NODE and node.data.isspace():
+            pass
+        else:
+            fatal("unknown node %s in diagram <header> element" % node)
+
+    s = ""
+    for f in header_fields:
+        s += "  %s: box \"%s\" width %s" % (f['tag'], f['name'], f['width'])
+        if f['fill'] == 'yes':
+            s += " fill"
+        s += '\n'
+    for f in header_fields:
+        s += "  \"%s\" at %s.n above\n" % (f['above'], f['tag'])
+        s += "  \"%s\" at %s.s below\n" % (f['below'], f['tag'])
+    name = header_node.getAttribute('name')
+    if name == "":
+        visible = " invis"
+    else:
+        visible = ""
+    s += "line <->%s \"%s\" above " % (visible, name)
+    s += "from %s.nw + (0,textht) " % header_fields[0]['tag']
+    s += "to %s.ne + (0,textht)\n" % header_fields[-1]['tag']
+    return s
+
+def diagram_to_nroff(nodes, para):
+    s = para + """
+.PS
+boxht = .2
+textht = 1/6
+fillval = .2
+"""
+    move = False
+    for node in nodes:
+        if node.nodeType == node.ELEMENT_NODE and node.tagName == 'header':
+            if move:
+                s += "move .1\n"
+            s += "[\n" + diagram_header_to_nroff(node) + "]\n"
+            move = True
+        elif node.nodeType == node.ELEMENT_NODE and node.tagName == 'nospace':
+            move = False
+        elif node.nodeType == node.ELEMENT_NODE and node.tagName == 'dots':
+            s += "move .1\n"
+            s += '". . ." ljust\n'
+        elif node.nodeType == node.COMMENT_NODE:
+            pass
+        elif node.nodeType == node.TEXT_NODE and node.data.isspace():
+            pass
+        else:
+            fatal("unknown node %s in diagram <header> element" % node)
+    s += '.PE\n'
+    return s
+
+def block_xml_to_nroff(nodes, para='.PP'):
     s = ''
     for node in nodes:
         if node.nodeType == node.TEXT_NODE:
-            s += textToNroff(node.data)
+            s += text_to_nroff(node.data)
             s = s.lstrip()
         elif node.nodeType == node.ELEMENT_NODE:
             if node.tagName in ['ul', 'ol']:
@@ -117,17 +188,17 @@ def blockXmlToNroff(nodes, para='.PP'):
                     s += "\n"
                 s += ".RS\n"
                 i = 0
-                for liNode in node.childNodes:
-                    if (liNode.nodeType == node.ELEMENT_NODE
-                        and liNode.tagName == 'li'):
+                for li_node in node.childNodes:
+                    if (li_node.nodeType == node.ELEMENT_NODE
+                        and li_node.tagName == 'li'):
                         i += 1
                         if node.tagName == 'ul':
                             s += ".IP \\(bu\n"
                         else:
                             s += ".IP %d. .25in\n" % i
-                        s += blockXmlToNroff(liNode.childNodes, ".IP")
-                    elif (liNode.nodeType != node.TEXT_NODE
-                          or not liNode.data.isspace()):
+                        s += block_xml_to_nroff(li_node.childNodes, ".IP")
+                    elif (li_node.nodeType != node.TEXT_NODE
+                          or not li_node.data.isspace()):
                         raise error.Error("<%s> element may only have <li> children" % node.tagName)
                 s += ".RE\n"
             elif node.tagName == 'dl':
@@ -135,30 +206,30 @@ def blockXmlToNroff(nodes, para='.PP'):
                     s += "\n"
                 s += ".RS\n"
                 prev = "dd"
-                for liNode in node.childNodes:
-                    if (liNode.nodeType == node.ELEMENT_NODE
-                        and liNode.tagName == 'dt'):
+                for li_node in node.childNodes:
+                    if (li_node.nodeType == node.ELEMENT_NODE
+                        and li_node.tagName == 'dt'):
                         if prev == 'dd':
                             s += '.TP\n'
                         else:
                             s += '.TQ .5in\n'
                         prev = 'dt'
-                    elif (liNode.nodeType == node.ELEMENT_NODE
-                          and liNode.tagName == 'dd'):
+                    elif (li_node.nodeType == node.ELEMENT_NODE
+                          and li_node.tagName == 'dd'):
                         if prev == 'dd':
                             s += '.IP\n'
                         prev = 'dd'
-                    elif (liNode.nodeType != node.TEXT_NODE
-                          or not liNode.data.isspace()):
+                    elif (li_node.nodeType != node.TEXT_NODE
+                          or not li_node.data.isspace()):
                         raise error.Error("<dl> element may only have <dt> and <dd> children")
-                    s += blockXmlToNroff(liNode.childNodes, ".IP")
+                    s += block_xml_to_nroff(li_node.childNodes, ".IP")
                 s += ".RE\n"
             elif node.tagName == 'p':
                 if s != "":
                     if not s.endswith("\n"):
                         s += "\n"
                     s += para + "\n"
-                s += blockXmlToNroff(node.childNodes, para)
+                s += block_xml_to_nroff(node.childNodes, para)
             elif node.tagName in ('h1', 'h2', 'h3'):
                 if s != "":
                     if not s.endswith("\n"):
@@ -166,7 +237,7 @@ def blockXmlToNroff(nodes, para='.PP'):
                 nroffTag = {'h1': 'SH', 'h2': 'SS', 'h3': 'ST'}[node.tagName]
                 s += '.%s "' % nroffTag
                 for child_node in node.childNodes:
-                    s += inlineXmlToNroff(child_node, r'\fR',
+                    s += inline_xml_to_nroff(child_node, r'\fR',
                                           to_upper=(nroffTag == 'SH'))
                 s += '"\n'
             elif node.tagName == 'pre':
@@ -176,8 +247,10 @@ def blockXmlToNroff(nodes, para='.PP'):
                 else:
                     font = r'\fB'
                 s += pre_to_nroff(node.childNodes, para, font)
+            elif node.tagName == 'diagram':
+                s += diagram_to_nroff(node.childNodes, para)
             else:
-                s += inlineXmlToNroff(node, r'\fR')
+                s += inline_xml_to_nroff(node, r'\fR')
         else:
             raise error.Error("unknown node %s in block xml" % node)
     if s != "" and not s.endswith('\n'):
-- 
2.1.3




More information about the dev mailing list