# Path: pyqi/commands/make_bash_completion.py
# class BashCompletion(Command):
#     BriefDescription = "Construct a bash completion script"
#     LongDescription = ("Construct a bash tab completion script that will search"
#         " through available commands and options")
#     
#     CommandIns = ParameterCollection([
#         CommandIn(Name='command_config_module', DataType=str,
#                   Description="CLI command configuration module",
#                   Required=True),
#         CommandIn(Name='driver_name', DataType=str,
#                   Description="name of the driver script", Required=True)
#     ])
# 
#     CommandOuts = ParameterCollection([
#         CommandOut(Name='result', DataType=str,
#                    Description="result bash completion script")
#     ])
# 
#     def run(self, **kwargs):
#         driver = kwargs['driver_name']
#         cfg_mod_path = kwargs['command_config_module']
#         cfg_mod = _get_cfg_module(cfg_mod_path)
#         command_names = get_command_names(cfg_mod_path)
#         command_list = ' '.join(command_names)
# 
#         commands = []
#         for cmd in command_names:
#             cmd_cfg, _ = get_command_config(cfg_mod_path, cmd,
#                                             exit_on_failure=False)
# 
#             if cmd_cfg is not None:
#                 command_options = []
#                 command_options.extend(
#                         sorted(['--%s' % p.Name for p in cmd_cfg.inputs]))
#                 opts = ' '.join(command_options)
# 
#                 commands.append(command_fmt % {'command':cmd, 'options':opts})
# 
#         all_commands = ''.join(commands)
#         return {'result':script_fmt % {'driver':driver,
#                                        'commands':all_commands,
#                                        'command_list':command_list}}
# 
# def _get_cfg_module(desc):
#     """Load a module"""
#     mod = importlib.import_module(desc)
#     return mod


import sys
import pyqi
from os import mkdir
from os.path import dirname, join
from shutil import copy, rmtree
from tempfile import mkdtemp
from unittest import TestCase, main
from pyqi.commands.make_bash_completion import BashCompletion, _get_cfg_module
        # in tearDown. Imports can then function as normal, e.g.:
        #
        #     import pyqi_test.make_bash_completion
        #     ...
        #
        self.temp_module_dir = mkdtemp()
        self.temp_module_name = 'pyqi_test'
        self.temp_config_dir = join(self.temp_module_dir,
                                    self.temp_module_name)
        mkdir(self.temp_config_dir)

        module = _get_cfg_module('pyqi.interfaces.optparse.config')
        module_dir = dirname(module.__file__)

        make_bash_completion_fp = join(module_dir, 'make_bash_completion.py')
        copy(make_bash_completion_fp, self.temp_config_dir)

        make_optparse_fp = join(module_dir, 'make_optparse.py')
        copy(make_optparse_fp, self.temp_config_dir)

        with open(join(self.temp_config_dir, '__init__.py'), 'w') as f:
            f.write('')

        sys.path.append(self.temp_module_dir)

    def tearDown(self):
        sys.path.remove(self.temp_module_dir)
        rmtree(self.temp_module_dir)

    def test_get_cfg_module(self):

# Path: pyqi/commands/make_bash_completion.py
# class BashCompletion(Command):
#     BriefDescription = "Construct a bash completion script"
#     LongDescription = ("Construct a bash tab completion script that will search"
#         " through available commands and options")
#     
#     CommandIns = ParameterCollection([
#         CommandIn(Name='command_config_module', DataType=str,
#                   Description="CLI command configuration module",
#                   Required=True),
#         CommandIn(Name='driver_name', DataType=str,
#                   Description="name of the driver script", Required=True)
#     ])
# 
#     CommandOuts = ParameterCollection([
#         CommandOut(Name='result', DataType=str,
#                    Description="result bash completion script")
#     ])
# 
#     def run(self, **kwargs):
#         driver = kwargs['driver_name']
#         cfg_mod_path = kwargs['command_config_module']
#         cfg_mod = _get_cfg_module(cfg_mod_path)
#         command_names = get_command_names(cfg_mod_path)
#         command_list = ' '.join(command_names)
# 
#         commands = []
#         for cmd in command_names:
#             cmd_cfg, _ = get_command_config(cfg_mod_path, cmd,
#                                             exit_on_failure=False)
# 
#             if cmd_cfg is not None:
#                 command_options = []
#                 command_options.extend(
#                         sorted(['--%s' % p.Name for p in cmd_cfg.inputs]))
#                 opts = ' '.join(command_options)
# 
#                 commands.append(command_fmt % {'command':cmd, 'options':opts})
# 
#         all_commands = ''.join(commands)
#         return {'result':script_fmt % {'driver':driver,
#                                        'commands':all_commands,
#                                        'command_list':command_list}}
# 
# def _get_cfg_module(desc):
#     """Load a module"""
#     mod = importlib.import_module(desc)
#     return mod


# Path: tests/test_commands/test_make_bash_completion.py
import sys
import pyqi
from os import mkdir
from os.path import dirname, join
from shutil import copy, rmtree
from tempfile import mkdtemp
from unittest import TestCase, main
from pyqi.commands.make_bash_completion import BashCompletion, _get_cfg_module

        # in tearDown. Imports can then function as normal, e.g.:
        #
        #     import pyqi_test.make_bash_completion
        #     ...
        #
        self.temp_module_dir = mkdtemp()
        self.temp_module_name = 'pyqi_test'
        self.temp_config_dir = join(self.temp_module_dir,
                                    self.temp_module_name)
        mkdir(self.temp_config_dir)

        module = _get_cfg_module('pyqi.interfaces.optparse.config')
        module_dir = dirname(module.__file__)

        make_bash_completion_fp = join(module_dir, 'make_bash_completion.py')
        copy(make_bash_completion_fp, self.temp_config_dir)

        make_optparse_fp = join(module_dir, 'make_optparse.py')
        copy(make_optparse_fp, self.temp_config_dir)

        with open(join(self.temp_config_dir, '__init__.py'), 'w') as f:
            f.write('')

        sys.path.append(self.temp_module_dir)

    def tearDown(self):
        sys.path.remove(self.temp_module_dir)
        rmtree(self.temp_module_dir)

    def test_get_cfg_module(self):

        self.assertEqual(_get_cfg_module('pyqi'), pyqi)