Changeset 1985


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
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/LATMOS-Accounts/MANIFEST

    r1965 r1985  
    5353bin/la-warn-expire 
    5454bin/la2xls 
     55bin/la-load-csv 
    5556etc/cron.d/latmos-accounts 
    5657etc/init.d/la-sync-manager 
  • trunk/LATMOS-Accounts/Makefile.PL

    r1963 r1985  
    8383        bin/la-ban-passwd 
    8484        bin/la-renewEmp 
     85        bin/la-load-csv 
    8586        ) ], 
    8687        macro => { 
  • 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 
  • trunk/LATMOS-Accounts/t/05_utils.t

    r1984 r1985  
    11use strict; 
    22use warnings; 
    3 use Test::More tests => 30; 
     3use Test::More tests => 32; 
    44use File::Temp qw(mkstemp); 
    55 
     
    1010ok(la_log(LA_NOTICE, "a notice"), "can run la_log"); 
    1111 
     12{ 
    1213my ($fh, $file) = mkstemp( "tmpfileXXXXX" ); 
    1314print $fh <<EOF; 
     
    3233ok(LATMOS::Accounts::Utils::dump_read_temp_file( 
    3334        sub { 1; }, sub { 1; }), "Can edit temp file"); 
     35 
     36} 
     37{ 
     38my ($fh, $file) = mkstemp( "tmpfileXXXXX" ); 
     39binmode($fh, ":encoding(UTF-8)"); 
     40print $fh <<EOF; 
     41sn,givenName 
     42Nom,Prénom 
     43EOF 
     44 
     45seek($fh, 0, 0); 
     46 
     47my $res = LATMOS::Accounts::Utils::loadCSV($fh); 
     48$fh = undef; 
     49unlink($file); 
     50 
     51# now testing 
     52is(scalar(@{ $res }), 1, "loadCSV return same number result"); 
     53is($res->[0]{givenName}, "Prénom", "Attribute givenName is properly return"); 
     54 
     55} 
    3456 
    3557ok(!LATMOS::Accounts::Utils::check_oid_validity('toto'), '"toto" is valid oid'); 
Note: See TracChangeset for help on using the changeset viewer.