[libcatmandu-perl] 06/101: make data command redundant
Jonas Smedegaard
dr at jones.dk
Tue Feb 23 13:43:48 UTC 2016
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository libcatmandu-perl.
commit 6e95b2ca67241f0f3b0d650f2560941f1ed3e239
Author: Nicolas Steenlant <nicolas.steenlant at ugent.be>
Date: Thu Dec 10 14:55:11 2015 +0100
make data command redundant
---
lib/Catmandu/CLI.pm | 1 +
lib/Catmandu/Cmd.pm | 33 ++++++++++
lib/Catmandu/Cmd/convert.pm | 37 +++++------
lib/Catmandu/Cmd/copy.pm | 86 ++++++++++++++++++++++++++
lib/Catmandu/Cmd/count.pm | 25 +++-----
lib/Catmandu/Cmd/data.pm | 20 ++++++
lib/Catmandu/Cmd/delete.pm | 28 +++------
lib/Catmandu/Cmd/export.pm | 52 +++++++---------
lib/Catmandu/Cmd/help.pm | 6 +-
lib/Catmandu/Cmd/import.pm | 43 ++++++-------
lib/Catmandu/Cmd/move.pm | 84 -------------------------
lib/Catmandu/Env.pm | 7 ++-
lib/Catmandu/Exporter/CSV.pm | 4 +-
lib/Catmandu/Fixable.pm | 4 +-
t/{Catmandu-Cmd-move.t => Catmandu-Cmd-copy.t} | 8 +--
15 files changed, 232 insertions(+), 206 deletions(-)
diff --git a/lib/Catmandu/CLI.pm b/lib/Catmandu/CLI.pm
index e8f3378..1c76b48 100644
--- a/lib/Catmandu/CLI.pm
+++ b/lib/Catmandu/CLI.pm
@@ -17,6 +17,7 @@ sub deleted_commands {
Catmandu::Cmd::fix_info
Catmandu::Cmd::importer_info
Catmandu::Cmd::module_info
+ Catmandu::Cmd::move
Catmandu::Cmd::store_info
)];
}
diff --git a/lib/Catmandu/Cmd.pm b/lib/Catmandu/Cmd.pm
index e29f149..243a1a7 100644
--- a/lib/Catmandu/Cmd.pm
+++ b/lib/Catmandu/Cmd.pm
@@ -54,6 +54,39 @@ sub description {
sub command_opt_spec {}
sub command {}
+# helpers
+sub _parse_options {
+ my ($self, $args) = @_;
+
+ my $a = my $from_args = [];
+ my $o = my $from_opts = {};
+ my $into_args = [];
+ my $into_opts = {};
+
+ for (my $i = 0; $i < @$args; $i++) {
+ my $arg = $args->[$i];
+ if ($arg eq 'to') {
+ $a = $into_args;
+ $o = $into_opts;
+ } elsif ($arg =~ s/^-+//) {
+ $arg =~ s/-/_/g;
+ if (exists $o->{$arg}) {
+ if (is_array_ref($o->{$arg})) {
+ push @{$o->{$arg}}, $args->[++$i];
+ } else {
+ $o->{$arg} = [$o->{$arg}, $args->[++$i]];
+ }
+ } else {
+ $o->{$arg} = $args->[++$i];
+ }
+ } else {
+ push @$a, $arg;
+ }
+ }
+
+ return $from_args, $from_opts, $into_args, $into_opts;
+}
+
1;
__END__
diff --git a/lib/Catmandu/Cmd/convert.pm b/lib/Catmandu/Cmd/convert.pm
index 09abada..71a982a 100644
--- a/lib/Catmandu/Cmd/convert.pm
+++ b/lib/Catmandu/Cmd/convert.pm
@@ -6,44 +6,35 @@ our $VERSION = '0.9505';
use parent 'Catmandu::Cmd';
use Catmandu;
-use Catmandu::Fix;
use namespace::clean;
sub command_opt_spec {
(
[ "verbose|v", "" ],
+ [ "fix=s@", "" ],
+ [ "start=i", "" ],
+ [ "total=i", "" ],
);
}
sub command {
my ($self, $opts, $args) = @_;
- my $a = my $from_args = [];
- my $o = my $from_opts = {};
- my $into_args = [];
- my $into_opts = {};
-
- for (my $i = 0; $i < @$args; $i++) {
- my $arg = $args->[$i];
- if ($arg eq 'to') {
- $a = $into_args;
- $o = $into_opts;
- } elsif ($arg =~ s/^-+//) {
- $arg =~ s/-/_/g;
- if ($arg eq 'fix') {
- push @{$o->{$arg} ||= []}, $args->[++$i];
- } else {
- $o->{$arg} = $args->[++$i];
- }
- } else {
- push @$a, $arg;
- }
- }
+ my ($from_args, $from_opts, $into_args, $into_opts) = $self->_parse_options($args);
my $from = Catmandu->importer($from_args->[0], $from_opts);
my $into = Catmandu->exporter($into_args->[0], $into_opts);
- $from = $from->benchmark if $opts->verbose;
+ if ($opts->start // $opts->total) {
+ $from = $from->slice($opts->start, $opts->total);
+ }
+ if ($opts->fix) {
+ $from = Catmandu->fixer($opts->fix)->fix($from);
+ }
+ if ($opts->verbose) {
+ $from = $from->benchmark;
+ }
+
my $n = $into->add_many($from);
$into->commit;
if ($opts->verbose) {
diff --git a/lib/Catmandu/Cmd/copy.pm b/lib/Catmandu/Cmd/copy.pm
new file mode 100644
index 0000000..27a56c0
--- /dev/null
+++ b/lib/Catmandu/Cmd/copy.pm
@@ -0,0 +1,86 @@
+package Catmandu::Cmd::move;
+
+use Catmandu::Sane;
+
+our $VERSION = '0.9505';
+
+use parent 'Catmandu::Cmd';
+use Catmandu;
+use namespace::clean;
+
+sub command_opt_spec {
+ (
+ [ "verbose|v", "" ],
+ [ "fix=s@", "" ],
+ [ "start=i", "" ],
+ [ "limit=i", "" ],
+ [ "total=i", "" ],
+ [ "cql-query|q=s", "" ],
+ [ "query=s", "" ],
+ [ "delete", "delete existing objects first" ],
+ );
+}
+
+sub command {
+ my ($self, $opts, $args) = @_;
+
+ my ($from_args, $from_opts, $into_args, $into_opts) = $self->_parse_options($args);
+
+ my $from_bag = delete $from_opts->{bag};
+ my $from = Catmandu->store($from_args->[0], $from_opts)->bag($from_bag);
+ my $into_bag = delete $into_opts->{bag};
+ my $into = Catmandu->store($into_args->[0], $into_opts)->bag($into_bag);
+
+ if ($opts->query // $opts->cql_query) {
+ $self->usage_error("Bag isn't searchable") unless $from->can('searcher');
+ $from = $from->searcher(
+ cql_query => $opts->cql_query,
+ query => $opts->query,
+ start => $opts->start,
+ total => $opts->total,
+ limit => $opts->limit,
+ );
+ } elsif ($opts->start // $opts->total) {
+ $from = $from->slice($opts->start, $opts->total);
+ }
+ if ($opts->fix) {
+ $from = Catmandu->fixer($opts->fix)->fix($from);
+ }
+ if ($opts->verbose) {
+ $from = $from->benchmark;
+ }
+
+ if ($opts->delete) {
+ $into->delete_all;
+ $into->commit;
+ }
+
+ my $n = $into->add_many($from);
+ $into->commit;
+ if ($opts->verbose) {
+ say STDERR $n ==1 ? "copied 1 object" : "copied $n objects";
+ say STDERR "done";
+ }
+}
+
+1;
+
+__END__
+
+=pod
+
+=head1 NAME
+
+Catmandu::Cmd::copy - copy objects to another store
+
+=head1 EXAMPLES
+
+ catmandu copy <STORE> <OPTIONS> to <STORE> <OPTIONS>
+
+ catmandu copy MongoDB --database_name items --bag book to \
+ ElasticSearch --index_name items --bag book
+
+ catmandu help store MongoDB
+ catmandu help store ElasticSearch
+
+=cut
diff --git a/lib/Catmandu/Cmd/count.pm b/lib/Catmandu/Cmd/count.pm
index e41d181..5b2eb3f 100644
--- a/lib/Catmandu/Cmd/count.pm
+++ b/lib/Catmandu/Cmd/count.pm
@@ -11,30 +11,25 @@ use namespace::clean;
sub command_opt_spec {
(
- [ "query|q=s", "" ],
+ [ "cql-query|q=s", "" ],
+ [ "query=s", "" ],
);
}
sub command {
my ($self, $opts, $args) = @_;
- my $from_args = [];
- my $from_opts = {};
-
- for (my $i = 0; $i < @$args; $i++) {
- my $arg = $args->[$i];
- if ($arg =~ s/^-+//) {
- $arg =~ s/-/_/g;
- $from_opts->{$arg} = $args->[++$i];
- } else {
- push @$from_args, $arg;
- }
- }
+ my ($from_args, $from_opts) = $self->_parse_options($args);
my $from_bag = delete $from_opts->{bag};
my $from = Catmandu->store($from_args->[0], $from_opts)->bag($from_bag);
- if (defined $opts->query) {
- $from = $from->searcher(query => $opts->query);
+
+ if ($opts->query // $opts->cql_query) {
+ $self->usage_error("Bag isn't searchable") unless $from->can('searcher');
+ $from = $from->searcher(
+ cql_query => $opts->cql_query,
+ query => $opts->query,
+ );
}
say $from->count;
diff --git a/lib/Catmandu/Cmd/data.pm b/lib/Catmandu/Cmd/data.pm
index 67b0b02..8e1caff 100644
--- a/lib/Catmandu/Cmd/data.pm
+++ b/lib/Catmandu/Cmd/data.pm
@@ -112,4 +112,24 @@ __END__
Catmandu::Cmd::data - store, index, search, import, export or convert objects
+=head1 DEPRECIATION NOTICE
+
+This fix is deprecated, Please use these commands instead:
+
+=over 4
+
+=item L<Catmandu::Cmd::convert>
+
+=item L<Catmandu::Cmd::copy>
+
+=item L<Catmandu::Cmd::import>
+
+=item L<Catmandu::Cmd::export>
+
+=item L<Catmandu::Cmd::count>
+
+=item L<Catmandu::Cmd::delete>
+
+=back
+
=cut
diff --git a/lib/Catmandu/Cmd/delete.pm b/lib/Catmandu/Cmd/delete.pm
index e25805c..536788a 100644
--- a/lib/Catmandu/Cmd/delete.pm
+++ b/lib/Catmandu/Cmd/delete.pm
@@ -6,44 +6,32 @@ our $VERSION = '0.9505';
use parent 'Catmandu::Cmd';
use Catmandu;
-use Catmandu::Fix;
use namespace::clean;
sub command_opt_spec {
(
- [ "query|q=s", "" ],
+ [ "cql-query|q=s", "" ],
+ [ "query=s", "" ],
);
}
sub command {
my ($self, $opts, $args) = @_;
- my $from_args = [];
- my $from_opts = {};
-
- for (my $i = 0; $i < @$args; $i++) {
- my $arg = $args->[$i];
- if ($arg =~ s/^-+//) {
- $arg =~ s/-/_/g;
- $from_opts->{$arg} = $args->[++$i];
- } else {
- push @$from_args, $arg;
- }
- }
+ my ($from_args, $from_opts) = $self->_parse_options($args);
my $from_bag = delete $from_opts->{bag};
my $from = Catmandu->store($from_args->[0], $from_opts)->bag($from_bag);
- if (defined $opts->query) {
- $from->delete_by_query(query => $opts->query);
+ if ($opts->query // $opts->cql_query) {
+ $from->delete_by_query(
+ cql_query => $opts->cql_query,
+ query => $opts->query,
+ );
} else {
$from->delete_all;
}
$from->commit;
-
- unless ($from->count == 0) {
- say STDERR "error: $from is not empty";
- }
}
1;
diff --git a/lib/Catmandu/Cmd/export.pm b/lib/Catmandu/Cmd/export.pm
index ecd11e9..975f96b 100644
--- a/lib/Catmandu/Cmd/export.pm
+++ b/lib/Catmandu/Cmd/export.pm
@@ -6,52 +6,48 @@ our $VERSION = '0.9505';
use parent 'Catmandu::Cmd';
use Catmandu;
-use Catmandu::Fix;
use namespace::clean;
sub command_opt_spec {
(
[ "verbose|v", "" ],
- [ "query|q=s", "" ],
+ [ "fix=s@", "" ],
+ [ "start=i", "" ],
[ "limit=i", "" ],
+ [ "total=i", "" ],
+ [ "cql-query|q=s", "" ],
+ [ "query=s", "" ],
);
}
sub command {
my ($self, $opts, $args) = @_;
- my $a = my $from_args = [];
- my $o = my $from_opts = {};
- my $into_args = [];
- my $into_opts = {};
-
- for (my $i = 0; $i < @$args; $i++) {
- my $arg = $args->[$i];
- if ($arg eq 'to') {
- $a = $into_args;
- $o = $into_opts;
- } elsif ($arg =~ s/^-+//) {
- $arg =~ s/-/_/g;
- if ($arg eq 'fix') {
- push @{$o->{$arg} ||= []}, $args->[++$i];
- } else {
- $o->{$arg} = $args->[++$i];
- }
- } else {
- push @$a, $arg;
- }
- }
+ my ($from_args, $from_opts, $into_args, $into_opts) = $self->_parse_options($args);
my $from_bag = delete $from_opts->{bag};
my $from = Catmandu->store($from_args->[0], $from_opts)->bag($from_bag);
my $into = Catmandu->exporter($into_args->[0], $into_opts);
- if (defined $opts->query) {
- $from = $from->searcher(query => $opts->query, total => $opts->limit);
- } elsif (defined $opts->limit) {
- $from = $from->take($opts->limit);
+
+ if ($opts->query // $opts->cql_query) {
+ $self->usage_error("Bag isn't searchable") unless $from->can('searcher');
+ $from = $from->searcher(
+ cql_query => $opts->cql_query,
+ query => $opts->query,
+ start => $opts->start,
+ total => $opts->total,
+ limit => $opts->limit,
+ );
+ } elsif ($opts->start // $opts->total) {
+ $from = $from->slice($opts->start, $opts->total);
+ }
+ if ($opts->fix) {
+ $from = Catmandu->fixer($opts->fix)->fix($from);
+ }
+ if ($opts->verbose) {
+ $from = $from->benchmark;
}
- $from = $from->benchmark if $opts->verbose;
my $n = $into->add_many($from);
$into->commit;
if ($opts->verbose) {
diff --git a/lib/Catmandu/Cmd/help.pm b/lib/Catmandu/Cmd/help.pm
index 49949c8..59c4265 100644
--- a/lib/Catmandu/Cmd/help.pm
+++ b/lib/Catmandu/Cmd/help.pm
@@ -29,12 +29,12 @@ my %MODULES = (
],
},
Store => {
- re => qr/^(store|move)$/i,
+ re => qr/^(store|copy)$/i,
usage => [
"catmandu import ... to %n [options]",
- "catmandu move ... to %n [options]",
+ "catmandu copy ... to %n [options]",
"catmandu export %n [options] ...",
- "catmandu move %n [options] ...",
+ "catmandu copy %n [options] ...",
]
},
Fix => {
diff --git a/lib/Catmandu/Cmd/import.pm b/lib/Catmandu/Cmd/import.pm
index 09d9d5d..8bdb799 100644
--- a/lib/Catmandu/Cmd/import.pm
+++ b/lib/Catmandu/Cmd/import.pm
@@ -6,45 +6,42 @@ our $VERSION = '0.9505';
use parent 'Catmandu::Cmd';
use Catmandu;
-use Catmandu::Fix;
use namespace::clean;
sub command_opt_spec {
(
[ "verbose|v", "" ],
+ [ "fix=s@", "" ],
+ [ "start=i", "" ],
+ [ "total=i", "" ],
+ [ "delete", "delete existing objects first" ],
);
}
sub command {
my ($self, $opts, $args) = @_;
- my $a = my $from_args = [];
- my $o = my $from_opts = {};
- my $into_args = [];
- my $into_opts = {};
-
- for (my $i = 0; $i < @$args; $i++) {
- my $arg = $args->[$i];
- if ($arg eq 'to') {
- $a = $into_args;
- $o = $into_opts;
- } elsif ($arg =~ s/^-+//) {
- $arg =~ s/-/_/g;
- if ($arg eq 'fix') {
- push @{$o->{$arg} ||= []}, $args->[++$i];
- } else {
- $o->{$arg} = $args->[++$i];
- }
- } else {
- push @$a, $arg;
- }
- }
+ my ($from_args, $from_opts, $into_args, $into_opts) = $self->_parse_options($args);
my $from = Catmandu->importer($from_args->[0], $from_opts);
my $into_bag = delete $into_opts->{bag};
my $into = Catmandu->store($into_args->[0], $into_opts)->bag($into_bag);
- $from = $from->benchmark if $opts->verbose;
+ if ($opts->start // $opts->total) {
+ $from = $from->slice($opts->start, $opts->total);
+ }
+ if ($opts->fix) {
+ $from = Catmandu->fixer($opts->fix)->fix($from);
+ }
+ if ($opts->verbose) {
+ $from = $from->benchmark;
+ }
+
+ if ($opts->delete) {
+ $into->delete_all;
+ $into->commit;
+ }
+
my $n = $into->add_many($from);
$into->commit;
if ($opts->verbose) {
diff --git a/lib/Catmandu/Cmd/move.pm b/lib/Catmandu/Cmd/move.pm
deleted file mode 100644
index 2f85249..0000000
--- a/lib/Catmandu/Cmd/move.pm
+++ /dev/null
@@ -1,84 +0,0 @@
-package Catmandu::Cmd::move;
-
-use Catmandu::Sane;
-
-our $VERSION = '0.9505';
-
-use parent 'Catmandu::Cmd';
-use Catmandu;
-use Catmandu::Fix;
-use namespace::clean;
-
-sub command_opt_spec {
- (
- [ "verbose|v", "" ],
- [ "query|q=s", "" ],
- [ "limit=i", "" ],
- );
-}
-
-sub command {
- my ($self, $opts, $args) = @_;
-
- my $a = my $from_args = [];
- my $o = my $from_opts = {};
- my $into_args = [];
- my $into_opts = {};
-
- for (my $i = 0; $i < @$args; $i++) {
- my $arg = $args->[$i];
- if ($arg eq 'to') {
- $a = $into_args;
- $o = $into_opts;
- } elsif ($arg =~ s/^-+//) {
- $arg =~ s/-/_/g;
- if ($arg eq 'fix') {
- push @{$o->{$arg} ||= []}, $args->[++$i];
- } else {
- $o->{$arg} = $args->[++$i];
- }
- } else {
- push @$a, $arg;
- }
- }
-
- my $from_bag = delete $from_opts->{bag};
- my $into_bag = delete $into_opts->{bag};
- my $from = Catmandu->store($from_args->[0], $from_opts)->bag($from_bag);
- my $into = Catmandu->store($into_args->[0], $into_opts)->bag($into_bag);
- if (defined $opts->query) {
- $from = $from->searcher(query => $opts->query, total => $opts->limit);
- } elsif (defined $opts->limit) {
- $from = $from->take($opts->limit);
- }
-
- $from = $from->benchmark if $opts->verbose;
- my $n = $into->add_many($from);
- $into->commit;
- if ($opts->verbose) {
- say STDERR $n ==1 ? "moved 1 object" : "moved $n objects";
- say STDERR "done";
- }
-}
-
-1;
-
-__END__
-
-=pod
-
-=head1 NAME
-
-Catmandu::Cmd::move - move objects to another store
-
-=head1 EXAMPLES
-
- catmandu move <STORE> <OPTIONS> to <STORE> <OPTIONS>
-
- catmandu move MongoDB --database_name items --bag book to \
- ElasticSearch --index_name items --bag book
-
- catmandu help store MongoDB
- catmandu help store ElasticSearch
-
-=cut
diff --git a/lib/Catmandu/Env.pm b/lib/Catmandu/Env.pm
index ec26078..35f5b75 100644
--- a/lib/Catmandu/Env.pm
+++ b/lib/Catmandu/Env.pm
@@ -141,10 +141,15 @@ sub store {
sub fixer {
my $self = shift;
- if (ref $_[0]) {
+
+ if (is_array_ref($_[0])) {
return Catmandu::Fix->new(fixes => $_[0]);
}
+ if (ref($_[0])) {
+ return Catmandu::Fix->new(fixes => [$_[0]]);
+ }
+
my $key = $_[0] || $self->default_fixer;
my $fixers = $self->fixers;
diff --git a/lib/Catmandu/Exporter/CSV.pm b/lib/Catmandu/Exporter/CSV.pm
index c0b2f57..f732ee2 100644
--- a/lib/Catmandu/Exporter/CSV.pm
+++ b/lib/Catmandu/Exporter/CSV.pm
@@ -24,7 +24,7 @@ has fields => (
$self->{fields} = _coerce_list($fields);
if (ref $fields and ref $fields eq 'HASH') {
$self->{header} = [
- map { $fields->{$_} // $_ } @{$self->{fields}}
+ map { $fields->{$_} // $_ } @{$self->{fields}}
];
}
},
@@ -43,7 +43,7 @@ sub _build_csv {
binary => 1,
eol => "\n",
sep_char => $self->sep_char,
- always_quote => $self->always_quote,
+ always_quote => $self->always_quote,
quote_char => $self->quote_char ? $self->quote_char : undef,
escape_char => $self->escape_char ? $self->escape_char : undef,
});
diff --git a/lib/Catmandu/Fixable.pm b/lib/Catmandu/Fixable.pm
index 6d4577c..0527240 100644
--- a/lib/Catmandu/Fixable.pm
+++ b/lib/Catmandu/Fixable.pm
@@ -12,9 +12,7 @@ use namespace::clean;
has _fixer => (
is => 'ro',
init_arg => 'fix',
- coerce => sub {
- is_instance($_[0]) ? $_[0] : Catmandu->fixer($_[0]);
- },
+ coerce => sub { Catmandu->fixer($_[0]) },
);
1;
diff --git a/t/Catmandu-Cmd-move.t b/t/Catmandu-Cmd-copy.t
similarity index 57%
rename from t/Catmandu-Cmd-move.t
rename to t/Catmandu-Cmd-copy.t
index 572bffd..fec9026 100644
--- a/t/Catmandu-Cmd-move.t
+++ b/t/Catmandu-Cmd-copy.t
@@ -8,16 +8,16 @@ use App::Cmd::Tester;
my $pkg;
BEGIN {
- $pkg = 'Catmandu::Cmd::move';
+ $pkg = 'Catmandu::Cmd::copy';
use_ok $pkg;
}
require_ok $pkg;
use Catmandu::CLI;
-my $result = test_app(qq|Catmandu::CLI| => [ qw(move -v test to Hash) ]);
+my $result = test_app(qq|Catmandu::CLI| => [ qw(copy -v test to Hash) ]);
-like $result->stderr, qr/moved 4 objects/, 'moved 4 objects' ;
+like $result->stderr, qr/copied 4 objects/, 'copied 4 objects' ;
is $result->error, undef, 'threw no exceptions' ;
-done_testing 4;
\ No newline at end of file
+done_testing;
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libcatmandu-perl.git
More information about the Pkg-perl-cvs-commits
mailing list