#! /usr/bin/env python
#
#    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.

"""Generate list of cinder drivers"""

import argparse
import os

from cinder.interface import util


parser = argparse.ArgumentParser(prog="generate_driver_list")

parser.add_argument("--format", default='str', choices=['str', 'dict'],
                    help="Output format type")

# Keep backwards compatibilty with the gate-docs test
# The tests pass ['docs'] on the cmdln, but it's never been used.
parser.add_argument("output_list", default=None, nargs='?')

CI_WIKI_ROOT = "https://wiki.openstack.org/wiki/ThirdPartySystems/"


class Output(object):

    def __init__(self, base_dir, output_list):
        # At this point we don't care what was passed in, just a trigger
        # to write this out to the doc tree for now
        self.driver_file = None
        if output_list:
            self.driver_file = open(
                '%s/doc/source/drivers.rst' % base_dir, 'w+')
            self.driver_file.write('===================\n')
            self.driver_file.write('Available Drivers\n')
            self.driver_file.write('===================\n\n')

    def __enter__(self):
        return self

    def __exit__(self, type, value, traceback):
        if self.driver_file:
            self.driver_file.close()

    def write(self, text):
        if self.driver_file:
            self.driver_file.write('%s\n' % text)
        else:
            print(text)


def format_description(desc, output):
    desc = desc or '<None>'
    lines = desc.rstrip('\n').split('\n')
    for line in lines:
        output.write('    %s' % line)


def print_drivers(drivers, config_name, output):
    for driver in sorted(drivers, key=lambda x: x.class_fqn):
        output.write(driver.class_name)
        output.write('-' * len(driver.class_name))
        if driver.version:
            output.write('* Version: %s' % driver.version)
        output.write('* %s=%s' % (config_name, driver.class_fqn))
        if driver.ci_wiki_name:
            output.write('* CI info: %s%s' % (CI_WIKI_ROOT,
                                              driver.ci_wiki_name))
        output.write('* Description:')
        format_description(driver.desc, output)
        output.write('')
    output.write('')


def output_str(cinder_root, args):
    with Output(cinder_root, args.output_list) as output:
        output.write('Volume Drivers')
        output.write('==============')
        print_drivers(util.get_volume_drivers(), 'volume_driver', output)

        output.write('Backup Drivers')
        output.write('==============')
        print_drivers(util.get_backup_drivers(), 'backup_driver', output)

        output.write('FC Zone Manager Drivers')
        output.write('=======================')
        print_drivers(util.get_fczm_drivers(), 'zone_driver', output)


def collect_driver_info(driver):
    """Build the dictionary that describes this driver."""

    info = {'name': driver.class_name,
            'version': driver.version,
            'fqn': driver.class_fqn,
            'description': driver.desc,
            'ci_wiki_name': driver.ci_wiki_name}

    return info


def output_dict():

    import pprint
    driver_list = []
    drivers = util.get_volume_drivers()
    for driver in drivers:
        driver_list.append(collect_driver_info(driver))

    pprint.pprint(driver_list)


def main():
    tools_dir = os.path.dirname(os.path.abspath(__file__))
    cinder_root = os.path.dirname(tools_dir)
    cur_dir = os.getcwd()
    os.chdir(cinder_root)
    args = parser.parse_args()

    try:
        if args.format == 'str':
            output_str(cinder_root, args)
        elif args.format == 'dict':
            output_dict()

    finally:
        os.chdir(cur_dir)

if __name__ == '__main__':
    main()

[((23, 9, 23, 61), 'argparse.ArgumentParser', 'argparse.ArgumentParser', (), '', False, 'import argparse\n'), ((116, 14, 116, 39), 'cinder.interface.util.get_volume_drivers', 'util.get_volume_drivers', ({}, {}), '()', False, 'from cinder.interface import util\n'), ((120, 4, 120, 30), 'pprint.pprint', 'pprint.pprint', ({(120, 18, 120, 29): 'driver_list'}, {}), '(driver_list)', False, 'import pprint\n'), ((125, 18, 125, 44), 'os.path.dirname', 'os.path.dirname', ({(125, 34, 125, 43): 'tools_dir'}, {}), '(tools_dir)', False, 'import os\n'), ((126, 14, 126, 25), 'os.getcwd', 'os.getcwd', ({}, {}), '()', False, 'import os\n'), ((127, 4, 127, 25), 'os.chdir', 'os.chdir', ({(127, 13, 127, 24): 'cinder_root'}, {}), '(cinder_root)', False, 'import os\n'), ((124, 32, 124, 57), 'os.path.abspath', 'os.path.abspath', ({(124, 48, 124, 56): '__file__'}, {}), '(__file__)', False, 'import os\n'), ((137, 8, 137, 25), 'os.chdir', 'os.chdir', ({(137, 17, 137, 24): 'cur_dir'}, {}), '(cur_dir)', False, 'import os\n'), ((89, 22, 89, 47), 'cinder.interface.util.get_volume_drivers', 'util.get_volume_drivers', ({}, {}), '()', False, 'from cinder.interface import util\n'), ((93, 22, 93, 47), 'cinder.interface.util.get_backup_drivers', 'util.get_backup_drivers', ({}, {}), '()', False, 'from cinder.interface import util\n'), ((97, 22, 97, 45), 'cinder.interface.util.get_fczm_drivers', 'util.get_fczm_drivers', ({}, {}), '()', False, 'from cinder.interface import util\n')]