1 |
dpavlin |
681 |
package WebPAC::Config; |
2 |
|
|
|
3 |
|
|
use warnings; |
4 |
|
|
use strict; |
5 |
|
|
|
6 |
|
|
use base qw/WebPAC::Common/; |
7 |
|
|
|
8 |
|
|
use Data::Dump qw/dump/; |
9 |
|
|
use YAML qw/LoadFile/; |
10 |
|
|
|
11 |
|
|
=head1 NAME |
12 |
|
|
|
13 |
|
|
WebPAC::Config - handle WebPAC configuration file |
14 |
|
|
|
15 |
|
|
=head1 VERSION |
16 |
|
|
|
17 |
|
|
Version 0.01 |
18 |
|
|
|
19 |
|
|
=cut |
20 |
|
|
|
21 |
|
|
our $VERSION = '0.01'; |
22 |
|
|
|
23 |
|
|
=head1 SYNOPSIS |
24 |
|
|
|
25 |
|
|
FIXME |
26 |
|
|
|
27 |
|
|
=head1 FUNCTIONS |
28 |
|
|
|
29 |
|
|
=head2 new |
30 |
|
|
|
31 |
|
|
Create new configuration object. |
32 |
|
|
|
33 |
|
|
=cut |
34 |
|
|
|
35 |
|
|
sub new { |
36 |
|
|
my $class = shift; |
37 |
|
|
my $self = {@_}; |
38 |
|
|
bless($self, $class); |
39 |
|
|
|
40 |
|
|
my $log = $self->_get_logger(); |
41 |
|
|
|
42 |
|
|
if (! $self->{path}) { |
43 |
|
|
my $hostname = `hostname`; |
44 |
|
|
chomp($hostname); |
45 |
|
|
$hostname =~ s/\..+$//; |
46 |
|
|
if (-e "conf/$hostname.yml") { |
47 |
|
|
$self->{path} = "conf/$hostname.yml"; |
48 |
|
|
$log->info("using host configuration file: ", $self->{path}); |
49 |
|
|
} |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
$self->{path} ||= 'conf/config.yml'; |
53 |
|
|
|
54 |
|
|
$self->{config} = LoadFile($self->{path}) || |
55 |
|
|
$log->logdie("can't open ",$self->{path}, ": $!"); |
56 |
|
|
|
57 |
|
|
$log->debug("config: ", dump( $self->{config} )); |
58 |
|
|
|
59 |
|
|
$self ? return $self : return undef; |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
=head2 databases |
63 |
|
|
|
64 |
|
|
my $config_databases_hash = $config->databases; |
65 |
|
|
my @config_databases_names = $config->databases; |
66 |
|
|
|
67 |
|
|
=cut |
68 |
|
|
|
69 |
|
|
sub databases { |
70 |
|
|
my $self = shift; |
71 |
|
|
return unless ($self->{config}); |
72 |
|
|
if (wantarray) { |
73 |
|
|
return keys %{ $self->{config}->{databases} }; |
74 |
|
|
} else { |
75 |
|
|
return $self->{config}->{databases}; |
76 |
|
|
} |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
=head2 use_indexer |
80 |
|
|
|
81 |
|
|
$config->use_indexer; |
82 |
|
|
|
83 |
|
|
=cut |
84 |
|
|
|
85 |
|
|
sub use_indexer { |
86 |
|
|
my $self = shift; |
87 |
|
|
my $default = 'hyperestraier'; |
88 |
|
|
return unless ($self->{config}); |
89 |
|
|
return $self->{config}->{use_indexer} || $default; |
90 |
|
|
} |
91 |
|
|
|
92 |
dpavlin |
682 |
=head2 get |
93 |
|
|
|
94 |
|
|
$config->get('top-level_key'); |
95 |
|
|
|
96 |
|
|
=cut |
97 |
|
|
|
98 |
|
|
sub get { |
99 |
|
|
my $self = shift; |
100 |
|
|
my $what = shift || return; |
101 |
|
|
return $self->{config}->{$what}; |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
=head2 webpac |
105 |
|
|
|
106 |
|
|
Return C<< config -> webpac >> parts |
107 |
|
|
|
108 |
|
|
my @supported_inputs = $config->webpac('inputs'); |
109 |
|
|
my $inputs_hash = $config->webpac('inputs'); |
110 |
|
|
|
111 |
|
|
=cut |
112 |
|
|
|
113 |
|
|
sub webpac { |
114 |
|
|
my $self = shift; |
115 |
|
|
|
116 |
|
|
$self->_get_logger()->logdie("can't find config->webpac") unless defined( $self->{config}->{webpac} ); |
117 |
|
|
|
118 |
|
|
my $what = shift || return $self->{config}->{webpac}; |
119 |
|
|
|
120 |
dpavlin |
683 |
if (wantarray && ref( $self->{config}->{webpac}->{$what} ) eq 'HASH') { |
121 |
dpavlin |
682 |
return keys %{ $self->{config}->{webpac}->{$what} }; |
122 |
|
|
} else { |
123 |
|
|
return $self->{config}->{webpac}->{$what}; |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
} |
127 |
|
|
|
128 |
dpavlin |
681 |
=head1 AUTHOR |
129 |
|
|
|
130 |
|
|
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
131 |
|
|
|
132 |
|
|
=head1 COPYRIGHT & LICENSE |
133 |
|
|
|
134 |
|
|
Copyright 2006 Dobrica Pavlinusic, All Rights Reserved. |
135 |
|
|
|
136 |
|
|
This program is free software; you can redistribute it and/or modify it |
137 |
|
|
under the same terms as Perl itself. |
138 |
|
|
|
139 |
|
|
=cut |
140 |
|
|
|
141 |
|
|
1; # End of WebPAC::Config |