/[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 866 - (hide annotations)
Thu Jun 21 12:53:41 2007 UTC (16 years, 10 months ago) by dpavlin
File size: 3604 byte(s)
 r1285@llin:  dpavlin | 2007-06-21 14:53:43 +0200
 make indexers optional [2.30]

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

  ViewVC Help
Powered by ViewVC 1.1.26