#!/usr/bin/env python
# -*- coding: utf-8 -*-
###############################################################################
# Copyright (C) Bull S.A.S (2010, 2011)
# Contributor: Pierre Vignéras <pierre.vigneras@bull.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
###############################################################################
"""
Guess name#type@category for each given components@list
"""
import ConfigParser
import logging
import optparse

from sequencer.dgm import cli as dgm_cli


__author__ = "Pierre Vigneras"
__copyright__ = "Copyright (c) 2010 Bull S.A.S."
__credits__ = ["Pierre Vigneras"]


_LOGGER = logging.getLogger(__name__)


def main():
    """
    The main! ;-)
    """
    usage = "%prog [options] component_list"

    doc = """For each component in the given component_list returns the related
component type and category. The given component_list should follow
the following format: name[range]#type@category where category"""

    parser = optparse.OptionParser(usage, description=doc)
    parser.add_option('-c', '--config', dest="config", type='string', nargs=1,
                      default=None,
                      help='Specify the configuration file.' + \
                      ' Default: %default.')
    parser.add_option('--module', dest="module", type='string', nargs=1,
                      default='sequencer.guesser',
                      help='Specify the module name to use.' + \
                      ' Default: %default.')
    parser.add_option('--param', dest="param", type='string', nargs=1,
                      default='None',
                      help='Specify the module instantiation parameter to use.' + \
                      ' Default: %default.')
    (options, args) = parser.parse_args()

    if len(args) < 1:
        parser.error("Wrong number of arguments.")

    config = ConfigParser.SafeConfigParser({'guesser.module.name': options.module,
                                            'guesser.factory.params': options.param,
                                            })
    config.add_section(dgm_cli.DEPMAKE_ACTION_NAME)
    if options.config is not None:
        _LOGGER.debug("Reading configuration file: %s", options.config)
        config.read(options.config)

    components_lists = dgm_cli.parse_components_lists(args)
    for component in dgm_cli.get_component_set_from(config, components_lists):
        print(component)

if __name__ == "__main__":
    main()
