source: products/quintagroup.themetemplate/trunk/quintagroup/themetemplate/localcommands/__init__.py @ 1005

Last change on this file since 1005 was 1005, checked in by mylan, 15 years ago

Import package: Copied http://svn.quintagroup.com/products/qtheme.template/trunk@2041 + modifications concerning changed package name, namespace

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