aboutsummaryrefslogtreecommitdiffstats
path: root/templatefile.py
blob: e08bc64208fbd1ef6e529d89ea0c4e50b7593ef1 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from string import Template
import os

class TemplateFile:
    """A template for creating a source file"""

    def __init__(self, path):
        # default values...
        # no name or file yet
        self.name = ''
        self.sections = {}
        self.file = None
        self.path = None
        # open a new file for each item
        self.file_per_item = False

        # empty footer if none specified:
        self.sections['footer'] = []

        # lines appended to body by default
        self.sections['body'] = []
        current = self.sections['body']

        self.template = open(path)
        for line in self.template:
            line = line.rstrip()
            if line.startswith('@'):
                if ' ' in line:
                    leading, trailing = line.split(' ', 1)
                else:
                    leading, trailing = line, None

                if leading == '@name':
                    if not trailing:
                        raise Exception("@name requires a file name.")
                    self.path = trailing
                    if '$' in self.path:
                        self.file_per_item = True
                else:
                    section = leading[1:]
                    if section not in self.sections:
                        self.sections[section] = []
                    current = self.sections[section]
            else:
                current.append(line)
        self.template.close()
        for section, data in self.sections.items():
            self.sections[section] = Template("\n".join(data))

        # You need a file if this isn't a file-per-item
        if not self.file_per_item:
            self.file = open(self.path, 'w')

    def close(self):
        """Close the associated file."""
        if self.file:
            self.file.close()
            self.file = None

    def __repr__(self):
        strings = []
        if self.file_per_item:
            strings.append("path: %s (per item)" % self.path)
        else:
            strings.append("path: %s" % self.path)
        for name, data in self.sections.items():
            strings.append("%s:" % name)
            strings.append(data.safe_substitute({}))
        return "\n".join(strings)

    def emit(self, template, item=None):
        """Emit a template, with optional interpolation of an item."""
        if self.file_per_item:
            if not item:
                return
            path = Template(self.path).safe_substitute(item)
            if os.path.exists(path):
                # print "We don't overwrite existing files."
                return
            self.file = open(path, 'w')
            if not self.file:
                print "Couldn't open '%s' (expanded from %s), " \
                      "not emitting '%s'." % \
                      (path, self.path, template)
                return

        if template == "copyright":
            # hey, at least it's not a global variable, amirite?
            self.file.write(TemplateFile.copyright)
        elif template in self.sections:
            templ = self.sections[template]
            self.file.write(templ.safe_substitute(item))
            self.file.write("\n")
        else:
            print "Warning: Unknown template '%s'." % template

        if self.file_per_item:
            self.file.close()
            self.file = None