User/Identity/Archive/Plain.pm untested

filter: Statementstmt 0.0% 0 / 153 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 24 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 280 SCAR 73.5
CC
39
Cov
0%
from
110.0
_collectItem
110.0
init
72.0
CRAP 1560.0
1 # Copyrights 2003-2022 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.03.
5 # This code is part of distribution User-Identity. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package User::Identity::Archive::Plain;
10 use vars '$VERSION';
11 $VERSION = '1.01';
12  
13 use base 'User::Identity::Archive';
14  
15 use strict;
16 use warnings;
17  
18 use Carp;
19  
20  
21 my %abbreviations =
22 ( user => 'User::Identity'
23 , email => 'Mail::Identity'
24 , location => 'User::Identity::Location'
25 , system => 'User::Identity::System'
26 , list => 'User::Identity::Collection::Emails'
27 );
28  
29 sub init($)
30 { my ($self, $args) = @_;
31 $self->SUPER::init($args) or return;
32  
33 # Define the keywords.
34  
35 my %only;
36 if(my $only = delete $args->{only})
37 { my @only = ref $only ? @$only : $only;
38 $only{$_}++ for @only;
39 }
40  
41 while( my($k,$v) = each %abbreviations)
42 { $self->abbreviation($k, $v) unless keys %only && !$only{$k};
43 }
44
45 if(my $abbrevs = delete $args->{abbreviations})
46 { $abbrevs = { @$abbrevs } if ref $abbrevs eq 'ARRAY';
47 while( my($k,$v) = each %$abbrevs)
48 { $self->abbreviation($k, $v) unless keys %only && !$only{$k};
49 }
50 }
51  
52 foreach (keys %only)
53 { warn "Option 'only' specifies undefined abbreviation '$_'\n"
54 unless defined $self->abbreviation($_);
55 }
56  
57 $self->{UIAP_items} = {};
58 $self->{UIAP_tabstop} = delete $args->{tabstop} || 8;
59 $self;
60 }
61  
62  
63 sub from($@)
64 { my ($self, $in, %args) = @_;
65  
66 my $verbose = $args{verbose} || 0;
67 my ($source, @lines);
68  
69 if(ref $in)
70 { ($source, @lines)
71 = ref $in eq 'ARRAY' ? ('array', @$in)
72 : ref $in eq 'GLOB' ? ('GLOB', <$in>)
73 : $in->isa('IO::Handle') ? (ref $in, $in->getlines)
74 : confess "Cannot read from a ", ref $in, "\n";
75 }
76 elsif(open IN, "<", $in)
77 { $source = "file $in";
78 @lines = <IN>;
79 }
80 else
81 { warn "Cannot read archive from file $in: $!\n";
82 return $self;
83 }
84  
85 print "reading data from $source\n" if $verbose;
86  
87 return $self unless @lines;
88 my $tabstop = $args{tabstop} || $self->defaultTabStop;
89  
90 $self->_set_lines($source, \@lines, $tabstop);
91  
92 while(my $starter = $self->_get_line)
93 { $self->_accept_line;
94 #warn "$verbose: $starter\n";
95 my $indent = $self->_indentation($starter);
96  
97 print " adding $starter" if $verbose > 1;
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 return () unless length $line || $linenr < @$lines;
144
145 $self->{UIAP_linenr} = $linenr;
146 $self->{UIAP_line} = $line;
147 $line;
148 }
149  
150 sub _accept_line()
151 { my $self = shift;
152 delete $self->{UIAP_line};
153 $self->{UIAP_linenr}++;
154 }
155  
156 sub _location() { @{ (shift) }{ qw/UIAP_source UIAP_linenr/ } }
157  
158 sub _indentation($)
159 { my ($self, $line) = @_;
160 return -1 unless defined $line;
161  
162 my ($indent) = $line =~ m/^(\s*)/;
163 return length($indent) unless index($indent, "\t") >= 0;
164  
165 my $column = 0;
166 my $tab = $self->{UIAP_curtab};
167 my @chars = split //, $indent;
168 while(my $char = shift @chars)
169 { $column++, next if $char eq ' ';
170 $column = (int($column/$tab+0.0001)+1)*$tab;
171 }
172 $column;
173 }
174  
175 sub _collectItem($$)
176 { my ($self, $starter, $indent) = @_;
177 my ($type, $name) = $starter =~ m/(\w+)\s*(.*?)\s*$/;
178 my $class = $abbreviations{$type};
179 my $skip = ! defined $class;
180 #warn "Skipping type $type\n" if $skip;
181  
182 my (@fields, @items);
183  
184 while(1)
185 { my $line = $self->_get_line;
186 my $this_indent = $self->_indentation($line);
187 last if $this_indent <= $indent;
188  
189 $self->_accept_line;
190 $line =~ s/[\r\n]+$//;
191 #warn "Skipping line $line\n" if $skip;
192 next if $skip;
193  
194 my $next_line = $self->_get_line;
195 my $next_indent = $self->_indentation($next_line);
196  
197 if($this_indent < $next_indent)
198 { # start a collectable item
199 #warn "Accepting item $line, $this_indent\n";
200 my $item = $self->_collectItem($line, $this_indent);
201 push @items, $item if defined $item;
202 #warn "Item ready $line\n";
203 }
204 elsif( $this_indent==$next_indent
205 && $line =~ m/^\s*(\w*)\s*(\w+)\s*\=\s*(.*)/ )
206 { # Lookup!
207 my ($group, $name, $lookup) = ($1,$2,$3);
208 #warn "Lookup ($group, $name, $lookup)";
209 my $item; # not implemented yet
210 push @items, $item if defined $item;
211 }
212 else
213 { # defined a field
214 #warn "Accepting field $line\n";
215 my ($group, $name) = $line =~ m/(\w+)\s*(.*)/;
216 $name =~ s/\s*$//;
217 push @fields, $group => $name;
218 next;
219 }
220 }
221  
222 return () unless @fields || @items;
223  
224 #warn "$class NAME=$name";
225 my $warn = 0;
226 my $warn_sub = $SIG{__WARN__};
227 $SIG{__WARN__}
228 = sub {$warn++; $warn_sub ? $warn_sub->(@_) : print STDERR @_};
229  
230 my $item = $class->new(name => $name, @fields);
231 $SIG{__WARN__} = $warn_sub;
232  
233 if($warn)
234 { my ($source, $linenr) = $self->_location;
235 $linenr -= 1;
236 warn " found in $source around line $linenr\n";
237 }
238 #warn $_->type foreach @items;
239  
240 $item->add($_->type => $_) foreach @items;
241 $item;
242 }
243  
244  
245 sub defaultTabStop(;$)
246 { my $self = shift;
247 @_ ? ($self->{UIAP_tabstop} = shift) : $self->{UIAP_tabstop};
248 }
249  
250  
251 sub abbreviation($;$)
252 { my ($self, $name) = (shift, shift);
253 return $self->{UIAP_abbrev}{$name} unless @_;
254  
255 my $class = shift;
256 return delete $self->{UIAP_abbrev}{$name} unless defined $class;
257  
258 eval "require $class";
259 die "Class $class is not usable, because of errors:\n$@" if $@;
260  
261 $self->{UIAP_abbrev}{$name} = $class;
262 }
263  
264  
265 sub abbreviations() { sort keys %{shift->{UIAP_abbrev}} }
266  
267  
268 1;
269