1 | """ |
---|
2 | Local templates for the qplone3_theme |
---|
3 | """ |
---|
4 | import os |
---|
5 | from ConfigParser import SafeConfigParser |
---|
6 | |
---|
7 | from zopeskel.base import var |
---|
8 | from zopeskel.localcommands import ZopeSkelLocalTemplate |
---|
9 | |
---|
10 | from quintagroup.themetemplate import getEggInfo |
---|
11 | from quintagroup.themetemplate import getThemeVarsFP |
---|
12 | |
---|
13 | class QThemeSubTemplate(ZopeSkelLocalTemplate): |
---|
14 | use_cheetah = True |
---|
15 | parent_templates = ['qplone3_theme'] |
---|
16 | |
---|
17 | # Flag for use template composition |
---|
18 | compose = None |
---|
19 | compodir_pref = "_compo" |
---|
20 | # list of 2 item tuple - |
---|
21 | # (compotemplate_name, compo marker), for ex.: |
---|
22 | compo_template_markers = [] |
---|
23 | # list vars names to add in theme_vars egg_info file |
---|
24 | shared_vars = [] |
---|
25 | |
---|
26 | def template_dir(self): |
---|
27 | if self.compose: |
---|
28 | # Prepare |
---|
29 | self._template_dir = os.path.join( \ |
---|
30 | self._template_dir + self.compodir_pref, \ |
---|
31 | self.compose ) |
---|
32 | |
---|
33 | return super(QThemeSubTemplate, self).template_dir() |
---|
34 | |
---|
35 | def pre(self, command, output_dir, vars): |
---|
36 | """ Get all previous template vars |
---|
37 | """ |
---|
38 | for k, v in self.get_template_vars(output_dir, vars).items(): |
---|
39 | if not k in vars.keys(): |
---|
40 | vars[k] = v |
---|
41 | super(QThemeSubTemplate, self).pre(command, output_dir, vars) |
---|
42 | |
---|
43 | def post(self, command, output_dir, vars): |
---|
44 | """ Call write_files function for every subtemplate, |
---|
45 | - change marker name for every subtemplate, |
---|
46 | - set compose prop for change subtemplate path calculation |
---|
47 | """ |
---|
48 | if self.compo_template_markers: |
---|
49 | for cname, cmarker in self.compo_template_markers: |
---|
50 | original_template_dir = self._template_dir |
---|
51 | self.compose = cname |
---|
52 | self.marker_name = cmarker |
---|
53 | self.write_files(command, output_dir, vars) |
---|
54 | self._template_dir = original_template_dir |
---|
55 | |
---|
56 | self.add_template_vars(output_dir, vars) |
---|
57 | super(QThemeSubTemplate, self).post(command, output_dir, vars) |
---|
58 | |
---|
59 | def get_template_vars(self, output_dir, vars): |
---|
60 | |
---|
61 | res = {} |
---|
62 | egg_info = getEggInfo(output_dir) |
---|
63 | theme_vars_fp = getThemeVarsFP(egg_info) |
---|
64 | |
---|
65 | if os.path.exists(theme_vars_fp): |
---|
66 | config = SafeConfigParser() |
---|
67 | config.read(theme_vars_fp) |
---|
68 | |
---|
69 | for section in config.sections(): |
---|
70 | for option in config.options(section): |
---|
71 | key = section + '_' + option |
---|
72 | val = config.get(section, option) |
---|
73 | if section == 'multiple_templates': |
---|
74 | val = val.split(',') |
---|
75 | res[key] = val |
---|
76 | |
---|
77 | return res |
---|
78 | |
---|
79 | def add_template_vars(self, output_dir, vars): |
---|
80 | |
---|
81 | egg_info = getEggInfo(output_dir) |
---|
82 | theme_vars_fp = getThemeVarsFP(egg_info) |
---|
83 | |
---|
84 | if os.path.exists(theme_vars_fp): |
---|
85 | config = SafeConfigParser() |
---|
86 | config.read(theme_vars_fp) |
---|
87 | |
---|
88 | # Update qplone3_theme used_subtemplate option |
---|
89 | sec, opt = 'qplone3_theme', 'used_subtemplates' |
---|
90 | val = filter(None,[st.strip() \ |
---|
91 | for st in config.get(sec,opt).split(',')]) |
---|
92 | val.append(self.name) |
---|
93 | config.set(sec, opt, ','.join(set(val))) |
---|
94 | |
---|
95 | # Add subtemplate vars |
---|
96 | if self.shared_vars: |
---|
97 | thesection = self.name |
---|
98 | if config.has_section(thesection): |
---|
99 | msection = 'multiple_templates' |
---|
100 | moption = self.name |
---|
101 | |
---|
102 | if not config.has_section(msection): |
---|
103 | config.add_section(msection) |
---|
104 | |
---|
105 | val = [] |
---|
106 | if config.has_option(msection, moption): |
---|
107 | val = config.get(msection, moption).split(',') |
---|
108 | else: |
---|
109 | val.append(moption) |
---|
110 | thesection = "%s_%d"%(moption,len(val)) |
---|
111 | val.append(thesection) |
---|
112 | |
---|
113 | config.set(msection, moption, ','.join(val)) |
---|
114 | |
---|
115 | config.add_section(thesection) |
---|
116 | for k in self.shared_vars: |
---|
117 | config.set(thesection, k, vars[k]) |
---|
118 | |
---|
119 | # Save theme_vars.txt file |
---|
120 | theme_file = file(theme_vars_fp,'w') |
---|
121 | config.write(theme_file) |
---|
122 | theme_file.close() |
---|