[r4080]: projects / ToolBox2 / trunk / toolbox2 / controllers / root.py History

Parent: [r4035] (diff)

Child: [r4081] (diff)

Download this file

root.py    73 lines (63 with data), 2.6 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""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