line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Cmd::convert; |
2
|
|
|
|
|
|
|
|
3
|
14
|
|
|
14
|
|
134843
|
use Catmandu::Sane; |
|
14
|
|
|
|
|
49
|
|
|
14
|
|
|
|
|
98
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.2020'; |
6
|
|
|
|
|
|
|
|
7
|
14
|
|
|
14
|
|
106
|
use parent 'Catmandu::Cmd'; |
|
14
|
|
|
|
|
44
|
|
|
14
|
|
|
|
|
77
|
|
8
|
14
|
|
|
14
|
|
1098
|
use Catmandu; |
|
14
|
|
|
|
|
38
|
|
|
14
|
|
|
|
|
77
|
|
9
|
14
|
|
|
14
|
|
3028
|
use Catmandu::Util qw(array_includes); |
|
14
|
|
|
|
|
47
|
|
|
14
|
|
|
|
|
714
|
|
10
|
14
|
|
|
14
|
|
90
|
use namespace::clean; |
|
14
|
|
|
|
|
30
|
|
|
14
|
|
|
|
|
81
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub command_opt_spec { |
13
|
|
|
|
|
|
|
( |
14
|
15
|
|
|
15
|
1
|
232
|
["verbose|v", ""], |
15
|
|
|
|
|
|
|
["fix=s@", ""], |
16
|
|
|
|
|
|
|
["var=s%", ""], |
17
|
|
|
|
|
|
|
["preprocess|pp", ""], |
18
|
|
|
|
|
|
|
["start=i", ""], |
19
|
|
|
|
|
|
|
["total=i", ""], |
20
|
|
|
|
|
|
|
["id=s@", ""], |
21
|
|
|
|
|
|
|
[ |
22
|
|
|
|
|
|
|
"id-file=s", |
23
|
|
|
|
|
|
|
"A line-delimited file containing the id's to include in the conversion. Other records will be ignored." |
24
|
|
|
|
|
|
|
], |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub command { |
29
|
15
|
|
|
15
|
1
|
54
|
my ($self, $opts, $args) = @_; |
30
|
|
|
|
|
|
|
|
31
|
15
|
|
|
|
|
87
|
my ($from_args, $from_opts, $into_args, $into_opts) |
32
|
|
|
|
|
|
|
= $self->_parse_options($args); |
33
|
|
|
|
|
|
|
|
34
|
15
|
|
|
|
|
99
|
my $from = Catmandu->importer($from_args->[0], $from_opts); |
35
|
14
|
|
|
|
|
194
|
my $into = Catmandu->exporter($into_args->[0], $into_opts); |
36
|
|
|
|
|
|
|
|
37
|
12
|
100
|
66
|
|
|
242
|
if ($opts->id // $opts->id_file) { |
|
|
100
|
66
|
|
|
|
|
38
|
1
|
|
|
|
|
11
|
my $id_map = {}; |
39
|
1
|
50
|
|
|
|
3
|
if (my $ids = $opts->id) { |
40
|
0
|
|
|
|
|
0
|
$id_map->{$_} = 1 for @$ids; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
else { |
43
|
|
|
|
|
|
|
Catmandu->importer('Text', file => $opts->id_file)->each( |
44
|
|
|
|
|
|
|
sub { |
45
|
3
|
|
|
3
|
|
9
|
$id_map->{$_[0]->{text}} = 1; |
46
|
|
|
|
|
|
|
} |
47
|
1
|
|
|
|
|
20
|
); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
$from = $from->select( |
50
|
1
|
50
|
|
4
|
|
32
|
sub {defined $_[0]->{_id} && exists $id_map->{$_[0]->{_id}}}); |
|
4
|
|
|
|
|
42
|
|
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
elsif ($opts->start // $opts->total) { |
53
|
1
|
|
|
|
|
23
|
$from = $from->slice($opts->start, $opts->total); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
12
|
100
|
|
|
|
268
|
if ($opts->fix) { |
57
|
5
|
|
|
|
|
63
|
$from = $self->_build_fixer($opts)->fix($from); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
9
|
100
|
|
|
|
98
|
if ($opts->verbose) { |
61
|
2
|
|
|
|
|
17
|
$from = $from->benchmark; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
9
|
|
|
|
|
98
|
my $n = $into->add_many($from); |
65
|
7
|
|
|
|
|
144
|
$into->commit; |
66
|
7
|
100
|
|
|
|
24
|
if ($opts->verbose) { |
67
|
2
|
50
|
|
|
|
153
|
say STDERR $n == 1 ? "converted 1 item" : "converted $n items"; |
68
|
2
|
|
|
|
|
37
|
say STDERR "done"; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
1; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
__END__ |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=pod |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 NAME |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
Catmandu::Cmd::convert - convert items |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 EXAMPLES |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
catmandu convert <IMPORTER> <OPTIONS> to <EXPORTER> <OPTIONS> |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
cat books.json | catmandu convert JSON to CSV --fields id,title |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
catmandu help importer JSON |
89
|
|
|
|
|
|
|
catmandu help exporter YAML |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=cut |