blib/lib/App/RecordStream/Operation/tocsv.pm

filter: Statementstmt 100% 57 / 57 Branchbran 87.5% 7 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 5 totaltotal 92.5% 75 / 81 CC 6 SCAR 8.7
CC
6
Cov
98%
accept_record
5.0
init
2.0
BEGIN
1.0
CRAP 2.4
« blib/lib/App/RecordStream/Clumper/Options.pm ↑ Summary blib/lib/App/RecordStream/Operation/fromxferlog.pm »
1 package App::RecordStream::Operation::tocsv;
2  
3 2 our $VERSION = "4.0.23";
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use base qw(App::RecordStream::Operation);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use App::RecordStream::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub init {
13 6 ▶ my $this = shift;
Subroutine init: called CC=2 SCAR=6.9
Pod undocumented
14 6 my $args = shift;
15  
16 6 my $header = 1;
17 6 my $key_groups = App::RecordStream::KeyGroups->new();
18 6 my $delim = ',';
19 my $spec = {
20 3 ▶ "key|k=s" => sub { $key_groups->add_groups($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 1 ▶ "noheader|nh" => sub { $header = 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 6 "delim|d=s" => \$delim,
23 };
24  
25 6 $this->parse_options($args, $spec);
26 5 $this->{'KEY_GROUPS'} = $key_groups;
27  
28 5 ▶ die "Delimiter must be a single character\n\n"
Branch: 1 branch
BranchTrueFalseTotal
unless length $delim == 1 0 5 5
29 unless length $delim == 1;
30  
31 # binary => 1 is to handle new lines in field values
32 5 $this->{'CSV'} = Text::CSV->new({
33 binary => 1,
34 sep_char => $delim,
35 });
36  
37 5 $this->{'FIRST'} = 1;
38 5 $this->{'HEADERS'} = $header;
39 };
40  
41 sub accept_record {
42 25 ▶ my $this = shift;
Subroutine accept_record: called CC=5 SCAR=16.1
Pod undocumented
43 25 my $record = shift;
44  
45 25 ▶ if ( $this->{'FIRST'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"FIRST"}) 5 20 25
46 5 $this->{'FIRST'} = 0;
47  
48 5 ▶ if ( $this->{'KEY_GROUPS'}->has_any_group() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'KEY_GROUPS'}->has_any_group) { } 3 2 5
49 3 $this->{'KEYS'} = $this->{'KEY_GROUPS'}->get_keyspecs($record);
50 }
51 else {
52 2 $this->{'KEYS'} = [sort keys %$record];
53 }
54  
55 5 ▶ if ( $this->{'HEADERS'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"HEADERS"}) 4 1 5
56 4 $this->output_values($this->{'KEYS'});
57 }
58 }
59  
60 25 my @values;
61 25 foreach my $key (@{$this->{'KEYS'}}) {
62 45 push @values, ${$record->guess_key_from_spec($key)};
63 }
64  
65 25 $this->output_values(\@values);
66  
67 25 return 1;
68 }
69  
70 sub output_values {
71 29 ▶ my $this = shift;
Subroutine output_values: called CC=1 SCAR=0.0
Pod undocumented
72 29 my $values = shift;
73  
74 29 my $csv = $this->{'CSV'};
75 29 $csv->combine(@$values);
76 29 $this->push_line($csv->string());
77 }
78  
79 sub add_help_types {
80 6 ▶ my $this = shift;
Subroutine add_help_types: called CC=1 SCAR=0.0
Pod undocumented
81 6 $this->use_help_type('keyspecs');
82 6 $this->use_help_type('keygroups');
83 6 $this->use_help_type('keys');
84 }
85  
86 sub usage {
87 1 ▶ my $this = shift;
Subroutine usage: called CC=1 SCAR=0.0
Pod undocumented
88  
89 1 my $options = [
90 ['noheader|--nh', 'Do not output headers on the first line'],
91 ['key|-k <keyspec>', 'Comma separated keys to output. Defaults to all fields in first record. May be a keyspec, may be a keyspec group'],
92 ['delim|-d <character>', "Field delimiter to use when outputting lines (default ',')."],
93 ];
94  
95 1 my $args_string = $this->options_string($options);
96  
97 1 return <<USAGE;
98 Usage: recs-tocsv <options> [files]
99 __FORMAT_TEXT__
100 This script outputs csv formatted recs. With the --delim option, it can
101 output tsv or other line-based formats with character-separated fields.
102 __FORMAT_TEXT__
103  
104 Arguments:
105 $args_string
106  
107 Examples
108 # Print records to csv format with headers
109 recs-tocsv myrecords
110  
111 # Only print time and a nested value of stat/avg
112 ... | recs-tocsv --key time,stat/avg
113 USAGE
114 }
115  
116 2 1;
« blib/lib/App/RecordStream/Clumper/Options.pm ↑ Summary blib/lib/App/RecordStream/Operation/fromxferlog.pm »