Python

Beware of uppercase letters in your config files

We got a surprise using a [theme:parameters] variable in the manifest.cfg of one of our Diazo theme.

We were defining a parameter like that :

isFrontPage = context/@@isFrontPage

and then we were using it in the rules :

<drop css:content="#footer-sitemap" if="$isFrontPage" />

But we got an error after having the Theme installed. And we found that the parameter we got in Theme control panel was “isfrontpage” and not “isFrontPage” so the “isFrontPage” parameter used in rules was undefined !

This is happening because plone.app.theming (as plone.resource, Products.GenericSetup, …) is using python’s ConfigParser to parse the manifest.cfg file (“from ConfigParser import ConfigParser“) and it does a lower() on the sections and variables that you put in your config files.

This is the same for RawConfigParser and SafeConfigParser.

So … don’t ever use camelcase there ;-)

 

Percent in python’s string formatting

In python, we don’t need to escape the char ‘%’ in a string, except when we want to format the specific string. Examples:

>>> 'Here is a percent: %'
'Here is a percent: %'

>>> 'Here is two percent %%'
'Here is two percent %%'

>>> 'Here is a percent %% %s' % 'in formatted string'
'Here is a percent % in formatted string'

>>> 'We have to double the percent % %s!' % 'in formatted string'
Traceback (most recent call last):
  File "", line 1, in 
TypeError: not all arguments converted during string formatting

>>> 'If we do not specify a format, we must not double the percent: %(foo)s %(bar)s!'
'If we do not specify a format, we must not double the percent: %(foo)s %(bar)s!'

>>> 'If we want to format specifically, we must double the percent: %(foo)s %%(bar)s!' % {'foo': 'test'}
'If we want to format specifically, we must double the percent: test %(bar)s!'

>>> 'And of course this will crash: %(foo)s %(bar)s!' % {'foo': 'test'}
Traceback (most recent call last):
  File "", line 1, in 
KeyError: 'bar'