[augeas-devel] [PATCH] Copying whitespace to new nodes

Dominic Cleal dcleal at redhat.com
Sun Apr 8 13:22:23 UTC 2012


Hello,

I've put together an experimental patch that aims to copy whitespace
from previous lines when creating new nodes.  Generally this is just to
make files prettier, but we saw in the Cobbler/YAML case it's required
as whitespace affects parsing.  It works by changing the definition of
"del" lenses (del RE STR) when they're used in the "create" path.

When a del lens is processed in the put direction, it now stores the
last string that it restored (matched by RE).  If the lens is then used
to create, this string is restored instead of the default string given
by STR.  The default string STR is only used when no previous put has
occurred.

Since del is used for whitespace and any other characters that don't
make it into the tree, this has the effect that they're "copied" from
the last occurrence that del lens, so the file looks more consistent.

This mostly seems to work in practice.  The main problem with it is that
lenses - especially for whitespace - are reused for many different
fields in a file.  If for example Util.del_ws was used for /etc/fstab,
the same del lens would be used five times for different field widths
throughout a single line.  If a new entry was created, this patch would
start restoring the width from the last deleted section on the last row,
rather than the first deleted section.

The change to lenses/tests/test_cgrules.aug shows how multiple fields
aren't copied correctly.  In tests/test-augtool/ini-yum-newsec.sh you
also see where a del lens is used for the whitespace around key/value
separators and again for the whitespace preceding comments - making it
less consistent now than it was.

To fix this would mean duplicating a lot of del lenses, one for each
use.  Perhaps a better way would be to create a set of registers (like
vim, or counter/seq) where deleted strings are stored and optionally
assigned a name so the right one can be restored.  This would mean a new
lens primitive, or addition to "del", so many changes would be needed to
existing file lenses to use the functionality.

Thoughts welcomed.

Thanks,

-- 
Dominic Cleal
Red Hat Consulting
m: +44 (0)7817 878113
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Re-use-last-deleted-string-when-del-lens-creates-new.patch
Type: text/x-patch
Size: 14567 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/augeas-devel/attachments/20120408/7d62c606/attachment.bin>


More information about the augeas-devel mailing list