| [4216] | 1 | """ |
|---|
| 2 | collection of project templates |
|---|
| 3 | """ |
|---|
| 4 | |
|---|
| 5 | import ConfigParser |
|---|
| 6 | |
|---|
| [5957] | 7 | from martini.config import ConfigMunger |
|---|
| [4216] | 8 | from paste.script.templates import var |
|---|
| 9 | from traclegos.pastescript.string import PasteScriptStringTemplate |
|---|
| 10 | from traclegos.pastescript.var import vars2dict, dict2vars |
|---|
| 11 | from traclegos.project import project_dict |
|---|
| 12 | from traclegos.project import TracProject |
|---|
| 13 | |
|---|
| 14 | from StringIO import StringIO |
|---|
| 15 | |
|---|
| 16 | class ProjectTemplates(object): |
|---|
| 17 | """class handling a group of templates of mixed type""" |
|---|
| 18 | |
|---|
| 19 | def __init__(self, *templates): |
|---|
| 20 | self.templates = templates |
|---|
| 21 | self.resolve() |
|---|
| 22 | |
|---|
| [5187] | 23 | def append(self, *templates): |
|---|
| 24 | """append templates to the configuraton""" |
|---|
| 25 | self.templates += templates |
|---|
| 26 | if hasattr(self, '_configuration'): |
|---|
| 27 | del self._configuration |
|---|
| 28 | self.resolve() |
|---|
| 29 | |
|---|
| 30 | def __iadd__(self, templates): |
|---|
| 31 | self.append(*templates) |
|---|
| 32 | |
|---|
| [4216] | 33 | def resolve(self): |
|---|
| 34 | """determine pastescript templates and .ini files""" |
|---|
| 35 | project_types = project_dict() |
|---|
| 36 | inifiles = [] |
|---|
| 37 | pastescript_templates = [] |
|---|
| 38 | for template in self.templates: |
|---|
| 39 | proj = None |
|---|
| 40 | if isinstance(template, TracProject): |
|---|
| 41 | proj = template |
|---|
| 42 | if isinstance(template, basestring): |
|---|
| 43 | proj = project_types.get(template) |
|---|
| 44 | if proj: |
|---|
| 45 | inifile = proj.inifile() |
|---|
| 46 | if inifile: |
|---|
| 47 | inifiles.append(inifile) |
|---|
| 48 | pastescript_templates.append(proj) |
|---|
| 49 | else: |
|---|
| 50 | inifiles.append(template) |
|---|
| 51 | self.pastescript_templates = pastescript_templates |
|---|
| 52 | self.inifiles = inifiles |
|---|
| 53 | |
|---|
| 54 | def vars(self, vars=()): |
|---|
| 55 | """return a dictionary of PasteScript vars""" |
|---|
| 56 | optdict = {} |
|---|
| 57 | for template in self.pastescript_templates: |
|---|
| 58 | vars2dict(optdict, *template.vars) |
|---|
| 59 | vars2dict(optdict, *vars) |
|---|
| 60 | return optdict |
|---|
| 61 | |
|---|
| 62 | def options(self, vars=()): |
|---|
| 63 | """return all PasteScript vars including those from the template""" |
|---|
| 64 | optdict = dict((missed, var(missed, '')) for missed in self.missing()) |
|---|
| 65 | optdict.update(self.vars(vars)) |
|---|
| 66 | return optdict |
|---|
| 67 | |
|---|
| 68 | def configuration(self): |
|---|
| 69 | """returns string of uninterpolated configuration""" |
|---|
| 70 | |
|---|
| 71 | if not hasattr(self, '_configuration'): |
|---|
| 72 | # could just do this on __init__ (or resolve) |
|---|
| 73 | munger = ConfigMunger() |
|---|
| 74 | try: |
|---|
| 75 | munger.read(*self.inifiles) |
|---|
| 76 | except ConfigParser.MissingSectionHeaderError, e: |
|---|
| 77 | # this probably means a bad URI |
|---|
| 78 | raise IOError('Resource not found: %s' % e.line) |
|---|
| 79 | |
|---|
| 80 | config = StringIO() |
|---|
| 81 | munger.write(config) |
|---|
| 82 | self._configuration = config.getvalue() |
|---|
| 83 | |
|---|
| 84 | return self._configuration |
|---|
| 85 | |
|---|
| 86 | def missing(self, vars=None): |
|---|
| 87 | if vars is None: |
|---|
| 88 | vars = {} |
|---|
| 89 | if not hasattr(self, 'template'): |
|---|
| 90 | self.template = PasteScriptStringTemplate(self.configuration()) |
|---|
| 91 | return self.template.missing(vars) |
|---|
| 92 | |
|---|
| 93 | def interpolate_configuration(self, vars): |
|---|
| 94 | """ |
|---|
| 95 | return configuration (trac.ini) as a string |
|---|
| 96 | interpolated with the vars dictionary |
|---|
| 97 | """ |
|---|
| 98 | assert not self.missing(vars) |
|---|
| 99 | return self.template.substitute(**vars) |
|---|
| 100 | |
|---|
| 101 | def options_tuples(self, vars): |
|---|
| 102 | """return the tuples for the configuration""" |
|---|
| 103 | # create a new munger with the interpolated configuration |
|---|
| 104 | munger = ConfigMunger() |
|---|
| 105 | munger.read(self.interpolate_configuration(vars)) |
|---|
| 106 | return munger.tuples() |
|---|
| 107 | |
|---|