Author: coudot
Date: 2010-07-19 18:14:33 +0200 (Mon, 19 Jul 2010)
New Revision: 97

Modified:
   ldap-scripts/trunk/file2ldif.pl
Log:
perltidy on file2ldif.pl

Modified: ldap-scripts/trunk/file2ldif.pl
===================================================================
--- ldap-scripts/trunk/file2ldif.pl     2010-07-19 16:13:35 UTC (rev 96)
+++ ldap-scripts/trunk/file2ldif.pl     2010-07-19 16:14:33 UTC (rev 97)
@@ -25,7 +25,7 @@
 #====================================================================
 # Containers begin and end characters for replacement
 my $beginc = "{";
-my $endc = "}";
+my $endc   = "}";
 
 # Write changes and not full entries
 my $change = 1;
@@ -40,39 +40,41 @@
 my $map = {
     l_person => {
         dn => 'uid={(lc)uid},ou=users,dc=example,dc=com',
-        objectClass => [ 'top', 'person', 'organizationalPerson', 
'inetOrgPerson' ],
-        cn => '{cn}',
-        sn => '{(uc)sn}',
+        objectClass =>
+          [ 'top', 'person', 'organizationalPerson', 'inetOrgPerson' ],
+        cn        => '{cn}',
+        sn        => '{(uc)sn}',
         givenName => '{(ucfirstlc)givenname}',
-        mail => '{(fmail)givenname}.{(fmail)[email protected]',
+        mail      => '{(fmail)givenname}.{(fmail)[email protected]',
     },
     l_group => {
-        dn => 'cn={employeetype},ou=groups,dc=example,dc=com',
-        objectClass => [ 'top', 'groupOfUniqueNames' ],
-       uniqueMember => 'cn=empty', 
+        dn           => 'cn={employeetype},ou=groups,dc=example,dc=com',
+        objectClass  => [ 'top', 'groupOfUniqueNames' ],
+        uniqueMember => 'cn=empty',
     },
     l_group_add => {
-        change_op => 'add',
-        dn => 'cn={(lc)employeetype},ou=groups,dc=example,dc=com',
-       uniqueMember => 'uid={(lc)uid},ou=users,dc=example,dc=com',
+        change_op    => 'add',
+        dn           => 'cn={(lc)employeetype},ou=groups,dc=example,dc=com',
+        uniqueMember => 'uid={(lc)uid},ou=users,dc=example,dc=com',
     },
     l_group_del => {
-        change_op => 'delete',
-        dn => 'cn={(lc)employeetype},ou=groups,dc=example,dc=com',
-       uniqueMember => 'cn=empty',
+        change_op    => 'delete',
+        dn           => 'cn={(lc)employeetype},ou=groups,dc=example,dc=com',
+        uniqueMember => 'cn=empty',
     },
     c_person => {
         dn => 'uid={0},ou=users,dc=example,dc=com',
-        objectClass => [ 'top', 'person', 'organizationalPerson', 
'inetOrgPerson' ],
-        uid => '{0}',
+        objectClass =>
+          [ 'top', 'person', 'organizationalPerson', 'inetOrgPerson' ],
+        uid       => '{0}',
         givenName => '{1}',
-        sn => '{2}',
-        cn => '{1} {2}',
+        sn        => '{2}',
+        cn        => '{1} {2}',
     },
     c_group => {
-        dn => 'cn={0},ou=groups,dc=example,dc=com',
-        objectClass => [ 'top', 'groupOfUniqueNames' ],
-        cn => '{0}',
+        dn           => 'cn={0},ou=groups,dc=example,dc=com',
+        objectClass  => [ 'top', 'groupOfUniqueNames' ],
+        cn           => '{0}',
         uniqueMember => 'uid={1},ou=users,dc=example,dc=com',
     },
 };
@@ -89,8 +91,10 @@
 # TODO: use getopts
 # Task
 my $task = shift @ARGV;
+
 # Input file
 my $file = shift @ARGV;
+
 # Changetype (add, modify, delete, modrdn)
 my $changetype = shift @ARGV;
 $changetype = "add" unless $changetype;
@@ -100,38 +104,44 @@
 my $inldif;
 
 # Determine input type file (CSV or LDIF)
-my ($type) = ($file =~ m/.*\.(\w+)/);
+my ($type) = ( $file =~ m/.*\.(\w+)/ );
 
 # If CSV, generate a tmp LDIF file
-if ($type =~ m/csv/i) {
+if ( $type =~ m/csv/i ) {
+
     # Load Text::CSV module
     use Text::CSV;
 
     # Open CSV
-    my $csv = Text::CSV->new({
-        sep_char => $csv_delimiter,
-        binary => 1,
-        });
-    open (CSV, "<", $file) or die $!;
+    my $csv = Text::CSV->new(
+        {
+            sep_char => $csv_delimiter,
+            binary   => 1,
+        }
+    );
+    open( CSV, "<", $file ) or die $!;
 
     # Parse CSV
-    $inldif = Net::LDAP::LDIF->new("$file.ldif", "w" );
+    $inldif = Net::LDAP::LDIF->new( "$file.ldif", "w" );
 
     while (<CSV>) {
 
         # Strip headers
-        next if (($. == 1) and ($csv_strip_headers == 1));
+        next if ( ( $. == 1 ) and ( $csv_strip_headers == 1 ) );
 
         # Parse CSV line
-        if ($csv->parse($_)) {
+        if ( $csv->parse($_) ) {
             my @columns = $csv->fields();
+
             # Write every column as attribute
             my $entry = Net::LDAP::Entry->new('o=fakedn');
-            for my $i (0 ..$#columns) {
-                 $entry->add($i => $columns[$i]);
+            for my $i ( 0 .. $#columns ) {
+                $entry->add( $i => $columns[$i] );
             }
             $inldif->write_entry($entry);
-        } else {
+        }
+        else {
+
             # Error in parsing
             my $err = $csv->error_input;
             print STDERR "Failed to parse line: $err\n";
@@ -144,53 +154,60 @@
     close CSV;
     $inldif->done();
     $inldif = Net::LDAP::LDIF->new("$file.ldif");
-} else {
+}
+else {
     $inldif = Net::LDAP::LDIF->new($file);
 }
 
 # Parse LDIF
-while( not $inldif->eof() ) {
+while ( not $inldif->eof() ) {
     my $entry = $inldif->read_entry();
     if ( $inldif->error() ) {
-        print STDERR "Error msg: ", $inldif->error (), "\n";
-        print STDERR "Error lines:\n", $inldif->error_lines (), "\n";
-    } else {
+        print STDERR "Error msg: ",    $inldif->error(),       "\n";
+        print STDERR "Error lines:\n", $inldif->error_lines(), "\n";
+    }
+    else {
+
         # Replace strings in map
-        my %localmap = %{$map->{$task}};
+        my %localmap = %{ $map->{$task} };
 
-        while ( my ($k, $v) = each %localmap ) {
-            if ( ref($v) eq "ARRAY") {
+        while ( my ( $k, $v ) = each %localmap ) {
+            if ( ref($v) eq "ARRAY" ) {
                 my @values = @$v;
-                foreach ( @values ) { $_ =~ 
s/$beginc([^$endc]*)?$endc/&replace_value($entry,$1)/ge; };
+                foreach (@values) {
+                    $_ =~
+                      s/$beginc([^$endc]*)?$endc/&replace_value($entry,$1)/ge;
+                }
                 $v = \...@values;
-            } else {
-               $v =~ s/$beginc([^$endc]*)?$endc/&replace_value($entry,$1)/ge;
             }
+            else {
+                $v =~ s/$beginc([^$endc]*)?$endc/&replace_value($entry,$1)/ge;
+            }
             $localmap{$k} = $v;
         }
 
-       # DN
+        # DN
         my $dn = $localmap{dn};
         delete $localmap{dn};
-        
-       # Change operation
-       my $change_op = $localmap{change_op};
+
+        # Change operation
+        my $change_op = $localmap{change_op};
         delete $localmap{change_op};
-       $change_op = "add" unless $change_op;
+        $change_op = "add" unless $change_op;
 
-       # Remove empty values
-       while (my ($key, $value) = each(%localmap)) {
-       delete $localmap{$key} if ( $value eq "");
-       }
-       
-       # Write entry
+        # Remove empty values
+        while ( my ( $key, $value ) = each(%localmap) ) {
+            delete $localmap{$key} if ( $value eq "" );
+        }
+
+        # Write entry
         my $outentry = Net::LDAP::Entry->new($dn);
-       $outentry->changetype($changetype);
-       $outentry->$change_op(%localmap);
-       $outldif->write_entry( $outentry );
-       if ( my $ldif_error = $outldif->error() ) {
-               print STDERR "Fail to add entry in LDIF: $ldif_error\n";
-       }
+        $outentry->changetype($changetype);
+        $outentry->$change_op(%localmap);
+        $outldif->write_entry($outentry);
+        if ( my $ldif_error = $outldif->error() ) {
+            print STDERR "Fail to add entry in LDIF: $ldif_error\n";
+        }
     }
 }
 
@@ -199,19 +216,20 @@
 # Apply subroutine if any
 sub replace_value {
     my $entry = shift;
-    my $key = shift;
+    my $key   = shift;
     my $sub;
     my $attr;
     my $value;
 
     # Check subroutine presence
     if ( $key =~ m/\((.*)\)(.*)/ ) {
-        $sub = $1;
+        $sub  = $1;
         $attr = $2;
-    } else { $attr = $key }
+    }
+    else { $attr = $key }
 
     # Replace DN
-    if ($attr eq "dn") { $value = $entry->dn(); }
+    if ( $attr eq "dn" ) { $value = $entry->dn(); }
 
     # Get first attribute value
     else { $value = $entry->get_value($attr); }
@@ -223,7 +241,7 @@
     $value =~ s/^\s+|\s+$//g;
 
     # Apply subroutine if any
-    $value = &apply_sub($value, $sub) if ($sub);
+    $value = &apply_sub( $value, $sub ) if ($sub);
 
     return $value;
 }
@@ -231,14 +249,14 @@
 # Apply subroutine
 sub apply_sub {
     my $value = shift;
-    my $sub = shift;
+    my $sub   = shift;
 
-    $value = lc($value) if ($sub eq "lc");
-    $value = lcfirst($value) if ($sub eq "lcfirst");
-    $value = uc($value) if ($sub eq "uc");
-    $value = ucfirst($value) if ($sub eq "ucfirst");
-    $value = ucfirst(lc($value)) if ($sub eq "ucfirstlc");
-    $value = &fmail($value) if ($sub eq "fmail");
+    $value = lc($value)            if ( $sub eq "lc" );
+    $value = lcfirst($value)       if ( $sub eq "lcfirst" );
+    $value = uc($value)            if ( $sub eq "uc" );
+    $value = ucfirst($value)       if ( $sub eq "ucfirst" );
+    $value = ucfirst( lc($value) ) if ( $sub eq "ucfirstlc" );
+    $value = &fmail($value)        if ( $sub eq "fmail" );
 
     return $value;
 }
@@ -257,15 +275,15 @@
     eval { require Text::Unaccent };
     if ($@) { return $value; }
     else {
-       $value = unac_string('UTF-8', $value);
-       return $value;
+        $value = unac_string( 'UTF-8', $value );
+        return $value;
     }
 }
 
 #====================================================================
 # Exit
 #====================================================================
-$inldif->done ();
-$outldif->done ();
-unlink "$file.ldif" if ($type =~ m/csv/i);
+$inldif->done();
+$outldif->done();
+unlink "$file.ldif" if ( $type =~ m/csv/i );
 exit 0;

_______________________________________________
ltb-changes mailing list
[email protected]
http://lists.ltb-project.org/listinfo/ltb-changes

Reply via email to