lib/App/Greple/xlate/Mask.pm

filter: Statementstmt 100% 152 / 152 Branchbran 80.6% 50 / 62 Conditioncond 77.7% 14 / 18 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 11 totaltotal 88.1% 237 / 269 CC 53 SCAR 22.2
CC
53
Cov
93%
load_anonymize_file
16.2
unmask
14.0
mask
7.1
CRAP 9.3
« blib/lib/App/Greple/xlate/Cache.pm ↑ Summary blib/lib/App/Greple/xlate/llm.pm »
1 package App::Greple::xlate::Mask;
2  
3 use v5.24;
4 use warnings;
5 use Data::Dumper;
6  
7 use Hash::Util qw(lock_keys);
8  
9 my %default = (
10 TAG => 'm',
11 INDEX => 'id',
12 NUMBER => 0,
13 PATTERN => [],
14 TABLE => [],
15 AUTORESET => 0,
16 # --- stable (anonymization) path ---
17 STABLE => 0, # same (tag, string) -> same tag
18 RULES => undef, # permanent [ [tag, pattern], ... ]
19 FILERULES => undef, # per-document rules, replaced via file_rules()
20 COUNTER => undef, # per-tag counters
21 ASSIGNED => undef, # "tag\0string" -> tag string
22 ASSIGN_ORDER => undef, # tag strings in assignment order
23 ORIGIN => undef, # tag string -> original string
24 TRACK => undef, # tag string -> expected response occurrence count
25 );
26  
27 sub new {
28 67 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
29 67 my $obj = bless { %default }, $class;
30 # NOTE: reference-valued defaults must get fresh copies here
31 67 $obj->{PATTERN} = [];
32 67 $obj->{TABLE} = [];
33 67 $obj->{RULES} = [];
34 67 $obj->{FILERULES} = [];
35 67 $obj->{COUNTER} = {};
36 67 $obj->{ASSIGNED} = {};
37 67 $obj->{ASSIGN_ORDER} = [];
38 67 $obj->{ORIGIN} = {};
39 67 $obj->{TRACK} = {};
40 67 lock_keys %{$obj};
41 67 $obj->configure(@_);
42 25 $obj;
43 }
44  
45 sub reset {
46 25 ▶ my $obj = shift;
Subroutine reset: called CC=1 SCAR=0.0
Pod undocumented
47 25 $obj->{NUMBER} = 0;
48 25 $obj->{TABLE} = [];
49 25 $obj->{TRACK} = {};
50 25 $obj;
51 }
52  
53 sub configure {
54 25 ▶ my $obj = shift;
Subroutine configure: called CC=6 SCAR=18.0
Pod undocumented
55 25 ▶ while (my($a, $b) = splice @_, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 25 25 50
56 25 ▶ if ($a eq 'pattern') {
Branch: 2 branches
BranchTrueFalseTotal
if ($a eq 'pattern') { } 1 24 25
elsif ($a eq 'file') { } 1 23 24
57 25 ▶ my @pattern = ref $b ? @$b : $b;
Branch: 1 branch
BranchTrueFalseTotal
ref $b ? : 1 0 1
58 25 push @{$obj->{PATTERN}}, @pattern;
59 }
60 elsif ($a eq 'file') {
61 25 ▶ open my $fh, '<:encoding(utf8)', $b or die "$b: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(utf8)", $b or die "${b}: $!\n" 1 0 1
62 25 my @p = map s/\\(?=\n)//gr, split /(?<!\\)\n/, do { local $/; <$fh> };
63 24 push @{$obj->{PATTERN}}, @p;
64 }
65 else {
66 24 $obj->{$a} = $b;
67 }
68 }
69 }
70  
71 sub add_rule {
72 24 ▶ my($obj, $tag, $pattern) = @_;
Subroutine add_rule: called CC=2 SCAR=7.0
Pod undocumented
73 25 ▶ $tag =~ /\A[a-z][a-z0-9_]*\z/
Branch: 1 branch
BranchTrueFalseTotal
$tag =~ /\A[a-z][a-z0-9_]*\z/u or die "${tag}: invalid category name.\n" 35 0 35
74 or die "$tag: invalid category name.\n";
75 25 push @{$obj->{RULES}}, [ $tag, $pattern ];
76 1 $obj;
77 }
78  
79 sub file_rules {
80 1 ▶ my($obj, $rules) = @_;
Subroutine file_rules: called CC=1 SCAR=0.0
Pod undocumented
81 1 $obj->{FILERULES} = [ @$rules ];
82 1 $obj;
83 }
84  
85 ##
86 ## Escape rule: hide pre-existing tag-shaped literals so that every
87 ## tag in the working text is one of ours. Must be the first rule;
88 ## restored last (rules are restored in reverse order of the TABLE).
89 ##
90 sub add_escape_rule {
91 1 ▶ my $obj = shift;
Subroutine add_escape_rule: called CC=1 SCAR=0.0
Pod undocumented
92 1 unshift @{$obj->{RULES}}, [
93 'lit',
94 q{<[a-z][a-z0-9_]* [a-z0-9_]+=(?:"\d+"|'\d+'|\d+) */>},
95 ];
96 1 $obj;
97 }
98  
99 use JSON;
100  
101 our $DEFAULT_MARK =
102 q[\{\{\s*(?<category>[a-z][a-z0-9_]*)\(\s*(?<q>["'])(?<text>.+?)\k<q>\s*\)\s*\}\}];
103  
104 sub _check_category {
105 1 ▶ my $tag = shift;
Subroutine _check_category: called CC=3 SCAR=11.0
106 23 ▶ die "lit: reserved category name.\n" if $tag eq 'lit';
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "lit" 2 29 31
107 35 ▶ $tag =~ /\A[a-z][a-z0-9_]*\z/
Branch: 1 branch
BranchTrueFalseTotal
$tag =~ /\A[a-z][a-z0-9_]*\z/u or die "${tag}: invalid category name.\n" 28 1 29
108 or die "$tag: invalid category name.\n";
109 35 $tag;
110 }
111  
112 sub load_anonymize_file {
113 35 ▶ my($obj, $path) = @_;
Subroutine load_anonymize_file: called CC=16 SCAR=27.9
Pod undocumented
114 35 ▶ open my $fh, '<:encoding(utf8)', $path or die "$path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(utf8)", $path or die "${path}: $!\n" 15 0 15
115 35 my $data = do { local $/; <$fh> };
116 1 $data =~ s/\A\x{FEFF}//; # tolerate a UTF-8 BOM
117 13 ▶ if ($data =~ /\A\s*\[/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /\A\s*\[/u) { } 13 2 15
118 13 my $list = JSON->new->decode($data);
119 13 ▶ ref $list eq 'ARRAY' or die "$path: JSON array expected.\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $list eq "ARRAY" or die "${path}: JSON array expected.\n" 13 0 13
120 13 for my $e (@$list) {
121 31 ▶ ref $e eq 'HASH' or die "$path: object expected.\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $e eq "HASH" or die "${path}: object expected.\n" 21 0 21
122 my $cat = _check_category($e->{category}
123 31 ▶ // die "$path: category missing.\n");
Condition50%1/2
$e->{"category"} // die("${path}: category missing.\n")
l!l
210
MC/DC0%0/1
$e->{"category"} // die("${path}: category missing.\n")
$e->{"category"}
Truth table50%1/2
A
A= $e->{"category"}
Aresult
11
00
124 29 my $has_text = defined $e->{text};
125 28 my $has_regex = defined $e->{regex};
126 15 ▶ die "$path: both text and regex given.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $has_text and $has_regex 1 18 19
Condition100%3/3
$has_text and $has_regex
!ll&&!rl&&r
2161
MC/DC100%2/2
$has_text and $has_regex
$has_text $has_regex
Truth table100%3/3
A and B
A= $has_textB= $has_regex
ABresult
0X0
100
111
127 if $has_text and $has_regex;
128 15 ▶ die "$path: either text or regex required.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $has_text or $has_regex 1 17 18
Condition100%3/3
$has_text or $has_regex
l!l&&r!l&&!r
1611
MC/DC100%2/2
$has_text or $has_regex
$has_text $has_regex
Truth table100%3/3
A or B
A= $has_textB= $has_regex
ABresult
1X1
011
000
129 unless $has_text or $has_regex;
130 $obj->add_rule($cat,
131 15 ▶ $has_text ? quotemeta($e->{text}) : $e->{regex});
Branch: 1 branch
BranchTrueFalseTotal
$has_text ? : 16 1 17
132 }
133 } else {
134 15 my @lines = map s/\\(?=\n)//gr, split /(?<!\\)\n/, $data;
135 15 for my $line (@lines) {
136 15 ▶ next if $line =~ /^\s*(#|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*(#|$)/u 1 3 4
137 15 ▶ my($cat, $pat) = $line =~ /\A\s*(\S+)\s+(.*?)\s*\z/s
Branch: 1 branch
BranchTrueFalseTotal
() = $line =~ /\A\s*(\S+)\s+(.*?)\s*\z/su or die "${path}: unparsable line: $line\n" 3 0 3
138 or die "$path: unparsable line: $line\n";
139 13 _check_category($cat);
140 13 ▶ if ($pat =~ m{\A/(.*)/\z}s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pat =~ m[\A/(.*)/\z]su) { } 1 2 3
141 13 $obj->add_rule($cat, $1);
142 } else {
143 21 $obj->add_rule($cat, quotemeta($pat));
144 }
145 }
146 }
147 21 $obj;
148 }
149  
150 sub extract_marks {
151 19 ▶ my($text, $regex) = @_;
Subroutine extract_marks: called CC=6 SCAR=17.9
Pod undocumented
152 19 ▶ index($regex, '(?<category>') >= 0 and index($regex, '(?<text>') >= 0
Branch: 1 branch
BranchTrueFalseTotal
index($regex, "(?<category>") != -1 and index($regex, "(?<text>") != -1 or die "mark regex needs (?<category>...) and (?<text>...) named captures. " 4 1 5
Condition66%2/3
index($regex, "(?<category>") != -1 and index($regex, "(?<text>") != -1
!ll&&!rl&&r
104
MC/DC50%1/2
index($regex, "(?<category>") != -1 and index($regex, "(?<text>") != -1
index($regex, "(?<category>") != -1 index($regex, "(?<text>") != -1
Truth table66%2/3
A and B
A= index($regex, "(?<category>") != -1B= index($regex, "(?<text>") != -1
ABresult
0X0
100
111
153 or die "mark regex needs (?<category>...) and (?<text>...) named captures.\n";
154 19 my(%cat, @rules);
155 18 ▶ while ($text =~ /$regex/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /$regex/gu) 7 2 9
156 17 my($cat, $str) = ($+{category}, $+{text});
157 2 _check_category($cat);
158 2 ▶ if (exists $cat{$str}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cat{$str}) 2 4 6
159 4 ▶ $cat{$str} eq $cat
Branch: 1 branch
BranchTrueFalseTotal
$cat{$str} eq $cat or die qq["$str": conflicting categories ($cat{$str} vs $cat).\n] 1 1 2
160 or die "\"$str\": conflicting categories ($cat{$str} vs $cat).\n";
161 3 next;
162 }
163 3 $cat{$str} = $cat;
164 3 push @rules, [ $cat, quotemeta($str) ];
165 }
166 1 \@rules;
167 }
168  
169 sub _all_rules {
170 2 ▶ my $obj = shift;
Subroutine _all_rules: called CC=1 SCAR=0.0
171 11 (@{$obj->{RULES}}, @{$obj->{FILERULES}});
172 }
173  
174 sub _stable_tag {
175 4 ▶ my($obj, $tag, $matched) = @_;
Subroutine _stable_tag: called CC=2 SCAR=6.9
176 4 my $key = "$tag\0$matched";
177 7 ▶ $obj->{ASSIGNED}{$key} //= do {
Condition66%2/3
$obj->{"ASSIGNED"}{$key} //= do { my $t = sprintf("<%s %s="%d" />", $tag, $obj->{"INDEX"}, ++$obj->{"COUNTER"}{$tag}); $obj->{"ORIGIN"}{$t} = $matched; push @{$$obj{"ASSIGN_ORDER"};}, $t; $t }
l!l&&r!l&&!r
19320
MC/DC0%0/2
$obj->{"ASSIGNED"}{$key} //= do { my $t = sprintf("<%s %s="%d" />", $tag, $obj->{"INDEX"}, ++$obj->{"COUNTER"}{$tag}); $obj->{"ORIGIN"}{$t} = $matched; push @{$$obj{"ASSIGN_ORDER"};}, $t; $t }
$obj->{"ASSIGNED"}{$key} do { my $t = sprintf("<%s %s="%d" />", $tag, $obj->{"INDEX"}, ++$obj->{"COUNTER"}{$tag}); $obj->{"ORIGIN"}{$t} = $matched; push @{$$obj{"ASSIGN_ORDER"};}, $t; $t }
Truth table66%2/3
A //= B
A= $obj->{"ASSIGNED"}{$key}B= do { my $t = sprintf("<%s %s="%d" />", $tag, $obj->{"INDEX"}, ++$obj->{"COUNTER"}{$tag}); $obj->{"ORIGIN"}{$t} = $matched; push @{$$obj{"ASSIGN_ORDER"};}, $t; $t }
ABresult
1X1
011
000
178 my $t = sprintf('<%s %s="%d" />',
179 7 $tag, $obj->{INDEX}, ++$obj->{COUNTER}{$tag});
180 6 $obj->{ORIGIN}{$t} = $matched;
181 2 push @{$obj->{ASSIGN_ORDER}}, $t;
182 4 $t;
183 };
184 }
185  
186 sub _mask_stable {
187 4 ▶ my($obj, $track) = splice @_, 0, 2;
Subroutine _mask_stable: called CC=4 SCAR=13.9
188 2 for (@_) {
189 55 for my $rule ($obj->_all_rules) {
190 55 my($tag, $pat) = @$rule;
191 55 s{$pat}{
192 55 my $t = $obj->_stable_tag($tag, ${^MATCH});
193 51 ▶ $obj->{TRACK}{$t}++ if $track;
Branch: 1 branch
BranchTrueFalseTotal
if $track 37 14 51
194 51 $t;
195 }gpe;
196 }
197 }
198 51 return $obj;
199 }
200  
201 sub mask {
202 32 ▶ my $obj = shift;
Subroutine mask: called CC=7 SCAR=19.6
Pod undocumented
203 32 ▶ if ($obj->{STABLE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{"STABLE"}) 24 2 26
204 32 return $obj->_mask_stable(1, @_);
205 }
206 32 ▶ my $pattern = $obj->{PATTERN} // die;
Condition50%1/2
$obj->{"PATTERN"} // die()
l!l
20
MC/DC0%0/1
$obj->{"PATTERN"} // die()
$obj->{"PATTERN"}
Truth table50%1/2
A
A= $obj->{"PATTERN"}
Aresult
11
00
207 32 ▶ my @patterns = ref $pattern ? @$pattern : $pattern;
Branch: 1 branch
BranchTrueFalseTotal
ref $pattern ? : 2 0 2
208 37 my $fromto = $obj->{TABLE};
209 # edit parameters in place
210 37 for (@_) {
211 55 for my $pat (@patterns) {
212 148 ▶ next if $pat =~ /^\s*(#|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if $pat =~ /^\s*(#|$)/u 0 2 2
213 148 s{$pat}{
214 my $tag = sprintf('<%s %s="%d" />',
215 51 $obj->{TAG}, $obj->{INDEX}, ++$obj->{NUMBER});
216 51 push @$fromto, [ $tag, ${^MATCH} ];
217 51 $tag;
218 }gpe;
219 }
220 }
221 37 return $obj;
222 }
223  
224 sub mask_reference {
225 26 ▶ my $obj = shift;
Subroutine mask_reference: called CC=2 SCAR=7.1
Pod undocumented
226 26 ▶ $obj->{STABLE} or die "mask_reference requires STABLE mode.\n";
Branch: 1 branch
BranchTrueFalseTotal
$obj->{"STABLE"} or die "mask_reference requires STABLE mode. " 13 0 13
227 24 $obj->_mask_stable(0, @_);
228 }
229  
230 sub unmask {
231 2 ▶ my $obj = shift;
Subroutine unmask: called CC=14 SCAR=26.4
Pod undocumented
232 2 ▶ if ($obj->{STABLE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{"STABLE"}) 22 2 24
233 # Verify the complete placeholder multiset before restoring anything.
234 # ASSIGN_ORDER also includes reference-only tags; those must not leak
235 # into the response unless the same stable tag occurs in the payload.
236 2 for my $t (@{$obj->{ASSIGN_ORDER}}) {
237 2 ▶ my $want = $obj->{TRACK}{$t} // 0;
Condition100%2/2
$obj->{"TRACK"}{$t} // 0
l!l
282
MC/DC100%1/1
$obj->{"TRACK"}{$t} // 0
$obj->{"TRACK"}{$t}
Truth table100%2/2
A
A= $obj->{"TRACK"}{$t}
Aresult
11
00
238 2 my $got = 0;
239 2 $got += () = /\Q$t\E/g for @_;
240 3 ▶ $got == $want or die sprintf(
Branch: 1 branch
BranchTrueFalseTotal
$got == $want or die sprintf("Masking error: "%s" count mismatch (expected %d, got %d). ", $t, $want, $got) 27 3 30
241 "Masking error: \"%s\" count mismatch (expected %d, got %d).\n",
242 $t, $want, $got);
243 }
244 3 for (@_) {
245 # Restore in REVERSE assignment order: the escape rule runs
246 # first, so its tags are assigned first and must be restored
247 # last -- an escaped literal may itself look like one of our
248 # later tags, and restoring it earlier would let a later
249 # substitution corrupt it.
250 3 for my $t (reverse @{$obj->{ASSIGN_ORDER}}) {
251 13 my $orig = $obj->{ORIGIN}{$t};
252 13 s/\Q$t/$orig/g;
253 }
254 }
255 13 return $obj;
256 }
257 24 my @tags = map $_->[0], @{$obj->{TABLE}};
258 22 for my $t (@tags) {
259 22 my $got = 0;
260 30 $got += () = /\Q$t\E/g for @_;
261 30 ▶ $got == 1 or die sprintf(
Branch: 1 branch
BranchTrueFalseTotal
$got == 1 or die sprintf("Masking error: "%s" count mismatch (expected 1, got %d). ", $t, $got) 3 0 3
262 "Masking error: \"%s\" count mismatch (expected 1, got %d).\n",
263 $t, $got);
264 }
265 # edit parameters in place
266 30 for (@_) {
267 30 for my $fromto (reverse @{$obj->{TABLE}}) {
268 28 my($from, $to) = @$fromto;
269 28 s/\Q$from/$to/g;
270 }
271 }
272 41 ▶ $obj->reset if $obj->{AUTORESET};
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"AUTORESET"} 0 2 2
273 41 return $obj;
274 }
275  
276 1;
« blib/lib/App/Greple/xlate/Cache.pm ↑ Summary blib/lib/App/Greple/xlate/llm.pm »