"""Main Controller"""
from toolbox2.lib.base import BaseController
from tg import expose, flash, redirect
from pylons.i18n import ugettext as _
import pkg_resources
import os
from tg.util import get_project_name
class RootController(BaseController):
"""ToolBox2 main class"""
@expose('genshi:toolbox2.templates.index')
def index(self):
"""provide gadgets information"""
#get toolbox
self.toolbox = self.get_toolbox()
#detect project
project = get_project_name()
if not project:
flash(_("Note: Some functions are not available as the toolbox was not started in a valid TurboGears2 project directory."), 'status_warning')
return dict(toolbox = self.toolbox, project=project)
def get_tools(self):
tools = []
for entrypoints in pkg_resources.iter_entry_points("turbogears2.toolboxcommand"):
tool = entrypoints.load()
tools.append((tool,entrypoints.name))
setattr(self, entrypoints.name, tool())
return tools
def get_toolbox(self):
"""search "turbogears2.toolboxcommand" entrypoint to find gadgets"""
tools = self.get_tools()
toolbox = []
for tool, name in tools:
args = {
'path': name,
'label': getattr(tool,'__label__',tool),
'description': self.tool_discriotioin(tool),
'version': getattr(tool,'__version__',''),
'author': getattr(tool,'__author__',''),
'email': getattr(tool,'__email__',''),
'copyright': getattr(tool,'__copyright__',''),
'license': getattr(tool,'__license__',''),
'group': getattr(tool,'__group__',''),
'icon': self.tool_icon(tool),
'disabled': self.tool_disabled(tool)
}
toolbox.append(args)
return toolbox
def tool_discriotioin(self, tool):
"""format descriptions"""
description = getattr(tool,'__doc__','')
if description:
return description.replace('\n', '<br/>')
def tool_icon(self,tool):
"""set icon"""
icon = getattr(tool,'__icon__','')
if icon: return icon
def tool_disabled(self, tool):
"""set project"""
if getattr(tool, 'need_project', False)==False:
return False
if getattr(tool, 'need_project', False)==True and not get_project_name():
return True
else:
return False