Ignore:
Timestamp:
04/20/17 18:30:00 (7 years ago)
Author:
nanardon
Message:

Add tools to create multiple object from csv file

Location:
trunk/LATMOS-Accounts/lib/LATMOS/Accounts
Files:
2 edited

Legend:

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

    r1023 r1985  
    112112} 
    113113 
     114=head2 $ochelper->Automate($info) 
     115 
     116Try to create object from C<$info> w/o interacting with user. 
     117If given infomation does not allow to create object, it failed. 
     118 
     119Return 1 on success. 
     120 
     121=cut 
     122 
     123sub Automate { 
     124    my ($self, $info) = @_; 
     125 
     126    for (my $count = 0; $count < 3; $count++) { 
     127        my $status; 
     128        ($status, $info) = $self->step($info); 
     129        if ($status eq 'CREATED') { 
     130            return 1; 
     131        } elsif ($status eq 'ERROR') { 
     132            return; 
     133        } 
     134    } 
     135} 
     136 
    1141371; 
    115138 
  • trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Utils.pm

    r1984 r1985  
    99use File::Temp qw(tempfile); 
    1010use Crypt::Cracklib; 
     11use Text::CSV; 
    1112 
    1213our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; 
     
    2122 
    2223@ISA = qw(Exporter); 
    23 @EXPORT = qw(to_ascii exec_command switch_user run_via_sudo buildLogin yesno); 
    24 @EXPORT_OK = qw(to_ascii exec_command switch_user run_via_sudo buildLogin yesno); 
     24@EXPORT = qw(to_ascii exec_command switch_user run_via_sudo buildLogin loadCSV yesno); 
     25@EXPORT_OK = qw(to_ascii exec_command switch_user run_via_sudo buildLogin loadCSV yesno); 
    2526 
    2627=head2 to_ascii($text) 
     
    175176} 
    176177 
     178=head2 loadCSV($fh, $callback, $initcallback) 
     179 
     180Parse CVS files and return an array for each parsed line. 
     181 
     182If defined call C<$callback> for parsed line. 
     183 
     184C<$initcallback>, if defined, is called just after the first line. 
     185 
     186=cut 
     187 
     188sub loadCSV { 
     189    my ($fh, $cb, $initcb) = @_; 
     190 
     191    my $csv = Text::CSV->new({ 
     192            blank_is_undef => 1, 
     193            binary => 1, 
     194    }); 
     195 
     196    binmode($fh, ":encoding(UTF-8)"); 
     197 
     198    # First line contains attribute 
     199    my $columns = $csv->getline( $fh ); 
     200 
     201    $csv->column_names($columns); 
     202 
     203    if ($initcb) { 
     204        $initcb->($csv); 
     205    } 
     206 
     207    my $all = []; 
     208    my $linecount = 1; 
     209    while ( my $row = $csv->getline_hr( $fh ) ) { 
     210        $linecount++; 
     211        if ($cb) { 
     212            if (! $cb->($row, $linecount)) { 
     213                return; 
     214            } 
     215        } 
     216        push(@{ $all }, $row); 
     217    } 
     218    $csv->eof () or do { 
     219        return; 
     220    }; 
     221 
     222    return $all; 
     223} 
     224 
    177225=head2 check_oid_validity($name) 
    178226 
Note: See TracChangeset for help on using the changeset viewer.