Make cleanup of encodings, moving webpac closer to having
[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         $log->logdie("can't open ", $self->{path}, ": $!") if ! -r $self->{path};
59
60         $self->{config} = LoadFile($self->{path}) ||
61                 $log->logdie("can't open ",$self->{path}, ": $!");
62
63         $log->debug("config: ", dump( $self->{config} ));
64
65         $self ? return $self : return undef;
66 }
67
68 =head2 databases
69
70 Return all databases in config
71
72   my $config_databases_hash = $config->databases;
73   my @config_databases_names = $config->databases;
74
75 =cut
76
77 sub databases {
78         my $self = shift;
79         return unless ($self->{config});
80         if (wantarray) {
81                 return keys %{ $self->{config}->{databases} };
82         } else {
83                 return $self->{config}->{databases};
84         }
85 }
86
87 =head2 use_indexer
88
89 Which indexer are we using?
90
91   $config->use_indexer;
92
93 =cut
94
95 sub use_indexer {
96         my $self = shift;
97         return unless ($self->{config});
98         return $self->{config}->{use_indexer} || undef;
99 }
100
101 =head2 get
102
103   $config->get('top-level_key');
104
105 =cut
106
107 sub get {
108         my $self = shift;
109         my $what = shift || return;
110         return $self->{config}->{$what};
111 }
112
113 =head2 webpac
114
115 Return C<< config -> webpac >> parts
116
117   my @supported_inputs = $config->webpac('inputs');
118   my $inputs_hash = $config->webpac('inputs');
119
120 =cut
121
122 sub webpac {
123         my $self = shift;
124
125         $self->_get_logger()->logdie("can't find config->webpac") unless defined( $self->{config}->{webpac} );
126
127         my $what = shift || return $self->{config}->{webpac};
128
129         if (wantarray && ref( $self->{config}->{webpac}->{$what} ) eq 'HASH') {
130                 return keys %{ $self->{config}->{webpac}->{$what} };
131         } else {
132                 return $self->{config}->{webpac}->{$what};
133         }
134
135 }
136
137 =head2 iterate_inputs
138
139   $config->iterate_inputs( sub {
140         my ($input, $database, $database_config_hash) = @_;
141         # ... do something with input config hash
142   } );
143
144 This function will also modify C<< $input->{normalize} >> to
145 be C<ARRAY>, even with just one element.
146
147 =cut
148
149 sub iterate_inputs {
150         my $self = shift;
151
152         my $log = $self->_get_logger();
153
154         my $code_ref = shift;
155         $log->logdie("called with CODE") unless ( ref($code_ref) eq 'CODE' );
156
157         while (my ($database, $db_config) = each %{ $self->{config}->{databases} }) {
158                 my @inputs;
159                 if (ref($db_config->{input}) eq 'ARRAY') {
160                         @inputs = @{ $db_config->{input} };
161                 } elsif ($db_config->{input}) {
162                         push @inputs, $db_config->{input};
163                 } else {
164                         $log->info("database $database doesn't have inputs defined");
165                 }
166
167                 foreach my $input (@inputs) {
168                         $log->debug("iterating over input ", dump($input));
169                         if ( defined( $input->{normalize} ) && ref($input->{normalize}) ne 'ARRAY' ) {
170                                 $input->{normalize} = [ $input->{normalize} ];
171                         }
172                         $code_ref->($input, $database, $db_config);
173                 }
174         }
175
176 }
177
178
179 =head1 AUTHOR
180
181 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
182
183 =head1 COPYRIGHT & LICENSE
184
185 Copyright 2006 Dobrica Pavlinusic, All Rights Reserved.
186
187 This program is free software; you can redistribute it and/or modify it
188 under the same terms as Perl itself.
189
190 =cut
191
192 1; # End of WebPAC::Config