script/svgcal.pl untested

filter: Statementstmt 0.0% 0 / 112 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 228 CC 22 SCAR 47.3
CC
22
Cov
0%
main
240.0
get_config
72.0
show_template
2.0
CRAP 113.6
1 #!/usr/local/bin/perl
2  
3 # Created on: 2006-05-05 22:44:23
4 # Create by: ivan
5 # $Id$
6 # # $Revision$, $HeadURL$, $Date$
7 # # $Revision$, $Source$, $Date$
8  
9 use strict;
10 use warnings;
11 use version;
12 use Scalar::Util;
13 use Getopt::Long;
14 use Pod::Usage;
15 use Config::Std;
16 use Data::Dumper qw/Dumper/;
17 use SVG::Calendar;
18 use Path::Tiny;
19 use FindBin qw/$Bin/;
20  
21 our $VERSION = version->new('0.3.14');
22  
23 my %option = (
24 moon => {},
25 date => {},
26 ical => {},
27 page => {},
28 image => {},
29 height => 0.5,
30 config => "$ENV{HOME}/.svgcal",
31 path => undef,
32 template => undef,
33 verbose => 0,
34 man => 0,
35 help => 0,
36 VERSION => 0,
37 );
38  
39 if ( !@ARGV ) {
40 pod2usage( -verbose => 1 );
41 }
42  
43 main();
44 exit 0;
45  
46 sub main {
47 Getopt::Long::Configure('bundling');
48 GetOptions(
49 \%option,
50 'moon|m=s%',
51 'date|d=s%',
52 'ical|c=s%',
53 'page|p=s%',
54 'image|i=s%',
55 'calendar_height|height|h=s',
56 'config|C=s',
57 'path|P=s',
58 'template|t=s',
59 'out|o=s',
60 'save|s',
61 'show-template',
62 'verbose|v!',
63 'man',
64 'help',
65 'VERSION'
66 ) or pod2usage(2);
67  
68 if ( $option{VERSION} ) {
69 print "svgcal.pl Version = $VERSION\n";
70 exit 1;
71 }
72 elsif ( $option{man} ) {
73 pod2usage( -verbose => 2 );
74 }
75 elsif ( $option{help} ) {
76 pod2usage( -verbose => 1 );
77 }
78 elsif ( !$option{date} ) {
79 warn "No dates specified, nothing to do!\n";
80 pod2usage( -verbose => 1 );
81 }
82  
83 # do stuff here
84 my $cal;
85  
86 if ( $option{save} && !-f $option{config} ) {
87 open my $fh, '>', $option{config} or warn "Cannot create the configuration file '$option{config}': $!"; ## no critic
88 if ($fh) {
89 print {$fh} "\n" or print {*STDERR} "Cannot print to file '$option{config}': $!\n";
90 close $fh or print {*STDERR} "Cannot close file '$option{config}': $!\n";
91 }
92 }
93  
94 if (@ARGV) {
95 for my $img (grep {/=/} @ARGV) {
96 my ($month, $src) = split /=/, $img, 2;
97 $option{image}{$month} = $src;
98 }
99 }
100  
101 if (
102 $option{image}{dir}
103 && -d $option{image}{dir}
104 && $option{date}
105 && $option{date}{year}
106 ) {
107 my $year = $option{date}{year};
108 for my $i ( 1 .. 12 ) {
109 my $month = $i < 10 ? "0$i" : $i;
110 $option{image}{"$year-$month"} = "$option{image}{dir}/$month.jpg";
111 }
112 }
113  
114 if ( $option{'show-template'} ) {
115 show_template();
116 }
117  
118 my %config = get_config();
119 $cal = SVG::Calendar->new(%config);
120  
121 if ( $option{verbose} ) {
122 $cal->{verbose} = $option{verbose};
123 }
124  
125 if ( $option{date}{month} ) {
126 $cal->output_month( $option{date}{month}, $option{out} || q/-/ );
127 }
128 else {
129 die "No page output file name base specified!\n" if !$option{out}; ## no critic
130  
131 if ( $option{date}{year} ) {
132 $cal->output_year( $option{date}{year}, $option{out} );
133 }
134 else {
135 $cal->output_year( $option{date}{start}, $option{date}{end}, $option{out} );
136 }
137 }
138  
139 return;
140 }
141  
142 sub show_template {
143  
144 require File::ShareDir;
145 my $dir = path( eval { File::ShareDir::dist_dir('SVG-Calendar') } || ( $Bin, '..', 'templates' ) );
146 print $dir->path('calendar.svg')->slurp;
147  
148 return exit 0;
149 }
150  
151 sub get_config {
152 if ( !-f $option{config} ) {
153 template_config($option{config});
154 }
155  
156 read_config $option{config} => my %config;
157  
158 OPTION:
159 for my $key ( keys %option ) {
160 next OPTION if !ref $option{$key};
161  
162 for my $subkey ( keys %{ $option{$key} } ) {
163  
164 # override the config file settings
165 $config{$key}{$subkey} = $option{$key}{$subkey};
166 }
167 }
168  
169 if ( $option{page} ) {
170 $config{page} = $option{page};
171 }
172 if ( $option{path} ) {
173 $config{path} = $option{path};
174 }
175 if ( $option{template} ) {
176 $config{template} = $option{template};
177 }
178 if ( $option{image} ) {
179 $config{image} = $option{image};
180 }
181  
182 if ( $option{save} && -f $option{config} ) {
183 write_config %config;
184 }
185  
186 return %config;
187 }
188  
189 sub template_config {
190 my ($file) = @_;
191  
192 open my $fh, '>', $file or return;
193  
194 print {$fh} <<"CONFIG";
195 # this section configures the information about the images
196 [image]
197  
198 # src
199 # This allows specifying one image file.
200 # src: image.jpg
201  
202 # YYYY-MM
203 # This is the format for specifying a specific month's image
204 #2012-01: car.gif
205 #2012-02: horse.tiff
206  
207 # dir
208 # This option allows the specifying of a directory to contain all the images
209 # for the months that calendars are going to be produced. The images should
210 # in the directory be specified as MM.[jpg|png] or month.[jpg|png] eg 01.jpg
211 # or January.png
212 #dir: .
213  
214 # The page section sets up the SVG document size
215 [page]
216  
217 # page
218 # This allows you to specify specific page sizes eg A4. Known page sizes
219 # include A0, A1, A2, A3, A4, A5, A6
220 #page: A4
221  
222 # height
223 # If the svg document size is not one of the predefined sizes you can use
224 # this setting to set the height of the document. If no units are specified
225 # the height is in pixels.
226 #height: 200mm
227  
228 # width
229 # Like height you set the SVG document size to an arbitart width
230 #width: 10cm
231  
232 # This section controlls the displaying of the moon on the produced calendars
233 [moon]
234  
235 # display
236 # Turn on displaying of the moon (requires Astro::Coord::ECI::Moon or
237 # Astro::MoonPhase to be installed)
238 #display: 1
239 display: 0
240  
241 # quarters
242 # Toggles weather to only display the moon for the various quarters or to
243 # show what phase the moon is in for every day.
244 #quarters: 1
245 quarters: 0
246  
247 # vpos
248 # Specifies weather the moon should be aligned with the top or bottom of the
249 # day's square.
250 #vpos: top
251  
252 # hpos
253 # Specifies weather the moon should be aligned with the left or right hand
254 # side of the day's square.
255 #hpos: right
256  
257 # xoffset, yoffset
258 # These parameters specify the x and y offsets of the moon, the units are
259 # relative units (ie page size independant) with the size of the dates square
260 # being approximatly 10x16
261 #xoffset: 10
262 #yoffset: 10
263  
264 # radius
265 # This specifies the size of the moon in the date's square
266 #radius: 1.0
267  
268 # image
269 # This option specifies an image file that will be used as the background for
270 # the moon.
271 #image: moon.jpg
272  
273 CONFIG
274  
275 close $fh;
276  
277 return;
278 }
279  
280 __DATA__