Friday, 15 July 2011

python - Which format is this config file in? -



python - Which format is this config file in? -

i have parse config files this:

begin key1 "value1" key2 "value2" begin key3 "value3" key4 "value4" end end

what format , there ready-made parser can utilize (preferably in perl/python)?

i don't know name of format, parser::mgc can create lite work of one.

by defining simple self-recursive parse method recognises begin blocks inner scopes, can build recursive tree of hash references input.

package myparser; utilize base of operations 'parser::mgc'; utilize strict; utilize warnings; # need exclude linefeeds whitespace pattern sub pattern_ws { qr/[\t ]+/ } sub parse { $self = shift; %items; $self->sequence_of( sub { $self->any_of( sub { # begin ... end block $self->expect( 'begin' ); $self->commit; $self->expect( qr/\n/ ); $self->scope_of( undef, sub { force @{$items{begin}}, $self->parse; }, 'end' ); $self->expect( qr/\n/ ); }, sub { # key "value" $key = $self->token_ident; $self->commit; $items{$key} = $self->token_string; $self->expect( qr/\n/ ); }, ) }); homecoming \%items; }

this can printed @ end, perhaps using data::dump:

use data::dump 'pp'; print stderr pp(myparser->new->from_file(\*stdin));

this gives

{ begin => [ { begin => [{ key3 => "value3", key4 => "value4" }], key1 => "value1", key2 => "value2", }, ], }

python perl config

No comments:

Post a Comment