Tools/FlatFile.pm

filter: Statementstmt 82.6% 95 / 115 Branchbran 42.5% 23 / 54 Conditioncond 61.9% 13 / 21 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 10 totaltotal 66.2% 151 / 228 CC 42 SCAR 26.8
CC
42
Cov
69%
emit_junk
37.3
load_thread
16.4
emit_thread
13.6
CRAP 14.6
« Tools/CLI/ToHTML.pm ↑ Summary Tools/ArchiveLink/GoogleGroups.pm »
1 #!/usr/bin/perl
2  
3 package Mail::Summary::Tools::FlatFile;
4 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use File::Slurp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use YAML::Syck ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Mail::Summary::Tools::ArchiveLink::Hardcoded;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 has summary => (
12 isa => "Mail::Summary::Tools::Summary",
13 is => "rw",
14 required => 1,
15 );
16  
17 2 has uri_type => (
18 isa => "Str",
19 is => "rw",
20 default => "thread_uri",
21 );
22  
23 2 has skip_summarized => (
24 isa => "Bool",
25 is => "rw",
26 default => 0,
27 );
28  
29 2 has include_hidden => (
30 isa => "Bool",
31 is => "rw",
32 default => 0,
33 );
34  
35 2 has list_posters => (
36 isa => "Bool",
37 is => "rw",
38 default => 0,
39 );
40  
41 2 has add_links => (
42 isa => "Bool",
43 is => "rw",
44 default => 0,
45 );
46  
47 2 has list_dates => (
48 isa => "Bool",
49 is => "rw",
50 default => 0,
51 );
52  
53 2 has list_misc => (
54 isa => "Bool",
55 is => "rw",
56 default => 0,
57 );
58  
59 has extra_fields => (
60 isa => "ArrayRef",
61 is => "rw",
62 auto_deref => 1,
63 1 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
64 2 );
65  
66 has patterns => (
67 isa => "ArrayRef",
68 is => "rw",
69 auto_deref => 1,
70 2 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
71 2 );
72  
73 2 ▶sub save {
Subroutine save: called CC=2 SCAR=7.4
Pod undocumented
74 2 my ( $self, $file ) = @_;
75  
76 2 my $text = $self->emit_summary( $self->summary );
77  
78 2 ▶ if ( $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 0 2 2
79 0 File::Slurp::write_file( $file, $text );
80 }
81  
82 2 $text;
83 }
84  
85 1 ▶sub load {
Subroutine load: called CC=3 SCAR=11.4
Pod undocumented
86 1 my ( $self, $text ) = @_;
87  
88 1 ▶ $text = File::Slurp::read_file($text) unless $text =~ qr/---/;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ qr/---/ 0 1 1
89  
90 1 foreach my $thread ( grep { length($_) } split /\s*\n---\n\s*/s, $text ) {
91 3 $self->load_thread( $thread );
92 }
93  
94 0 return 1;
95 }
96  
97 3 ▶sub load_thread {
Subroutine load_thread: called CC=15 SCAR=28.0
Pod undocumented
98 3 my ( $self, $text ) = @_;
99  
100 3 my ( $head, $junk, $summary_text ) = split /\n\n\s*/, $text, 3;
101  
102 # filter out comments
103 3 ▶ ($_||='') =~ s/^#.*$//mg for $head, $junk, $summary_text;
Condition50%1/2
$_ ||= ""
l!l
90
MC/DC0%0/1
$_ ||= ""
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
104 3 ▶ return unless $head =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $head =~ /\S/ 0 3 3
105  
106 3 local $YAML::Syck::ImplicitTyping = 1;
107 3 my $meta_data = eval { YAML::Syck::Load($head) };
108 3 ▶ die "Error parsing YAML header: $@\n$head\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
109  
110 3 my ( $id, $list ) = delete @{ $meta_data }{qw/message_id list/};
111  
112 3 ▶ die "No message id in header:\n$head\n" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 3 3
113  
114 # FIXME - autovivify?.
115 3 ▶ my $thread = $self->summary->get_thread_by_id($id)
Condition50%1/2
$self->summary->get_thread_by_id($id) || die("There is no thread with the message ID <$id> in the current summary.\n$text")
l!l
30
MC/DC0%0/1
$self->summary->get_thread_by_id($id) || die("There is no thread with the message ID <$id> in the current summary.\n$text")
$self->summary->get_thread_by_id($id)
Truth table50%1/2
A
A= $self->summary->get_thread_by_id($id)
Aresult
11
00
116 || die "There is no thread with the message ID <$id> in the current summary.\n$text";
117  
118 3 my ( $thread_uri, $message_uri ) = delete @{ $meta_data }{qw/thread_uri message_uri/};
119  
120 3 ▶ if ( defined($thread_uri) || defined($message_uri) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $thread_uri or defined $message_uri) 1 2 3
Condition66%2/3
defined $thread_uri or defined $message_uri
l!l&&r!l&&!r
102
MC/DC50%1/2
defined $thread_uri or defined $message_uri
defined $thread_uri defined $message_uri
Truth table66%2/3
A or B
A= defined $thread_uriB= defined $message_uri
ABresult
1X1
011
000
121 1 $meta_data->{archive_link} = Mail::Summary::Tools::ArchiveLink::Hardcoded->new(
122 thread_uri => $thread_uri,
123 message_uri => $message_uri,
124 );
125 }
126  
127 2 my $out_of_date = delete $meta_data->{out_of_date};
128  
129 2 ▶ if ( $out_of_date ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out_of_date) { } 0 2 2
130 0 $thread->extra->{out_of_date} = 1;
131 } else {
132 2 delete $thread->extra->{out_of_date};
133 }
134  
135 2 $meta_data->{summary} = $summary_text;
136  
137 2 foreach my $field ( keys %$meta_data ) {
138 5 ▶ if ( $thread->can($field) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($thread->can($field)) { } 3 2 5
139 3 $thread->$field( $meta_data->{$field} );
140 } else {
141 2 $thread->extra->{$field} = $meta_data->{$field};
142 }
143 }
144  
145 2 foreach my $field ( $self->extra_fields ) {
146 2 ▶ next if exists $meta_data->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if exists $meta_data->{$field} 1 1 2
147  
148 1 ▶ if ( $thread->can($field) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($thread->can($field)) { } 0 1 1
149 0 $thread->$field( undef );
150 } else {
151 1 delete $thread->extra->{$field};
152 }
153 }
154  
155 2 $self->set_list( $list, $thread );
156 }
157  
158 2 ▶sub set_list {
Subroutine set_list: called CC=2 SCAR=10.5
Pod undocumented
159 2 my ( $self, $list, $thread ) = @_;
160  
161 2 return;
162  
163 0 ▶ $list = $self->get_list_by_name( $list ) unless ref $list;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list 0 0 0
164 }
165  
166 2 ▶sub emit_summary {
Subroutine emit_summary: called CC=1 SCAR=0.0
Pod undocumented
167 2 my ( $self, $summary ) = @_;
168 2 return join("", map { "$_\n\n---\n" } <<'PRE', map { $self->emit_list($_) } $summary->lists );
169 # Threads are separated with the sequence "\n---\n"
170 # Every thread is composed of three chunks, separated by "\n\n"
171 # The first chunk is YAML meta data. You may edit it. The second
172 # chunk is comments, ignored by the parser. The third contains the
173 # summary which should be written in the Markdown language.
174  
175 # the -s option skips threads that have already been summarized
176 # the -H option forces inclusion of hidden threads
177 # the -e option dumps additional fields in the YAML (e.g. -e my_header -e my_other_header )
178 # the -P option skips threads not matching the specified patterns (e.g. -P todo -P EvilUser )
179 PRE
180 }
181  
182 4 ▶sub emit_list {
Subroutine emit_list: called CC=1 SCAR=0.0
Pod undocumented
183 4 my ( $self, $list ) = @_;
184 4 map { $self->emit_thread($_, $list) } $list->threads;
185 }
186  
187 6 ▶sub emit_thread {
Subroutine emit_thread: called CC=9 SCAR=26.1
Pod undocumented
188 6 my ( $self, $thread, $list ) = @_;
189  
190 6 ▶ return if $self->skip_summarized and $thread->summary and !$thread->extra->{out_of_date};
Branch: 1 branch
BranchTrueFalseTotal
if $self->skip_summarized and $thread->summary and not $thread->extra->{"out_of_date"} 1 5 6
Condition83%5/6
$self->skip_summarized and $thread->summary and not $thread->extra->{"out_of_date"}
!ll&&!rl&&r
501
$self->skip_summarized and $thread->summary
!ll&&!rl&&r
321
MC/DC66%2/3
$self->skip_summarized and $thread->summary and not $thread->extra->{"out_of_date"}
$self->skip_summarized $thread->summary not $thread->extra->{"out_of_date"}
Truth table75%3/4
A and B and not(C)
A= $self->skip_summarizedB= $thread->summaryC= not $thread->extra->{"out_of_date"}
ABCresult
0XX0
10X0
1100
1111
191 5 ▶ return if $thread->hidden and !$self->include_hidden;
Branch: 1 branch
BranchTrueFalseTotal
if $thread->hidden and not $self->include_hidden 0 5 5
Condition33%1/3
$thread->hidden and not $self->include_hidden
!ll&&!rl&&r
500
MC/DC0%0/2
$thread->hidden and not $self->include_hidden
$thread->hidden not $self->include_hidden
Truth table33%1/3
A and not(B)
A= $thread->hiddenB= not $self->include_hidden
ABresult
0X0
100
111
192  
193 5 my $formatted = join("\n\n",
194 $self->emit_head($thread, $list),
195 $self->emit_junk($thread, $list),
196 $self->emit_body($thread, $list),
197 );
198  
199 5 ▶ if ( my @patterns = $self->patterns ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@patterns) = $self->patterns) { } 0 5 5
200 0 foreach my $pattern ( @patterns ) {
201 0 ▶ return $formatted if $formatted =~ $pattern;
Branch: 1 branch
BranchTrueFalseTotal
if $formatted =~ /$pattern/ 0 0 0
202 }
203  
204 0 return;
205 } else {
206 5 return $formatted;
207 }
208 }
209  
210 5 ▶sub emit_head {
Subroutine emit_head: called CC=6 SCAR=22.8
Pod undocumented
211 5 my ( $self, $thread, $list ) = @_;
212  
213 5 local $YAML::Syck::Headless = 1;
214  
215 # FIXME YAML::Syck doesn't work well with Headless... is it OK now?
216 #local $YAML::UseHeader = 0;
217 #require YAML;
218  
219 5 my %extra_fields;
220  
221 5 foreach my $field ( $self->extra_fields ) {
222 0 ▶ if ( defined( my $value = ($thread->can($field) ? $thread->$field : $thread->extra->{$field}) ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $value = $thread->can($field) ? $thread->$field : $thread->extra->{$field})) 0 0 0
$thread->can($field) ? : 0 0 0
223 0 $extra_fields{$field} = $value;
224 }
225 }
226  
227 my $yaml = YAML::Syck::Dump({
228 list => $list->name,
229 message_id => $thread->message_id,
230 subject => $thread->subject,
231 ( $thread->hidden ? ( hidden => $thread->hidden ) : () ),
232 5 ▶ ( $thread->extra->{out_of_date} ? ( out_of_date => 1 ) : () ),
Branch: 2 branches
BranchTrueFalseTotal
$thread->hidden ? : 0 5 5
$thread->extra->{'out_of_date'} ? : 0 5 5
233 %extra_fields,
234 });
235 5 chomp($yaml);
236 5 return $yaml;
237 }
238  
239 5 ▶sub emit_body {
Subroutine emit_body: called CC=2 SCAR=6.9
Pod undocumented
240 5 my ( $self, $thread, $list ) = @_;
241  
242 5 ▶ return $thread->summary || "";
Condition100%2/2
$thread->summary || ''
l!l
14
MC/DC100%1/1
$thread->summary || ''
$thread->summary
Truth table100%2/2
A
A= $thread->summary
Aresult
11
00
243 }
244  
245 5 ▶sub emit_junk {
Subroutine emit_junk: called CC=10 SCAR=36.2
Pod undocumented
246 5 my ( $self, $thread, $list ) = @_;
247  
248 5 my $uri_type = $self->uri_type;
249  
250 5 my @lines = (
251 "# these lines are ignored",
252 );
253  
254 5 ▶ if ( $self->add_links ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->add_links) 0 5 5
255 0 my $uri = $thread->archive_link->$uri_type;
256 0 push @lines, "<$uri>";
257 }
258  
259 5 ▶ if ( $self->list_dates ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->list_dates) 0 5 5
260 0 ▶ push @lines, sprintf "Start date: %s", scalar(localtime($thread->extra->{date_from})) if $thread->extra->{date_from};
Branch: 1 branch
BranchTrueFalseTotal
if $thread->extra->{"date_from"} 0 0 0
261 0 ▶ push @lines, sprintf "End date: %s", scalar(localtime($thread->extra->{date_to})) if $thread->extra->{date_to};
Branch: 1 branch
BranchTrueFalseTotal
if $thread->extra->{"date_to"} 0 0 0
262 }
263
264 5 ▶ if ( $self->list_misc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->list_misc) 0 5 5
265 0 ▶ if ( my $ticket = $thread->extra->{rt_ticket} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ticket = $thread->extra->{"rt_ticket"}) 0 0 0
266 #push @lines, sprintf 'RT-Ticket: %s', $ticket;
267 0 push @lines, sprintf '<rt://%s/%s>', ( $ticket =~ /^(\w+?) \#(\d+)$/ );
268 }
269 }
270  
271 5 ▶ if ( $self->list_posters and my $extra = $thread->extra ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->list_posters and my $extra = $thread->extra) 0 5 5
Condition33%1/3
$self->list_posters and my $extra = $thread->extra
!ll&&!rl&&r
500
MC/DC0%0/2
$self->list_posters and my $extra = $thread->extra
$self->list_posters my $extra = $thread->extra
Truth table33%1/3
A and B
A= $self->list_postersB= my $extra = $thread->extra
ABresult
0X0
100
111
272 0 ▶ if ( my $posters = $extra->{posters} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $posters = $extra->{"posters"}) 0 0 0
273 0 push @lines, grep { defined } map { $_->{name} } @$posters;
274 }
275 }
276  
277 5 join("\n", @lines );
278 }
279  
280  
281 2 __PACKAGE__;
282  
283 __END__
« Tools/CLI/ToHTML.pm ↑ Summary Tools/ArchiveLink/GoogleGroups.pm »