| 1 | #/usr/bin/env perl | ||
| 2 | ############################################################################### | ||
| 3 | # | ||
| 4 | # @file CSV.pm | ||
| 5 | # | ||
| 6 | # @brief Eulerian Data Warehouse REST CSV Parser Module definition. | ||
| 7 | # | ||
| 8 | # @author Thorillon Xavier:x.thorillon@eulerian.com | ||
| 9 | # | ||
| 10 | # @date 26/11/2021 | ||
| 11 | # | ||
| 12 | # @version 1.0 | ||
| 13 | # | ||
| 14 | ############################################################################### | ||
| 15 | # | ||
| 16 | # Setup module name | ||
| 17 | # | ||
| 18 | package API::Eulerian::EDW::Parser::CSV; | ||
| 19 | # | ||
| 20 | # Enforce compilor rules | ||
| 21 | # | ||
| 22 | use strict; use warnings; | ||
| 23 | # | ||
| 24 | # Inherited interface from API::Eulerian::EDW::Parser | ||
| 25 | # | ||
| 26 | use parent 'API::Eulerian::EDW::Parser'; | ||
| 27 | # | ||
| 28 | # Import Text::CSV | ||
| 29 | # | ||
| 30 | use Text::CSV; | ||
| 31 | # | ||
| 32 | # @brief | ||
| 33 | # | ||
| 34 | # @param $class - API::Eulerian::EDW::Parser class. | ||
| 35 | # @param $path - File Path. | ||
| 36 | # @param $uuid - Request uuid. | ||
| 37 | # | ||
| 38 | # @return API::Eulerian::EDW::Json Parser. | ||
| 39 | # | ||
| 40 | sub new | ||
| 41 | { | ||
| 42 | my ( $class, $path, $uuid ) = @_; | ||
| 43 | my $self; | ||
| 44 | my $file; | ||
| 45 | my $fd; | ||
| 46 | |||
| 47 | if( open( $file, '<:encoding(utf8)', $path ) ) { | ||
| 48 | $self = $class->SUPER::new( $path, $uuid ); | ||
| 49 | $self->{ _FILE } = $file; | ||
| 50 | $self->{ _PARSER } = Text::CSV->new( { | ||
| 51 | binary => 1, | ||
| 52 | auto_diag => 1, | ||
| 53 | sep_char => ',', | ||
| 54 | } ); | ||
| 55 | } | ||
| 56 | |||
| 57 | return $self; | ||
| 58 | } | ||
| 59 | # | ||
| 60 | # @brief | ||
| 61 | # | ||
| 62 | # @param $self | ||
| 63 | # | ||
| 64 | # @return | ||
| 65 | # | ||
| 66 | sub file | ||
| 67 | { | ||
| 68 | return shift->{ _FILE }; | ||
| 69 | } | ||
| 70 | # | ||
| 71 | # @brief | ||
| 72 | # | ||
| 73 | # @param $self | ||
| 74 | # | ||
| 75 | # @return | ||
| 76 | # | ||
| 77 | sub parser | ||
| 78 | { | ||
| 79 | return shift->{ _PARSER }; | ||
| 80 | } | ||
| 81 | # | ||
| 82 | # @brief | ||
| 83 | # | ||
| 84 | # @param $self - API::Eulerian::EDW::Parser | ||
| 85 | # | ||
| 86 | sub do | ||
| 87 | { | ||
| 88 | my ( $self, $hooks ) = @_; | ||
| 89 | my $parser = $self->parser(); | ||
| 90 | my $file = $self->file(); | ||
| 91 | my $uuid = $self->uuid(); | ||
| 92 | my @headers = (); | ||
| 93 | my $start = 0; | ||
| 94 | my $end = 0; | ||
| 95 | my @rows; | ||
| 96 | my $line; | ||
| 97 | |||
| 98 | # in case of Noop - do not do any treatment on returned data - exit | ||
| 99 | if ( $hooks eq 'API::Eulerian::EDW::Hook::Noop' ) { | ||
| 100 | return; | ||
| 101 | } | ||
| 102 | |||
| 103 | # Process Headers line | ||
| 104 | $line = <$file>; chomp $line; | ||
| 105 | if( $parser->parse( $line ) ) { | ||
| 106 | foreach my $field ( $parser->fields() ) { | ||
| 107 | push @headers, [ 'UNKNOWN', $field ]; | ||
| 108 | } | ||
| 109 | $hooks->on_headers( $uuid, $start, $end, \@headers ); | ||
| 110 | } | ||
| 111 | |||
| 112 | # Process Next lines | ||
| 113 | while( my $line = <$file> ) { | ||
| 114 | chomp $line; | ||
| 115 | if( $parser->parse( $line ) ) { | ||
| 116 | @rows = [ $parser->fields() ]; | ||
| 117 | $hooks->on_add( $uuid, \@rows ); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | $hooks->on_status( $uuid, '', 0, 'Success', 0 ); | ||
| 122 | |||
| 123 | } | ||
| 124 | # | ||
| 125 | # Endup module properly | ||
| 126 | # | ||
| 127 | 1; |