[Checkins] SVN: zc.buildout/trunk/ Merged branch that extends
configuration syntax with support for -= and += operators.
Malthe Borch
mborch at gmail.com
Fri Jun 6 11:28:03 EDT 2008
Log message for revision 87201:
Merged branch that extends configuration syntax with support for -= and += operators.
Changed:
U zc.buildout/trunk/CHANGES.txt
U zc.buildout/trunk/src/zc/buildout/buildout.py
U zc.buildout/trunk/src/zc/buildout/buildout.txt
-=-
Modified: zc.buildout/trunk/CHANGES.txt
===================================================================
--- zc.buildout/trunk/CHANGES.txt 2008-06-06 15:24:57 UTC (rev 87200)
+++ zc.buildout/trunk/CHANGES.txt 2008-06-06 15:28:02 UTC (rev 87201)
@@ -7,6 +7,8 @@
1.0.4 (unreleased)
==================
+- Extended configuration syntax to allow -= and += operators (malthe).
+
1.0.3 (2008-06-01)
==================
Modified: zc.buildout/trunk/src/zc/buildout/buildout.py
===================================================================
--- zc.buildout/trunk/src/zc/buildout/buildout.py 2008-06-06 15:24:57 UTC (rev 87200)
+++ zc.buildout/trunk/src/zc/buildout/buildout.py 2008-06-06 15:28:02 UTC (rev 87201)
@@ -1146,10 +1146,25 @@
result.append(os.path.basename(location))
return result
+def _update_section(s1, s2):
+ for k, v in s2.items():
+ if k.endswith('+'):
+ key = k.rstrip(' +')
+ s2[key] = "\n".join(s1.get(key, "").split() + s2[k].split())
+ del s2[k]
+ elif k.endswith('-'):
+ key = k.rstrip(' -')
+ s2[key] = "\n".join([v for v in s1.get(key, "").split()
+ if v not in s2[k].split()])
+ del s2[k]
+
+ s1.update(s2)
+ return s1
+
def _update(d1, d2):
for section in d2:
if section in d1:
- d1[section].update(d2[section])
+ d1[section] = _update_section(d1[section], d2[section])
else:
d1[section] = d2[section]
return d1
Modified: zc.buildout/trunk/src/zc/buildout/buildout.txt
===================================================================
--- zc.buildout/trunk/src/zc/buildout/buildout.txt 2008-06-06 15:24:57 UTC (rev 87200)
+++ zc.buildout/trunk/src/zc/buildout/buildout.txt 2008-06-06 15:28:02 UTC (rev 87201)
@@ -688,6 +688,9 @@
- option values can use a substitution syntax, described below, to
refer to option values in specific sections.
+- option values can be appended or removed using the - and +
+ operators.
+
The ConfigParser syntax is very flexible. Section names can contain
any characters other than newlines and right square braces ("]").
Option names can contain any characters other than newlines, colons,
@@ -893,6 +896,137 @@
parts = data-dir debug
...
+
+Adding and removing options
+---------------------------
+
+We can append and remove values to an option by using the + and -
+operators.
+
+This is illustrated below; first we define a base configuration.
+
+ >>> write(sample_buildout, 'base.cfg',
+ ... """
+ ... [buildout]
+ ... parts = part1 part2 part3
+ ...
+ ... [part1]
+ ... recipe =
+ ... option = a1 a2
+ ...
+ ... [part2]
+ ... recipe =
+ ... option = b1 b2 b3 b4
+ ...
+ ... [part3]
+ ... recipe =
+ ... option = c1 c2
+ ...
+ ... """)
+
+Extending this configuration, we can "adjust" the values set in the
+base configuration file.
+
+ >>> write(sample_buildout, 'extension1.cfg',
+ ... """
+ ... [buildout]
+ ... extends = base.cfg
+ ...
+ ... # appending values
+ ... [part1]
+ ... option += a3 a4
+ ...
+ ... # removing values
+ ... [part2]
+ ... option -= b1 b2
+ ...
+ ... # alt. spelling
+ ... [part3]
+ ... option+=c3 c4 c5
+ ...
+ ... # normal assignment
+ ... [part4]
+ ... option = h1 h2
+ ...
+ ... """)
+
+An additional extension.
+
+ >>> write(sample_buildout, 'extension2.cfg',
+ ... """
+ ... [buildout]
+ ... extends = extension1.cfg
+ ...
+ ... # appending values
+ ... [part1]
+ ... option += a5
+ ...
+ ... # removing values
+ ... [part2]
+ ... option -= b1 b2 b3
+ ...
+ ... """)
+
+To verify that the options are adjusted correctly, we'll set up an
+extension that prints out the options.
+
+ >>> mkdir(sample_buildout, 'demo')
+ >>> write(sample_buildout, 'demo', 'demo.py',
+ ... """
+ ... def ext(buildout):
+ ... print [part['option'] for name, part in buildout.items() \
+ ... if name.startswith('part')]
+ ... """)
+
+ >>> write(sample_buildout, 'demo', 'setup.py',
+ ... """
+ ... from setuptools import setup
+ ...
+ ... setup(
+ ... name="demo",
+ ... entry_points={'zc.buildout.extension': ['ext = demo:ext']},
+ ... )
+ ... """)
+
+Set up a buildout configuration for this extension.
+
+ >>> write(sample_buildout, 'buildout.cfg',
+ ... """
+ ... [buildout]
+ ... develop = demo
+ ... parts =
+ ... """)
+
+ >>> os.chdir(sample_buildout)
+ >>> print system(os.path.join(sample_buildout, 'bin', 'buildout')),
+ Develop: '/sample-buildout/demo'
+ Uninstalling debug.
+ Getting distribution for 'recipes'.
+ zip_safe flag not set; analyzing archive contents...
+ Got recipes 0.0.0.
+ Uninstalling data-dir.
+ warning: install_lib: 'build/lib' does not exist -- no Python modules to install
+
+Verify option values.
+
+ >>> write(sample_buildout, 'buildout.cfg',
+ ... """
+ ... [buildout]
+ ... develop = demo
+ ... extensions = demo
+ ... extends = extension2.cfg
+ ... """)
+
+ >>> print system(os.path.join('bin', 'buildout')),
+ ['a1/na2/na3/na4/na5', 'b4', 'c1/nc2/nc3/nc4/nc5', 'h1 h2']
+ Develop: '/sample-buildout/demo'
+
+Cleanup.
+
+ >>> os.remove(os.path.join(sample_buildout, 'base.cfg'))
+ >>> os.remove(os.path.join(sample_buildout, 'extension1.cfg'))
+ >>> os.remove(os.path.join(sample_buildout, 'extension2.cfg'))
+
Multiple configuration files
----------------------------
@@ -928,8 +1062,6 @@
>>> print system(buildout),
Develop: '/sample-buildout/recipes'
- Uninstalling debug.
- Uninstalling data-dir.
Installing debug.
op buildout
recipe recipes:debug
@@ -1591,6 +1723,7 @@
d d1
d d2
d d3
+ d demo
d develop-eggs
d eggs
d parts
@@ -1670,6 +1803,7 @@
d d2
d data2-extra
d data3
+ d demo
d develop-eggs
d eggs
d parts
@@ -1746,6 +1880,7 @@
d data2
d data2-extra
d data3
+ d demo
d develop-eggs
d eggs
d parts
@@ -2126,6 +2261,7 @@
- base.cfg
d bin
- buildout.cfg
+ d demo
d develop-eggs
d eggs
- inst.cfg
@@ -2147,6 +2283,7 @@
- base.cfg
d bin
- buildout.cfg
+ d demo
d develop-eggs
d eggs
d parts
@@ -2170,6 +2307,7 @@
- base.cfg
d bin
- buildout.cfg
+ d demo
d develop-eggs
d eggs
d parts
More information about the Checkins
mailing list