Example #1
0
def _convertConfigLinesToArguments(parser, lines):
    """
    Converts configuration file lines of the format:

       myoption 1
       mybooloption False

    to the equivalent command-line arguments for the specified OptionParser.
    For example, the configuration file above would return the argument
    list ['--myoption', '1', '--mybooloption'] if mybooloption has action
    store_false, and ['--myoption', '1'] if mybooloption has action store_true.

    @parameter parser: OptionParser object containing configuration options.
    @type parser: OptionParser
    @parameter lines: List of dictionary object parsed from a configuration file.
                      Each option is expected to have 'type', 'key', 'value' entries.
    @type lines: list of dictionaries.
    @return: List of command-line arguments corresponding to the configuration file.
    @rtype: list of strings
    """
    # valid key
    #     an option's string without the leading "--"
    #     can differ from an option's destination
    validOpts = set(
        (opt.get_opt_string() for opt in getAllParserOptionsGen(parser)))

    args = []
    for line in lines:
        if line.get('type', None) != 'option':
            continue
        optstring = configToFlag(line['key'])
        if optstring in validOpts:
            option = parser.get_option(optstring)
            value = line.get('value', '')
            boolean_value = value.lower() in ('true', 'yes',
                                              '1') if value else False
            if option.action == 'store_true':
                if boolean_value:
                    args.append(optstring)
            elif option.action == 'store_false':
                if not boolean_value:
                    args.append(optstring)
            else:
                args.extend([
                    optstring,
                    line['value'],
                ])
        else:
            log.debug("Unknown option: %s", optstring)

    return args
Example #2
0
def _convertConfigLinesToArguments(parser, lines):
    """
    Converts configuration file lines of the format:

       myoption 1
       mybooloption False

    to the equivalent command-line arguments for the specified OptionParser.
    For example, the configuration file above would return the argument
    list ['--myoption', '1', '--mybooloption'] if mybooloption has action
    store_false, and ['--myoption', '1'] if mybooloption has action store_true.

    @parameter parser: OptionParser object containing configuration options.
    @type parser: OptionParser
    @parameter lines: List of dictionary object parsed from a configuration file.
                      Each option is expected to have 'type', 'key', 'value' entries.
    @type lines: list of dictionaries.
    @return: List of command-line arguments corresponding to the configuration file.
    @rtype: list of strings
    """
    # valid key
    #     an option's string without the leading "--"
    #     can differ from an option's destination
    validOpts = set((opt.get_opt_string() for opt in getAllParserOptionsGen(parser)))

    args = []
    for line in lines:
        if line.get("type", None) != "option":
            continue
        optstring = configToFlag(line["key"])
        if optstring in validOpts:
            option = parser.get_option(optstring)
            value = line.get("value", "")
            boolean_value = value.lower() in ("true", "yes", "1") if value else False
            if option.action == "store_true":
                if boolean_value:
                    args.append(optstring)
            elif option.action == "store_false":
                if not boolean_value:
                    args.append(optstring)
            else:
                args.extend([optstring, line["value"]])
        else:
            log.debug("Unknown option: %s", optstring)

    return args
Example #3
0
    def generate_xml_configs(self, parser, options):
        """
        Create an XML file that can be used to create Docbook files
        as well as used as the basis for GUI-based daemon option
        configuration.
        """

        #
        # Header for the configuration file
        #
        unused(options)
        daemon_name = os.path.basename(sys.argv[0])
        daemon_name = daemon_name.replace('.py', '')

        export_date = datetime.datetime.now()

        print """<?xml version="1.0" encoding="UTF-8"?>

<!-- Default daemon configuration generated on %s -->
<configuration id="%s" >

""" % (export_date, daemon_name)

        options_to_ignore = (
            'help',
            'version',
            '',
            'genconf',
            'genxmltable',
            'genxmlconfigs',
        )

        #
        # Create an entry for each of the command line flags
        #
        # NB: Ideally, this should print out only the option parser dest
        #     entries, rather than the command line options.
        #
        import re
        for opt in getAllParserOptionsGen(parser):
            if opt.help is SUPPRESS_HELP:
                continue

            #
            # Don't display anything we shouldn't be displaying
            #
            option_name = re.sub(r'.*/--', '', "%s" % opt)
            option_name = re.sub(r'^--', '', "%s" % option_name)
            if option_name in options_to_ignore:
                continue

            default_value = parser.defaults.get(opt.dest)
            if default_value is NO_DEFAULT or default_value is None:
                default_string = ""
            else:
                default_string = str(default_value)

#
# TODO: Determine the variable name used and display the --option_name=variable_name
#
            if opt.action in ['store_true', 'store_false']:
                print """    <option id="%s" type="%s" default="%s" help="%s" />
""" % (
                    option_name,
                    "boolean",
                    default_string,
                    quote(opt.help),
                )

            else:
                target = opt.dest.lower()
                print """    <option id="%s" type="%s" default="%s" target="%s" help="%s" />
""" % (
                    option_name,
                    opt.type,
                    quote(default_string),
                    target,
                    quote(opt.help),
                )

        #
        # Close the table elements
        #
        print """
</configuration>
"""
        sys.exit(0)
Example #4
0
    def generate_xml_table(self, parser, options):
        """
        Create a Docbook table based on the long-form of the option names

        @parameter parser: an optparse parser object which contains defaults, help
        @parameter options: parsed options list containing actual values
        """

        #
        # Header for the configuration file
        #
        unused(options)
        daemon_name = os.path.basename(sys.argv[0])
        daemon_name = daemon_name.replace('.py', '')

        print """<?xml version="1.0" encoding="UTF-8"?>

<section version="4.0" xmlns="http://docbook.org/ns/docbook"
   xmlns:xlink="http://www.w3.org/1999/xlink"
   xmlns:xi="http://www.w3.org/2001/XInclude"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns:mml="http://www.w3.org/1998/Math/MathML"
   xmlns:html="http://www.w3.org/1999/xhtml"
   xmlns:db="http://docbook.org/ns/docbook"

  xml:id="%s.options"
>

<title>%s Options</title>
<para />
<table frame="all">
  <caption>%s <indexterm><primary>Daemons</primary><secondary>%s</secondary></indexterm> options</caption>
<tgroup cols="2">
<colspec colname="option" colwidth="1*" />
<colspec colname="description" colwidth="2*" />
<thead>
<row>
<entry> <para>Option</para> </entry>
<entry> <para>Description</para> </entry>
</row>
</thead>
<tbody>
""" % (daemon_name, daemon_name, daemon_name, daemon_name)

        options_to_ignore = ('help', 'version', '', 'genconf', 'genxmltable')

        #
        # Create an entry for each of the command line flags
        #
        # NB: Ideally, this should print out only the option parser dest
        #     entries, rather than the command line options.
        #
        import re
        for opt in getAllParserOptionsGen(parser):
            if opt.help is SUPPRESS_HELP:
                continue

            #
            # Create a Docbook-happy version of the option strings
            # Yes, <arg></arg> would be better semantically, but the output
            # just looks goofy in a table.  Use literal instead.
            #
            all_options = '<literal>' + re.sub(
                r'/', '</literal>,</para> <para><literal>',
                "%s" % opt) + '</literal>'

            #
            # Don't display anything we shouldn't be displaying
            #
            option_name = re.sub(r'.*/--', '', "%s" % opt)
            option_name = re.sub(r'^--', '', "%s" % option_name)
            if option_name in options_to_ignore:
                continue

            default_value = parser.defaults.get(opt.dest)
            if default_value is NO_DEFAULT or default_value is None:
                default_value = ""
            default_string = ""
            if default_value != "":
                default_string = "<para> Default: <literal>" + str(
                    default_value) + "</literal></para>\n"

            comment = self.pretty_print_config_comment(opt.help)

            #
            # TODO: Determine the variable name used and display the --option_name=variable_name
            #
            if opt.action in ['store_true', 'store_false']:
                print """<row>
<entry> <para>%s</para> </entry>
<entry>
<para>%s</para>
%s</entry>
</row>
""" % (all_options, comment, default_string)

            else:
                target = '=<replaceable>' + opt.dest.lower() + '</replaceable>'
                all_options = all_options + target
                all_options = re.sub(r',', target + ',', all_options)
                print """<row>
<entry> <para>%s</para> </entry>
<entry>
<para>%s</para>
%s</entry>
</row>
""" % (all_options, comment, default_string)

        #
        # Close the table elements
        #
        print """</tbody></tgroup>
</table>
<para />
</section>
"""
        sys.exit(0)
Example #5
0
    def generate_configs(self, parser, options):
        """
        Create a configuration file based on the long-form of the option names

        @parameter parser: an optparse parser object which contains defaults, help
        @parameter options: parsed options list containing actual values
        """

        #
        # Header for the configuration file
        #
        unused(options)
        daemon_name = os.path.basename(sys.argv[0])
        daemon_name = daemon_name.replace('.py', '')

        print """#
# Configuration file for %s
#
#  To enable a particular option, uncomment the desired entry.
#
# Parameter     Setting
# ---------     -------""" % (daemon_name)

        options_to_ignore = ('help', 'version', '', 'genconf', 'genxmltable')

        #
        # Create an entry for each of the command line flags
        #
        # NB: Ideally, this should print out only the option parser dest
        #     entries, rather than the command line options.
        #
        import re
        for opt in getAllParserOptionsGen(parser):
            if opt.help is SUPPRESS_HELP:
                continue

            #
            # Get rid of the short version of the command
            #
            option_name = re.sub(r'.*/--', '', "%s" % opt)

            #
            # And what if there's no short version?
            #
            option_name = re.sub(r'^--', '', "%s" % option_name)

            #
            # Don't display anything we shouldn't be displaying
            #
            if option_name in options_to_ignore:
                continue

            #
            # Find the actual value specified on the command line, if any,
            # and display it
            #

            value = getattr(parser.values, opt.dest)

            default_value = parser.defaults.get(opt.dest)
            if default_value is NO_DEFAULT or default_value is None:
                default_value = ""
            default_string = ""
            if default_value != "":
                default_string = ", default: " + str(default_value)

            comment = self.pretty_print_config_comment(opt.help +
                                                       default_string)

            #
            # NB: I would prefer to use tabs to separate the parameter name
            #     and value, but I don't know that this would work.
            #
            print """#
# %s
#%s %s""" % (comment, option_name, value)

        #
        # Pretty print and exit
        #
        print "#"
        sys.exit(0)
Example #6
0
    def generate_xml_configs( self, parser, options ):
        """
        Create an XML file that can be used to create Docbook files
        as well as used as the basis for GUI-based daemon option
        configuration.
        """

        #
        # Header for the configuration file
        #
        unused(options)
        daemon_name= os.path.basename( sys.argv[0] )
        daemon_name= daemon_name.replace( '.py', '' )

        export_date = datetime.datetime.now()

        print """<?xml version="1.0" encoding="UTF-8"?>

<!-- Default daemon configuration generated on %s -->
<configuration id="%s" >

""" % ( export_date, daemon_name )

        options_to_ignore= (
            'help', 'version', '', 'genconf', 'genxmltable',
            'genxmlconfigs',
        )

        #
        # Create an entry for each of the command line flags
        #
        # NB: Ideally, this should print out only the option parser dest
        #     entries, rather than the command line options.
        #
        import re
        for opt in getAllParserOptionsGen(parser):
                if opt.help is SUPPRESS_HELP:
                        continue

                #
                # Don't display anything we shouldn't be displaying
                #
                option_name= re.sub( r'.*/--', '', "%s" % opt )
                option_name= re.sub( r'^--', '', "%s" % option_name )
                if option_name in options_to_ignore:
                        continue

                default_value= parser.defaults.get( opt.dest )
                if default_value is NO_DEFAULT or default_value is None:
                        default_string= ""
                else:
                        default_string= str( default_value )

#
# TODO: Determine the variable name used and display the --option_name=variable_name
#
                if opt.action in [ 'store_true', 'store_false' ]:
                   print """    <option id="%s" type="%s" default="%s" help="%s" />
""" % ( option_name, "boolean", default_string, quote(opt.help),  )

                else:
                   target= opt.dest.lower()
                   print """    <option id="%s" type="%s" default="%s" target="%s" help="%s" />
""" % ( option_name, opt.type, quote(default_string), target, quote(opt.help), )


        #
        # Close the table elements
        #
        print """
</configuration>
"""
        sys.exit( 0 )
Example #7
0
    def generate_xml_table( self, parser, options ):
        """
        Create a Docbook table based on the long-form of the option names

        @parameter parser: an optparse parser object which contains defaults, help
        @parameter options: parsed options list containing actual values
        """

        #
        # Header for the configuration file
        #
        unused(options)
        daemon_name= os.path.basename( sys.argv[0] )
        daemon_name= daemon_name.replace( '.py', '' )

        print """<?xml version="1.0" encoding="UTF-8"?>

<section version="4.0" xmlns="http://docbook.org/ns/docbook"
   xmlns:xlink="http://www.w3.org/1999/xlink"
   xmlns:xi="http://www.w3.org/2001/XInclude"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns:mml="http://www.w3.org/1998/Math/MathML"
   xmlns:html="http://www.w3.org/1999/xhtml"
   xmlns:db="http://docbook.org/ns/docbook"

  xml:id="%s.options"
>

<title>%s Options</title>
<para />
<table frame="all">
  <caption>%s <indexterm><primary>Daemons</primary><secondary>%s</secondary></indexterm> options</caption>
<tgroup cols="2">
<colspec colname="option" colwidth="1*" />
<colspec colname="description" colwidth="2*" />
<thead>
<row>
<entry> <para>Option</para> </entry>
<entry> <para>Description</para> </entry>
</row>
</thead>
<tbody>
""" % ( daemon_name, daemon_name, daemon_name, daemon_name )


        options_to_ignore= ( 'help', 'version', '', 'genconf', 'genxmltable' )

        #
        # Create an entry for each of the command line flags
        #
        # NB: Ideally, this should print out only the option parser dest
        #     entries, rather than the command line options.
        #
        import re
        for opt in getAllParserOptionsGen(parser):
                if opt.help is SUPPRESS_HELP:
                        continue

                #
                # Create a Docbook-happy version of the option strings
                # Yes, <arg></arg> would be better semantically, but the output
                # just looks goofy in a table.  Use literal instead.
                #
                all_options= '<literal>' + re.sub( r'/', '</literal>,</para> <para><literal>', "%s" % opt ) + '</literal>'

                #
                # Don't display anything we shouldn't be displaying
                #
                option_name= re.sub( r'.*/--', '', "%s" % opt )
                option_name= re.sub( r'^--', '', "%s" % option_name )
                if option_name in options_to_ignore:
                        continue

                default_value= parser.defaults.get( opt.dest )
                if default_value is NO_DEFAULT or default_value is None:
                        default_value= ""
                default_string= ""
                if default_value != "":
                        default_string= "<para> Default: <literal>" + str( default_value ) + "</literal></para>\n"

                comment= self.pretty_print_config_comment( opt.help )

#
# TODO: Determine the variable name used and display the --option_name=variable_name
#
                if opt.action in [ 'store_true', 'store_false' ]:
                   print """<row>
<entry> <para>%s</para> </entry>
<entry>
<para>%s</para>
%s</entry>
</row>
""" % ( all_options, comment, default_string )

                else:
                   target= '=<replaceable>' +  opt.dest.lower() + '</replaceable>'
                   all_options= all_options + target
                   all_options= re.sub( r',', target + ',', all_options )
                   print """<row>
<entry> <para>%s</para> </entry>
<entry>
<para>%s</para>
%s</entry>
</row>
""" % ( all_options, comment, default_string )



        #
        # Close the table elements
        #
        print """</tbody></tgroup>
</table>
<para />
</section>
"""
        sys.exit( 0 )
Example #8
0
    def generate_configs( self, parser, options ):
        """
        Create a configuration file based on the long-form of the option names

        @parameter parser: an optparse parser object which contains defaults, help
        @parameter options: parsed options list containing actual values
        """

        #
        # Header for the configuration file
        #
        unused(options)
        daemon_name= os.path.basename( sys.argv[0] )
        daemon_name= daemon_name.replace( '.py', '' )

        print """#
# Configuration file for %s
#
#  To enable a particular option, uncomment the desired entry.
#
# Parameter     Setting
# ---------     -------""" % ( daemon_name )


        options_to_ignore= ( 'help', 'version', '', 'genconf', 'genxmltable' )

        #
        # Create an entry for each of the command line flags
        #
        # NB: Ideally, this should print out only the option parser dest
        #     entries, rather than the command line options.
        #
        import re
        for opt in getAllParserOptionsGen(parser):
                if opt.help is SUPPRESS_HELP:
                        continue

                #
                # Get rid of the short version of the command
                #
                option_name= re.sub( r'.*/--', '', "%s" % opt )

                #
                # And what if there's no short version?
                #
                option_name= re.sub( r'^--', '', "%s" % option_name )

                #
                # Don't display anything we shouldn't be displaying
                #
                if option_name in options_to_ignore:
                        continue

                #
                # Find the actual value specified on the command line, if any,
                # and display it
                #

                value= getattr( parser.values,  opt.dest )

                default_value= parser.defaults.get( opt.dest )
                if default_value is NO_DEFAULT or default_value is None:
                        default_value= ""
                default_string= ""
                if default_value != "":
                        default_string= ", default: " + str( default_value )

                comment=  self.pretty_print_config_comment( opt.help + default_string )

                #
                # NB: I would prefer to use tabs to separate the parameter name
                #     and value, but I don't know that this would work.
                #
                print """#
# %s
#%s %s""" % ( comment, option_name, value )

        #
        # Pretty print and exit
        #
        print "#"
        sys.exit( 0 )