Export.pm

filter: Statementstmt 66.6% 70 / 105 Branchbran 40.0% 16 / 40 Conditioncond 58.3% 7 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 83.3% 10 / 12 Podpod 100% 7 / 7 totaltotal 61.4% 113 / 184 CC 28 SCAR 23.9
CC
28
Cov
59%
verbose
20.0
run_command
20.0
get_record_hash
13.7
CRAP 10.9
« Export/LDAP.pm ↑ Summary Export/vCard.pm »
1 package BBDB::Export;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 our $VERSION = '0.015';
6  
7  
8 #
9 #_* Config
10 #
11  
12 # TODO: make this configurable
13 3 my %colors = (
14 info => 'green',
15 command => 'bold yellow',
16 error => 'red',
17 verbose => 'blue',
18 );
19  
20 #
21 #_* Libraries
22 #
23 3 ▶use BBDB;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 #
28 #_* new
29 #
30  
31 sub new
32 28 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
33 28 my ( $class, $data ) = @_;
34  
35 28 my $objref = ( { data => $data } );
36  
37 28 bless $objref, $class;
38  
39 28 return $objref;
40 }
41  
42 #
43 #_* get_record_hash
44 #
45 sub get_record_hash
46 28 ▶{
Subroutine get_record_hash: called CC=13 SCAR=26.1
Pod documented
47 28 my ( $self, $record ) = @_;
48  
49 28 ▶ return unless ( $record );
Branch: 1 branch
BranchTrueFalseTotal
unless $record 0 28 28
50  
51 # store record data
52 28 my %record;
53  
54 # get entire data structure
55 28 my $data = $record->part('all');
56  
57 # first/last names
58 28 $record{'first'} = $data->[0];
59 28 $record{'last'} = $data->[1];
60 28 $record{'full' } = join( " ", ( $record{'first'}, $record{'last'} ) );
61 28 $record{'full' } =~ s|^\s+||;
62 28 $record{'full' } =~ s|\s+$||;
63  
64 # nicks
65 28 ▶ @{$record{'aka'}} = @{ $data->[2] } if $data->[2] && $data->[2]->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $data->[2] and $data->[2][0] 4 24 28
Condition66%2/3
$data->[2] and $data->[2][0]
!ll&&!rl&&r
0244
MC/DC50%1/2
$data->[2] and $data->[2][0]
$data->[2] $data->[2][0]
Truth table66%2/3
A and B
A= $data->[2]B= $data->[2][0]
ABresult
0X0
100
111
66  
67 # company
68 28 $record{'company'} = $data->[3];
69  
70 # phone numbers
71 28 ▶ if ( $data->[4] && $data->[4]->[0] )
Branch: 1 branch
BranchTrueFalseTotal
if ($data->[4] and $data->[4][0]) 8 20 28
Condition66%2/3
$data->[4] and $data->[4][0]
!ll&&!rl&&r
0208
MC/DC50%1/2
$data->[4] and $data->[4][0]
$data->[4] $data->[4][0]
Truth table66%2/3
A and B
A= $data->[4]B= $data->[4][0]
ABresult
0X0
100
111
72 {
73 8 for my $phone ( @{ $data->[4] } )
74 {
75 20 my $loc = $phone->[0];
76  
77 20 my @numbers = @{ $phone->[1] };
78 20 pop @numbers;
79  
80 20 my $number;
81  
82 20 ▶ if ( $#numbers == 2 )
Branch: 1 branch
BranchTrueFalseTotal
if ($#numbers == 2) { } 20 0 20
83 {
84 20 $number = "(" . $numbers[0] . ") " . $numbers[1] . "-" . $numbers[2];
85 }
86 else
87 {
88 0 $number = join( "-", @numbers );
89 }
90  
91 20 $record{'phone'}->{ $loc } = $number;
92 }
93 }
94  
95 # TODO: addresses
96  
97 # nicks
98 28 ▶ @{$record{'net'}} = @{ $data->[6] } if $data->[6] && $data->[6]->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $data->[6] and $data->[6][0] 8 20 28
Condition66%2/3
$data->[6] and $data->[6][0]
!ll&&!rl&&r
0208
MC/DC50%1/2
$data->[6] and $data->[6][0]
$data->[6] $data->[6][0]
Truth table66%2/3
A and B
A= $data->[6]B= $data->[6][0]
ABresult
0X0
100
111
99  
100  
101 # notes
102 28 ▶ if ( $data->[7] && $data->[7]->[0] )
Branch: 1 branch
BranchTrueFalseTotal
if ($data->[7] and $data->[7][0]) 28 0 28
Condition33%1/3
$data->[7] and $data->[7][0]
!ll&&!rl&&r
0028
MC/DC0%0/2
$data->[7] and $data->[7][0]
$data->[7] $data->[7][0]
Truth table33%1/3
A and B
A= $data->[7]B= $data->[7][0]
ABresult
0X0
100
111
103 {
104 28 for my $note ( @{ $data->[7] } )
105 {
106 72 $record{ $note->[0] } = $note->[1];
107 }
108 }
109  
110 28 return \%record;
111 }
112  
113 #
114 #_* process_record
115 #
116  
117  
118 #
119 #_* export
120 #
121 sub export
122 21 ▶{
Subroutine export: called CC=6 SCAR=20.1
Pod documented
123 21 my ( $self ) = @_;
124  
125 21 ▶ unless ( $self->{'data'}->{'bbdb_file'} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"data"}{"bbdb_file"}) 0 21 21
126 {
127 0 $self->error( "export called but bbdb_file not specified" );
128 0 return;
129 }
130  
131 21 ▶ unless ( -r $self->{'data'}->{'bbdb_file'} )
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $self->{"data"}{"bbdb_file"}) 0 21 21
132 {
133 0 $self->error( "export called but bbdb_file not readable" );
134 0 return;
135 }
136  
137 21 my $return = "";
138  
139 21 for my $record ( @{BBDB::simple( $self->{'data'}->{'bbdb_file'} )} )
140 {
141 21 my $record_hash = $self->get_record_hash( $record );
142 21 ▶ unless ( $record_hash )
Branch: 1 branch
BranchTrueFalseTotal
unless ($record_hash) 0 21 21
143 {
144 0 $self->error( "No data returned from record" );
145 0 next;
146 }
147  
148 21 my ( $output ) = $self->process_record( $record_hash );
149 21 ▶ $return .= $output if $output;
Branch: 1 branch
BranchTrueFalseTotal
if $output 16 5 21
150 }
151  
152 21 $self->post_processing( $return );
153  
154 21 return $return;
155 }
156  
157  
158 #
159 #_* output
160 #
161  
162 sub info
163 7 ▶{
Subroutine info: called CC=3 SCAR=18.1
Pod documented
164 7 my ( $self, @info ) = @_;
165  
166 7 ▶ return if $self->{'data'}->{'quiet'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"data"}{"quiet"} 7 0 7
167  
168 0 print color $colors{'info'};
169 0 ▶ print join ( "\n", @info ) if $info[0];
Branch: 1 branch
BranchTrueFalseTotal
if $info[0] 0 0 0
170 0 print color 'reset';
171 0 print "\n";
172 }
173  
174 sub error
175 7 ▶{
Subroutine error: called CC=3 SCAR=18.1
Pod documented
176 7 my ( $self, @error ) = @_;
177  
178 7 ▶ return if $self->{'data'}->{'quiet'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"data"}{"quiet"} 7 0 7
179  
180 0 print color $colors{'error'};
181 0 ▶ print join ( "\n", @error ) if $error[0];
Branch: 1 branch
BranchTrueFalseTotal
if $error[0] 0 0 0
182 0 print color 'reset';
183 0 print "\n";
184 }
185  
186 sub verbose
187 0 ▶{
Subroutine verbose: not called CC=4 SCAR=30.0
Pod documented
188 0 my ( $self, @verbose ) = @_;
189  
190 0 ▶ return if $self->{'data'}->{'quiet'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"data"}{"quiet"} 0 0 0
191 0 ▶ return unless $self->{'verbose'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"verbose"} 0 0 0
192  
193 0 print color $colors{'verbose'};
194 0 ▶ print join ( "\n", @verbose ) if $verbose[0];
Branch: 1 branch
BranchTrueFalseTotal
if $verbose[0] 0 0 0
195 0 print color 'reset';
196 0 print "\n";
197 }
198  
199 #
200 #_* spawn external command, e.g. ldapadd
201 #
202 sub run_command
203 0 ▶{
Subroutine run_command: not called CC=4 SCAR=30.0
Pod documented
204 0 my ( $self, $command, $supress_error ) = @_;
205  
206 0 $self->verbose("COMMAND: $command");
207  
208 0 open( my $run_fh, "-|", "$command 2>&1" );
209 0 my $out = join( "\n", <$run_fh> );
210  
211 0 ▶ if ( close $run_fh )
Branch: 1 branch
BranchTrueFalseTotal
if (close $run_fh) { } 0 0 0
212 {
213 0 ▶ if ( $self->{'verbose'} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 0 0 0
214 {
215 0 print color $colors{'command'};
216 0 print $out;
217 0 print color 'reset';
218 }
219  
220 0 return $out;
221 }
222 else
223 {
224 0 ▶ unless ( $self->{'data'}->{'supress_error'} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"data"}{"supress_error"}) 0 0 0
225 {
226 0 $self->error( $out );
227 }
228  
229 0 return;
230 }
231 }
232  
233  
234 3 1;
235 __END__
« Export/LDAP.pm ↑ Summary Export/vCard.pm »