Changeset 1281

Show
Ignore:
Timestamp:
09/17/08 13:07:50
Author:
mylan
Message:

Implement saving template vars in generated theme-product egg-info dir

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • qtheme.template/trunk/qthemetemplate/__init__.py

    r1176 r1281  
    11# 
     2import os,types 
     3from StringIO import StringIO 
     4from ConfigParser import SafeConfigParser 
     5 
    26from qthemetemplate.qplone3_theme import qPlone3Theme 
     7 
     8def write_map(cmd, basename, filename, force=False): 
     9    argname = os.path.splitext(basename)[0] 
     10    value = getattr(cmd.distribution, argname, None) 
     11 
     12    if value: 
     13        config = SafeConfigParser() 
     14        config.add_section('qplone3_theme') 
     15        for name, val in value.items(): 
     16            val = val and str(val) or '' 
     17            config.set('qplone3_theme', name, val) 
     18 
     19        strvalue = StringIO() 
     20        config.write(strvalue) 
     21        value = strvalue.getvalue() 
     22 
     23    cmd.write_or_delete_file(argname, filename, value, force) 
     24 
     25 
     26def assert_dict(dist, attr, value): 
     27    """Verify that value is a dict or None""" 
     28    try: 
     29        assert type(value) == types.DictType 
     30        print 'success assert' 
     31    except (TypeError,ValueError,AttributeError,AssertionError): 
     32        raise DistutilsSetupError( 
     33            "%r must be a dict (got %r)" % (attr,value) 
     34        ) 
  • qtheme.template/trunk/qthemetemplate/localcommands/__init__.py

    r1251 r1281  
    33""" 
    44import os 
     5from ConfigParser import SafeConfigParser 
     6from paste.script import pluginlib 
     7 
    58from zopeskel.base import var 
    69from zopeskel.localcommands import ZopeSkelLocalTemplate 
     
    1619    # (compotemplate_name, compo marker), for ex.: 
    1720    compo_template_markers = [] 
     21    # list vars names to add in theme_vars egg_info file 
     22    shared_vars = [] 
    1823 
    1924    def template_dir(self): 
     
    2631        return super(QThemeSubTemplate, self).template_dir() 
    2732 
     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 
    2841    def post(self, command, output_dir, vars): 
    2942        """ Call write_files function for every subtemplate, 
     
    3144             - set compose prop for change subtemplate path calculation 
    3245        """ 
    33  
    3446        if self.compo_template_markers: 
    3547            for cname, cmarker in self.compo_template_markers: 
     
    4052                self._template_dir = original_template_dir 
    4153 
     54        self.add_template_vars(output_dir, vars) 
    4255        super(QThemeSubTemplate, self).post(command, output_dir, vars) 
    4356 
     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        if not self.shared_vars: 
     80            return 
     81 
     82        egg_info = pluginlib.find_egg_info_dir(output_dir) 
     83        theme_vars_fp = os.path.join(egg_info, 'theme_vars.txt') 
     84 
     85        if egg_info and os.path.exists(theme_vars_fp): 
     86            config = SafeConfigParser() 
     87            config.read(theme_vars_fp) 
     88 
     89            thesection = self.name 
     90            if config.has_section(thesection): 
     91                msection = 'multiple_templates' 
     92                moption = self.name 
     93 
     94                if not config.has_section(msection): 
     95                    config.add_section(msection) 
     96 
     97                val = [] 
     98                if config.has_option(msection, moption): 
     99                    val = config.get(msection, moption).split(',') 
     100                else: 
     101                    val.append(moption) 
     102                thesection = "%s_%d"%(moption,len(val)) 
     103                val.append(thesection) 
     104 
     105                config.set(msection, moption, ','.join(val)) 
     106 
     107            config.add_section(thesection) 
     108            for k in self.shared_vars: 
     109                config.set(thesection, k, vars[k]) 
     110 
     111            theme_file = file(theme_vars_fp,'w') 
     112            config.write(theme_file) 
     113            theme_file.close() 
  • qtheme.template/trunk/qthemetemplate/templates/qplone3_theme/setup.py_tmpl

    r1245 r1281  
    4848      [distutils.setup_keywords] 
    4949      paster_plugins = setuptools.dist:assert_string_list 
     50      theme_vars = qthemetemplate:assert_dict 
    5051 
    5152      [egg_info.writers] 
    5253      paster_plugins.txt = setuptools.command.egg_info:write_arg 
     54      theme_vars.txt = qthemetemplate:write_map 
     55 
    5356      """, 
    5457      paster_plugins = ["ZopeSkel", "PasteScript"], 
    55       setup_requires = ["setuptools", "ZopeSkel", "PasteScript"], 
     58      setup_requires = ["setuptools", "ZopeSkel", "PasteScript", "qtheme.template"], 
     59      theme_vars = {'skinname': '$skinname', 
     60          'skinbase'          : '$skinbase', 
     61          'namespace_package' : '$namespace_package', 
     62          'namespace_package2': '$namespace_package2', 
     63          'package'           : '$package', 
     64           
     65      }, 
    5666      ) 
  • qtheme.template/trunk/setup.py

    r1259 r1281  
    3333          viewlet_hidden = qthemetemplate.localcommands.subtemplates:ViewletHiddenSubTemplate 
    3434 
     35          [distutils.setup_keywords] 
     36          theme_vars = qthemetemplate:assert_dict 
     37 
     38          [egg_info.writers] 
     39          theme_vars.txt = qthemetemplate:write_map 
     40 
    3541      # -*- Entry points: -*- 
    3642      """,