[Checkins]
SVN: zc.buildout/branches/plus-minus-option-syntax-for-review/
A patch contributed by Mustapha Benali that provide - and +
prefix operators for buildout configuration option values;
this allow adding and removing option values to an already existing
section option (applicable when extending configuration
files). See also:
http://answers.edge.launchpad.net/zc.buildout/+question/35159.
Malthe Borch
mborch at gmail.com
Thu Jun 5 13:48:08 EDT 2008
Log message for revision 87171:
A patch contributed by Mustapha Benali that provide - and + prefix operators for buildout configuration option values; this allow adding and removing option values to an already existing section option (applicable when extending configuration files). See also: http://answers.edge.launchpad.net/zc.buildout/+question/35159.
Changed:
A zc.buildout/branches/plus-minus-option-syntax-for-review/
U zc.buildout/branches/plus-minus-option-syntax-for-review/src/zc/buildout/buildout.py
U zc.buildout/branches/plus-minus-option-syntax-for-review/src/zc/buildout/buildout.txt
-=-
Copied: zc.buildout/branches/plus-minus-option-syntax-for-review (from rev 87133, zc.buildout/trunk)
Modified: zc.buildout/branches/plus-minus-option-syntax-for-review/src/zc/buildout/buildout.py
===================================================================
--- zc.buildout/trunk/src/zc/buildout/buildout.py 2008-06-03 22:57:04 UTC (rev 87133)
+++ zc.buildout/branches/plus-minus-option-syntax-for-review/src/zc/buildout/buildout.py 2008-06-05 17:48:06 UTC (rev 87171)
@@ -1146,10 +1146,29 @@
result.append(os.path.basename(location))
return result
+def _update_section(s1, s2):
+ for k, v in s2.items():
+ ks = k.split()
+ key, sign = None, None
+ if len(ks) == 2:
+ key, sign = ks
+
+ if k not in s1 and key in s1:
+ if sign == '+':
+ s2[key] = "\n".join(s1[key].split() + s2[k].split())
+ del s2[k]
+ elif sign == '-':
+ s2[key] = "\n".join([v for v in s1[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/branches/plus-minus-option-syntax-for-review/src/zc/buildout/buildout.txt
===================================================================
--- zc.buildout/trunk/src/zc/buildout/buildout.txt 2008-06-03 22:57:04 UTC (rev 87133)
+++ zc.buildout/branches/plus-minus-option-syntax-for-review/src/zc/buildout/buildout.txt 2008-06-05 17:48:06 UTC (rev 87171)
@@ -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,116 @@
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, 'extension.cfg',
+ ... """
+ ... [buildout]
+ ... extends = base.cfg
+ ... parts = part1 part2
+ ...
+ ... [part1]
+ ... option += a3 a4
+ ...
+ ... [part2]
+ ... option -= b1 b2
+ ...
+ ... [part3]
+ ... option += c3 c4 c5
+ ...
+ ... [part4]
+ ... option = h1 h2
+ ...
+ ... """)
+
+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 = extension.cfg
+ ... """)
+
+ >>> print system(os.path.join('bin', 'buildout')),
+ ['a1/na2/na3/na4', 'b3/nb4', '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, 'extension.cfg'))
+
Multiple configuration files
----------------------------
@@ -928,8 +1041,6 @@
>>> print system(buildout),
Develop: '/sample-buildout/recipes'
- Uninstalling debug.
- Uninstalling data-dir.
Installing debug.
op buildout
recipe recipes:debug
@@ -1591,6 +1702,7 @@
d d1
d d2
d d3
+ d demo
d develop-eggs
d eggs
d parts
@@ -1670,6 +1782,7 @@
d d2
d data2-extra
d data3
+ d demo
d develop-eggs
d eggs
d parts
@@ -1746,6 +1859,7 @@
d data2
d data2-extra
d data3
+ d demo
d develop-eggs
d eggs
d parts
@@ -2126,6 +2240,7 @@
- base.cfg
d bin
- buildout.cfg
+ d demo
d develop-eggs
d eggs
- inst.cfg
@@ -2147,6 +2262,7 @@
- base.cfg
d bin
- buildout.cfg
+ d demo
d develop-eggs
d eggs
d parts
@@ -2170,6 +2286,7 @@
- base.cfg
d bin
- buildout.cfg
+ d demo
d develop-eggs
d eggs
d parts
More information about the Checkins
mailing list