User/Identity/Archive/Plain.pm untested

filter: Statementstmt 0.0% 0 / 149 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 292 CC 27 SCAR 37.5
CC
27
Cov
0%
_collectItem
90.0
init
56.0
from
42.0
CRAP 42.6
1 # This code is part of Perl distribution User-Identity version 4.00.
2 # The POD got stripped from this file by OODoc version 3.05.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 2003-2025 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11  
12 package User::Identity::Archive::Plain;{
13 our $VERSION = '4.00';
14 }
15  
16 use parent 'User::Identity::Archive';
17  
18 use strict;
19 use warnings;
20  
21 use Log::Report 'user-identity';
22  
23 #--------------------
24  
25 my %abbreviations = (
26 user => 'User::Identity',
27 email => 'Mail::Identity',
28 location => 'User::Identity::Location',
29 system => 'User::Identity::System',
30 list => 'User::Identity::Collection::Emails',
31 );
32  
33 sub init($)
34 { my ($self, $args) = @_;
35 $self->SUPER::init($args);
36  
37 # Define the keywords.
38  
39 my %only;
40 if(my $only = delete $args->{only})
41 { my @only = ref $only ? @$only : $only;
42 $only{$_}++ for @only;
43 }
44  
45 while( my($k,$v) = each %abbreviations)
46 { $self->abbreviation($k, $v) unless keys %only && !$only{$k};
47 }
48  
49 if(my $abbrevs = delete $args->{abbreviations})
50 { $abbrevs = { @$abbrevs } if ref $abbrevs eq 'ARRAY';
51 while( my($k,$v) = each %$abbrevs)
52 { $self->abbreviation($k, $v) unless keys %only && !$only{$k};
53 }
54 }
55  
56 warning __x"option 'only' specifies undefined abbreviation '{abbrev}'.", abbrev => $_
57 for grep ! defined $self->abbreviation($_), keys %only;
58  
59 $self->{UIAP_items} = {};
60 $self->{UIAP_tabstop} = delete $args->{tabstop} || 8;
61 $self;
62 }
63  
64  
65 sub from($@)
66 { my ($self, $in, %args) = @_;
67  
68 exists $args{verbose} and error "from(verbose) now from Log::Report dispatcher mode";
69 my ($source, @lines);
70  
71 if(ref $in)
72 { ($source, @lines)
73 = ref $in eq 'ARRAY' ? ('array', @$in)
74 : ref $in eq 'GLOB' ? ('GLOB', <$in>)
75 : $in->isa('IO::Handle') ? (ref $in, $in->getlines)
76 : error __x"cannot read archive from a {type}.", type => ref $in;
77 }
78 else
79 { open my($fh), "<", $in
80 or fault __x"cannot read archive from file {file}", file => $in;
81  
82 $source = "file $in";
83 @lines = $fh->getlines;
84 }
85  
86 info "reading archive data from {source}.", source => $source;
87 @lines or return $self;
88  
89 my $tabstop = $args{tabstop} || $self->defaultTabStop;
90  
91 $self->_set_lines($source, \@lines, $tabstop);
92  
93 while(my $starter = $self->_get_line)
94 { $self->_accept_line;
95 my $indent = $self->_indentation($starter);
96  
97 trace " adding $starter";
98  
99 my $item = $self->_collectItem($starter, $indent);
100 $self->add($item->type => $item) if defined $item;
101 }
102 $self;
103 }
104  
105 sub _set_lines($$$)
106 { my ($self, $source, $lines, $tab) = @_;
107 $self->{UIAP_lines} = $lines;
108 $self->{UIAP_source} = $source;
109 $self->{UIAP_curtab} = $tab;
110 $self->{UIAP_linenr} = 0;
111 $self;
112 }
113  
114 sub _get_line()
115 { my $self = shift;
116 my ($lines, $linenr, $line) = @$self{ qw/UIAP_lines UIAP_linenr UIAP_line/};
117  
118 # Accept old read line, if it was not accepted.
119 return $line if defined $line;
120  
121 # Need to read a new line;
122 $line = '';
123 while($linenr < @$lines)
124 { my $reading = $lines->[$linenr];
125  
126 $linenr++, next if $reading =~ m/^\s*\#/; # skip comments
127 $linenr++, next unless $reading =~ m/\S/; # skip blanks
128 $line .= $reading;
129  
130 if($line =~ s/\\\s*$//)
131 { $linenr++;
132 next;
133 }
134  
135 if($line =~ m/^\s*tabstop\s*\=\s*(\d+)/ )
136 { $self->{UIAP_curtab} = $1;
137 $line = '';
138 next;
139 }
140  
141 last;
142 }
143  
144 length $line || $linenr < @$lines
145 or return ();
146  
147 $self->{UIAP_linenr} = $linenr;
148 $self->{UIAP_line} = $line;
149 $line;
150 }
151  
152 sub _accept_line()
153 { my $self = shift;
154 delete $self->{UIAP_line};
155 $self->{UIAP_linenr}++;
156 }
157  
158 sub _location() { @{$_[0]}{ qw/UIAP_source UIAP_linenr/ } }
159  
160 sub _indentation($)
161 { my ($self, $line) = @_;
162 defined $line or return -1;
163  
164 my ($indent) = $line =~ m/^(\s*)/;
165 index($indent, "\t") >= 0
166 or return length $indent;
167  
168 my $column = 0;
169 my $tab = $self->{UIAP_curtab};
170 my @chars = split //, $indent;
171 while(my $char = shift @chars)
172 { $column++, next if $char eq ' ';
173 $column = (int($column/$tab+0.0001)+1)*$tab;
174 }
175 $column;
176 }
177  
178 sub _collectItem($$)
179 { my ($self, $starter, $indent) = @_;
180 my ($type, $name) = $starter =~ m/(\w+)\s*(.*?)\s*$/;
181 my $class = $abbreviations{$type};
182 my $skip = ! defined $class;
183  
184 my (@fields, @items);
185  
186 while(1)
187 { my $line = $self->_get_line;
188 my $this_indent = $self->_indentation($line);
189 last if $this_indent <= $indent;
190  
191 $self->_accept_line;
192 $line =~ s/[\r\n]+$//;
193 next if $skip;
194  
195 my $next_line = $self->_get_line;
196 my $next_indent = $self->_indentation($next_line);
197  
198 if($this_indent < $next_indent)
199 { # start a collectable item
200 my $item = $self->_collectItem($line, $this_indent);
201 push @items, $item if defined $item;
202 }
203 elsif($this_indent==$next_indent && $line =~ m/^\s*(\w*)\s*(\w+)\s*\=\s*(.*)/ )
204 { # Lookup!
205 my ($group, $name, $lookup) = ($1,$2,$3);
206 my $item; # not implemented yet
207 push @items, $item if defined $item;
208 }
209 else
210 { # defined a field
211 my ($group, $name) = $line =~ m/(\w+)\s*(.*)/;
212 $name =~ s/\s*$//;
213 push @fields, $group => $name;
214 next;
215 }
216 }
217  
218 @fields || @items or return ();
219  
220 #XXX needs conversion to Log::Report
221  
222 my $warn = 0;
223 my $warn_sub = $SIG{__WARN__};
224 $SIG{__WARN__} = sub { $warn++; $warn_sub ? $warn_sub->(@_) : print STDERR @_ };
225  
226 my $item = $class->new(name => $name, @fields);
227 $SIG{__WARN__} = $warn_sub;
228  
229 if($warn)
230 { my ($source, $linenr) = $self->_location;
231 $linenr -= 1;
232 warn " found in $source around line $linenr\n";
233 }
234  
235 $item->add($_->type => $_) for @items;
236 $item;
237 }
238  
239 #--------------------
240  
241 sub defaultTabStop(;$)
242 { my $self = shift;
243 @_ ? ($self->{UIAP_tabstop} = shift) : $self->{UIAP_tabstop};
244 }
245  
246  
247 sub abbreviation($;$)
248 { my ($self, $name) = (shift, shift);
249 @_ or return $self->{UIAP_abbrev}{$name};
250  
251 my $class = shift;
252 defined $class
253 or return delete $self->{UIAP_abbrev}{$name};
254  
255 eval "require $class";
256 $@ and die $@;
257  
258 $self->{UIAP_abbrev}{$name} = $class;
259 }
260  
261  
262 sub abbreviations() { sort keys %{ $_[0]->{UIAP_abbrev}} }
263  
264 #--------------------
265  
266 1;