1 | # $Id$ |
---|
2 | |
---|
3 | package ObsData; |
---|
4 | |
---|
5 | use strict; |
---|
6 | use warnings; |
---|
7 | use Config::IniFiles; |
---|
8 | use POSIX qw(strftime); |
---|
9 | use ObsData::Repository; |
---|
10 | |
---|
11 | my @loglevel = ( |
---|
12 | 'DEBUG', |
---|
13 | 'INFO', |
---|
14 | 'RESULT', |
---|
15 | 'WARNING', |
---|
16 | 'ERROR', |
---|
17 | 'FATAL', |
---|
18 | ); |
---|
19 | |
---|
20 | our $VERSION = 0.01; |
---|
21 | our $CVSID = q$Id$; |
---|
22 | our $CVSREV = (q$Revision$ =~ /^Revision: (.*) $/)[0]; |
---|
23 | |
---|
24 | =head1 NAME |
---|
25 | |
---|
26 | ObsData - Main object to manage data files |
---|
27 | |
---|
28 | =head1 SYNOPSIS |
---|
29 | |
---|
30 | use ObsData; |
---|
31 | my $conf = "configfile"; |
---|
32 | my $obsdata = ObsData->new($conf); |
---|
33 | |
---|
34 | =head1 METHODS |
---|
35 | |
---|
36 | =head2 new($configfile) |
---|
37 | |
---|
38 | Create a new Obsdata object from $configfile |
---|
39 | |
---|
40 | =cut |
---|
41 | |
---|
42 | sub new { |
---|
43 | my ($class, $configfile, %options) = @_; |
---|
44 | my $obsdata = { |
---|
45 | config => new Config::IniFiles( |
---|
46 | -file => $configfile, |
---|
47 | -default => 'global', |
---|
48 | -allowcontinue => 1 |
---|
49 | ), |
---|
50 | verbose => defined($options{verbose}) ? $options{verbose} : 1, |
---|
51 | logcallback => $options{logcallback}, |
---|
52 | logfile => $options{logfile} || "obsdata.log", |
---|
53 | }; |
---|
54 | |
---|
55 | if ($configfile) { |
---|
56 | (-f $configfile && -r _) or return undef; |
---|
57 | } |
---|
58 | |
---|
59 | $obsdata->{config} or return undef; |
---|
60 | |
---|
61 | bless($obsdata, $class); |
---|
62 | } |
---|
63 | |
---|
64 | sub DESTROY { |
---|
65 | my ($self) = @_; |
---|
66 | |
---|
67 | if ($self->{loghandle}) { |
---|
68 | close($self->{loghandle}); |
---|
69 | $self->{loghandle} = undef; |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | sub load { |
---|
74 | my ($self) = @_; |
---|
75 | |
---|
76 | if (!open($self->{loghandle}, ">> $self->{logfile}")) { |
---|
77 | $self->{loghandle} = undef; |
---|
78 | $self->loging(5, "Can't open log file %s, exiting", "$self->{logfile}"); |
---|
79 | return 0; |
---|
80 | } |
---|
81 | |
---|
82 | return 1; |
---|
83 | } |
---|
84 | |
---|
85 | sub loging { |
---|
86 | my ($self, $level, $fmt, @val) = @_; |
---|
87 | my $msg = sprintf($fmt, @val); |
---|
88 | my $logh = $self->{loghandle}; |
---|
89 | if ($self->{logcallback}) { |
---|
90 | $self->{logcallback}->($level, $msg); |
---|
91 | } |
---|
92 | if($level >= 0 && $level >= $self->{verbose}) { |
---|
93 | if ($logh) { |
---|
94 | printf $logh |
---|
95 | "%-9s %s %s\n", |
---|
96 | sprintf("[%s]", $self->loglevel($level)), |
---|
97 | strftime("%b %d %H:%M:%S %Y", gmtime), |
---|
98 | $msg; |
---|
99 | } |
---|
100 | } |
---|
101 | $msg |
---|
102 | } |
---|
103 | |
---|
104 | sub loglevel { |
---|
105 | my $l = pop(@_); |
---|
106 | defined($l) or $l = pop(@_); |
---|
107 | return $loglevel[ $l ] || "?????"; |
---|
108 | } |
---|
109 | |
---|
110 | =head2 checkconfig() |
---|
111 | |
---|
112 | Check the validity of information contains in configfile. |
---|
113 | |
---|
114 | Notice: This test does not include the syntaxe validity |
---|
115 | |
---|
116 | =cut |
---|
117 | |
---|
118 | sub checkconfig { |
---|
119 | my ($self) = @_; |
---|
120 | foreach my $g ($self->{config}->GroupMembers('Obs')) { |
---|
121 | my ($obs) = $g =~ /\S+\s+(.*)/; |
---|
122 | if (!$self->{config}->SectionExists($obs)) { |
---|
123 | print STDERR "E: '$obs' is listed as Obs but it does not exists\n"; |
---|
124 | next; |
---|
125 | } |
---|
126 | foreach my $param ($self->{config}->Parameters($obs)) { |
---|
127 | } |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | =head2 getvalue($section, $var, $default) |
---|
132 | |
---|
133 | Return a value from the configuration, $default is assumed if the value is not set |
---|
134 | |
---|
135 | =cut |
---|
136 | |
---|
137 | sub getvalue { |
---|
138 | my ($self, $section, $var, $default) = @_; |
---|
139 | $self->{config}->val($section, $var, $default); |
---|
140 | } |
---|
141 | |
---|
142 | sub config_mtime { |
---|
143 | my ($self) = @_; |
---|
144 | return $self->{configmtime} ||= (stat($self->{config}->GetFileName))[9]; |
---|
145 | } |
---|
146 | |
---|
147 | =head2 list_obs |
---|
148 | |
---|
149 | Return the list of observatories defined in configuration |
---|
150 | |
---|
151 | =cut |
---|
152 | |
---|
153 | sub list_obs { |
---|
154 | my ($self) = @_; |
---|
155 | grep { $self->{config}->SectionExists($_) } |
---|
156 | map { s/^\S+\s+//; $_ } |
---|
157 | $self->{config}->GroupMembers('Obs'); |
---|
158 | } |
---|
159 | |
---|
160 | =head2 is_obs($obsname) |
---|
161 | |
---|
162 | Return True if $obsname is an observatory |
---|
163 | |
---|
164 | =cut |
---|
165 | |
---|
166 | sub is_obs { |
---|
167 | my ($self, $obs) = @_; |
---|
168 | scalar(grep { $_ eq "Obs $obs" } $self->{config}->GroupMembers('Obs')); |
---|
169 | } |
---|
170 | |
---|
171 | =head2 list_obsdatadir($obsname) |
---|
172 | |
---|
173 | Return a hash of data directories per data type for $obsname observatories |
---|
174 | |
---|
175 | =cut |
---|
176 | |
---|
177 | sub list_obsdatadir { |
---|
178 | my ($self, $obs) = @_; |
---|
179 | $self->is_obs($obs) or return undef; |
---|
180 | map { m,^datadir/(.*),; ( ($1 || "") => $self->{config}->val($obs, $_) ) } |
---|
181 | grep { m,^datadir/, || $_ eq 'datadir' } |
---|
182 | $self->{config}->Parameters($obs) |
---|
183 | } |
---|
184 | |
---|
185 | =head2 list_typedatadir($type) |
---|
186 | |
---|
187 | List all directories for $type data |
---|
188 | |
---|
189 | =cut |
---|
190 | |
---|
191 | sub list_typedatadir { |
---|
192 | my ($self, $type) = @_; |
---|
193 | my %dirs; |
---|
194 | foreach my $obs ($self->list_obs) { |
---|
195 | $dirs{$_} = 1 foreach(grep { $_ } $self->get_datadir($obs, $type)); |
---|
196 | } |
---|
197 | keys %dirs; |
---|
198 | |
---|
199 | } |
---|
200 | |
---|
201 | =head2 get_datadir($obs, $type) |
---|
202 | |
---|
203 | Return a list of directories for $type data on $obs observatory |
---|
204 | |
---|
205 | =cut |
---|
206 | |
---|
207 | sub get_datadir { |
---|
208 | my ($self, $obs, $type) = @_; |
---|
209 | $self->is_obs($obs) or return undef; |
---|
210 | grep { defined($_) } ($self->getvalue($obs, "datadir/$type"), $self->getvalue($obs, "datadir")); |
---|
211 | } |
---|
212 | |
---|
213 | =head2 list_datatype |
---|
214 | |
---|
215 | Return a list of datatype handle by config |
---|
216 | |
---|
217 | =cut |
---|
218 | |
---|
219 | sub list_datatype { |
---|
220 | my ($self) = @_; |
---|
221 | grep { $_ ne 'global' } |
---|
222 | grep { $_ !~ /^Obs\s+/ } |
---|
223 | grep { !$self->is_obs($_) } $self->{config}->Sections; |
---|
224 | } |
---|
225 | |
---|
226 | sub match_data_type { |
---|
227 | my ($self, $datatype, $label) = @_; |
---|
228 | my $regexp = $self->getvalue($datatype, 'match') or return; |
---|
229 | my @data = $label =~ /$regexp/; |
---|
230 | if (! @data) { |
---|
231 | return; |
---|
232 | } |
---|
233 | return @data; |
---|
234 | } |
---|
235 | |
---|
236 | sub find_match_data_type { |
---|
237 | my ($self, $label) = @_; |
---|
238 | foreach my $datatype ($self->list_datatype) { |
---|
239 | my @data = $self->match_data_type($datatype, $label); |
---|
240 | if (@data) { |
---|
241 | return($datatype, @data); |
---|
242 | } |
---|
243 | } |
---|
244 | } |
---|
245 | |
---|
246 | sub build_dest_filename { |
---|
247 | my ($self, $label, $datatype) = @_; |
---|
248 | my @data; |
---|
249 | if ($datatype) { |
---|
250 | @data = $self->match_data_type($datatype, $label); |
---|
251 | } else { |
---|
252 | ($datatype, @data) = $self->find_match_data_type($label); |
---|
253 | } |
---|
254 | |
---|
255 | if ($datatype && @data) { |
---|
256 | my $dest = $self->getvalue($datatype, 'dest'); |
---|
257 | my @matcharg = split(/\s+/, $self->getvalue($datatype, 'matcharg')); |
---|
258 | my @destarg = split(/\s+/, $self->getvalue($datatype, 'destarg')); |
---|
259 | my @gmtime = gmtime; |
---|
260 | my %val; |
---|
261 | foreach (@matcharg) { |
---|
262 | $val{$_} = shift(@data); |
---|
263 | } |
---|
264 | return ($datatype, sprintf( |
---|
265 | $dest, |
---|
266 | map { m/^%/ ? strftime($_, @gmtime) : $val{$_} } @destarg, |
---|
267 | )); |
---|
268 | } |
---|
269 | undef; |
---|
270 | } |
---|
271 | |
---|
272 | sub process_obs { |
---|
273 | my ($self, $obs) = @_; |
---|
274 | my %datadir = $self->list_obsdatadir($obs); |
---|
275 | $self->loging(0, "Starting %s() for %s", (caller(0))[3], $obs); |
---|
276 | |
---|
277 | foreach my $datatype (keys %datadir) { |
---|
278 | my $or = $self->get_obs_data_handle($obs, $datatype); |
---|
279 | $or or next; |
---|
280 | $or->process; |
---|
281 | } |
---|
282 | } |
---|
283 | |
---|
284 | sub get_obs_data_handle { |
---|
285 | my ($self, $obs, $datatype) = @_; |
---|
286 | |
---|
287 | my $dir = $self->getvalue($obs, ($datatype ? "datadir/$datatype" : "datadir")); |
---|
288 | if (!$dir) { |
---|
289 | $self->loging(4, |
---|
290 | "Can't find data directory for %s, type: %s", |
---|
291 | $obs, $datatype || '(none)' |
---|
292 | ); |
---|
293 | return undef; |
---|
294 | } |
---|
295 | my $or = ObsData::Repository::dir->new( |
---|
296 | { |
---|
297 | obsdata => $self, |
---|
298 | dir => $dir, |
---|
299 | obs => $obs, |
---|
300 | datatype => $datatype, |
---|
301 | statusfile => "$dir/obsdata.ini" |
---|
302 | } |
---|
303 | ); |
---|
304 | if (!defined($or)) { |
---|
305 | $self->loging(4, "Can't parse %s, check directory exists", $dir); |
---|
306 | return undef; |
---|
307 | } |
---|
308 | |
---|
309 | return($or); |
---|
310 | } |
---|
311 | |
---|
312 | 1; |
---|