simple CSV file import
authorDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 22 Jun 2010 11:29:27 +0000 (13:29 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 22 Jun 2010 11:29:27 +0000 (13:29 +0200)
lib/MojoFacets/Data.pm
lib/MojoFacets/Import/CSV.pm [new file with mode: 0644]

index 17cd670..5e59e7f 100644 (file)
@@ -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 (file)
index 0000000..7d62f15
--- /dev/null
@@ -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