r993@llin: dpavlin | 2006-09-25 13:52:43 +0200
[webpac2] / lib / WebPAC / Config.pm
1 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.02
18
19 =cut
20
21 our $VERSION = '0.02';
22
23 =head1 SYNOPSIS
24
25 FIXME
26
27 =head1 FUNCTIONS
28
29 =head2 new
30
31 Create new configuration object.
32
33   my $config = new WebPAC::Config(
34         path => '/optional/path/to/config.yml'
35   );
36
37 =cut
38
39 sub new {
40         my $class = shift;
41         my $self = {@_};
42         bless($self, $class);
43
44         my $log = $self->_get_logger();
45
46         if (! $self->{path}) {
47                 my $hostname = `hostname`;
48                 chomp($hostname);
49                 $hostname =~ s/\..+$//;
50                 if (-e "conf/$hostname.yml") {
51                         $self->{path} = "conf/$hostname.yml";
52                         $log->info("using host configuration file: ", $self->{path});
53                 }
54         }
55
56         $self->{path} ||= 'conf/config.yml';
57
58         $self->{config} = LoadFile($self->{path}) ||
59                 $log->logdie("can't open ",$self->{path}, ": $!");
60
61         $log->debug("config: ", dump( $self->{config} ));
62
63         $self ? return $self : return undef;
64 }
65
66 =head2 databases
67
68 Return all databases in config
69
70   my $config_databases_hash = $config->databases;
71   my @config_databases_names = $config->databases;
72
73 =cut
74
75 sub databases {
76         my $self = shift;
77         return unless ($self->{config});
78         if (wantarray) {
79                 return keys %{ $self->{config}->{databases} };
80         } else {
81                 return $self->{config}->{databases};
82         }
83 }
84
85 =head2 use_indexer
86
87 Which indexer are we using?
88
89   $config->use_indexer;
90
91 =cut
92
93 sub use_indexer {
94         my $self = shift;
95         my $default = 'hyperestraier';
96         return unless ($self->{config});
97         return $self->{config}->{use_indexer} || $default;
98 }
99
100 =head2 get
101
102   $config->get('top-level_key');
103
104 =cut
105
106 sub get {
107         my $self = shift;
108         my $what = shift || return;
109         return $self->{config}->{$what};
110 }
111
112 =head2 webpac
113
114 Return C<< config -> webpac >> parts
115
116   my @supported_inputs = $config->webpac('inputs');
117   my $inputs_hash = $config->webpac('inputs');
118
119 =cut
120
121 sub webpac {
122         my $self = shift;
123
124         $self->_get_logger()->logdie("can't find config->webpac") unless defined( $self->{config}->{webpac} );
125
126         my $what = shift || return $self->{config}->{webpac};
127
128         if (wantarray && ref( $self->{config}->{webpac}->{$what} ) eq 'HASH') {
129                 return keys %{ $self->{config}->{webpac}->{$what} };
130         } else {
131                 return $self->{config}->{webpac}->{$what};
132         }
133
134 }
135
136 =head2 iterate_inputs
137
138   $config->iterate_inputs( sub {
139         my ($input, $database, $database_config_hash) = @_;
140         # ... do something with input config hash
141   } );
142
143 This function will also modify C<< $input->{normalize} >> to
144 be C<ARRAY>, even with just one element.
145
146 =cut
147
148 sub iterate_inputs {
149         my $self = shift;
150
151         my $log = $self->_get_logger();
152
153         my $code_ref = shift;
154         $log->logdie("called with CODE") unless ( ref($code_ref) eq 'CODE' );
155
156         while (my ($database, $db_config) = each %{ $self->{config}->{databases} }) {
157                 my @inputs;
158                 if (ref($db_config->{input}) eq 'ARRAY') {
159                         @inputs = @{ $db_config->{input} };
160                 } elsif ($db_config->{input}) {
161                         push @inputs, $db_config->{input};
162                 } else {
163                         $log->info("database $database doesn't have inputs defined");
164                 }
165
166                 foreach my $input (@inputs) {
167                         $log->debug("iterating over input ", dump($input));
168                         if ( defined( $input->{normalize} ) && ref($input->{normalize}) ne 'ARRAY' ) {
169                                 $input->{normalize} = [ $input->{normalize} ];
170                         }
171                         $code_ref->($input, $database, $db_config);
172                 }
173         }
174
175 }
176
177
178 =head1 AUTHOR
179
180 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
181
182 =head1 COPYRIGHT & LICENSE
183
184 Copyright 2006 Dobrica Pavlinusic, All Rights Reserved.
185
186 This program is free software; you can redistribute it and/or modify it
187 under the same terms as Perl itself.
188
189 =cut
190
191 1; # End of WebPAC::Config