# Path: lightbulb/cli/show.py
# class LibraryModules(Lister):
#     "Shows available library modules"
# 
#     log = logging.getLogger(__name__)
# 
#     def get_parser(self, prog_name):
#         """
#         Argument parser
#         Args:
#             prog_name (object): The current program
#         Returns:
#             object: A parser for the provided arguments
#         """
#         parser = super(LibraryModules, self).get_parser(prog_name)
#         parser.add_argument('folder', nargs='?', default='.')
#         return parser
# 
#     def take_action(self, parsed_args):
#         """
#         Returns available library modules
#         Args:
#             None
#         Returns:
#             tuple: Available library modules
#         """
#         folder = parsed_args.folder
#         if len(folder) > 0 and folder[0] == '/':
#             folder = folder[1:]
#         if len(folder) > 1 and folder[0:2] == './':
#             folder = folder[2:]
# 
#         path = imp.find_module('lightbulb')[1]+'/data/' + folder
#         if folder == "my_saved_models":
#             path = expanduser("~")+"/.LightBulb/models/"
#         if folder == "my_saved_regex":
#             path = expanduser("~")+"/.LightBulb/regex/"
#         if folder == "my_saved_trees":
#             path = expanduser("~")+"/.LightBulb/trees/"
#         if folder == "my_saved_grammars":
#             path = expanduser("~")+"/.LightBulb/grammars/"
#         data = []
#         # First check if module is in CORE modules
#         sys.path.insert(1, os.path.join(sys.path[0], path))
#         module_names = [name[:-3] for name in os.listdir(path)
#                         if name.endswith(".py") and name != "__init__.py"]
# 
#         for module_name in module_names:
#             meta = importmodule(module_name).META
#             data.append((module_name, meta['description'], meta['type']))
#         if folder == ".":
#             data.append(("my_saved_models", "My saved models", "Folder"))
#             data.append(("my_saved_regex", "My saved regex", "Folder"))
#             data.append(("my_saved_trees", "My saved trees", "Folder"))
#             data.append(("my_saved_grammars", "My saved grammars", "Folder"))
# 
#         return (('Name', 'Value', 'Type'), (data))
# 
# class LibraryCat(Lister):
#     """
#     Returns the content of a library module
#     """
# 
#     log = logging.getLogger(__name__)
# 
#     def get_parser(self, prog_name):
#         """
#         Argument parser
#         Args:
#             prog_name (object): The current program
#         Returns:
#             object: A parser for the provided arguments
#         """
#         parser = super(LibraryCat, self).get_parser(prog_name)
#         parser.add_argument('component', nargs='?', default='.')
#         return parser
# 
#     def take_action(self, parsed_args):
#         """
#         Returns the content of a library module
#         Args:
#             None
#         Returns:
#             tuple: Library module content
#         """
#         component = parsed_args.component
# 
#         component_splitted = component.split('/')
#         module_name = component_splitted[-1]
#         for component in component_splitted:
#             if component == '.':
#                 component_splitted.remove(component)
#         folder = "/".join(component_splitted[:-1])
#         path = imp.find_module('lightbulb')[1]+'/data/'+ folder
#         if folder == "my_saved_models":
#             path = expanduser("~")+"/.LightBulb/models/"
#         if folder == "my_saved_regex":
#             path = expanduser("~")+"/.LightBulb/regex/"
#         if folder == "my_saved_trees":
#             path = expanduser("~")+"/.LightBulb/trees/"
#         if folder == "my_saved_grammars":
#             path = expanduser("~")+"/.LightBulb/grammars/"
# 
#         # First check if module is in CORE modules
#         sys.path.insert(1, os.path.join(sys.path[0], path))
#         meta = importmodule(module_name).META
# 
#         self.app.stdout.write('\n\nComponent ' + module_name + ' Information:\n')
# 
#         content = ""
#         if meta['type'] == 'Grammar' or meta['type'] == 'Regex':
#             with open(path +"/"+ module_name + '.y') as grammaregex_file:
#                 content = grammaregex_file.read()
#         elif meta['type'] == 'Configuration' or meta['type'] == 'Tree':
#             with open(path +"/"+ module_name + '.json') as conf_file:
#                 content = conf_file.read()
#         else:
#             content = 'This is a folder and the command cannot be used.'
#         return (('Name', 'Value'), ([(module_name, content)]))


from cliff.app import App
from cliff.commandmanager import CommandManager
from lightbulb.cli.show import LibraryModules, LibraryCat
        super(LightBulb, self).__init__(
            description='LightBulb App',
            version='0.1',
            command_manager=CommandManager('cliff.lightbulb'),
            deferred_help=False,
            )
        self.library = LibraryModules(self,self,None)
        self.libraryCat = LibraryCat(self,self,None)

    def readlibrary(self, folder):
        try:
            target = parsed_args()
            target.folder = folder
            details = self.library.take_action(target)[1]
            if len(details) > 0:
                for x in details:
                    if x:
                        print x
                        if x[2] == 'Folder':
                            self.readlibrary(folder + "/" + x[0])
                        else:
                            if x[2] != 'FST':
                                print "Reading ", folder + "/" + x[0]
                                self.readlibraryfile(folder + "/" + x[0])
        except:
            pass

    def readlibraryfile(self, requestedfile):
        target = parsed_args()
        target.component = requestedfile

# Path: lightbulb/cli/show.py
# class LibraryModules(Lister):
#     "Shows available library modules"
# 
#     log = logging.getLogger(__name__)
# 
#     def get_parser(self, prog_name):
#         """
#         Argument parser
#         Args:
#             prog_name (object): The current program
#         Returns:
#             object: A parser for the provided arguments
#         """
#         parser = super(LibraryModules, self).get_parser(prog_name)
#         parser.add_argument('folder', nargs='?', default='.')
#         return parser
# 
#     def take_action(self, parsed_args):
#         """
#         Returns available library modules
#         Args:
#             None
#         Returns:
#             tuple: Available library modules
#         """
#         folder = parsed_args.folder
#         if len(folder) > 0 and folder[0] == '/':
#             folder = folder[1:]
#         if len(folder) > 1 and folder[0:2] == './':
#             folder = folder[2:]
# 
#         path = imp.find_module('lightbulb')[1]+'/data/' + folder
#         if folder == "my_saved_models":
#             path = expanduser("~")+"/.LightBulb/models/"
#         if folder == "my_saved_regex":
#             path = expanduser("~")+"/.LightBulb/regex/"
#         if folder == "my_saved_trees":
#             path = expanduser("~")+"/.LightBulb/trees/"
#         if folder == "my_saved_grammars":
#             path = expanduser("~")+"/.LightBulb/grammars/"
#         data = []
#         # First check if module is in CORE modules
#         sys.path.insert(1, os.path.join(sys.path[0], path))
#         module_names = [name[:-3] for name in os.listdir(path)
#                         if name.endswith(".py") and name != "__init__.py"]
# 
#         for module_name in module_names:
#             meta = importmodule(module_name).META
#             data.append((module_name, meta['description'], meta['type']))
#         if folder == ".":
#             data.append(("my_saved_models", "My saved models", "Folder"))
#             data.append(("my_saved_regex", "My saved regex", "Folder"))
#             data.append(("my_saved_trees", "My saved trees", "Folder"))
#             data.append(("my_saved_grammars", "My saved grammars", "Folder"))
# 
#         return (('Name', 'Value', 'Type'), (data))
# 
# class LibraryCat(Lister):
#     """
#     Returns the content of a library module
#     """
# 
#     log = logging.getLogger(__name__)
# 
#     def get_parser(self, prog_name):
#         """
#         Argument parser
#         Args:
#             prog_name (object): The current program
#         Returns:
#             object: A parser for the provided arguments
#         """
#         parser = super(LibraryCat, self).get_parser(prog_name)
#         parser.add_argument('component', nargs='?', default='.')
#         return parser
# 
#     def take_action(self, parsed_args):
#         """
#         Returns the content of a library module
#         Args:
#             None
#         Returns:
#             tuple: Library module content
#         """
#         component = parsed_args.component
# 
#         component_splitted = component.split('/')
#         module_name = component_splitted[-1]
#         for component in component_splitted:
#             if component == '.':
#                 component_splitted.remove(component)
#         folder = "/".join(component_splitted[:-1])
#         path = imp.find_module('lightbulb')[1]+'/data/'+ folder
#         if folder == "my_saved_models":
#             path = expanduser("~")+"/.LightBulb/models/"
#         if folder == "my_saved_regex":
#             path = expanduser("~")+"/.LightBulb/regex/"
#         if folder == "my_saved_trees":
#             path = expanduser("~")+"/.LightBulb/trees/"
#         if folder == "my_saved_grammars":
#             path = expanduser("~")+"/.LightBulb/grammars/"
# 
#         # First check if module is in CORE modules
#         sys.path.insert(1, os.path.join(sys.path[0], path))
#         meta = importmodule(module_name).META
# 
#         self.app.stdout.write('\n\nComponent ' + module_name + ' Information:\n')
# 
#         content = ""
#         if meta['type'] == 'Grammar' or meta['type'] == 'Regex':
#             with open(path +"/"+ module_name + '.y') as grammaregex_file:
#                 content = grammaregex_file.read()
#         elif meta['type'] == 'Configuration' or meta['type'] == 'Tree':
#             with open(path +"/"+ module_name + '.json') as conf_file:
#                 content = conf_file.read()
#         else:
#             content = 'This is a folder and the command cannot be used.'
#         return (('Name', 'Value'), ([(module_name, content)]))


# Path: examples/test_cliff_api_example_1.py
from cliff.app import App
from cliff.commandmanager import CommandManager
from lightbulb.cli.show import LibraryModules, LibraryCat

        super(LightBulb, self).__init__(
            description='LightBulb App',
            version='0.1',
            command_manager=CommandManager('cliff.lightbulb'),
            deferred_help=False,
            )
        self.library = LibraryModules(self,self,None)
        self.libraryCat = LibraryCat(self,self,None)

    def readlibrary(self, folder):
        try:
            target = parsed_args()
            target.folder = folder
            details = self.library.take_action(target)[1]
            if len(details) > 0:
                for x in details:
                    if x:
                        print x
                        if x[2] == 'Folder':
                            self.readlibrary(folder + "/" + x[0])
                        else:
                            if x[2] != 'FST':
                                print "Reading ", folder + "/" + x[0]
                                self.readlibraryfile(folder + "/" + x[0])
        except:
            pass

    def readlibraryfile(self, requestedfile):
        target = parsed_args()
        target.component = requestedfile

        details = self.libraryCat.take_action(target)[1]