# Path: contrail_api_cli/context.py
# class Context(object):
#     _schema = None
#     _shell = ShellContext
#     _session = None
# 
#     @property
#     def schema(self):
#         if self._schema is None:
#             raise SchemaNotInitialized("The schema must be first initialized")
#         else:
#             return self._schema
# 
#     @schema.setter
#     def schema(self, schema):
#         self._schema = schema
# 
#     @property
#     def session(self):
#         if self._session is None:
#             raise SessionNotInitialized("The session must be first initialized")
#         else:
#             return self._session
# 
#     @session.setter
#     def session(self, session):
#         self._session = session
# 
#     @property
#     def shell(self):
#         return self._shell
# 
# Path: contrail_api_cli/parser.py
# class NoCompletions(Exception):
# class CommandParser(object):
#     def __init__(self, document):
#     def used_options(self):
#     def available_options(self):
#     def used_args(self):
#     def available_args(self):
#     def get_option(self, option_str):
# 
# Path: contrail_api_cli/exceptions.py
# class CommandNotFound(Exception):
#     pass
# 
# Path: contrail_api_cli/utils.py
# def printo(msg, encoding=None, errors='replace', std_type='stdout'):
#     """Write msg on stdout. If no encoding is specified
#     the detected encoding of stdout is used. If the encoding
#     can't encode some chars they are replaced by '?'
# 
#     :param msg: message
#     :type msg: unicode on python2 | str on python3
#     """
#     std = getattr(sys, std_type, sys.stdout)
#     if encoding is None:
#         try:
#             encoding = std.encoding
#         except AttributeError:
#             encoding = None
#     # Fallback to ascii if no encoding is found
#     if encoding is None:
#         encoding = 'ascii'
#     # https://docs.python.org/3/library/sys.html#sys.stdout
#     # write in the binary buffer directly in python3
#     if hasattr(std, 'buffer'):
#         std = std.buffer
#     std.write(msg.encode(encoding, errors=errors))
#     std.write(b'\n')
#     std.flush()


import logging
from prompt_toolkit.completion import Completer, Completion
from prompt_toolkit.document import Document
from stevedore import extension
from .context import Context
from .parser import CommandParser, CommandInvalid
from .exceptions import CommandNotFound
from .utils import printo
            for choice in option.kwargs['choices']:
                yield Completion(choice,
                                 -len(word_before_cursor))
        # complete resources
        elif option.complete is not None:
            logger.debug('Complete using option matcher %s' % option.complete)
            completer_name = option.complete.split(':')[0]
            if completer_name in self.completers:
                for c in self.completers[completer_name].get_completions(word_before_cursor, self.context, option):
                    yield c
            else:
                logger.warning('No completer found for %s' % completer_name)

    def get_last_option(self, word_before_cursor, words):
        # current option value
        if word_before_cursor:
            try:
                option = self.parser.get_option(words[-2])
            except IndexError:
                option = None
        # no value typed
        else:
            option = self.parser.get_option(words[-1])
        return option

    def get_last_arg(self, word_before_cursor, words):
        # current arg value
        if word_before_cursor and list(self.parser.available_args):
            # depending on the cursor position, use a relative
            # parser to get the current argument

# Path: contrail_api_cli/context.py
# class Context(object):
#     _schema = None
#     _shell = ShellContext
#     _session = None
# 
#     @property
#     def schema(self):
#         if self._schema is None:
#             raise SchemaNotInitialized("The schema must be first initialized")
#         else:
#             return self._schema
# 
#     @schema.setter
#     def schema(self, schema):
#         self._schema = schema
# 
#     @property
#     def session(self):
#         if self._session is None:
#             raise SessionNotInitialized("The session must be first initialized")
#         else:
#             return self._session
# 
#     @session.setter
#     def session(self, session):
#         self._session = session
# 
#     @property
#     def shell(self):
#         return self._shell
# 
# Path: contrail_api_cli/parser.py
# class NoCompletions(Exception):
# class CommandParser(object):
#     def __init__(self, document):
#     def used_options(self):
#     def available_options(self):
#     def used_args(self):
#     def available_args(self):
#     def get_option(self, option_str):
# 
# Path: contrail_api_cli/exceptions.py
# class CommandNotFound(Exception):
#     pass
# 
# Path: contrail_api_cli/utils.py
# def printo(msg, encoding=None, errors='replace', std_type='stdout'):
#     """Write msg on stdout. If no encoding is specified
#     the detected encoding of stdout is used. If the encoding
#     can't encode some chars they are replaced by '?'
# 
#     :param msg: message
#     :type msg: unicode on python2 | str on python3
#     """
#     std = getattr(sys, std_type, sys.stdout)
#     if encoding is None:
#         try:
#             encoding = std.encoding
#         except AttributeError:
#             encoding = None
#     # Fallback to ascii if no encoding is found
#     if encoding is None:
#         encoding = 'ascii'
#     # https://docs.python.org/3/library/sys.html#sys.stdout
#     # write in the binary buffer directly in python3
#     if hasattr(std, 'buffer'):
#         std = std.buffer
#     std.write(msg.encode(encoding, errors=errors))
#     std.write(b'\n')
#     std.flush()


# Path: contrail_api_cli/completer.py
import logging
from prompt_toolkit.completion import Completer, Completion
from prompt_toolkit.document import Document
from stevedore import extension
from .context import Context
from .parser import CommandParser, CommandInvalid
from .exceptions import CommandNotFound
from .utils import printo

            for choice in option.kwargs['choices']:
                yield Completion(choice,
                                 -len(word_before_cursor))
        # complete resources
        elif option.complete is not None:
            logger.debug('Complete using option matcher %s' % option.complete)
            completer_name = option.complete.split(':')[0]
            if completer_name in self.completers:
                for c in self.completers[completer_name].get_completions(word_before_cursor, self.context, option):
                    yield c
            else:
                logger.warning('No completer found for %s' % completer_name)

    def get_last_option(self, word_before_cursor, words):
        # current option value
        if word_before_cursor:
            try:
                option = self.parser.get_option(words[-2])
            except IndexError:
                option = None
        # no value typed
        else:
            option = self.parser.get_option(words[-1])
        return option

    def get_last_arg(self, word_before_cursor, words):
        # current arg value
        if word_before_cursor and list(self.parser.available_args):
            # depending on the cursor position, use a relative
            # parser to get the current argument

            p = CommandParser(Document(text=" ".join(words)))