added .pairs format NAME1="VAL1" NAME2="VAL2" ...
authorDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 24 Aug 2012 12:51:29 +0000 (14:51 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 24 Aug 2012 13:20:57 +0000 (15:20 +0200)
README
data/lsblk/lsblk.sh [new file with mode: 0755]
lib/MojoFacets/Data.pm
lib/MojoFacets/Import/Pairs.pm [new file with mode: 0644]
t/MojoFacets-Import-Pairs.t [new file with mode: 0755]

diff --git a/README b/README
index 55a3abb..da01a38 100644 (file)
--- a/README
+++ b/README
@@ -18,6 +18,8 @@ and can be specified in filename, before extension like this: data.encoding.csv
 CouchDB data can be imported using files which contain full url to CouchDB database
 or url to CouchDB view to import. URL's filename should end in *.couchdb
 
+lsblk .pairs format is basically shell variables in form NAME="value"
+
 
 Start with:
 
diff --git a/data/lsblk/lsblk.sh b/data/lsblk/lsblk.sh
new file mode 100755 (executable)
index 0000000..a907819
--- /dev/null
@@ -0,0 +1,5 @@
+#!/bin/sh -x
+
+cols=`lsblk -h | grep '^ *[A-Z][A-Z][A-Z]*' | sed 's/^ *//' | cut -d" " -f1 | grep -v DISC-ZERO | xargs echo | sed 's/ /,/g'`
+echo $cols
+lsblk --pairs --output $cols > `hostname`.pairs
index d5a7284..03c1668 100644 (file)
@@ -22,6 +22,7 @@ use MojoFacets::Import::HTMLTable;
 use MojoFacets::Import::CSV;
 use MojoFacets::Import::CouchDB;
 use MojoFacets::Import::SQL;
+use MojoFacets::Import::Pairs;
 
 our $loaded;
 our $filters;
@@ -44,7 +45,7 @@ sub index {
                } elsif ( -d $file && $file =~ m/\.html$/ ) {
                        $file =~ s/$data_dir\/*//;
                        push @files, $file;
-               } elsif ( -f $file && $file =~ m/\.(csv|storable|couchdb|sql)$/i ) {
+               } elsif ( -f $file && $file =~ m/\.(csv|storable|couchdb|sql|pairs)$/i ) {
                        $file =~ s/$data_dir\/*//;
                        push @files, $file;
                } else {
@@ -222,6 +223,8 @@ sub _load_path {
                        $data = MojoFacets::Import::SQL->new( full_path => $full_path )->data;
                } elsif ( $full_path =~ m/.couchdb/i ) {
                        $data = MojoFacets::Import::CouchDB->new( full_path => $full_path )->data;
+               } elsif ( $full_path =~ m/.pairs/i ) {
+                       $data = MojoFacets::Import::Pairs->new( full_path => $full_path )->data;
                } else {
                        $data = MojoFacets::Import::File->new( full_path => $full_path, path => $path )->data;
                }
diff --git a/lib/MojoFacets/Import/Pairs.pm b/lib/MojoFacets/Import/Pairs.pm
new file mode 100644 (file)
index 0000000..5bef5a1
--- /dev/null
@@ -0,0 +1,44 @@
+package MojoFacets::Import::Pairs;
+
+use warnings;
+use strict;
+
+use base 'Mojo::Base';
+
+use Data::Dump qw(dump);
+
+__PACKAGE__->attr('full_path');
+
+sub data {
+       my $self = shift;
+
+       my $path = $self->full_path;
+
+       my $data = { items => [] };
+       my $need_header = 1;
+
+       open(my $fh, $path) || die "$path: $!";
+       while(<$fh>) {
+               chomp;
+               warn "## $_\n";
+               my @header;
+               my %item = (
+                       map {
+                               my ($k,$v) = split(/="/,$_,2);
+                               push @header, $k if $need_header;
+                               ( $k => $v );
+                       } split(/"\s/, $_)
+               );
+               push @{ $data->{items} }, \%item;
+
+               if ( $need_header ) {
+                       $data->{header} = [ @header ];
+                       $need_header = 0;
+               }
+       }
+
+       return $data;
+
+}
+
+1
diff --git a/t/MojoFacets-Import-Pairs.t b/t/MojoFacets-Import-Pairs.t
new file mode 100755 (executable)
index 0000000..332ff5c
--- /dev/null
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 3;
+use Data::Dump qw(dump);
+
+use lib 'lib';
+
+use_ok('MojoFacets::Import::Pairs');
+
+my $path = $ARGV[0] || (glob 'data/lsblk/*.pairs')[0];
+diag "using $path";
+
+ok( my $o = MojoFacets::Import::Pairs->new( path => $path ), 'new' );
+
+ok( my $data = $o->data, 'data' );
+diag dump($data);