# Path: lightbulb/core/operate.py
# def manage(module_name, configuration):
#     """
#     This function manages the module initialization and execution
#     Args:
#         module_name (class): The module class to be initiated
#         configuration (dict): The module options
#     Returns:
#        list: A list with the results and stats as tuples
#     """
# 
#     module = __import__(module_name)
#     classmembers = [classmodule for classmodule in inspect.getmembers(module, inspect.isclass)
#                     if classmodule[0] == 'Module' or classmodule[0] == 'Module_A'
#                     or classmodule[0] == 'Module_B']
#     if len(classmembers) == 1:
#         return  operate_learn(classmembers[0][1], configuration)
#     else:
#         if classmembers[0][0] == "Module_A":
#             return operate_diff(classmembers[0][1], configuration, classmembers[1][1], configuration)
#         else:
#             return operate_diff(classmembers[1][1], configuration, classmembers[0][1], configuration)
# 
# def operate_diff(module_class_A, configuration_A, module_class_B, configuration_B):
#     """
#     Initiates the execution of two modules in parallel
#     Args:
#         module_class_A (class): The module class to be initiated
#         configuration_A (dict): The module options
#         module_class_B (class): The module class to be initiated
#         configuration_B (dict): The module options
#     Returns:
#         None
#     """
#     object_a = None
#     object_b = None
#     shared_a = None
#     shared_b = None
#     target_a = None
#     target_b = None
#     target_a_pipe, target_b_pipe = Pipe()
#     result = []
#     shared_memory = [target_a_pipe, target_b_pipe, object_a, object_b, shared_a, shared_b, result]
# 
#     target_a = Thread(target=operate_diff_part,
#                       args=(module_class_A, configuration_A, shared_memory, 1))
#     target_a.setDaemon(True)
#     target_a.start()
#     target_b = Thread(target=operate_diff_part,
#                       args=(module_class_B, configuration_B, shared_memory, 2))
#     target_b.setDaemon(True)
#     target_b.start()
#     while True:
#         target_a.join(600)
#         if not target_a.isAlive():
#             target_a = None
#             break
#     print 'thread one finished'
#     # End Cross _check
#     target_b.join(0)
#     target_b = None
#     print 'thread two finished'
#     return  shared_memory[6]
# 
# def operate_learn(module, configuration):
#     """
#    Initiates the execution of a module
#    Args:
#        module (class): The module class to be initiated
#        configuration (dict): The module options
#    Returns:
#        list: A list with the results and stats as tuples
#    """
#     initmodule = module(configuration)
#     initmodule.learn()
#     name, bypass = initmodule.getresult()
#     return [(name, bypass)] + initmodule.stats()
# 
# Path: lightbulb/core/base.py
# def options_as_dictionary(meta):
#     """
#     Transforms a list of tuples to a dictionary
#     Args:
#         meta (list): A list ot option tuples
#     Retuns:
#         dict: A dictionary of options key - values
#     """
#     module_config = {}
#     for seltuple in meta:
#         module_config.update({seltuple[0]: seltuple[1]})
#     return  module_config
# 
# def importmodule(name):
#     try:
#        try:
#           mod = __import__(name)
#        except:
#           modfile, pathname, description = imp.find_module(name)
#           mod = imp.load_module(name, modfile, pathname, description)
#        components = name.split('.')
#        for comp in components[1:]:
#            mod = getattr(mod, comp)
#     except ImportError as error:
#        print error.__class__.__name__ + ": " + error.message
# 
#     return mod
# 
# def create_object(object_type, object_type_configuration, handler, handler_configuration):
#     object_mod = importmodule('lightbulb.core.modules.' + object_type.lower())
#     object_class = getattr(object_mod, object_type)
#     handler_mod = importmodule('lightbulb.core.utils.' + handler.lower())
#     handler_class = getattr(handler_mod, handler)
#     class Module(object_class):
#         def setup(self, configuration):
#             super(Module,self).setup(configuration)
#             self.handler = handler_class(handler_configuration)
#     return  Module


import logging
import sys
import os
import imp
import inspect
import platform as distplatform
from cliff.command import Command
from cliff.lister import Lister
from lightbulb.core.operate import manage, operate_diff, operate_learn
from lightbulb.core.base import options_as_dictionary, importmodule, create_object
from sys import platform


    def check_for_fst(self):
        try:
            imp.find_module('fst')
            return 'OK'
        except ImportError:
            try:
                imp.find_module('pywrapfst')
                return 'OK'
            except ImportError:
                print 'It is recommended to use openfst python bindings (either pyfst or pywrapfst)' \
                      ' for the DFA implementation. While this is not necessary, using openfst python' \
                      ' bindings will increase execution speed significally.'
                install = raw_input(
                    ('* Install openfst 1.5.4 with pywrapfst now? (sudo may be required) [Y/n] ')
                )
                if install == 'n':
                    return 'FAIL'
                else:
                    if hasattr(sys, 'real_prefix'):
                        installpywrapfst = """
                            wget http://www.openfst.org/twiki/pub/FST/FstDownload/openfst-1.5.4.tar.gz
                            tar zxvf openfst-1.5.4.tar.gz
                            cd openfst-1.5.4
                            ./configure --enable-python --enable-far --prefix """+getattr(sys, 'prefix')+"""/local/ && make clean && make install
                            """
                    else:
                        installpywrapfst = """
                            wget http://www.openfst.org/twiki/pub/FST/FstDownload/openfst-1.5.4.tar.gz

# Path: lightbulb/core/operate.py
# def manage(module_name, configuration):
#     """
#     This function manages the module initialization and execution
#     Args:
#         module_name (class): The module class to be initiated
#         configuration (dict): The module options
#     Returns:
#        list: A list with the results and stats as tuples
#     """
# 
#     module = __import__(module_name)
#     classmembers = [classmodule for classmodule in inspect.getmembers(module, inspect.isclass)
#                     if classmodule[0] == 'Module' or classmodule[0] == 'Module_A'
#                     or classmodule[0] == 'Module_B']
#     if len(classmembers) == 1:
#         return  operate_learn(classmembers[0][1], configuration)
#     else:
#         if classmembers[0][0] == "Module_A":
#             return operate_diff(classmembers[0][1], configuration, classmembers[1][1], configuration)
#         else:
#             return operate_diff(classmembers[1][1], configuration, classmembers[0][1], configuration)
# 
# def operate_diff(module_class_A, configuration_A, module_class_B, configuration_B):
#     """
#     Initiates the execution of two modules in parallel
#     Args:
#         module_class_A (class): The module class to be initiated
#         configuration_A (dict): The module options
#         module_class_B (class): The module class to be initiated
#         configuration_B (dict): The module options
#     Returns:
#         None
#     """
#     object_a = None
#     object_b = None
#     shared_a = None
#     shared_b = None
#     target_a = None
#     target_b = None
#     target_a_pipe, target_b_pipe = Pipe()
#     result = []
#     shared_memory = [target_a_pipe, target_b_pipe, object_a, object_b, shared_a, shared_b, result]
# 
#     target_a = Thread(target=operate_diff_part,
#                       args=(module_class_A, configuration_A, shared_memory, 1))
#     target_a.setDaemon(True)
#     target_a.start()
#     target_b = Thread(target=operate_diff_part,
#                       args=(module_class_B, configuration_B, shared_memory, 2))
#     target_b.setDaemon(True)
#     target_b.start()
#     while True:
#         target_a.join(600)
#         if not target_a.isAlive():
#             target_a = None
#             break
#     print 'thread one finished'
#     # End Cross _check
#     target_b.join(0)
#     target_b = None
#     print 'thread two finished'
#     return  shared_memory[6]
# 
# def operate_learn(module, configuration):
#     """
#    Initiates the execution of a module
#    Args:
#        module (class): The module class to be initiated
#        configuration (dict): The module options
#    Returns:
#        list: A list with the results and stats as tuples
#    """
#     initmodule = module(configuration)
#     initmodule.learn()
#     name, bypass = initmodule.getresult()
#     return [(name, bypass)] + initmodule.stats()
# 
# Path: lightbulb/core/base.py
# def options_as_dictionary(meta):
#     """
#     Transforms a list of tuples to a dictionary
#     Args:
#         meta (list): A list ot option tuples
#     Retuns:
#         dict: A dictionary of options key - values
#     """
#     module_config = {}
#     for seltuple in meta:
#         module_config.update({seltuple[0]: seltuple[1]})
#     return  module_config
# 
# def importmodule(name):
#     try:
#        try:
#           mod = __import__(name)
#        except:
#           modfile, pathname, description = imp.find_module(name)
#           mod = imp.load_module(name, modfile, pathname, description)
#        components = name.split('.')
#        for comp in components[1:]:
#            mod = getattr(mod, comp)
#     except ImportError as error:
#        print error.__class__.__name__ + ": " + error.message
# 
#     return mod
# 
# def create_object(object_type, object_type_configuration, handler, handler_configuration):
#     object_mod = importmodule('lightbulb.core.modules.' + object_type.lower())
#     object_class = getattr(object_mod, object_type)
#     handler_mod = importmodule('lightbulb.core.utils.' + handler.lower())
#     handler_class = getattr(handler_mod, handler)
#     class Module(object_class):
#         def setup(self, configuration):
#             super(Module,self).setup(configuration)
#             self.handler = handler_class(handler_configuration)
#     return  Module


# Path: lightbulb/cli/use.py
import logging
import sys
import os
import imp
import inspect
import platform as distplatform
from cliff.command import Command
from cliff.lister import Lister
from lightbulb.core.operate import manage, operate_diff, operate_learn
from lightbulb.core.base import options_as_dictionary, importmodule, create_object
from sys import platform



    def check_for_fst(self):
        try:
            imp.find_module('fst')
            return 'OK'
        except ImportError:
            try:
                imp.find_module('pywrapfst')
                return 'OK'
            except ImportError:
                print 'It is recommended to use openfst python bindings (either pyfst or pywrapfst)' \
                      ' for the DFA implementation. While this is not necessary, using openfst python' \
                      ' bindings will increase execution speed significally.'
                install = raw_input(
                    ('* Install openfst 1.5.4 with pywrapfst now? (sudo may be required) [Y/n] ')
                )
                if install == 'n':
                    return 'FAIL'
                else:
                    if hasattr(sys, 'real_prefix'):
                        installpywrapfst = """
                            wget http://www.openfst.org/twiki/pub/FST/FstDownload/openfst-1.5.4.tar.gz
                            tar zxvf openfst-1.5.4.tar.gz
                            cd openfst-1.5.4
                            ./configure --enable-python --enable-far --prefix """+getattr(sys, 'prefix')+"""/local/ && make clean && make install
                            """
                    else:
                        installpywrapfst = """
                            wget http://www.openfst.org/twiki/pub/FST/FstDownload/openfst-1.5.4.tar.gz

                            tar zxvf openfst-1.5.4.tar.gz