| 1 | #!/usr/bin/env python |
|---|
| 2 | """ |
|---|
| 3 | definition of a TracLegos project template |
|---|
| 4 | """ |
|---|
| 5 | |
|---|
| 6 | import os |
|---|
| 7 | import pkg_resources |
|---|
| 8 | import subprocess |
|---|
| 9 | from paste.script import templates |
|---|
| 10 | from traclegos.db import available_databases |
|---|
| 11 | |
|---|
| 12 | class TracProject(templates.Template): |
|---|
| 13 | """a trac project""" |
|---|
| 14 | # eventually this class should do most if not all of what TracLegos |
|---|
| 15 | # does and its functionality should be factored to that end |
|---|
| 16 | # methodologies for doing so: |
|---|
| 17 | |
|---|
| 18 | # * container: self.project_creator = TracLegos(...): this method favors |
|---|
| 19 | # keeping TracLegos as the canonical project factory which is called via |
|---|
| 20 | # the command line and via `paster create` or TTW |
|---|
| 21 | |
|---|
| 22 | # * deprecator: the functionalities of TracLegos are mostly (if not |
|---|
| 23 | # entirely) moved into this class and TracLegos becomes a front-end for |
|---|
| 24 | # the TracProject PasteScript template (assuming it needs to exist) |
|---|
| 25 | |
|---|
| 26 | # practicalities going forward should determine which of these |
|---|
| 27 | # methodologies is favored |
|---|
| 28 | |
|---|
| 29 | ### class defaults |
|---|
| 30 | |
|---|
| 31 | # Trac permissions for the template |
|---|
| 32 | permissions = {} |
|---|
| 33 | |
|---|
| 34 | # database to be used |
|---|
| 35 | db = None |
|---|
| 36 | |
|---|
| 37 | # wiki directory to import from |
|---|
| 38 | _wiki_dir = None |
|---|
| 39 | |
|---|
| 40 | ### attrs for PasteScript Template |
|---|
| 41 | |
|---|
| 42 | def pre(self, command, output_dir, vars): |
|---|
| 43 | pass |
|---|
| 44 | |
|---|
| 45 | def post(self, command, output_dir, vars): |
|---|
| 46 | pass |
|---|
| 47 | |
|---|
| 48 | ### internal methods |
|---|
| 49 | |
|---|
| 50 | def inifile(self): |
|---|
| 51 | """ |
|---|
| 52 | returns the path to the trac.ini template associated with this TracProject |
|---|
| 53 | (if any) |
|---|
| 54 | """ |
|---|
| 55 | files = [ 'trac.ini_tmpl', 'trac.ini' ] |
|---|
| 56 | for f in files: |
|---|
| 57 | filename = os.path.join(self.template_dir(), 'conf', f) |
|---|
| 58 | if os.path.exists(filename): |
|---|
| 59 | return filename |
|---|
| 60 | |
|---|
| 61 | def database(self): |
|---|
| 62 | if self.db is None: |
|---|
| 63 | return self.db |
|---|
| 64 | if isinstance(self.db, basestring): |
|---|
| 65 | return available_databases()[self.db] |
|---|
| 66 | return self.db() |
|---|
| 67 | |
|---|
| 68 | def wiki_dir(self): |
|---|
| 69 | if self._wiki_dir is None: |
|---|
| 70 | return None |
|---|
| 71 | return os.path.join(self.module_dir(), self._wiki_dir) |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | def projects(): |
|---|
| 75 | """return TracProject templates installed as eggs""" |
|---|
| 76 | templates = [] |
|---|
| 77 | for entry_point in pkg_resources.iter_entry_points('paste.paster_create_template'): |
|---|
| 78 | try: |
|---|
| 79 | template = entry_point.load() |
|---|
| 80 | except: |
|---|
| 81 | continue |
|---|
| 82 | if issubclass(template, TracProject): |
|---|
| 83 | templates.append(template(entry_point.name)) |
|---|
| 84 | return templates |
|---|
| 85 | |
|---|
| 86 | def project_dict(): |
|---|
| 87 | return dict((template.name, template) for template in projects()) |
|---|
| 88 | |
|---|
| 89 | if __name__ == '__main__': |
|---|
| 90 | from traclegos.project import TracProject |
|---|
| 91 | templates = project_dict() |
|---|
| 92 | for name, template in templates.items(): |
|---|
| 93 | print name, template |
|---|
| 94 | |
|---|
| 95 | template.inifile() |
|---|