File Coverage

blib/lib/Luka/Conf.pm
Criterion Covered Total %
statement 24 61 39.3
branch 0 18 0.0
condition 0 9 0.0
subroutine 8 11 72.7
pod 0 2 0.0
total 32 101 31.6


line stmt bran cond sub pod time code
1             # $Id: Conf.pm,v 1.5 2006/02/27 21:43:59 toni Exp $
2             package Luka::Conf;
3 2     2   11 use strict;
  2         3  
  2         66  
4 2     2   11 use Carp;
  2         3  
  2         126  
5 2     2   10 use Sys::Syslog;
  2         3  
  2         132  
6 2     2   9 use warnings;
  2         3  
  2         50  
7 2     2   2436 use Config::IniFiles;
  2         67492  
  2         86  
8 2     2   25 use Luka::Exceptions;
  2         4  
  2         46  
9 2     2   11 use Data::Dumper;
  2         6  
  2         115  
10 2     2   9 use vars qw($DEBUG);
  2         5  
  2         1597  
11             $DEBUG=undef;
12              
13             =head1 NAME
14              
15             Luka::Conf - Configuration file interface class.
16              
17             =head1 SYNOPSIS
18              
19             $conf = Luka::Conf->new( conf => $conf, syslogd => $syslogd );
20             my $ip = $conf->get_conf('global','expected_ip');
21              
22             =cut
23              
24             sub get_conf {
25 0     0 0   my $self = shift @_;
26 0           return $self->_parse_config_ini(@_);
27             }
28              
29             sub _parse_config_ini {
30 0     0     my ($self,$section,$param) = @_;
31              
32 0 0 0       if (not $self->{conf_obj}->SectionExists($section) ) {
    0          
33 0 0         if ($self->{syslogd}) {
34 0           openlog(__PACKAGE__, 'pid,nowait','daemon');
35 0           syslog('warning', "Luka system not functional for script '%s'. " .
36             "Couldn't read its section in config file '%s'" , $section , $self->{conf_file});
37 0           closelog;
38             }
39 0           throw Luka::Exception::Program(error => "Luka system not functional for '$section' script. " .
40             "Couldn't read its section '$section' in config file '" . $self->{conf_file} . "'.\n");
41              
42             } elsif (defined $DEBUG and defined($self->{syslogd})) {
43 0           my $call_file = (caller(1))[1];
44 0           my $call_line = (caller(1))[2];
45 0           openlog(__PACKAGE__, 'pid,nowait','daemon');
46 0           syslog('debug', "Called from file %s, line %s",
47             $call_file, $call_line );
48 0           syslog('debug', "Reading config file '%s', " .
49             "section '%s', param '%s'.", $self->{conf_file}, $section, $param);
50 0           closelog;
51             }
52              
53 0           return $self->{conf_obj}->val($section,$param);
54             };
55              
56             sub new {
57 0     0 0   my $class = shift;
58 0   0       my $type = ref($class) || $class;
59 0           my $self;
60 0 0         if (defined(@_)) {
61 0 0 0       my $arg =
62             defined $_[0] && UNIVERSAL::isa($_[0], 'HASH')
63             ? shift
64             : { @_ };
65 0           $self = bless { arg => $arg }, $type;
66             } else {
67 0           $self = bless {}, $type;
68             }
69            
70 0 0         $self->{conf_file} = defined($self->{arg}->{conf}) ? $self->{arg}->{conf} : "luka.conf";
71 0 0         $self->{syslogd} = defined($self->{arg}->{syslogd}) ? $self->{arg}->{syslogd} : undef;
72 0           my ($cfg,$val);
73              
74 0           eval { $cfg = Config::IniFiles->new( -file => $self->{conf_file} ) };
  0            
75 0 0         if ($@) {
76 0           my $error = $@;
77 0 0         if ($self->{syslogd}) {
78 0           openlog(__PACKAGE__, 'pid,nowait','daemon');
79 0           syslog('warning', "Luka system disabled. Couldn't read its config file '%s': %s", $self->{conf_file}, $error->message);
80 0           closelog;
81             };
82 0           throw Luka::Exception::Program
83             (error => "Luka system disabled. Couldn't read its config file '" .
84             $self->{conf_file} . "': " . $error->message);
85             } else {
86 0           $self->{conf_obj} = $cfg;
87             }
88            
89 0           return $self;
90             }
91              
92             1;
93              
94             =head1 SEE ALSO
95              
96             L, L
97              
98             =head1 AUTHOR
99              
100             Toni Prug
101              
102             =head1 COPYRIGHT
103              
104             Copyright (c) 2006. Toni Prug. All rights reserved.
105              
106             This program is free software; you can redistribute it and/or modify
107             it under the terms of the GNU General Public License as published by
108             the Free Software Foundation; either version 2 of the License, or (at
109             your option) any later version.
110              
111             This program is distributed in the hope that it will be useful, but
112             WITHOUT ANY WARRANTY; without even the implied warranty of
113             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
114             General Public License for more details.
115              
116             You should have received a copy of the GNU General Public License
117             along with this program; if not, write to the Free Software
118             Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
119             USA
120              
121             See L
122              
123             =cut