[ovs-dev] [PATCH 7/8] xml2nroff: New program to generate a manpage from XML input.

Ben Pfaff blp at nicira.com
Thu Feb 19 19:08:41 UTC 2015


On Thu, Feb 19, 2015 at 12:24:42PM -0500, Russell Bryant wrote:
> On 02/19/2015 03:12 AM, Ben Pfaff wrote:
> > I really can't stand nroff syntax.  This makes it possible to install
> > nroff but write in a more sensible XML syntax.
> > 
> > The following commit adds the first user.
> > 
> > Signed-off-by: Ben Pfaff <blp at nicira.com>
> > ---
> >  Makefile.am         |   1 +
> >  build-aux/xml2nroff | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++
> >  2 files changed, 111 insertions(+)
> >  create mode 100755 build-aux/xml2nroff
> > 
> > diff --git a/Makefile.am b/Makefile.am
> > index 28496b3..0480d20 100644
> > --- a/Makefile.am
> > +++ b/Makefile.am
> > @@ -104,6 +104,7 @@ EXTRA_DIST = \
> >  	build-aux/dist-docs \
> >  	build-aux/sodepends.pl \
> >  	build-aux/soexpand.pl \
> > +	build-aux/xml2nroff \
> >  	$(MAN_FRAGMENTS) \
> >  	$(MAN_ROOTS) \
> >  	Vagrantfile
> > diff --git a/build-aux/xml2nroff b/build-aux/xml2nroff
> > new file mode 100755
> > index 0000000..817abc6
> > --- /dev/null
> > +++ b/build-aux/xml2nroff
> > @@ -0,0 +1,110 @@
> > +#! /usr/bin/python
> 
> A license header would be good here.

Oops, thanks for pointing that out.  I started from ovsdb-doc, so I
grabbed copyright dates from ovsdb-doc's history (ovsdb-doc lacked a
license header; I'll fix that separately).

> > +if __name__ == "__main__":
> > +    try:
> 
> Instead of the double nesting here, I would move the outer one to just
> around the part that could raise error.Error, which appears to be only
> the call to manpage_to_nroff().

Done, thanks.

> > +        try:
> > +            options, args = getopt.gnu_getopt(sys.argv[1:], 'hV',
> > +                                              ['version=', 'help'])
> > +        except getopt.GetoptError, geo:
> > +            sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
> > +            sys.exit(1)
> > +
> > +        er_diagram = None
> > +        title = None
> > +        version = None
> > +        for key, value in options:
> > +            if key == '--version':
> > +                version = value
> > +            elif key in ['-h', '--help']:
> > +                usage()
> > +            else:
> > +                sys.exit(0)
> > +
> > +        if len(args) != 1:
> > +            sys.stderr.write("%s: exactly 1 non-option arguments required "
> > +                             "(use --help for help)\n" % argv0)
> > +            sys.exit(1)
> > +
> > +        s = manpage_to_nroff(args[0], version)
> > +        for line in s.split("\n"):
> 
> can also use:
> 
>     for line in s.splitlines():
>         ...

Thanks, changed.

> > +            line = line.strip()
> > +            if len(line):
> 
> also equivalent:
> 
>     if line:

Thanks, changed.

> > +                print line
> > +
> > +    except error.Error, e:
> > +        sys.stderr.write("%s: %s\n" % (argv0, e.msg))
> > +        sys.exit(1)
> > +
> > +# Local variables:
> > +# mode: python
> > +# End:

I'll repost the patch separately; here's an incremental:

diff --git a/build-aux/xml2nroff b/build-aux/xml2nroff
index 817abc6..8dc9d4f 100755
--- a/build-aux/xml2nroff
+++ b/build-aux/xml2nroff
@@ -1,5 +1,19 @@
 #! /usr/bin/python
 
+# Copyright (c) 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at:
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
 from datetime import date
 import getopt
 import os
@@ -58,11 +72,10 @@ def manpage_to_nroff(xml_file, version=None):
 
 def usage():
     print """\
-%(argv0)s: ovsdb schema documentation generator
-Prints documentation for an OVSDB schema as an nroff-formatted manpage.
-usage: %(argv0)s [OPTIONS] SCHEMA XML
-where SCHEMA is an OVSDB schema in JSON format
-  and XML is OVSDB documentation in XML format.
+%(argv0)s: converts XML in a somewhat HTML-like format to nroff
+usage: %(argv0)s [OPTIONS] XML
+where XML is documentation in a somewhat HTML-like XML format.
+The manpage, in nroff "man" format, is output on stdout.
 
 The following options are also available:
   --version=VERSION           use VERSION to display on document footer
@@ -72,38 +85,38 @@ The following options are also available:
 
 if __name__ == "__main__":
     try:
-        try:
-            options, args = getopt.gnu_getopt(sys.argv[1:], 'hV',
-                                              ['version=', 'help'])
-        except getopt.GetoptError, geo:
-            sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
-            sys.exit(1)
-
-        er_diagram = None
-        title = None
-        version = None
-        for key, value in options:
-            if key == '--version':
-                version = value
-            elif key in ['-h', '--help']:
-                usage()
-            else:
-                sys.exit(0)
-
-        if len(args) != 1:
-            sys.stderr.write("%s: exactly 1 non-option arguments required "
-                             "(use --help for help)\n" % argv0)
-            sys.exit(1)
+        options, args = getopt.gnu_getopt(sys.argv[1:], 'hV',
+                                          ['version=', 'help'])
+    except getopt.GetoptError, geo:
+        sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
+        sys.exit(1)
 
-        s = manpage_to_nroff(args[0], version)
-        for line in s.split("\n"):
-            line = line.strip()
-            if len(line):
-                print line
+    er_diagram = None
+    title = None
+    version = None
+    for key, value in options:
+        if key == '--version':
+            version = value
+        elif key in ['-h', '--help']:
+            usage()
+        else:
+            sys.exit(0)
+
+    if len(args) != 1:
+        sys.stderr.write("%s: exactly 1 non-option arguments required "
+                         "(use --help for help)\n" % argv0)
+        sys.exit(1)
 
+    try:
+        s = manpage_to_nroff(args[0], version)
     except error.Error, e:
         sys.stderr.write("%s: %s\n" % (argv0, e.msg))
         sys.exit(1)
+    for line in s.splitlines():
+        line = line.strip()
+        if line:
+            print line
+
 
 # Local variables:
 # mode: python



More information about the dev mailing list