| 1 | ########################################################################### | ||
| 2 | # | ||
| 3 | # $Id: Config.pm,v 1.7 2006/09/17 19:23:40 pkaluski Exp $ | ||
| 4 | # $Name: Stable_0_16 $ | ||
| 5 | # | ||
| 6 | # Module supporting handling of parameters needed by LReport tools to run. | ||
| 7 | # | ||
| 8 | # $Log: Config.pm,v $ | ||
| 9 | # Revision 1.7 2006/09/17 19:23:40 pkaluski | ||
| 10 | # Added new settings for chunk size, global keys file, diff report separator | ||
| 11 | # | ||
| 12 | # Revision 1.6 2006/02/10 22:32:16 pkaluski | ||
| 13 | # Major redesign in progress. Updated POD. Works. | ||
| 14 | # | ||
| 15 | # Revision 1.5 2006/01/07 22:56:17 pkaluski | ||
| 16 | # Redesigned simple reporting works. Diff reports still to be implemented. | ||
| 17 | # | ||
| 18 | # Revision 1.4 2006/01/01 13:13:49 pkaluski | ||
| 19 | # Initial revision. Works. | ||
| 20 | # | ||
| 21 | # | ||
| 22 | ########################################################################### | ||
| 23 | package LRpt::Config; | ||
| 24 | use strict; | ||
| 25 | |||
| 26 | # | ||
| 27 | # Reference to a hash in which all current values of parameters are stored. | ||
| 28 | # | ||
| 29 | our $settings = ""; | ||
| 30 | |||
| 31 | # | ||
| 32 | # A hash storing information about each known parameter. These informations | ||
| 33 | # are: | ||
| 34 | # ENV - Name of environmental variable in which a parametr's value is | ||
| 35 | # stored. | ||
| 36 | # default - default value of a parameter | ||
| 37 | # | ||
| 38 | our %defaults = | ||
| 39 | ( 'ext' => { 'ENV' => 'LRPT_CSV_FILE_EXT', | ||
| 40 | 'default' => 'txt' }, | ||
| 41 | 'path' => { 'ENV' => 'LRPT_CSV_FILE_PATH', | ||
| 42 | 'default' => '.' }, | ||
| 43 | 'sep' => { 'ENV' => 'LRPT_CSV_FIELD_SEPARATOR', | ||
| 44 | 'default' => "\t" }, | ||
| 45 | 'conn_file' => | ||
| 46 | { 'ENV' => 'LRPT_CONNECTION_FILE', | ||
| 47 | 'default' => 'conn_file.txt' }, | ||
| 48 | 'chunk_size' => | ||
| 49 | { 'ENV' => 'LRPT_CHUNK_SIZE', | ||
| 50 | 'default' => 1000 }, | ||
| 51 | 'global_keys_file' => | ||
| 52 | { 'ENV' => 'LRPT_GLOBAL_KEYS_FILE', | ||
| 53 | 'default' => 'keys.txt' }, | ||
| 54 | 'dv_sep' => | ||
| 55 | { 'ENV' => 'LRPT_DIFF_VALUE_SEPARATOR', | ||
| 56 | 'default' => '--#>' } | ||
| 57 | ); | ||
| 58 | |||
| 59 | =head1 NAME | ||
| 60 | |||
| 61 | LRpt::Config - A module for managing B<LReport> defaults and runtime | ||
| 62 | parameters | ||
| 63 | |||
| 64 | =head1 DESCRIPTION | ||
| 65 | |||
| 66 | This class is a part of L<C<LRpt>|LRpt> library. | ||
| 67 | It provides a consistent interface to all run time parameters, which | ||
| 68 | are needed by B<LReport> tools | ||
| 69 | |||
| 70 | =head1 METHODS | ||
| 71 | |||
| 72 | In this sections you will find a more or less complete listing of all | ||
| 73 | methods provided by the package. Note that the package itself is not | ||
| 74 | public so none of those methods are guaranteed to be maintained in future | ||
| 75 | (including the package itself). | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | ########################################################################## | ||
| 80 | |||
| 81 | =head2 C<new> | ||
| 82 | |||
| 83 | my $config = LRpt::Config->new( %params ); | ||
| 84 | |||
| 85 | Constructor. If Config has been already initialised, does nothing. | ||
| 86 | Otherwise initilizes internal structures | ||
| 87 | |||
| 88 | =cut | ||
| 89 | |||
| 90 | ########################################################################## | ||
| 91 | sub new | ||
| 92 | { | ||
| 93 | my $proto = shift; | ||
| 94 | my @params = @_; | ||
| 95 | |||
| 96 | my $class = ref( $proto ) || $proto; | ||
| 97 | |||
| 98 | my $self = {}; | ||
| 99 | bless( $self, $class ); | ||
| 100 | if( !$settings ){ | ||
| 101 | $self->initialize( @_ ); | ||
| 102 | } | ||
| 103 | return $self; | ||
| 104 | } | ||
| 105 | |||
| 106 | ########################################################################## | ||
| 107 | |||
| 108 | =head2 C<initialize> | ||
| 109 | |||
| 110 | $config->initialize( %params ); | ||
| 111 | |||
| 112 | Decides what actual values for each parameter. Parameters values given | ||
| 113 | in function paramers have priority. | ||
| 114 | |||
| 115 | =cut | ||
| 116 | |||
| 117 | ########################################################################## | ||
| 118 | sub initialize | ||
| 119 | { | ||
| 120 | my $self = shift; | ||
| 121 | my %params = @_; | ||
| 122 | |||
| 123 | my $settings = {}; | ||
| 124 | |||
| 125 | foreach my $key ( keys %defaults ){ | ||
| 126 | if( defined $params{ $key } and $params{ $key } ne "" ){ | ||
| 127 | $settings->{ $key } = $params{ $key }; | ||
| 128 | }elsif( exists $ENV{ | ||
| 129 | $defaults{ $key }->{ 'ENV' } | ||
| 130 | } ) | ||
| 131 | { | ||
| 132 | $settings->{ $key } = $ENV{ | ||
| 133 | $defaults{ $key }->{ 'ENV' } | ||
| 134 | }; | ||
| 135 | }else{ | ||
| 136 | $settings->{ $key } = $defaults{ $key }->{ 'default' }; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | foreach my $key ( keys %params ){ | ||
| 141 | if( not exists $defaults{ $key } ){ | ||
| 142 | die "Unknown parameter $key"; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | $LRpt::Config::settings = $settings; | ||
| 147 | } | ||
| 148 | |||
| 149 | ########################################################################## | ||
| 150 | |||
| 151 | =head2 C<get_value> | ||
| 152 | |||
| 153 | my $value = $config->get_value( $name ); | ||
| 154 | |||
| 155 | Returns an actual value of a given parameter | ||
| 156 | |||
| 157 | =cut | ||
| 158 | |||
| 159 | ########################################################################## | ||
| 160 | sub get_value | ||
| 161 | { | ||
| 162 | my $self = shift; | ||
| 163 | my $name = shift; | ||
| 164 | return $settings->{ $name }; | ||
| 165 | } | ||
| 166 | |||
| 167 | =head1 SEE ALSO | ||
| 168 | |||
| 169 | The project is maintained on Source Forge L<http://lreport.sourceforge.net>. | ||
| 170 | You can find there links to some helpful documentation like tutorial. | ||
| 171 | |||
| 172 | =head1 AUTHORS | ||
| 173 | |||
| 174 | Piotr Kaluski E<lt>pkaluski@piotrkaluski.comE<gt> | ||
| 175 | |||
| 176 | =head1 COPYRIGHT | ||
| 177 | |||
| 178 | Copyright (c) 2004-2006 Piotr Kaluski. Poland. All rights reserved. | ||
| 179 | |||
| 180 | You may distribute under the terms of either the GNU General Public License | ||
| 181 | or the Artistic License, as specified in the Perl README file. | ||
| 182 | |||
| 183 | =cut | ||
| 184 | |||
| 185 | 1; | ||
| 186 |