windows: Define RTLD_NOW, even if it is not used
[strongswan.git] / conf / format-options.py
index 04afed6..d046e24 100755 (executable)
@@ -54,6 +54,7 @@ import sys
 import re
 from textwrap import TextWrapper
 from optparse import OptionParser
+from operator import attrgetter
 
 class ConfigOption:
        """Representing a configuration option or described section in strongswan.conf"""
@@ -66,10 +67,8 @@ class ConfigOption:
                self.desc = []
                self.options = []
 
-       def __cmp__(self, other):
-               if self.section == other.section:
-                       return  cmp(self.name, other.name)
-               return 1 if self.section else -1
+       def __lt__(self, other):
+               return  self.name < other.name
 
        def add_paragraph(self):
                """Adds a new paragraph to the description"""
@@ -93,8 +92,9 @@ class ConfigOption:
 
 class Parser:
        """Parses one or more files of configuration options"""
-       def __init__(self):
+       def __init__(self, sort = True):
                self.options = []
+               self.sort = sort
 
        def parse(self, file):
                """Parses the given file and adds all options to the internal store"""
@@ -146,7 +146,8 @@ class Parser:
                        found.adopt(option)
                else:
                        parent.options.append(option)
-                       parent.options.sort()
+                       if self.sort:
+                               parent.options.sort()
 
        def __get_option(self, parts, create = False):
                """Searches/Creates the option (section) based on a list of section names"""
@@ -161,7 +162,8 @@ class Parser:
                                        break
                                option = ConfigOption(fullname, section = True)
                                options.append(option)
-                               options.sort()
+                               if self.sort:
+                                       options.sort()
                        options = option.options
                return option
 
@@ -228,37 +230,38 @@ class ConfFormatter:
                if len(opt.desc):
                        self.__wrapper.initial_indent = '{0}# '.format(self.__indent * indent)
                        self.__wrapper.subsequent_indent = self.__wrapper.initial_indent
-                       print format(self.__wrapper.fill(self.__tags.replace(opt.desc[0])))
+                       print(self.__wrapper.fill(self.__tags.replace(opt.desc[0])))
 
        def __print_option(self, opt, indent, commented):
                """Print a single option with description and default value"""
                comment = "# " if commented or opt.commented else ""
                self.__print_description(opt, indent)
                if opt.default:
-                       print '{0}{1}{2} = {3}'.format(self.__indent * indent, comment, opt.name, opt.default)
+                       print('{0}{1}{2} = {3}'.format(self.__indent * indent, comment, opt.name, opt.default))
                else:
-                       print '{0}{1}{2} ='.format(self.__indent * indent, comment, opt.name)
-               print
+                       print('{0}{1}{2} ='.format(self.__indent * indent, comment, opt.name))
+               print('')
 
        def __print_section(self, section, indent, commented):
                """Print a section with all options"""
-               comment = "# " if commented or section.commented else ""
+               commented = commented or section.commented
+               comment = "# " if commented else ""
                self.__print_description(section, indent)
-               print '{0}{1}{2} {{'.format(self.__indent * indent, comment, section.name)
-               print
-               for o in section.options:
+               print('{0}{1}{2} {{'.format(self.__indent * indent, comment, section.name))
+               print('')
+               for o in sorted(section.options, key=attrgetter('section')):
                        if o.section:
-                               self.__print_section(o, indent + 1, section.commented)
+                               self.__print_section(o, indent + 1, commented)
                        else:
-                               self.__print_option(o, indent + 1, section.commented)
-               print '{0}{1}}}'.format(self.__indent * indent, comment)
-               print
+                               self.__print_option(o, indent + 1, commented)
+               print('{0}{1}}}'.format(self.__indent * indent, comment))
+               print('')
 
        def format(self, options):
                """Print a list of options"""
                if not options:
                        return
-               for option in options:
+               for option in sorted(options, key=attrgetter('section')):
                        if option.section:
                                self.__print_section(option, 0, False)
                        else:
@@ -283,14 +286,14 @@ class ManFormatter:
                if option.section and not len(option.desc):
                        return
                if option.section:
-                       print '.TP\n.B {0}\n.br'.format(option.fullname)
+                       print('.TP\n.B {0}\n.br'.format(option.fullname))
                else:
-                       print '.TP'
+                       print('.TP')
                        default = option.default if option.default else ''
-                       print '.BR {0} " [{1}]"'.format(option.fullname, default)
+                       print('.BR {0} " [{1}]"'.format(option.fullname, default))
                for para in option.desc if len(option.desc) < 2 else option.desc[1:]:
-                       print self.__groffize(self.__wrapper.fill(para))
-                       print ''
+                       print(self.__groffize(self.__wrapper.fill(para)))
+                       print('')
 
        def format(self, options):
                """Print a list of options"""
@@ -310,9 +313,12 @@ options.add_option("-f", "--format", dest="format", type="choice", choices=["con
 options.add_option("-r", "--root", dest="root", metavar="NAME",
                                   help="root section of which options are printed, "
                                   "if not found everything is printed")
+options.add_option("-n", "--nosort", action="store_false", dest="sort",
+                                  default=True, help="do not sort sections alphabetically")
+
 (opts, args) = options.parse_args()
 
-parser = Parser()
+parser = Parser(opts.sort)
 if len(args):
        for filename in args:
                try: