| 1 | #!/usr/bin/python |
|---|
| 2 | import os |
|---|
| 3 | import ConfigParser |
|---|
| 4 | |
|---|
| 5 | import sys |
|---|
| 6 | sys.stdout = sys.stderr |
|---|
| 7 | |
|---|
| 8 | # From Patrick Logan at http://patricklogan.blogspot.com/2004/01/python-and-groupby.html |
|---|
| 9 | class groupby(dict): |
|---|
| 10 | def __init__(self, seq, key=lambda x:x): |
|---|
| 11 | for value in seq: |
|---|
| 12 | k = key(value) |
|---|
| 13 | self.setdefault(k, []).append(value) |
|---|
| 14 | __iter__ = dict.iteritems |
|---|
| 15 | |
|---|
| 16 | def application(environ, start_response): |
|---|
| 17 | """This is a fast index page generator that just reads the workspace trac.ini |
|---|
| 18 | files rather than creating environments.""" |
|---|
| 19 | |
|---|
| 20 | # environ.get('wsgi.errors').write('Environ is %s' % environ) |
|---|
| 21 | |
|---|
| 22 | # no error checking. just get this right. |
|---|
| 23 | index_template_file = environ.get('trac.env_index_template') |
|---|
| 24 | env_parent_dir = environ.get('trac.env_parent_dir') |
|---|
| 25 | trac_base_uri = '/' + (environ.get('trac.base_uri',environ.get('REQUEST_URI'))).strip('/') |
|---|
| 26 | |
|---|
| 27 | # setup defaults to prevent configuration not found |
|---|
| 28 | default_vals = {'name': "Project Name Missing", |
|---|
| 29 | 'descr': "", |
|---|
| 30 | 'category': 'client', } |
|---|
| 31 | |
|---|
| 32 | paths = os.listdir(env_parent_dir)[:] |
|---|
| 33 | |
|---|
| 34 | projects = [] |
|---|
| 35 | categoryDescriptions = {'client':'Active Client Projects', |
|---|
| 36 | 'internal':'Internal Projects', |
|---|
| 37 | 'archive':'Completed Projects', } |
|---|
| 38 | for project in paths: |
|---|
| 39 | project_conf=os.path.join(env_parent_dir, project,'conf','trac.ini') |
|---|
| 40 | if os.path.isfile(project_conf): |
|---|
| 41 | try: |
|---|
| 42 | config = ConfigParser.ConfigParser(default_vals) |
|---|
| 43 | config.read(project_conf) |
|---|
| 44 | proj = { 'name': config.get("project","name"), |
|---|
| 45 | 'description': config.get("project","descr"), |
|---|
| 46 | 'category': config.get('project', 'category'), |
|---|
| 47 | 'href': trac_base_uri + '/' + project, |
|---|
| 48 | } |
|---|
| 49 | if proj['category']!='hidden': |
|---|
| 50 | proj['category']=categoryDescriptions[proj['category']] |
|---|
| 51 | projects.append(proj) |
|---|
| 52 | #ignore exceptions |
|---|
| 53 | except Exception, e: |
|---|
| 54 | pass |
|---|
| 55 | |
|---|
| 56 | # sort by name then group by category |
|---|
| 57 | projects.sort(lambda x, y: cmp(x['category'].lower()+x['name'].lower(),y['category'].lower()+y['name'].lower())) |
|---|
| 58 | groupedProjects = groupby(projects, lambda x:x['category']) |
|---|
| 59 | |
|---|
| 60 | if (environ.get('REQUEST_METHOD') == 'GET'): |
|---|
| 61 | from genshi.template import TemplateLoader |
|---|
| 62 | loader = TemplateLoader() |
|---|
| 63 | tmpl = loader.load(index_template_file) |
|---|
| 64 | stream = tmpl.generate(projectlist=groupedProjects) |
|---|
| 65 | start_response('200 OK',[('Content-type','text/html')]) |
|---|
| 66 | return [stream.render('xhtml')] |
|---|
| 67 | else: |
|---|
| 68 | import xmlrpclib |
|---|
| 69 | response = xmlrpclib.dumps((projects,), methodresponse=True) |
|---|
| 70 | start_response('200 OK',[('Content-type','text/xml')]) |
|---|
| 71 | return [response] |
|---|