Changeset 2280


Ignore:
Timestamp:
08/30/19 18:37:00 (5 years ago)
Author:
nanardon
Message:

la-dump: add support for json and yaml, allow to write into files

Location:
trunk/LATMOS-Accounts
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts/bin/la-dump

    r2278 r2280  
    7676    'u|username=s'      => \my $username, 
    7777    'help'              => sub { pod2usage(0) }, 
     78    'f=s'               => \my $filename, 
    7879) or pod2usage(); 
    7980 
     
    9394foreach (@result) { 
    9495    my $o = $labase->get_object($otype, $_) or next; 
    95     push(@Dump, $o->DataDump({ recur => $recur, SubOtype => \@SubOtype })); 
     96    push(@Dump, $o->DataDump( 
     97            { 
     98                recur => $recur, 
     99                SubOtype => \@SubOtype, 
     100                noSchema => 1, 
     101            } 
     102    )); 
    96103} 
    97104 
    98 require Data::Dumper; 
    99 print Data::Dumper::Dumper(\@Dump); 
     105@fmt = qw(xml) unless (@fmt); 
    100106 
    101107foreach my $Format (@fmt) { 
     
    105111    for ($Format) { 
    106112        /^xml$/ and do { 
    107             require XML::Simple; 
     113            eval { 
     114                require XML::Simple; 
     115            }; 
     116            if ($@) { 
     117                warn "Cannot output xml: XML::Simple perl module missing"; 
     118                next; 
     119            } 
    108120            my $xml = XML::Simple->new(); 
    109121            $string = $xml->XMLout(\@Dump); 
    110122            next; 
     123        }; 
     124        /^json$/ and do { 
     125            eval { 
     126                require JSON; 
     127            }; 
     128            if ($@) { 
     129                warn "Cannot output xml: XML::Simple perl module missing"; 
     130                next; 
     131            } 
     132            my $json = JSON->new; 
     133            $json->indent(1); 
     134            $string = $json->encode(\@Dump); 
     135            next; 
     136        }; 
     137        /^yaml$/ and do { 
     138            eval { 
     139                require YAML; 
     140            }; 
     141            if ($@) { 
     142                warn "Cannot output xml: XML::Simple perl module missing"; 
     143                next; 
     144            } 
     145            $string = YAML::Dump(\@Dump); 
     146            next; 
     147        }; 
     148        /^pl$/ and do { 
     149            require Data::Dumper; 
     150            $string = Data::Dumper::Dumper(\@Dump); 
    111151        } 
    112152    } 
    113153 
    114     print $string; 
     154    if ($filename) { 
     155        my $realFilename = $filename . '.' . $Format; 
     156        open(my $handle, '>', $realFilename) or do { 
     157            warn "Cannot open $realFilename for wrinting"; 
     158            next; 
     159        }; 
     160        print $handle $string; 
     161        close($handle); 
     162    } else { 
     163        print $string; 
     164    } 
    115165} 
    116166 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Objects.pm

    r2278 r2280  
    882882        } 
    883883 
    884         $dump->{roAttrs} = [ sort keys %roAttrs ]; 
     884        $dump->{roAttrs} = [ sort keys %roAttrs ] 
     885            unless($config->{noSchema}); 
    885886    } 
    886887 
Note: See TracChangeset for help on using the changeset viewer.