conf: Add a format-options --nosort option to keep order of sections as defined

This commit is contained in:
Martin Willi 2014-04-29 12:13:33 +02:00
parent 85d26e0c87
commit ae98a39e71
1 changed files with 10 additions and 4 deletions

View File

@ -92,8 +92,9 @@ class ConfigOption:
class Parser: class Parser:
"""Parses one or more files of configuration options""" """Parses one or more files of configuration options"""
def __init__(self): def __init__(self, sort = True):
self.options = [] self.options = []
self.sort = sort
def parse(self, file): def parse(self, file):
"""Parses the given file and adds all options to the internal store""" """Parses the given file and adds all options to the internal store"""
@ -145,7 +146,8 @@ class Parser:
found.adopt(option) found.adopt(option)
else: else:
parent.options.append(option) parent.options.append(option)
parent.options.sort() if self.sort:
parent.options.sort()
def __get_option(self, parts, create = False): def __get_option(self, parts, create = False):
"""Searches/Creates the option (section) based on a list of section names""" """Searches/Creates the option (section) based on a list of section names"""
@ -160,7 +162,8 @@ class Parser:
break break
option = ConfigOption(fullname, section = True) option = ConfigOption(fullname, section = True)
options.append(option) options.append(option)
options.sort() if self.sort:
options.sort()
options = option.options options = option.options
return option return option
@ -310,9 +313,12 @@ options.add_option("-f", "--format", dest="format", type="choice", choices=["con
options.add_option("-r", "--root", dest="root", metavar="NAME", options.add_option("-r", "--root", dest="root", metavar="NAME",
help="root section of which options are printed, " help="root section of which options are printed, "
"if not found everything is 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() (opts, args) = options.parse_args()
parser = Parser() parser = Parser(opts.sort)
if len(args): if len(args):
for filename in args: for filename in args:
try: try: