| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package MetaStore::Config; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
MetaStore::Config - Configuration file class. |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use MetaStore::Config; |
|
10
|
|
|
|
|
|
|
my $conf = new MetaStore::Config:: ( $opt{config} ); |
|
11
|
|
|
|
|
|
|
my $value = $conf->general->{db_name}; |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
Configuration file class |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head3 Format of INI-FILE |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
Data is organized in sections. Each key/value pair is delimited with an |
|
21
|
|
|
|
|
|
|
equal (=) sign. Sections are declared on their own lines enclosed in |
|
22
|
|
|
|
|
|
|
'[' and ']': |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
[BLOCK1] |
|
25
|
|
|
|
|
|
|
KEY1 ?=VALUE1 |
|
26
|
|
|
|
|
|
|
KEY2 +=VALUE2 |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
[BLOCK2] |
|
30
|
|
|
|
|
|
|
KEY1=VALUE1 |
|
31
|
|
|
|
|
|
|
KEY2=VALUE2 |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
#%INCLUDE file.inc% |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
B=> - set value unless it defined before |
|
36
|
|
|
|
|
|
|
B<+=> - add value |
|
37
|
|
|
|
|
|
|
B<=> - set value to key |
|
38
|
|
|
|
|
|
|
B<#%INCLUDE file.inc%> - include config ini file |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=cut |
|
41
|
|
|
|
|
|
|
|
|
42
|
1
|
|
|
1
|
|
1559
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
55
|
|
|
43
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
42
|
|
|
44
|
1
|
|
|
1
|
|
268
|
use WebDAO::Base; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
use base 'WebDAO::Base'; |
|
46
|
|
|
|
|
|
|
use Text::ParseWords 'parse_line'; |
|
47
|
|
|
|
|
|
|
use IO::File; |
|
48
|
|
|
|
|
|
|
our $VERSION = '0.3'; |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
__PACKAGE__->mk_attr( __conf=>undef, _path=>undef); |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
#method for convert 'file_name', \*FH, \$string, to hash |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub convert_ini2hash { |
|
56
|
|
|
|
|
|
|
my $data = shift; |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
#if we got filename |
|
59
|
|
|
|
|
|
|
unless ( ref $data ) { |
|
60
|
|
|
|
|
|
|
my $fh = new IO::File:: "< $data"; |
|
61
|
|
|
|
|
|
|
my $res = &convert_ini2hash($fh); |
|
62
|
|
|
|
|
|
|
close $fh; |
|
63
|
|
|
|
|
|
|
return $res; |
|
64
|
|
|
|
|
|
|
} |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
#We got file descriptor ? |
|
67
|
|
|
|
|
|
|
if ( ref $data |
|
68
|
|
|
|
|
|
|
and ( UNIVERSAL::isa( $data, 'IO::Handle' ) or ( ref $data ) eq 'GLOB' ) |
|
69
|
|
|
|
|
|
|
or UNIVERSAL::isa( $data, 'Tie::Handle' ) ) |
|
70
|
|
|
|
|
|
|
{ |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
#read all data from file descripto to scalar |
|
73
|
|
|
|
|
|
|
my $str; |
|
74
|
|
|
|
|
|
|
{ |
|
75
|
|
|
|
|
|
|
local $/; |
|
76
|
|
|
|
|
|
|
$str = <$data>; |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
return &convert_ini2hash( \$str ); |
|
79
|
|
|
|
|
|
|
} |
|
80
|
|
|
|
|
|
|
my %result = (); |
|
81
|
|
|
|
|
|
|
my $line_num = 0; |
|
82
|
|
|
|
|
|
|
my $section = 'default'; |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
#if in param ref to scalar |
|
85
|
|
|
|
|
|
|
foreach ( split /(?:\015{1,2}\012|\015|\012)/, $$data ) { |
|
86
|
|
|
|
|
|
|
my $line = $_; |
|
87
|
|
|
|
|
|
|
$line_num++; |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# skipping comments and empty lines: |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
$line =~ /^\s*(\n|\#|;)/ and next; |
|
92
|
|
|
|
|
|
|
$line =~ /\S/ or next; |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
chomp $line; |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
$line =~ s/^\s+//g; |
|
97
|
|
|
|
|
|
|
$line =~ s/\s+$//g; |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# parsing the block name: |
|
100
|
|
|
|
|
|
|
$line =~ /^\s*\[\s*([^\]]+)\s*\]$/ and $section = lc($1), next; |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# parsing key/value pairs |
|
103
|
|
|
|
|
|
|
# process ?= and += features |
|
104
|
|
|
|
|
|
|
if ( $line =~ /^\s*([^=]*\w)\s*([\?\+]?=)\s*(.*)\s*$/ ) { |
|
105
|
|
|
|
|
|
|
my $key = lc($1); |
|
106
|
|
|
|
|
|
|
my @value = parse_line( '\s*,\s*', 0, $3 ); |
|
107
|
|
|
|
|
|
|
my $op = $2; |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
#add current key |
|
110
|
|
|
|
|
|
|
if ( $op =~ /\+=/ ) { |
|
111
|
|
|
|
|
|
|
push @{ $result{$section}->{$key} }, @value; |
|
112
|
|
|
|
|
|
|
next; |
|
113
|
|
|
|
|
|
|
} |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# skip if already defined key |
|
116
|
|
|
|
|
|
|
elsif ( $op =~ /\?=/ ) { |
|
117
|
|
|
|
|
|
|
next if defined $result{$section}->{$key}; |
|
118
|
|
|
|
|
|
|
} |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# set current value to result hash |
|
121
|
|
|
|
|
|
|
$result{$section}->{$key} = \@value; |
|
122
|
|
|
|
|
|
|
next; |
|
123
|
|
|
|
|
|
|
} |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# if we came this far, the syntax couldn't be validated: |
|
126
|
|
|
|
|
|
|
warn "syntax error on line $line_num: '$line'"; |
|
127
|
|
|
|
|
|
|
return {}; |
|
128
|
|
|
|
|
|
|
} |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
#strip values |
|
131
|
|
|
|
|
|
|
while ( my ( $sect_name, $sect_hash ) = each %result ) { |
|
132
|
|
|
|
|
|
|
while ( my ( $key, $val ) = each %$sect_hash ) { |
|
133
|
|
|
|
|
|
|
if ( scalar(@$val) < 2 ) { |
|
134
|
|
|
|
|
|
|
$result{$sect_name}->{$key} = shift @$val; |
|
135
|
|
|
|
|
|
|
} |
|
136
|
|
|
|
|
|
|
} |
|
137
|
|
|
|
|
|
|
} |
|
138
|
|
|
|
|
|
|
return \%result; |
|
139
|
|
|
|
|
|
|
} |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub get_full_path_for { |
|
142
|
|
|
|
|
|
|
my $root_file = shift; |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
# my $file_to = shift; |
|
145
|
|
|
|
|
|
|
my @req_path = @_; |
|
146
|
|
|
|
|
|
|
my $req_path = join "/", @req_path; |
|
147
|
|
|
|
|
|
|
return $req_path if $req_path =~ /^\//; |
|
148
|
|
|
|
|
|
|
my @ini_path = split( "/", $root_file ); |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
#strip file name |
|
151
|
|
|
|
|
|
|
pop @ini_path; |
|
152
|
|
|
|
|
|
|
my $path = join "/" => @ini_path, $req_path; |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
return $path; |
|
155
|
|
|
|
|
|
|
} |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
sub process_includes { |
|
158
|
|
|
|
|
|
|
my $file = shift; |
|
159
|
|
|
|
|
|
|
my $fh = ( new IO::File:: "< $file" ) || die "$file: $!"; |
|
160
|
|
|
|
|
|
|
my $str = ''; |
|
161
|
|
|
|
|
|
|
while ( defined( my $line = <$fh> ) ) { |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
$str .= |
|
164
|
|
|
|
|
|
|
$line =~ /#%INCLUDE\s*(.*)\s*%/ |
|
165
|
|
|
|
|
|
|
? &process_includes( &get_full_path_for( $file, $1 ) ) |
|
166
|
|
|
|
|
|
|
: $line; |
|
167
|
|
|
|
|
|
|
} |
|
168
|
|
|
|
|
|
|
close $fh; |
|
169
|
|
|
|
|
|
|
return $str; |
|
170
|
|
|
|
|
|
|
} |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
sub new { |
|
173
|
|
|
|
|
|
|
my $class = shift; |
|
174
|
|
|
|
|
|
|
my $self = {}; |
|
175
|
|
|
|
|
|
|
my $stat; |
|
176
|
|
|
|
|
|
|
bless( $self, $class ); |
|
177
|
|
|
|
|
|
|
return ( $stat = $self->_init(@_) ) ? $self : $stat; |
|
178
|
|
|
|
|
|
|
} |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
sub _init { |
|
181
|
|
|
|
|
|
|
my $self = shift; |
|
182
|
|
|
|
|
|
|
my $file_path = shift; |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
#process inludes in in data |
|
185
|
|
|
|
|
|
|
my $inc = &process_includes($file_path); |
|
186
|
|
|
|
|
|
|
$self->__conf( &convert_ini2hash(\$inc) ); |
|
187
|
|
|
|
|
|
|
$self->_path($file_path); |
|
188
|
|
|
|
|
|
|
return 1; |
|
189
|
|
|
|
|
|
|
} |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
sub get_full_path { |
|
192
|
|
|
|
|
|
|
my $self = shift; |
|
193
|
|
|
|
|
|
|
my @req_path = @_; |
|
194
|
|
|
|
|
|
|
my $req_path = join "/", @req_path; |
|
195
|
|
|
|
|
|
|
return $req_path if $req_path =~ /^\//; |
|
196
|
|
|
|
|
|
|
my @ini_path = split( "/", $self->_path ); |
|
197
|
|
|
|
|
|
|
pop @ini_path; |
|
198
|
|
|
|
|
|
|
my $path = join "/" => @ini_path, $req_path; |
|
199
|
|
|
|
|
|
|
return $path; |
|
200
|
|
|
|
|
|
|
} |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
sub AUTOLOAD { |
|
203
|
|
|
|
|
|
|
my $self = shift; |
|
204
|
|
|
|
|
|
|
return if $MetaStore::Config::AUTOLOAD =~ /::DESTROY$/; |
|
205
|
|
|
|
|
|
|
( my $auto_sub ) = $MetaStore::Config::AUTOLOAD =~ /.*::(.*)/; |
|
206
|
|
|
|
|
|
|
return $self->__conf->{$auto_sub}; |
|
207
|
|
|
|
|
|
|
} |
|
208
|
|
|
|
|
|
|
1; |
|
209
|
|
|
|
|
|
|
__END__ |