OpenPKG CVS Repository
  http://cvs.openpkg.org/
  ____________________________________________________________________________

  Server: cvs.openpkg.org                  Name:   Thomas Lotterer
  Root:   /v/openpkg/cvs                   Email:  [EMAIL PROTECTED]
  Module: openpkg-registry                 Date:   10-Jul-2006 11:24:23
  Branch: HEAD                             Handle: 2006071010242200

  Modified files:
    openpkg-registry        registry-ui.pl

  Log:
    support merging of user defined overrides into default config

  Summary:
    Revision    Changes     Path
    1.67        +54 -17     openpkg-registry/registry-ui.pl
  ____________________________________________________________________________

  patch -p0 <<'@@ .'
  Index: openpkg-registry/registry-ui.pl
  ============================================================================
  $ cvs diff -u -r1.66 -r1.67 registry-ui.pl
  --- openpkg-registry/registry-ui.pl   6 Jul 2006 23:02:57 -0000       1.66
  +++ openpkg-registry/registry-ui.pl   10 Jul 2006 09:24:22 -0000      1.67
  @@ -37,6 +37,7 @@
   use MIME::Base64;
   use XML::Simple;
   use String::Divert;
  +use Clone qw(clone);
   
   #   configure optional debugging
   $Data::Dumper::Purity = 1;
  @@ -58,10 +59,9 @@
   $response->{message} = new String::Divert;
   $response->{message}->fold("message");
   
  -#   configuration preset defaults then read config
  +#   default configuration text
   #
  -
  -my $cfg = qq{
  +my $defcfgtxt = qq{
       ##
       ##  registry-ui.cfg - OpenPKG Registry User Interface Configuration
       ##
  @@ -113,31 +113,68 @@
           showsid 0;
       };
   };
  -$cfg =~ s/^[ ]{4}//mg;
  -$cfg =~ s/^\n//s;
  +$defcfgtxt =~ s/^[ ]{4}//mg;
  +$defcfgtxt =~ s/^\n//s;
  +
  +#   outside CGI environment just print default config (useful during 
installation)
  +if ($ENV{GATEWAY_INTERFACE} !~ m/^CGI/) {
  +    print $defcfgtxt;
  +    exit 0;
  +}
  +
  +#   merges two hashes. First argument is a hash working as accumulator and 
the
  +#   second argument is the hash to be added to it. Either argument can be
  +#   empty.  A typical application is the accumulator being prefilled with
  +#   defaults and the adder comes with overrides. The function recursively
  +#   processes nested hashes. Values with dissimilar keys will be merged,
  +#   missing data on either side will be accumulated and identical keys on 
both
  +#   sides will be overwritten with the added data. Returns a new hash with
  +#   cloned data. The original accumulator and adder passed in remain 
untouched.
  +#   If the first element in an array is a hash it is assumed a pseudo-hash as
  +#   described in perlref(1) encountered and hash merging is attempted.
  +#
  +sub mergehashes ($$) {
  +    my ($acc, $add) = @_;
  +    $acc = clone($acc);
  +    $add = clone($add);
  +    foreach my $k (sort keys %{$add}) {
  +        if (    (ref($acc->{$k}) eq "HASH" || (ref($acc->{$k}) eq "ARRAY" 
and ref($acc->{$k}[0]) eq "HASH"))
  +            and (ref($add->{$k}) eq "HASH" || (ref($add->{$k}) eq "ARRAY" 
and ref($add->{$k}[0]) eq "HASH"))
  +           ) {
  +            $acc->{$k} = &mergehashes($acc->{$k}, $add->{$k});
  +        }
  +        else {
  +            $acc->{$k} = $add->{$k};
  +        }
  +    }
  +    return $acc;
  +}
   
  -sub readconfig ($)
  +sub parsecfgtxt ($)
   {
  -    my ($cfgfile) = @_;
  +    my ($text) = @_;
  +    my $parse = {};
       my $osspcfg = new OSSP::cfg::simple;
  +    $osspcfg->parse($text) || die "error parsing config file";
  +    $parse = $osspcfg->unpack(-index => '.*', -strip => '.*', -flatten => 1) 
|| die "error unpacking config file";
  +    return $parse;
  +}
  +
  +sub readcfgtxt ($)
  +{
  +    my ($cfgfile) = @_;
   
       die "no config file specified" unless (defined $cfgfile);
       my $io = new IO::File "<$cfgfile";
       die "cannot open \"$cfgfile\" for reading ($!)" if(not defined $io);
       my $txt = ''; $txt .= $_ while (<$io>);
       $io->close();
  -    $osspcfg->parse($txt) || die "error parsing config file";
  -    $cfg = $osspcfg->unpack(-index => '.*', -strip => '.*', -flatten => 1) 
|| die "error unpacking config file";
  -    return($cfg);
  +    return $txt;
   }
  -$cfg = &readconfig("$PREFIX/etc/openpkg-registry/registry-ui.cfg");
  -#FIXME die Dumper($cfg); # currently default config is ignored and replaced 
by readconfig. They should merge.
   
  -#   outside CGI environment just print default config (useful during 
installation)
  -if ($ENV{GATEWAY_INTERFACE} !~ m/^CGI/) {
  -    print $cfg;
  -    exit 0;
  -}
  +my $defcfg = &parsecfgtxt($defcfgtxt);
  +my $usecfg = 
&parsecfgtxt(&readcfgtxt("$PREFIX/etc/openpkg-registry/registry-ui.cfg"));
  +my $cfg = &mergehashes($defcfg, $usecfg);
   
   my $ase;
   $ase = undef;
  @@ .
______________________________________________________________________
The OpenPKG Project                                    www.openpkg.org
CVS Repository Commit List                     [email protected]

Reply via email to