| 1 | package App::Tables::Provider::xls; | ||
| 2 | 2 | ▶ | use Modern::Perl; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | require App::Tables::Excel; | ||
| 4 | |||
| 5 | =head1 TODO | ||
| 6 | |||
| 7 | everything works fine now. need comments, tests and documentation. | ||
| 8 | |||
| 9 | =head1 Contribute | ||
| 10 | |||
| 11 | yes, please | ||
| 12 | |||
| 13 | https://github.com/eiro/app-tables | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | # qw< tables2xls xls2tables >; | ||
| 18 | # qw< tables2xls xls2tables >; | ||
| 19 | # use Data::Table::Excel qw< tables2xls xls2tables >; | ||
| 20 | # use Modern::Perl; | ||
| 21 | |||
| 22 | 0 | ▶ | sub new { |
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||
| 23 | 0 | my $self = pop; | |
| 24 | state $io = | ||
| 25 | 0 | ▶ | { reader => sub { |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 26 | 0 | App::Tables::Excel::tables_from_file | |
| 27 | ( (shift) | ||
| 28 | , qw( format 2003 headers_are built )) | ||
| 29 | } | ||
| 30 | 0 | , writer => Data::Table::Excel->can('tables2xls') }; | |
| 31 | 0 | bless { %$self, %$io } , __PACKAGE__; | |
| 32 | } | ||
| 33 | |||
| 34 | 0 | ▶ | sub read { |
|
Subroutine
read: not called CC=1 SCAR=6.9
| |||
| 35 | 0 | my ( $self ) = @_; | |
| 36 | 0 | my ( $data, $headers ) = $self->{reader}( $$self{base} ); | |
| 37 | 0 | my %whole; | |
| 38 | 0 | @whole{@$headers} = @$data; | |
| 39 | 0 | \%whole; | |
| 40 | } | ||
| 41 | |||
| 42 | 0 | ▶ | sub write { |
|
Subroutine
write: not called CC=1 SCAR=6.9
| |||
| 43 | 0 | my ( $self, $data ) = @_; | |
| 44 | 0 | my @headers = keys %$data; | |
| 45 | 0 | my @data = map { $$data{$_} } @headers; | |
| 46 | 0 | $self->{writer}( $$self{base}, \@data, \@headers ); | |
| 47 | } | ||
| 48 | |||
| 49 | package App::Tables::Provider::xlsx; | ||
| 50 | require Data::Table::Excel; | ||
| 51 | 2 | ▶ | use Modern::Perl; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 52 | our @ISA = 'App::Tables::Provider::xls'; | ||
| 53 | # use parent 'App::Tables::Provider::xls'; | ||
| 54 | |||
| 55 | 0 | ▶ | sub new { |
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||
| 56 | 0 | my $self = pop; | |
| 57 | state $io = | ||
| 58 | 0 | ▶ | { reader => sub { |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 59 | 0 | App::Tables::Excel::tables_from_file | |
| 60 | ( (shift) | ||
| 61 | , qw( format 2007 headers_are built )) | ||
| 62 | } | ||
| 63 | 0 | , writer => Data::Table::Excel->can('tables2xlsx') }; | |
| 64 | 0 | bless { %$self, %$io } , __PACKAGE__; | |
| 65 | } | ||
| 66 | |||
| 67 | package App::Tables::Provider::dir; | ||
| 68 | 2 | ▶ | use Modern::Perl; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 69 | 2 | ▶ | use IO::All; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 70 | |||
| 71 | 0 | ▶ | sub new { bless pop, __PACKAGE__ } |
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||
| 72 | |||
| 73 | 0 | ▶ | sub read { |
|
Subroutine
read: not called CC=1 SCAR=3.3
| |||
| 74 | 0 | my ( $self ) = @_; | |
| 75 | 0 | my @headers; | |
| 76 | my @data = map { | ||
| 77 | 0 | push @headers, m{ ([^/]+) $ }x; # basename | |
| 78 | 0 | Data::Table::fromTSV $_ | |
| 79 | 2 | ▶ | } glob "$$self{base}/*"; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 80 | 0 | my %whole; | |
| 81 | 0 | @whole{ @headers } = @data; | |
| 82 | 0 | \%whole | |
| 83 | } | ||
| 84 | |||
| 85 | sub write { | ||
| 86 | my ( $self, $data ) = @_; | ||
| 87 | map { -d $_ or io($_)->mkpath } $$self{base}; | ||
| 88 | while ( my ( $name, $sheet) = each $data ) { | ||
| 89 | io( "$$self{base}/$name" ) < $sheet->tsv(0); | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | package App::Tables; | ||
| 94 | # ABSTRACT: manipulation of tables from any sources | ||
| 95 | our $VERSION = '0.4'; | ||
| 96 | |||
| 97 | use Modern::Perl; | ||
| 98 | use Exporter 'import'; | ||
| 99 | our @EXPORT_OK = qw< | ||
| 100 | init | ||
| 101 | >; | ||
| 102 | |||
| 103 | our %EXPORT_TAGS = | ||
| 104 | ( all=> \@EXPORT_OK ); | ||
| 105 | |||
| 106 | # possible types are xls, xslx and / | ||
| 107 | # could be some urlized dsn+query stuff | ||
| 108 | |||
| 109 | sub _init_file { | ||
| 110 | my ( $put, $type, $desc, $args ) = @_; | ||
| 111 | { base => ($$args{$put} || die "no data while grabbing $desc" ) | ||
| 112 | , type => ($$args{$type} || do { | ||
| 113 | }) } | ||
| 114 | } | ||
| 115 | |||
| 116 | sub extension_of { | ||
| 117 | (shift) =~ qr{ | ||
| 118 | (?: (?<type> / ) | ||
| 119 | | [.] (?<type> xlsx? ) | ||
| 120 | )$ | ||
| 121 | }x and $+{type}; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub _file_spec { | ||
| 125 | my ( $data, $type ) = @_; | ||
| 126 | defined $data or die "no data"; | ||
| 127 | map { $_ eq '/' and $_ = 'dir' } | ||
| 128 | $type ||= extension_of($data) || 'dir'; | ||
| 129 | |||
| 130 | { base => $data | ||
| 131 | , type => $type } | ||
| 132 | |||
| 133 | } | ||
| 134 | |||
| 135 | sub init { | ||
| 136 | my %args = @_ ? @_ : @ARGV; | ||
| 137 | my %conf = map { | ||
| 138 | $args{$_} | ||
| 139 | ? ( $_ => [ split /,/, $args{$_} ] ) | ||
| 140 | : () | ||
| 141 | } qw< can >; | ||
| 142 | |||
| 143 | map { die "no $_" unless $args{$_} } | ||
| 144 | qw< from to >; | ||
| 145 | |||
| 146 | $conf{from} = _file_spec @args{qw< from is >}; | ||
| 147 | $conf{to} = _file_spec @args{qw< to will >}; | ||
| 148 | \%conf | ||
| 149 | } | ||
| 150 | |||
| 151 | sub provider { | ||
| 152 | my $spec = shift; | ||
| 153 | my $provider = "App::Tables::Provider::$$spec{type}"; | ||
| 154 | $provider->new( $spec ) | ||
| 155 | } | ||
| 156 | |||
| 157 | 1; | ||