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 $self->{config} = LoadFile($self->{path}) ||
59 $log->logdie("can't open ",$self->{path}, ": $!");
61 $log->debug("config: ", dump( $self->{config} ));
63 $self ? return $self : return undef;
68 Return all databases in config
70 my $config_databases_hash = $config->databases;
71 my @config_databases_names = $config->databases;
77 return unless ($self->{config});
79 return keys %{ $self->{config}->{databases} };
81 return $self->{config}->{databases};
87 Which indexer are we using?
95 my $default = 'hyperestraier';
96 return unless ($self->{config});
97 return $self->{config}->{use_indexer} || $default;
102 $config->get('top-level_key');
108 my $what = shift || return;
109 return $self->{config}->{$what};
114 Return C<< config -> webpac >> parts
116 my @supported_inputs = $config->webpac('inputs');
117 my $inputs_hash = $config->webpac('inputs');
124 $self->_get_logger()->logdie("can't find config->webpac") unless defined( $self->{config}->{webpac} );
126 my $what = shift || return $self->{config}->{webpac};
128 if (wantarray && ref( $self->{config}->{webpac}->{$what} ) eq 'HASH') {
129 return keys %{ $self->{config}->{webpac}->{$what} };
131 return $self->{config}->{webpac}->{$what};
136 =head2 iterate_inputs
138 $config->iterate_inputs( sub {
139 my ($input, $database, $database_config_hash) = @_;
140 # ... do something with input config hash
143 This function will also modify C<< $input->{normalize} >> to
144 be C<ARRAY>, even with just one element.
151 my $log = $self->_get_logger();
153 my $code_ref = shift;
154 $log->logdie("called with CODE") unless ( ref($code_ref) eq 'CODE' );
156 while (my ($database, $db_config) = each %{ $self->{config}->{databases} }) {
158 if (ref($db_config->{input}) eq 'ARRAY') {
159 @inputs = @{ $db_config->{input} };
160 } elsif ($db_config->{input}) {
161 push @inputs, $db_config->{input};
163 $log->info("database $database doesn't have inputs defined");
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} ];
171 $code_ref->($input, $database, $db_config);
180 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
182 =head1 COPYRIGHT & LICENSE
184 Copyright 2006 Dobrica Pavlinusic, All Rights Reserved.
186 This program is free software; you can redistribute it and/or modify it
187 under the same terms as Perl itself.
191 1; # End of WebPAC::Config