[augeas-devel] Is there way to get path of inserted label?

Josef Reidinger jreidinger at suse.com
Wed Mar 15 08:19:18 UTC 2017


On Tue, 14 Mar 2017 11:35:03 -0700
"David Lutterkort" <lutter at watzmann.net> wrote:

> Hi Josef,
> 
> that is indeed an icky problem. There is currently no direct way to
> get the newly inserted node. Besides carefully recomputing indices
> like you described, the only other way I can think of is to use the
> following-sibling and preceding-sibling axes in path expressions. For
> example, to get the comment right before the first real entry
> in /etc/hosts you'd do
> 
> match /files/etc/hosts/#comment[following-sibling::1][last()]
> 
> This is based on the fact that the first real entry in /etc/hosts is
> /files/etc/hosts/1 (i.e., the '1' in the above path expression is the
> label of a node). /files/etc/hosts/#comment matches all comments
> in /etc/hosts, [following-sibling::1] restricts that to all the
> comments that come before /files/etc/hosts/1, and the [last()] picks
> out the last of those comments.
> 
> Similarly, you'd pick the comment right after /files/etc/hosts/1 with
> 
> match /files/etc/hosts/#comment[preceding-sibling::1][1]
> 
> I'd agree though that writing these path expressions isn't anybody's
> idea of fun. Another approach would be to change some commands to set
> a variable (say $_last) to point to the newly inserted node, so that
> you could just do 'match $_last' to get the inserted node. There's
> probably more commands that could benefit from that, particularly
> set. What do you think ?

Hi David,
it sounds like good idea. so then command can look like
ins #comment after /files/etc/hosts/1
set $_last comment1
or even "set /files/etc/hosts/$_last comment1" is fine for me.

Which looks better then current way with siblings.

JFYI and anyone else: After a lot of thinking I solve issue with
renumbering by small trick. I go tree in reverse order and do
modifications. So if I need to remove #comment[50] and #comment[36] and
then I want to add new #comment before original #comment[10] and
modify #comment[40]. I will start with removing #comment[50], then
modify 40, remove 36 and as last insert before 10, this way renumbering
do not affect nodes I want to process.

Thanks for response.

Josef

> 
> David
> 
> 
> On Tue, Mar 14, 2017 at 3:12 AM, Josef Reidinger <jreidinger at suse.com>
> wrote:
> 
> > Good morning to list,
> > Let me at first get context. I am using augeas ruby bindings to do
> > some automatic changes in configuration files. I build some tools
> > around allowing easier access to augeas, but I have now one
> > challenge.
> >
> > In general I have set of modifications, additional entries and
> > removed entries. My challenge is that path is not stable, so when
> > new entry is added or removed from array like #comment, then it
> > changes path of other items in array.
> >
> > So consider this scenario. I need to modify two comments, remove two
> > comments and add comment before another comment. The first part is
> > easy and I use set. The second part I already solved as I know
> > original path and do it in reverse order so e.g. remove
> > #comment[50] and #comment[36]. Then I want to add new #comment
> > before original #comment[10] but it is no longer #comment[10] and I
> > also do not know what will be number of new comment ( ok, in this
> > case I know it is path of comment I inserted before, but sometimes
> > I need to add it before another key like "inst #comment before
> > $prefix/alias[5]" then is there easy way how to get path of newly
> > inserted label? like $prefix/#comment[16] ?
> >
> > Also is there way how to get what is old path? Value is not unique,
> > especially in comments and I find manually counting and changing all
> > indexes quite complex.
> >
> > Thanks for help and tips.
> >
> > Josef
> >
> > _______________________________________________
> > augeas-devel mailing list
> > augeas-devel at redhat.com
> > https://www.redhat.com/mailman/listinfo/augeas-devel
> >  




More information about the augeas-devel mailing list