Changeset 2276


Ignore:
Timestamp:
08/21/19 18:50:19 (5 years ago)
Author:
nanardon
Message:

Allow to dump object as perl code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Objects.pm

    r2267 r2276  
    818818} 
    819819 
     820=head2 DataDump($config) 
     821 
     822Return a structure about the object 
     823 
     824=cut 
     825 
     826sub DataDump { 
     827    my ($self, $config, $base) = @_; 
     828 
     829    # { 
     830    #   base => # base latmoslocal: object address/thauvin-GY 
     831    #   otype => 'otype', 
     832    #   id => 'name', 
     833    #   roAttrs => [] 
     834    #   Attrs => { 
     835    #       'Attr' => [], 
     836    #   } 
     837    #   subObjs => { 
     838    #       'otype' => [], 
     839    #   } 
     840    # } 
     841 
     842 
     843    $config->{level} ||= 0; 
     844    my $otype = $self->type; 
     845    $base ||= $self->base; 
     846    my $dump = { 
     847        otype => $otype, 
     848        id => $self->id, 
     849    }; 
     850    if (ref $self) { 
     851        $dump->{base} = $base->label; 
     852    } 
     853 
     854    my %roAttrs = (); 
     855    foreach my $attr (sort { $a cmp $b } $base->list_canonical_fields($otype, 
     856        $config->{only_rw} ? 'rw' : 'r')) { 
     857        my $oattr = ref $self ? $self->attribute($attr) : $base->attribute($otype, $attr); 
     858        next if ($oattr->hidden); 
     859 
     860        if (ref $self) { 
     861            my $val = $self->get_c_field($attr); 
     862            if ($val || $config->{empty_attr}) { 
     863                my @vals = ref $val ? @{ $val } : $val; 
     864                $dump->{Attrs}{$attr} = \@vals; 
     865                $roAttrs{ $attr } = 1 if ($oattr->ro); 
     866            } 
     867        } else { 
     868            $dump->{Attrs}{$attr} = undef; 
     869            $roAttrs{ $attr } = 1 if ($oattr->ro); 
     870        } 
     871 
     872        $dump->{roAttrs} = [ sort keys %roAttrs ]; 
     873    } 
     874 
     875    if ($config->{cb}) { 
     876        $config->{cb}->($config, $dump); 
     877    } 
     878 
     879    if (ref $self && $config->{recur}) { 
     880          my %subobj = $self->ListChildObjects; 
     881          foreach my $otype (sort keys %subobj) { 
     882              foreach my $oname (sort @{ $subobj{$otype} }) { 
     883                  my $obj = $self->base->get_object($otype, $oname) or next; 
     884                  push(@{ $dump->{subObjs}{$otype} }, $obj->DataDump({ %{$config || {}}, recur => $config->{recur}, level => $config->{level} + 2 })); 
     885              } 
     886          } 
     887    } 
     888 
     889    return $dump; 
     890} 
     891 
    820892=head2 text_dump ($handle, $config, $base) 
    821893 
     
    832904=head2 dump 
    833905 
    834 Return dump for tihs object 
     906Return dump for this object 
    835907 
    836908=cut 
    837909 
    838910sub dump { 
    839     my ($self, $config, $base) = @_; 
    840  
    841     $config->{level} ||= 0; 
    842     my $otype = $self->type; 
     911    my ($self, $InitConfig, $base) = @_; 
     912 
     913    $InitConfig->{level} ||= 0; 
    843914    $base ||= $self->base; 
    844     my $dump; 
    845     if (ref $self) { 
    846         $dump .= sprintf "%s# base %s: object %s/%s\n", 
     915    my $dump = ''; 
     916 
     917    $InitConfig->{cb} = sub { 
     918        my ( $config, $Dump ) = @_; 
     919 
     920        if ($config->{level}) { 
     921            $dump .= "\n"; 
     922        } 
     923 
     924        if (ref $self) { 
     925            $dump .= sprintf "%s# base %s: object %s/%s\n", 
     926                ' ' x $config->{level}, 
     927                $Dump->{base}, $Dump->{otype}, $Dump->{id}; 
     928        } 
     929        $dump .= sprintf( 
     930            "%s# %s\n", 
    847931            ' ' x $config->{level}, 
    848             $base->label, $self->type, $self->id; 
    849     } 
    850     $dump .= sprintf( 
    851         "%s# %s\n",  
    852         ' ' x $config->{level}, 
    853         scalar(localtime) 
    854     ); 
    855  
    856     foreach my $attr (sort { $a cmp $b } $base->list_canonical_fields($otype, 
    857         $config->{only_rw} ? 'rw' : 'r')) { 
    858         my $oattr = ref $self ? $self->attribute($attr) : $base->attribute($otype, $attr); 
    859         if ($oattr->hidden) { next; } 
    860         if (ref $self) { 
    861             my $val = $self->get_c_field($attr); 
    862             if ($val || $config->{empty_attr}) { 
    863                 if (my @allowed = $base->obj_attr_allowed_values($otype, $attr)) { 
    864                     $dump .= sprintf("# %s must be%s: %s\n", 
     932            scalar(localtime) 
     933        ); 
     934 
     935        my %roAttrs = map { $_ => 1 } @{ $Dump->{roAttrs} || [] }; 
     936 
     937        foreach my $attr (sort { $a cmp $b } sort keys %{ $Dump->{Attrs} || {} }) { 
     938            my $val = $Dump->{Attrs}{$attr}; 
     939            my $oattr = $base->attribute($Dump->{otype}, $attr); 
     940            if ($val) { 
     941                if (my @allowed = $base->obj_attr_allowed_values($Dump->{otype}, $attr)) { 
     942                    $dump .= sprintf("%s# %s must be%s: %s\n", 
     943                        ' ' x $config->{level}, 
    865944                        $attr, 
    866945                        ($oattr->mandatory ? '' : ' empty or either'), 
     
    868947                    ); 
    869948                } 
    870                 my @vals = ref $val ? @{ $val } : $val; 
    871                 foreach (@vals) { 
     949 
     950                foreach (@$val) { 
    872951                    $_ ||= ''; 
    873952                    s/\r?\n/\\n/g; 
    874953                    $dump .= sprintf("%s%s%s%s:%s\n", 
    875954                        ' ' x $config->{level}, 
    876                         $oattr->ro ? '# (ro) ' : '', 
    877                         $config->{level} ? $self->type . '[' . $self->id . '].' : '', 
     955                        $roAttrs{$attr} ? '# (ro) ' : '', 
     956                        $config->{level} ? $Dump->{otype} . '[' . $Dump->{id} . '].' : '', 
    878957                        $attr, $_ ? " $_" : ''); 
    879958                } 
     959            } elsif ( $config->{empty_attr} || ! ref $self) { 
     960                if (my @allowed = $base->obj_attr_allowed_values($Dump->{otype}, $attr)) { 
     961                    $dump .= sprintf("%s# %s must be%s: %s\n", 
     962                        ' ' x $config->{level}, 
     963                        $attr, 
     964                        ($oattr->mandatory ? '' : ' empty or either'), 
     965                        join(', ', @allowed) 
     966                    ); 
     967                } 
     968                $dump .= sprintf("%s%s%s%s:\n", 
     969                    ' ' x $config->{level}, 
     970                    $roAttrs{$attr} ? '# (ro) ' : '', 
     971                    $config->{level} ? $Dump->{otype} . '[' . $Dump->{id} . '].' : '', 
     972                    $attr); 
    880973            } 
    881         } else { 
    882             if (my @allowed = $base->obj_attr_allowed_values($otype, $attr)) { 
    883                 $dump .= sprintf("# %s must be empty or either: %s\n", 
    884                     $attr, 
    885                     join(', ', @allowed) 
    886                 ); 
    887             } 
    888             $dump .= sprintf("%s%s: %s\n",  
    889                 $oattr->ro ? '# (ro) ' : '', 
    890                 $attr, ''); 
    891         } 
    892     } 
    893  
    894     if (ref $self && $config->{recur}) { 
    895           my %subobj = $self->ListChildObjects; 
    896           foreach my $otype (keys %subobj) { 
    897               foreach my $oname (sort @{ $subobj{$otype} }) { 
    898                   my $obj = $self->base->get_object($otype, $oname) or next; 
    899                   $dump .= "\n"; 
    900                   $dump .= $obj->dump({ %{$config || {}}, recur => $config->{recur}, level => $config->{level} + 2 }); 
    901               } 
    902           } 
    903     } 
     974        } 
     975    }; 
     976 
     977    $self->DataDump($InitConfig, $base); 
    904978 
    905979    return $dump; 
Note: See TracChangeset for help on using the changeset viewer.