HolidayParser/iCalendar.pm

filter: Statementstmt 98.9% 92 / 93 Branchbran 86.3% 19 / 22 Conditioncond 46.6% 7 / 15 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 21 / 21 Podpod 15.3% 2 / 13 totaltotal 83.1% 143 / 172 CC 17 SCAR 9.8
CC
17
Cov
91%
get_timeinfo
5.4
get_dateinfo
4.1
list_events
4.1
CRAP 2.7
« HolidayParser.pm ↑ Summary
1 #!/usr/bin/perl
2 # Date::HolidayParser
3 # A parser of ~/.holiday-style files.
4 # The format is based off of the holiday files found bundled
5 # with the plan program, not any official spec. This because no
6 # official spec could be found.
7 # Copyright (C) Eskild Hustvedt 2006, 2007, 2008, 2010
8 #
9 # This program is free software; you can redistribute it and/or modify it
10 # under the same terms as Perl itself. There is NO warranty;
11 # not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 #
13 # This is the iCalendar component, which emulates a DP::iCalendar-like interface
14 # in order to make it easier to use for users familiar with iCalendar, and
15 # make it compatible with DP::iCalendar::Manager.
16  
17 package Date::HolidayParser::iCalendar;
18  
19 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Date::HolidayParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use constant { true => 1, false => undef };
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 our $VERSION = 0.4_2;
24  
25 2 extends 'Date::HolidayParser';
26  
27 has '_UID_List' => (
28 is => 'rw',
29 4 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
30 2 );
31 has '_iCal_cache' => (
32 is => 'rw',
33 4 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 2 );
35  
36 # -- Public methods --
37  
38 # Purpose: Get an iCalendar hash with holiday info matching the supplied UID
39 # Usage: get_info(UID);
40 sub get_info
41 72 ▶{
Subroutine get_info: called CC=2 SCAR=6.9
Pod documented
42 72 my $self = shift;
43 72 my $UID = shift;
44 72 ▶ return($self->_UID_List->{$UID}) if $self->_UID_List->{$UID};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_UID_List->{$UID} 69 3 72
45 3 return(false);
46 }
47  
48 # Purpose: List events in said year, on said month and day
49 # Usage: obj->list_events(year?,month?,day?);
50 # year is required, others are optional.
51 #
52 # This is the primary API for this module. It does only wrap the other
53 # methods, but provides a cleaner interface for new code.
54 sub list_events
55 24 ▶{
Subroutine list_events: called CC=4 SCAR=14.0
Pod documented
56 24 my ($self,$Year,$Month,$Day) = @_;
57 24 ▶ if(not defined $Year)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Year) 0 24 24
58 {
59 0 croak('Requried parameter "Year" not supplied');
60 }
61 24 ▶ if(defined $Day)
Branch: 2 branches
BranchTrueFalseTotal
if (defined $Day) { } 9 15 24
elsif (defined $Month) { } 12 3 15
62 {
63 9 return $self->get_timeinfo($Year,$Month,$Day,'DAY');
64 }
65 elsif(defined $Month)
66 {
67 12 return $self->get_monthinfo($Year,$Month);
68 }
69 else
70 {
71 3 return $self->get_months($Year);
72 }
73 }
74  
75 # Purpose: Get information for the supplied month (list of days there are events)
76 # Usage: my $TimeRef = $object->get_monthinfo(YEAR,MONTH,DAY);
77 sub get_monthinfo
78 24 ▶{
Subroutine get_monthinfo: called CC=3 SCAR=11.4
Pod undocumented
79 24 my($self, $Year, $Month) = @_; # TODO: verify that they are set
80 24 $self->get($Year);
81 24 my @Array;
82 24 ▶ if(defined($self->_iCal_cache->{$Year}) and defined($self->_iCal_cache->{$Year}{$Month})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_iCal_cache->{$Year} and defined $self->_iCal_cache->{$Year}{$Month}) 24 0 24
Condition33%1/3
defined $self->_iCal_cache->{$Year} and defined $self->_iCal_cache->{$Year}{$Month}
!ll&&!rl&&r
0024
MC/DC0%0/2
defined $self->_iCal_cache->{$Year} and defined $self->_iCal_cache->{$Year}{$Month}
defined $self->_iCal_cache->{$Year} defined $self->_iCal_cache->{$Year}{$Month}
Truth table33%1/3
A and B
A= defined $self->_iCal_cache->{$Year}B= defined $self->_iCal_cache->{$Year}{$Month}
ABresult
0X0
100
111
83 24 @Array = sort keys(%{$self->_iCal_cache->{$Year}{$Month}});
84 }
85 24 return(\@Array);
86 }
87  
88 # Purpose: Get information for the supplied date (list of times in the day there are events)
89 # Usage: my $TimeRef = $object->get_dateinfo(YEAR,MONTH,DAY);
90 sub get_dateinfo
91 6 ▶{
Subroutine get_dateinfo: called CC=4 SCAR=14.2
Pod undocumented
92 6 my($self, $Year, $Month, $Day) = @_; # TODO: verify that they are set
93 6 $self->get($Year);
94 6 my @Array;
95 6 ▶ if(defined($self->_iCal_cache->{$Year}) and defined($self->_iCal_cache->{$Year}{$Month}) and defined($self->_iCal_cache->{$Year}{$Month}{$Day})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_iCal_cache->{$Year} and defined $self->_iCal_cache->{$Year}{$Month} and defined $self->_iCal_cache->{$Year}{$Month}{$Day}) 3 3 6
Condition50%3/6
defined $self->_iCal_cache->{$Year} and defined $self->_iCal_cache->{$Year}{$Month} and defined $self->_iCal_cache->{$Year}{$Month}{$Day}
!ll&&!rl&&r
033
defined $self->_iCal_cache->{$Year} and defined $self->_iCal_cache->{$Year}{$Month}
!ll&&!rl&&r
006
MC/DC33%1/3
defined $self->_iCal_cache->{$Year} and defined $self->_iCal_cache->{$Year}{$Month} and defined $self->_iCal_cache->{$Year}{$Month}{$Day}
defined $self->_iCal_cache->{$Year} defined $self->_iCal_cache->{$Year}{$Month} defined $self->_iCal_cache->{$Year}{$Month}{$Day}
Truth table50%2/4
A and B and C
A= defined $self->_iCal_cache->{$Year}B= defined $self->_iCal_cache->{$Year}{$Month}C= defined $self->_iCal_cache->{$Year}{$Month}{$Day}
ABCresult
0XX0
10X0
1100
1111
96 3 @Array = sort keys(%{$self->_iCal_cache->{$Year}{$Month}{$Day}});
97 }
98 6 return(\@Array);
99 }
100  
101 # Purpose: Return an empty array, unsupported.
102 # Usage: my $UIDRef = $object->get_timeinfo(YEAR,MONTH,DAY,TIME);
103 sub get_timeinfo
104 18 ▶{
Subroutine get_timeinfo: called CC=5 SCAR=16.8
Pod undocumented
105 18 my($self, $Year, $Month, $Day,$Time) = @_;
106  
107 18 ▶ return(undef) if not $Time eq 'DAY';
Branch: 1 branch
BranchTrueFalseTotal
unless $Time eq "DAY" 0 18 18
108  
109 18 $self->get($Year);
110  
111 18 ▶ if( defined($self->_iCal_cache->{$Year}) and
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_iCal_cache->{$Year} and defined $self->_iCal_cache->{$Year}{$Month} and defined $self->_iCal_cache->{$Year}{$Month}{$Day}) 12 6 18
Condition50%3/6
defined $self->_iCal_cache->{$Year} and defined $self->_iCal_cache->{$Year}{$Month} and defined $self->_iCal_cache->{$Year}{$Month}{$Day}
!ll&&!rl&&r
0612
defined $self->_iCal_cache->{$Year} and defined $self->_iCal_cache->{$Year}{$Month}
!ll&&!rl&&r
0018
MC/DC33%1/3
defined $self->_iCal_cache->{$Year} and defined $self->_iCal_cache->{$Year}{$Month} and defined $self->_iCal_cache->{$Year}{$Month}{$Day}
defined $self->_iCal_cache->{$Year} defined $self->_iCal_cache->{$Year}{$Month} defined $self->_iCal_cache->{$Year}{$Month}{$Day}
Truth table50%2/4
A and B and C
A= defined $self->_iCal_cache->{$Year}B= defined $self->_iCal_cache->{$Year}{$Month}C= defined $self->_iCal_cache->{$Year}{$Month}{$Day}
ABCresult
0XX0
10X0
1100
1111
112 defined($self->_iCal_cache->{$Year}{$Month}) and
113 defined($self->_iCal_cache->{$Year}{$Month}{$Day})
114 )
115 {
116 12 return($self->_iCal_cache->{$Year}{$Month}{$Day}{$Time});
117 }
118 6 return([]);
119 }
120  
121 # Purpose: Get a list of months which have events (those with *only* recurring not counted)
122 # Usage: my $ArrayRef = $object->get_months(YEAR);
123 sub get_months
124 6 ▶{
Subroutine get_months: called CC=1 SCAR=0.0
Pod undocumented
125 6 my ($self, $Year) = @_;
126 6 $self->get($Year);
127 6 my @Array = sort keys(%{$self->_iCal_cache->{$Year}});
128 6 return(\@Array);
129 }
130  
131 # Purpose: Check if there is an holiday event with the supplied UID
132 # Usage: $bool = $object->exists($UID);
133 sub exists
134 36 ▶{
Subroutine exists: called CC=2 SCAR=6.9
Pod undocumented
135 36 my $self = shift;
136 36 my $UID = shift;
137 36 ▶ return(true) if defined($self->_UID_List->{$UID});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_UID_List->{$UID} 33 3 36
138 3 return(false);
139 }
140  
141 # -- Unsupported or dummy methods, here for compatibility --
142  
143 # Purpose: Return an empty array, unsupported.
144 # Usage: my $ArrayRef = $object->get_years();
145 sub get_years
146 3 ▶{
Subroutine get_years: called CC=1 SCAR=0.0
Pod undocumented
147 3 return([]);
148 }
149  
150 # -- DP::iCalendar compatibility code --
151  
152 # Used by DP::iCalendar::Manager to set the prodid in output iCalendar files.
153 # We can't output iCalendar files, so we just ignore calls to it.
154 3 ▶sub set_prodid { }
Subroutine set_prodid: called CC=1 SCAR=0.0
Pod undocumented
155  
156 # Purpose: Return manager information
157 # Usage: get_manager_version();
158 sub get_manager_version
159 6 ▶{
Subroutine get_manager_version: called CC=1 SCAR=0.0
Pod undocumented
160 6 my $self = shift;
161 6 return('01_capable');
162 }
163  
164 # Purpose: Return manager capability information
165 # Usage: get_manager_capabilities
166 sub get_manager_capabilities
167 6 ▶{
Subroutine get_manager_capabilities: called CC=1 SCAR=0.0
Pod undocumented
168 # All capabilites as of 01_capable
169 6 return(['LIST_DPI',])
170 }
171  
172  
173 # -- Private methods --
174  
175 # Purpose: Wraps _addParsedEvent in Date::HolidayParser so that an iCalendar version
176 # is also created at the same time.
177 around '_addParsedEvent' => sub
178 44 ▶{
Subroutine __ANON__: called CC=2 SCAR=6.9
179 44 my $orig = shift;
180 44 my $self = shift;
181  
182 44 my($FinalParsing,$final_mon,$final_mday,$HolidayName,$holidayType,$FinalYDay,$PosixYear) = @_;
183  
184 44 my $UID = $self->_event_to_iCalendar($FinalYDay,$PosixYear,$HolidayName);
185 44 my $Year = $PosixYear+1900;
186  
187 44 ▶ if(not $self->_iCal_cache->{$Year}->{$final_mon}{$final_mday}{'DAY'})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_iCal_cache->{$Year}{$final_mon}{$final_mday}{"DAY"}) 32 12 44
188 {
189 32 $self->_iCal_cache->{$Year}->{$final_mon}{$final_mday}{'DAY'} = [];
190 }
191 44 push(@{$self->_iCal_cache->{$Year}->{$final_mon}{$final_mday}{'DAY'}}, $UID);
192  
193 44 return $self->$orig(@_);
194 2 };
195  
196 # Purpose: Generate an iCalendar entry
197 # Usage: this->_event_to_iCalendar(UNIXTIME, NAME);
198 sub _event_to_iCalendar
199 44 ▶{
Subroutine _event_to_iCalendar: called CC=1 SCAR=0.0
200 44 my $self = shift;
201 44 my $FinalYDay = shift;
202 44 my $PosixYear = shift;
203 44 my $name = shift;
204 44 $name =~ s/\s/-/g;
205  
206 44 my $unixtime = POSIX::mktime(0, 0, 0, $FinalYDay, 0, $PosixYear);
207  
208 # Generate the UID of the event, this is simply a
209 44 my $sum = unpack('%32C*', $name);
210 # This should be unique enough for our needs.
211 # We don't want it to be random, because if someone copies the events to their
212 # own calendar, we want DP::iCalendar::Manager to fetch the information from
213 # the changed calendar, instead of from the HolidayParser object.
214 44 my $UID = 'D-HP-ICS-'.$FinalYDay.'-'.$PosixYear.'-'.$sum;
215
216 44 $self->_UID_List->{$UID} = {
217 UID => $UID,
218 DTSTART => iCal_ConvertFromUnixTime($unixtime),
219 DTEND => iCal_ConvertFromUnixTime($unixtime+86390), # Yes, this is purposefully not 86400
220 SUMMARY => $name,
221 };
222 44 return($UID);
223 }
224  
225 # The following three functions are originally from DP::iCalendar
226  
227 # Purpose: Generate an iCalendar date-time from multiple values
228 # Usage: my $iCalDateTime = iCal_GenDateTime(YEAR, MONTH, DAY, TIME);
229 88 ▶sub iCal_GenDateTime {
Subroutine iCal_GenDateTime: called CC=1 SCAR=0.0
Pod undocumented
230 # NOTE: This version ignores $Time because it isn't used in HolidayParser
231 88 my ($Year, $Month, $Day, $Time) = @_;
232 # Fix the month and day
233 88 my $iCalMonth = _PrefixZero($Month);
234 88 my $iCalDay = _PrefixZero($Day);
235 88 return("$Year$iCalMonth$iCalDay");
236 }
237  
238 # Purpose: Generate an iCalendar date-time string from a UNIX time string
239 # Usage: my $iCalDateTime = iCal_ConvertFromUnixTime(UNIX TIME);
240 88 ▶sub iCal_ConvertFromUnixTime {
Subroutine iCal_ConvertFromUnixTime: called CC=1 SCAR=0.0
Pod undocumented
241 88 my $UnixTime = shift;
242 88 my ($realsec,$realmin,$realhour,$realmday,$realmonth,$realyear,$realwday,$realyday,$realisdst) = localtime($UnixTime);
243 88 $realyear += 1900; # Fix the year
244 88 $realmonth++; # Fix the month
245 # Return data from iCal_GenDateTime
246 88 return(iCal_GenDateTime($realyear,$realmonth,$realmday,"$realhour:$realmin"));
247 }
248  
249 # Purpose: Prefix a "0" to a number if it is only one digit.
250 # Usage: my $NewNumber = PrefixZero(NUMBER);
251 176 ▶sub _PrefixZero {
Subroutine _PrefixZero: called CC=2 SCAR=6.9
252 176 ▶ if ($_[0] =~ /^\d$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\d$/) 88 88 176
253 88 return("0$_[0]");
254 }
255 88 return($_[0]);
256 }
257  
258 # End of Date::HolidayParser::iCalendar
259 2 1;
260  
261 __END__
« HolidayParser.pm ↑ Summary