/[webpac2]/trunk/lib/WebPAC/Config.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/lib/WebPAC/Config.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 967 - (hide annotations)
Fri Nov 2 13:11:35 2007 UTC (16 years, 5 months ago) by dpavlin
File size: 3680 byte(s)
 r1475@llin:  dpavlin | 2007-11-02 14:11:19 +0100
 fix ./run.pl --config conf/foobar.yml to actually work!

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 dpavlin 688 Version 0.02
18 dpavlin 681
19     =cut
20    
21 dpavlin 688 our $VERSION = '0.02';
22 dpavlin 681
23     =head1 SYNOPSIS
24    
25     FIXME
26    
27     =head1 FUNCTIONS
28    
29     =head2 new
30    
31     Create new configuration object.
32    
33 dpavlin 685 my $config = new WebPAC::Config(
34     path => '/optional/path/to/config.yml'
35     );
36    
37 dpavlin 681 =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 dpavlin 967 $log->logdie("can't open ", $self->{path}, ": $!") if ! -r $self->{path};
59    
60 dpavlin 681 $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 dpavlin 685 Return all databases in config
71    
72 dpavlin 681 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 dpavlin 685 Which indexer are we using?
90    
91 dpavlin 681 $config->use_indexer;
92    
93     =cut
94    
95     sub use_indexer {
96     my $self = shift;
97     return unless ($self->{config});
98 dpavlin 866 return $self->{config}->{use_indexer} || undef;
99 dpavlin 681 }
100    
101 dpavlin 682 =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 dpavlin 683 if (wantarray && ref( $self->{config}->{webpac}->{$what} ) eq 'HASH') {
130 dpavlin 682 return keys %{ $self->{config}->{webpac}->{$what} };
131     } else {
132     return $self->{config}->{webpac}->{$what};
133     }
134    
135     }
136    
137 dpavlin 685 =head2 iterate_inputs
138    
139     $config->iterate_inputs( sub {
140 dpavlin 688 my ($input, $database, $database_config_hash) = @_;
141 dpavlin 685 # ... do something with input config hash
142     } );
143    
144 dpavlin 700 This function will also modify C<< $input->{normalize} >> to
145     be C<ARRAY>, even with just one element.
146    
147 dpavlin 685 =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 dpavlin 700 if ( defined( $input->{normalize} ) && ref($input->{normalize}) ne 'ARRAY' ) {
170     $input->{normalize} = [ $input->{normalize} ];
171     }
172 dpavlin 688 $code_ref->($input, $database, $db_config);
173 dpavlin 685 }
174     }
175    
176     }
177    
178    
179 dpavlin 681 =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

  ViewVC Help
Powered by ViewVC 1.1.26