Package turbogears :: Package toolbox :: Module base

Source Code for Module turbogears.toolbox.base

  1  """Graphical user interface for managing TurboGears projects""" 
  2   
  3  import pkg_resources 
  4   
  5  import cherrypy 
  6   
  7  from turbogears import controllers, expose 
  8  from turbogears.util import get_project_name, setlike 
9 10 11 -class Info(controllers.Controller):
12 """TurboGears System Information. 13 14 Lists your TurboGears packages and version information. 15 16 """ 17 18 __label__ = 'System Info' 19 __version__ = '0.1' 20 __author__ = 'Ronald Jaramillo' 21 __email__ = 'ronald@checkandshare.com' 22 __copyright__ = 'Copyright 2005 Ronald Jaramillo' 23 __license__ = 'MIT' 24 25 icon = "/tg_static/images/info.png" 26 27 @expose('turbogears.toolbox.info')
28 - def index(self):
29 from turbogears.command.info import retrieve_info 30 packages, plugins = retrieve_info(with_links=True) 31 return dict(packages=packages, plugins=plugins)
32
33 34 -class WidgetBrowser(controllers.Controller):
35 """The widget browser. 36 37 Browse usage samples, description and source code for the available 38 TurboGears Widgets. 39 40 """ 41 42 __label__ = 'Widget Browser' 43 __version__ = '0.1' 44 __author__ = 'Kevin Dangoor' 45 __email__ = 'dangoor+turbogears@gmail.com' 46 __copyright__ = 'Copyright 2005 Kevin Dangoor' 47 __license__ = 'MIT' 48 49 all_descs = None 50 icon = '/tg_static/images/widgets.png' 51 52 @expose('turbogears.toolbox.widgets')
53 - def index(self, name=None):
54 from turbogears import widgets 55 from turbogears.widgets import js_location, Tabber, SyntaxHighlighter 56 all_descs = self.all_descs 57 if not all_descs: 58 widgets.load_widgets() 59 all_descs = dict() 60 for widgetdesc in widgets.all_widgets: 61 wd = widgetdesc() 62 all_descs[wd.full_class_name.replace('.', '_')] = wd 63 self.all_descs = all_descs 64 if name: 65 all_descs = {name: all_descs[name]} 66 desclist = sorted(all_descs.itervalues(), key=lambda x: x.name.lower()) 67 output = dict(descs=desclist, viewing_one=name != None) 68 if name: 69 # do not extend desclist! 70 desclist = desclist + [Tabber(), SyntaxHighlighter()] 71 72 css = setlike() 73 js = dict() 74 for location in js_location: 75 js[location] = setlike() 76 77 for widgetdesc in desclist: 78 if not name and widgetdesc.show_separately: 79 continue 80 css.add_all(widgetdesc.retrieve_css()) 81 for script in widgetdesc.retrieve_javascript(): 82 js[getattr(script, 'location', js_location.head)].add(script) 83 84 output['widget_css'] = css 85 for location in js: 86 output['widget_js_%s' % str(location)] = js[location] 87 88 return output
89
90 - def __getattr__(self, widgetname):
91 try: 92 return self.all_descs[widgetname] 93 except: 94 raise AttributeError(widgetname)
95
96 97 -class Toolbox(controllers.RootController):
98
99 - def __init__(self):
100 self.toolbox = self.get_tools()
101
102 - def tool_icon(self, tool):
103 icon = getattr(tool, 'icon', '') 104 if icon: 105 return icon
106
107 - def get_tools(self):
108 project_name = get_project_name() 109 toolbox = [] 110 for i in pkg_resources.iter_entry_points('turbogears.toolboxcommand'): 111 tool = i.load() 112 args = { 113 'path': i.name, 114 'label': getattr(tool, '__label__', i.name), 115 'description': getattr(tool, '__doc__', ''), 116 'version': getattr(tool, '__version__', ''), 117 'author': getattr(tool, '__author__', ''), 118 'email': getattr(tool, '__email__', ''), 119 'copyright': getattr(tool, '__copyright__', ''), 120 'license': getattr(tool, '__license__', ''), 121 'icon': self.tool_icon(tool), 122 'disabled': False} 123 if project_name or not getattr(tool, 'need_project', False): 124 try: 125 setattr(self, i.name, tool()) 126 except Exception, e: 127 args['description'] = str(e) or 'Tool could not be loaded.' 128 args['disabled'] = 'disabled' 129 else: 130 args['description'] += '\nNeeds project.' 131 args['disabled'] = 'disabled' 132 toolbox.append(args) 133 return toolbox
134
135 - def arrange_in_pairs(self, tools):
136 p = [[], []] 137 for idx, tool in enumerate(tools): 138 p[idx % 2].append(tool) 139 pairs = zip(p[0], p[1]) 140 if len(p[0]) > len(p[1]): 141 pairs.append([p[0][-1], None]) 142 if len(p[0]) < len(p[1]): 143 pairs.append([p[1][-1], None]) 144 return pairs
145 146 @expose('turbogears.toolbox.main')
147 - def index(self):
148 return dict(toolbox = self.arrange_in_pairs(self.toolbox), 149 project = get_project_name())
150 151 @expose()
152 - def noaccess(self):
153 return """<h3>No access for %s</h3> 154 <p> 155 By default only localhost (127.0.0.1) 156 has access to the Toolbox 157 </p> 158 <p> 159 You can provide access to your client by passing 160 your host address to Toolbox as a parameter. Ex: 161 </p> 162 <pre> 163 tg-admin toolbox -c %s 164 </pre> 165 """ % (cherrypy.request.remoteAddr,cherrypy.request.remoteAddr)
166