From: Dobrica Pavlinusic Date: Tue, 22 Jun 2010 11:29:27 +0000 (+0200) Subject: simple CSV file import X-Git-Url: http://git.rot13.org/?p=MojoFacets.git;a=commitdiff_plain;h=3b7227ff4c1d353f00c3e0a63c25e45f98771aca;ds=sidebyside simple CSV file import --- diff --git a/lib/MojoFacets/Data.pm b/lib/MojoFacets/Data.pm index 17cd670..5e59e7f 100644 --- a/lib/MojoFacets/Data.pm +++ b/lib/MojoFacets/Data.pm @@ -16,6 +16,7 @@ use File::Path qw(mkpath); use MojoFacets::Import::File; use MojoFacets::Import::HTMLTable; +use MojoFacets::Import::CSV; our $loaded; our $filters; @@ -38,6 +39,9 @@ sub index { } elsif ( -d $file && $file =~ m/\.html$/ ) { $file =~ s/$data_dir\/*//; push @files, $file; + } elsif ( -f $file && $file =~ m/\.csv$/i ) { + $file =~ s/$data_dir\/*//; + push @files, $file; } else { #warn "IGNORE: $file\n"; } @@ -178,7 +182,11 @@ sub _load_path { my $data; if ( -f $full_path ) { - $data = MojoFacets::Import::File->new( full_path => $full_path, path => $path )->data; + if ( $full_path =~ m/.csv/i ) { + $data = MojoFacets::Import::CSV->new( full_path => $full_path )->data; + } else { + $data = MojoFacets::Import::File->new( full_path => $full_path, path => $path )->data; + } } elsif ( -d $full_path && $full_path =~ m/.html/ ) { $data = MojoFacets::Import::HTMLTable->new( dir => $full_path )->data; } else { diff --git a/lib/MojoFacets/Import/CSV.pm b/lib/MojoFacets/Import/CSV.pm new file mode 100644 index 0000000..7d62f15 --- /dev/null +++ b/lib/MojoFacets/Import/CSV.pm @@ -0,0 +1,50 @@ +package MojoFacets::Import::CSV; + +use warnings; +use strict; + +use base 'Mojo::Base'; + +use HTML::TableExtract; +use File::Slurp; +use Data::Dump qw(dump); +use JSON; + +__PACKAGE__->attr('path'); +__PACKAGE__->attr('full_path'); + +sub data { + my $self = shift; + + my $path = $self->path; + + my $data = read_file $self->full_path; + + my @lines = split(/\r?\n/, $data); + $data = { items => [] }; + + my $delimiter = qr/;/; + + shift @lines; # FIXME ship non-header line + my $header_line = shift @lines; + + my @header = split( $delimiter, $header_line ); + warn "# header ",dump( @header ); + + while ( my $line = shift @lines ) { + chomp $line; + my @v = split($delimiter, $line); + my $item; + foreach my $i ( 0 .. $#v ) { + $item->{ $header[$i] || "f_$i" } = [ $v[$i] ]; + } + push @{ $data->{items} }, $item; + } + + $data->{header} = [ @header ]; + + return $data; + +} + +1