1 package WebPAC::Config;
6 use base qw/WebPAC::Common/;
8 use Data::Dump qw/dump/;
13 WebPAC::Config - handle WebPAC configuration file
21 our $VERSION = '0.02';
31 Create new configuration object.
33 my $config = new WebPAC::Config(
34 path => '/optional/path/to/config.yml'
44 my $log = $self->_get_logger();
46 if (! $self->{path}) {
47 my $hostname = `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});
56 $self->{path} ||= 'conf/config.yml';
58 $log->logdie("can't open ", $self->{path}, ": $!") if ! -r $self->{path};
60 $self->{config} = LoadFile($self->{path}) ||
61 $log->logdie("can't open ",$self->{path}, ": $!");
63 $log->debug("config: ", dump( $self->{config} ));
65 $self ? return $self : return undef;
70 Return all databases in config
72 my $config_databases_hash = $config->databases;
73 my @config_databases_names = $config->databases;
79 return unless ($self->{config});
81 return keys %{ $self->{config}->{databases} };
83 return $self->{config}->{databases};
89 Which indexer are we using?
97 return unless ($self->{config});
98 return $self->{config}->{use_indexer} || undef;
103 $config->get('top-level_key');
109 my $what = shift || return;
110 return $self->{config}->{$what};
115 Return C<< config -> webpac >> parts
117 my @supported_inputs = $config->webpac('inputs');
118 my $inputs_hash = $config->webpac('inputs');
125 $self->_get_logger()->logdie("can't find config->webpac") unless defined( $self->{config}->{webpac} );
127 my $what = shift || return $self->{config}->{webpac};
129 if (wantarray && ref( $self->{config}->{webpac}->{$what} ) eq 'HASH') {
130 return keys %{ $self->{config}->{webpac}->{$what} };
132 return $self->{config}->{webpac}->{$what};
137 =head2 iterate_inputs
139 $config->iterate_inputs( sub {
140 my ($input, $database, $database_config_hash) = @_;
141 # ... do something with input config hash
144 This function will also modify C<< $input->{normalize} >> to
145 be C<ARRAY>, even with just one element.
152 my $log = $self->_get_logger();
154 my $code_ref = shift;
155 $log->logdie("called with CODE") unless ( ref($code_ref) eq 'CODE' );
157 while (my ($database, $db_config) = each %{ $self->{config}->{databases} }) {
159 if (ref($db_config->{input}) eq 'ARRAY') {
160 @inputs = @{ $db_config->{input} };
161 } elsif ($db_config->{input}) {
162 push @inputs, $db_config->{input};
164 $log->info("database $database doesn't have inputs defined");
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} ];
172 $code_ref->($input, $database, $db_config);
181 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
183 =head1 COPYRIGHT & LICENSE
185 Copyright 2006 Dobrica Pavlinusic, All Rights Reserved.
187 This program is free software; you can redistribute it and/or modify it
188 under the same terms as Perl itself.
192 1; # End of WebPAC::Config