lib/File/Namaste.pm

filter: Statementstmt 96.3% 106 / 110 Branchbran 68.9% 40 / 58 Conditioncond 56.4% 22 / 39 MC/DCmcdc 31.8% 7 / 22 Subroutinesub 100% 11 / 11 Podpod 16.6% 1 / 6 totaltotal 76.0% 187 / 246 CC 45 SCAR 25.9
CC
45
Cov
81%
nam_get
19.3
nam_elide
17.1
nam_add
16.9
CRAP 13.3
↑ Summary script/nam »
1 package File::Namaste;
2  
3 25 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 25 require Exporter;
8 25 our @ISA = qw(Exporter);
9  
10 25 our $VERSION;
11 25 $VERSION = sprintf "%d.%02d", q$Name: Release-1-04 $ =~ /Release-(\d+)-(\d+)/;
12  
13 25 our @EXPORT = qw();
14 #our @EXPORT_OK = qw();
15 25 our @EXPORT_OK = qw(
16 nam_get nam_add nam_elide
17 );
18 25 our %EXPORT_TAGS = (all => [ @EXPORT_OK ]);
19 #our @EXPORT_OK = qw();
20  
21 25 ▶use File::Spec::Functions; # we want catfile()
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # Default setting for tranformations is non-portable for Unix.
24 25 our $portable_default = grep(/Win32|OS2/i, @File::Spec::ISA);
25  
26 # Instead of "use File::Value;" for a small dependency, use this
27 # abbreviated version of "raw" File::Value::file_value() (that we
28 # also use commonly in test scripts).
29 #
30 40 ▶sub filval { my( $file, $value )=@_; # $file must begin with >, <, or >>
Subroutine filval: called CC=5 SCAR=16.3
Pod undocumented
31 40 ▶ if ($file =~ /^\s*>>?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /^\s*>>?/) 12 28 40
32 12 ▶ open(OUT, $file) or return "$file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open OUT, $file or return "${file}: $!" 12 0 12
33 12 my $r = print OUT $value;
34 12 ▶ close(OUT); return ($r ? '' : "write failed: $!");
Branch: 1 branch
BranchTrueFalseTotal
$r ? : 12 0 12
35 } # If we get here, we're doing file-to-value case.
36 28 ▶ open(IN, $file) or return "$file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open IN, $file or return "${file}: $!" 28 0 28
37 28 local $/; $_[1] = <IN>; # slurp mode (entire file)
38 28 close(IN); return '';
39 }
40  
41 # xxx not yet doing unicode or i18n
42 # only first arg required
43 # return tvalue given fvalue
44 12 ▶sub nam_tvalue { my( $fvalue, $portable, $max, $ellipsis )=@_;
Subroutine nam_tvalue: called CC=3 SCAR=11.0
Pod undocumented
45  
46 12 ▶ defined($portable) or $portable = $portable_default;
Branch: 1 branch
BranchTrueFalseTotal
defined $portable or $portable = $File::Namaste::portable_default 2 10 12
47 12 my $tvalue = $fvalue;
48 12 $tvalue =~ s,/,=,g;
49 12 $tvalue =~ s,\s+, ,g;
50 12 $tvalue =~ s,\p{IsC},?,g; # control characters
51  
52 12 ▶ $portable and # more portable (Win32) mapping
Branch: 1 branch
BranchTrueFalseTotal
$portable and $tvalue =~ tr["*/:<>?\\|][.] 2 10 12
53 $tvalue =~ tr {"*/:<>?|\\}{.};
54  
55 12 return nam_elide($tvalue, $max, $ellipsis);
56 }
57  
58 # Ordered list of labels, mostly kernel element names
59 # yyy should this be coming from ERC module, or is that creating
60 # a big dependency hurdle for a sliver of functionality?
61 25 our @namaste_labels = qw(
62 dir_type
63 who
64 what
65 when
66 where
67 how
68 why
69 huh
70 );
71  
72 12 ▶sub num2label { my $num = shift;
Subroutine num2label: called CC=4 SCAR=14.3
Pod undocumented
73  
74 12 my $last = $#namaste_labels;
75 12 $num =~ s/^(\d+).*/$1/; # forgive, eg, 3=foo
76 12 ▶ $num =~ /\D/ || $num < 0 || $num > $last and
Branch: 1 branch
BranchTrueFalseTotal
$num =~ /\D/ || $num < 0 || $num > $last and return $File::Namaste::namaste_labels[$last] 1 11 12
Condition50%3/6
$num =~ /\D/ or $num < 0 or $num > $last
l!l&&r!l&&!r
0111
$num =~ /\D/ or $num < 0
l!l&&r!l&&!r
0012
MC/DC33%1/3
$num =~ /\D/ or $num < 0 or $num > $last
$num =~ /\D/ $num < 0 $num > $last
Truth table50%2/4
A or B or C
A= $num =~ /\D/B= $num < 0C= $num > $last
ABCresult
1XX1
01X1
0011
0000
77 return $namaste_labels[$last]; # last label
78 # last label doubles as unknown (huh?) if number is bad
79 11 return $namaste_labels[$num]; # normal return
80 }
81  
82  
83 # xxx should create shadow tag files with highly deterministic names?
84 # easier for a machine to fine a specific element
85 25 my $dtname = ".dir_type"; # canonical name of directory type file
86 # xxx .=dir_type
87 # xxx .=how
88 # xxx .=huh
89 # xxx .=what
90 # xxx .=when
91 # xxx .=where
92 # xxx .=who
93 # xxx .=why
94  
95 # xxx to do
96 # N means create N=...
97 # .N means create or add to .=wh{o,at,en,ere}
98 # N. means do both N and .N
99  
100 # $num and $fvalue required
101 # returns empty string on success, otherwise a diagnostic
102 12 ▶sub nam_add { my( $dir, $portable, $num, $fvalue, $max, $ellipsis )=@_;
Subroutine nam_add: called CC=8 SCAR=28.3
Pod undocumented
103  
104 12 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $num and defined $fvalue 0 12 12
Condition33%1/3
defined $num and defined $fvalue
!ll&&!rl&&r
0012
MC/DC0%0/2
defined $num and defined $fvalue
defined $num defined $fvalue
Truth table33%1/3
A and B
A= defined $numB= defined $fvalue
ABresult
0X0
100
111
105 if (! defined($num) || ! defined($fvalue));
106  
107 12 ▶ $dir ||= "";
Condition50%1/2
$dir ||= ""
l!l
120
MC/DC0%0/1
$dir ||= ""
$dir
Truth table50%1/2
A
A= $dir
Aresult
11
00
108 #$dir = catfile($dir, "") # add portable separator
109 # if $dir; # (eg, slash) if there's a dir name
110  
111 #my $fname = $dir . $dtname; # path to .dir_type, if needed
112 12 my $fname = catfile($dir, $dtname); # path to .dir_type, if needed
113 12 my $tvalue = nam_tvalue($fvalue, $portable, $max, $ellipsis);
114 # ".0" means set .dir_type also; "." means only set .dir_type
115 12 ▶ if ($num =~ s/^\.0/0/ || $num eq ".") {
Branch: 1 branch
BranchTrueFalseTotal
if ($num =~ s/^\.0/0/ or $num eq ".") 0 12 12
Condition33%1/3
$num =~ s/^\.0/0/ or $num eq "."
l!l&&r!l&&!r
0012
MC/DC0%0/2
$num =~ s/^\.0/0/ or $num eq "."
$num =~ s/^\.0/0/ $num eq "."
Truth table33%1/3
A or B
A= $num =~ s/^\.0/0/B= $num eq "."
ABresult
1X1
011
000
116 # "append only" supports multi-typing in .dir_type, so
117 # caller must remove .dir_type to re-set (see "nam" script)
118 # right now $fname contains catfile($dir, $dtname)
119 0 my $ret = filval(">>$fname", $fvalue);
120 0 ▶ return $ret # return if error or only .dir_type
Branch: 1 branch
BranchTrueFalseTotal
if $ret or $num eq "." 0 0 0
Condition0%0/3
$ret or $num eq "."
l!l&&r!l&&!r
000
MC/DC0%0/2
$ret or $num eq "."
$ret $num eq "."
Truth table0%0/3
A or B
A= $retB= $num eq "."
ABresult
1X1
011
000
121 if $ret || $num eq ".";
122 }
123  
124 #$fname = "$dir$num=$tvalue";
125 12 $fname = catfile($dir, "$num=$tvalue");
126 #nam_tvalue($fvalue, $portable, $max, $ellipsis);
127 # why is this sometimes null?
128  
129 12 return filval(">$fname", $fvalue);
130 }
131  
132 25 ▶use File::Glob ':glob'; # standard use of module, which we need
Subroutine BEGIN: called CC=1 SCAR=0.0
133 # as vanilla glob won't match whitespace
134  
135 # first arg is directory, remaining args give numbers to fetch;
136 # no args means return all
137 # args can be file globs
138 # returns array of number/fname/value triples (every third elem is number)
139 25 ▶sub nam_get {
Subroutine nam_get: called CC=13 SCAR=29.6
Pod undocumented
140  
141 25 my $dir = shift @_;
142  
143 25 ▶ $dir ||= "";
Condition50%1/2
$dir ||= ""
l!l
250
MC/DC0%0/1
$dir ||= ""
$dir
Truth table50%1/2
A
A= $dir
Aresult
11
00
144 #$dir = catfile($dir, "") # add portable separator
145 # if $dir; # (eg, slash) if there's a dir name
146 #my $dir_type = $dir . $dtname; # path to .dir_type, if needed
147 25 my $dir_type = catfile($dir, $dtname); # path to .dir_type, if needed
148  
149 25 my (@in, @out);
150 25 ▶ if ($#_ < 0) { # if no args, get all files starting
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ < 0) { } 5 20 25
151 # Surprisingly, with bsd_glob a / separator works in Win32
152 5 @in = bsd_glob("$dir/[0-9]=*"); # so no need for catfile()
153 # @in = bsd_glob(catfile($dir, '[0-9]=*')); # "<digit>=..."
154 5 ▶ -e $dir_type and # since we're getting all,
Branch: 1 branch
BranchTrueFalseTotal
-e $dir_type and unshift @in, $dir_type 0 5 5
155 unshift @in, $dir_type; # if it exists, add .dir_type
156 }
157 else { # else do globs for each arg
158 20 ▶ while (defined(my $n = shift @_)) { # next number
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $n = shift @_)) 20 20 40
159 20 ▶ if (($n =~ s/^\.0/0/ || $n eq ".") && -e $dir_type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ s/^\.0/0/ || $n eq "." and -e $dir_type) 0 20 20
Condition33%2/6
$n =~ s/^\.0/0/ || $n eq "." and -e $dir_type
!ll&&!rl&&r
2000
$n =~ s/^\.0/0/ or $n eq "."
l!l&&r!l&&!r
0020
MC/DC0%0/3
$n =~ s/^\.0/0/ || $n eq "." and -e $dir_type
$n =~ s/^\.0/0/ $n eq "." -e $dir_type
Truth table20%1/5
A or B and C
A= $n =~ s/^\.0/0/B= $n eq "."C= -e $dir_type
ABCresult
00X0
1X00
0100
1X11
0111
160 # if requested and it exists, add .dir_type
161 0 push @in, $dir_type;
162 next # next if only .dir_type
163 0 ▶ if $n eq ".";
Branch: 1 branch
BranchTrueFalseTotal
if $n eq "." 0 0 0
164 }
165 20 push @in, bsd_glob("$dir/$n=*");
166 #push @in, bsd_glob(catfile($dir, $n . '=*'));
167 }
168 }
169 # Now create the output array.
170 25 my ($number, $fname, $fvalue, $status, $regex);
171 25 ▶ while (defined($fname = shift(@in))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($fname = shift @in)) 28 25 53
172  
173 28 $status = filval("<$fname", $fvalue);
174  
175 #($number) = ($fname =~ m{^$dir(\d*)=});
176 #$regex = catfile($dir, '(\d*)=');
177  
178 # # ask for a dummy file 'x' in order to get a dir separator
179 # #
180 # $regex = catfile($dir, 'x'); # separator might be \ or /
181 # temporary crap to flush out bug in Windows version
182 #$regex =~ s,/,\\,g;
183 #$fname =~ s,/,\\,g;
184 # $regex =~ s/\\/\\\\/g; # preserve any \ separators for regex
185 #
186 # # replace dummy file with pattern we want, leaving separator
187 # #
188 # $regex =~ s/x$/(\\d*)=/; # replace with literal pattern
189 #print "xxx regex=$regex\n";
190 # ($number) = ($fname =~ m{^$regex});
191 #print "xxx number=$number, fname=$fname\n";
192 28 ($number) = ($fname =~ m{^$dir/(\d*)=});
193  
194 # if there's no number matched, it may be for .dir_type,
195 # in which case use "." for number, else give up with ""
196 #
197 # $regex = catfile($dir, $dtname);
198 #$regex =~ s,/,\\,g;
199 # $regex =~ s/\\/\\\\/g; # preserve any \ separators for regex
200 #print "xxx dtname=$dtname, regex=$regex\n";
201 #$number = ($fname =~ m{^$dir$dtname} ? "." : "")
202 # yyy matching on $dtname is imperfect if it contains
203 # a '.' -- eg, ".dir_type" matches "adir_type"
204 # contains a
205 #$number = ($fname =~ m{^$regex} ? "." : "")
206  
207 # \Q prevents chars in $dtname (eg, '.') being used in regex
208 28 ▶ $number = ($fname =~ m{^$dir/\Q$dtname\E} ? "." : "")
Branch: 2 branches
BranchTrueFalseTotal
unless defined $number 0 28 28
$fname =~ m[^$dir/\Q$dtname\E] ? : 0 0 0
209 if (! defined($number));
210  
211 28 ▶ push @out, $number, $fname, ($status ? $status : $fvalue);
Branch: 1 branch
BranchTrueFalseTotal
$status ? : 0 28 28
212 }
213 25 return @out;
214 }
215  
216 # xxx unicode friendly??
217 25 our $max_default = 16; # is there some sense to this? xxx use
218 # fraction of display width maybe?
219  
220 23 ▶sub nam_elide { my( $s, $max, $ellipsis )=@_;
Subroutine nam_elide: called CC=17 SCAR=28.4
Pod documented
221  
222 23 ▶ $s or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$s or return undef 23 0 23
223 # $max can be zero (0) so that nam_add() can ask for no elision.
224 23 ▶ defined($max) or $max = $max_default;
Branch: 1 branch
BranchTrueFalseTotal
defined $max or $max = $File::Namaste::max_default 12 11 23
225 23 ▶ $max !~ /^(\d+)([esmESM]*)([+-]\d+%?)?$/ and
Branch: 1 branch
BranchTrueFalseTotal
$max =~ /^(\d+)([esmESM]*)([+-]\d+%?)?$/ or return undef 23 0 23
226 return undef;
227 23 my ($maxlen, $where, $tweak) = ($1, $2, $3);
228  
229 23 ▶ $where ||= "e";
Condition100%2/2
$where ||= "e"
l!l
815
MC/DC100%1/1
$where ||= "e"
$where
Truth table100%2/2
A
A= $where
Aresult
11
00
230 23 $where = lc($where);
231  
232 23 ▶ $ellipsis ||= ($where eq "m" ? "..." : "..");
Branch: 1 branch
BranchTrueFalseTotal
$where eq 'm' ? : 1 16 17
Condition66%2/3
$ellipsis ||= $where eq "m" ? "..." : ".."
l!l&&r!l&&!r
6170
MC/DC0%0/2
$ellipsis ||= $where eq "m" ? "..." : ".."
$ellipsis $where eq "m" ? "..." : ".."
Truth table66%2/3
A ||= B
A= $ellipsisB= $where eq "m" ? "..." : ".."
ABresult
1X1
011
000
233 23 my $elen = length($ellipsis);
234  
235 23 my ($side, $offset, $percent); # xxx only used for "m"?
236 23 ▶ if (defined($tweak)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tweak) 1 22 23
237 1 ($side, $offset, $percent) = ($tweak =~ /^([+-])(\d+)(%?)$/);
238 }
239 23 ▶ $side ||= ""; $offset ||= 0; $percent ||= "";
Condition100%6/6
$side ||= ""
l!l
122
$offset ||= 0
l!l
122
$percent ||= ""
l!l
122
MC/DC100%1/1
$side ||= ""
$side
MC/DC100%1/1
$offset ||= 0
$offset
MC/DC100%1/1
$percent ||= ""
$percent
Truth table100%2/2
A
A= $side
Aresult
11
00
Truth table100%2/2
A
A= $offset
Aresult
11
00
Truth table100%2/2
A
A= $percent
Aresult
11
00
240 # XXXXX finish this! print "side=$side, n=$offset, p=$percent\n";
241  
242 23 my $slen = length($s);
243 23 ▶ return $s
Branch: 1 branch
BranchTrueFalseTotal
if $slen <= $maxlen or $maxlen == 0 11 12 23
Condition100%3/3
$slen <= $maxlen or $maxlen == 0
l!l&&r!l&&!r
10112
MC/DC100%2/2
$slen <= $maxlen or $maxlen == 0
$slen <= $maxlen $maxlen == 0
Truth table100%3/3
A or B
A= $slen <= $maxlenB= $maxlen == 0
ABresult
1X1
011
000
244 if ($slen <= $maxlen || $maxlen == 0); # doesn't need elision
245  
246 12 my $re; # we will create a regex to edit the string
247 # length of orig string after that will be left after edit
248 12 my $left = $maxlen - $elen;
249  
250 12 my $retval = $s;
251 # Example: if $left is 5, then
252 # if "e" then s/^(.....).*$/$1$ellipsis/
253 # if "s" then s/^.*(.....)$/$ellipsis$1/
254 # if "m" then s/^.*(...).*(..)$/$1$ellipsis$2/
255 # In order to make '.' match \n, we use s///s ('s' modifier).
256 12 ▶ if ($where eq "m") {
Branch: 1 branch
BranchTrueFalseTotal
if ($where eq 'm') { } 5 7 12
257 # if middle, we split the string
258 5 my $half = int($left / 2);
259 5 ▶ $half += 1 # bias larger half to front if $left is odd
Branch: 1 branch
BranchTrueFalseTotal
if $half > $left - $half 0 5 5
260 if ($half > $left - $half); # xxx test
261 5 $re = "^(" . ("." x $half) . ").*("
262 . ("." x ($left - $half)) . ")\$";
263 # $left - $half might be zero, but this still works
264 5 $retval =~ s/$re/$1$ellipsis$2/s;
265 }
266 else {
267 7 my $dots = "." x $left;
268 7 ▶ $re = ($where eq "e" ? "^($dots).*\$" : "^.*($dots)\$");
Branch: 1 branch
BranchTrueFalseTotal
$where eq 'e' ? : 4 3 7
269 7 ▶ if ($where eq "e") {
Branch: 1 branch
BranchTrueFalseTotal
if ($where eq 'e') { } 4 3 7
270 4 $retval =~ s/$re/$1$ellipsis/s;
271 }
272 else { # else "s"
273 3 $retval =~ s/$re/$ellipsis$1/s;
274 }
275 }
276 12 return $retval;
277 }
278  
279 25 1;
280  
281 __END__
↑ Summary script/nam »