line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
use Catmandu::Sane; |
3
|
147
|
|
|
147
|
|
87887
|
|
|
147
|
|
|
|
|
279
|
|
|
147
|
|
|
|
|
843
|
|
4
|
|
|
|
|
|
|
our $VERSION = '1.2019'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use Catmandu::Util qw(require_package use_lib read_yaml read_json :is :check); |
7
|
147
|
|
|
147
|
|
1029
|
use Catmandu::Fix; |
|
147
|
|
|
|
|
397
|
|
|
147
|
|
|
|
|
57983
|
|
8
|
147
|
|
|
147
|
|
21119
|
use Config::Onion; |
|
147
|
|
|
|
|
381
|
|
|
147
|
|
|
|
|
4569
|
|
9
|
147
|
|
|
147
|
|
61415
|
use File::Spec; |
|
147
|
|
|
|
|
1954762
|
|
|
147
|
|
|
|
|
5571
|
|
10
|
147
|
|
|
147
|
|
1208
|
use Moo; |
|
147
|
|
|
|
|
385
|
|
|
147
|
|
|
|
|
3570
|
|
11
|
147
|
|
|
147
|
|
863
|
require Catmandu; |
|
147
|
|
|
|
|
333
|
|
|
147
|
|
|
|
|
807
|
|
12
|
|
|
|
|
|
|
use namespace::clean; |
13
|
147
|
|
|
147
|
|
72022
|
|
|
147
|
|
|
|
|
324
|
|
|
147
|
|
|
|
|
1129
|
|
14
|
|
|
|
|
|
|
with 'Catmandu::Logger'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $dir = $_[0]; |
17
|
|
|
|
|
|
|
my @dirs = grep length, File::Spec->splitdir(Catmandu->default_load_path); |
18
|
0
|
|
|
0
|
|
0
|
for (; @dirs; pop @dirs) { |
19
|
0
|
|
|
|
|
0
|
my $path = File::Spec->catdir(File::Spec->rootdir, @dirs); |
20
|
0
|
|
|
|
|
0
|
opendir my $dh, $path or last; |
21
|
0
|
|
|
|
|
0
|
return $path |
22
|
0
|
0
|
|
|
|
0
|
if grep {-r File::Spec->catfile($path, $_)} |
23
|
|
|
|
|
|
|
grep /^catmandu.+(?:yaml|yml|json|pl)$/, readdir $dh; |
24
|
0
|
0
|
|
|
|
0
|
} |
|
0
|
|
|
|
|
0
|
|
25
|
|
|
|
|
|
|
Catmandu->default_load_path; |
26
|
|
|
|
|
|
|
} |
27
|
0
|
|
|
|
|
0
|
|
28
|
|
|
|
|
|
|
has load_paths => ( |
29
|
|
|
|
|
|
|
is => 'ro', |
30
|
|
|
|
|
|
|
default => sub {[]}, |
31
|
|
|
|
|
|
|
coerce => sub { |
32
|
|
|
|
|
|
|
[ |
33
|
|
|
|
|
|
|
map {File::Spec->canonpath($_)} |
34
|
|
|
|
|
|
|
map {$_ eq ':up' ? _search_up($_) : $_} split /,/, |
35
|
|
|
|
|
|
|
join ',', |
36
|
|
|
|
|
|
|
ref $_[0] ? @{$_[0]} : $_[0] |
37
|
|
|
|
|
|
|
]; |
38
|
|
|
|
|
|
|
}, |
39
|
|
|
|
|
|
|
); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
has config => (is => 'rwp', default => sub {+{}}); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
has stores => (is => 'ro', default => sub {+{}}); |
44
|
|
|
|
|
|
|
has validators => (is => 'ro', default => sub {+{}}); |
45
|
|
|
|
|
|
|
has fixers => (is => 'ro', default => sub {+{}}); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
has default_store => (is => 'ro', default => sub {'default'}); |
48
|
|
|
|
|
|
|
has default_importer => (is => 'ro', default => sub {'default'}); |
49
|
|
|
|
|
|
|
has default_exporter => (is => 'ro', default => sub {'default'}); |
50
|
|
|
|
|
|
|
has default_validator => (is => 'ro', default => sub {'default'}); |
51
|
|
|
|
|
|
|
has default_fixer => (is => 'ro', default => sub {'default'}); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
has default_store_package => (is => 'ro'); |
54
|
|
|
|
|
|
|
has default_importer_package => (is => 'ro', default => sub {'JSON'}); |
55
|
|
|
|
|
|
|
has default_exporter_package => (is => 'ro', default => sub {'JSON'}); |
56
|
|
|
|
|
|
|
has default_validator_package => (is => 'ro'); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
has store_namespace => (is => 'ro', default => sub {'Catmandu::Store'}); |
59
|
|
|
|
|
|
|
has importer_namespace => (is => 'ro', default => sub {'Catmandu::Importer'}); |
60
|
|
|
|
|
|
|
has exporter_namespace => (is => 'ro', default => sub {'Catmandu::Exporter'}); |
61
|
|
|
|
|
|
|
has validator_namespace => |
62
|
|
|
|
|
|
|
(is => 'ro', default => sub {'Catmandu::Validator'}); |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
my ($self) = @_; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
my @config_dirs = @{$self->load_paths}; |
67
|
65
|
|
|
65
|
0
|
374
|
my @lib_dirs; |
68
|
|
|
|
|
|
|
|
69
|
65
|
|
|
|
|
119
|
for my $dir (@config_dirs) { |
|
65
|
|
|
|
|
298
|
|
70
|
65
|
|
|
|
|
119
|
if (!-d $dir) { |
71
|
|
|
|
|
|
|
Catmandu::Error->throw("load path $dir doesn't exist"); |
72
|
65
|
|
|
|
|
193
|
} |
73
|
65
|
50
|
|
|
|
1506
|
|
74
|
0
|
|
|
|
|
0
|
my $lib_dir = File::Spec->catdir($dir, 'lib'); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
if (-d $lib_dir && -r $lib_dir) { |
77
|
65
|
|
|
|
|
684
|
push @lib_dirs, $lib_dir; |
78
|
|
|
|
|
|
|
} |
79
|
65
|
50
|
33
|
|
|
1698
|
} |
80
|
65
|
|
|
|
|
305
|
|
81
|
|
|
|
|
|
|
if (@config_dirs) { |
82
|
|
|
|
|
|
|
my @globs = map { |
83
|
|
|
|
|
|
|
my $dir = $_; |
84
|
65
|
50
|
|
|
|
182
|
map {File::Spec->catfile($dir, "catmandu*.$_")} |
85
|
|
|
|
|
|
|
qw(yaml yml json pl) |
86
|
65
|
|
|
|
|
170
|
} reverse @config_dirs; |
|
65
|
|
|
|
|
117
|
|
87
|
65
|
|
|
|
|
128
|
|
|
260
|
|
|
|
|
1876
|
|
88
|
|
|
|
|
|
|
my $config = Config::Onion->new(prefix_key => '_prefix'); |
89
|
|
|
|
|
|
|
$config->load_glob(@globs); |
90
|
|
|
|
|
|
|
|
91
|
65
|
|
|
|
|
1157
|
if ($self->log->is_debug) { |
92
|
65
|
|
|
|
|
51451
|
use Data::Dumper; |
93
|
|
|
|
|
|
|
$self->log->debug(Dumper($config->get)); |
94
|
65
|
100
|
|
|
|
937913
|
} |
95
|
147
|
|
|
147
|
|
272292
|
$self->_set_config($config->get); |
|
147
|
|
|
|
|
616
|
|
|
147
|
|
|
|
|
134894
|
|
96
|
13
|
|
|
|
|
654
|
} |
97
|
|
|
|
|
|
|
|
98
|
65
|
|
|
|
|
62421
|
if (@lib_dirs) { |
99
|
|
|
|
|
|
|
lib->import(@lib_dirs); |
100
|
|
|
|
|
|
|
} |
101
|
65
|
50
|
|
|
|
14598
|
} |
102
|
65
|
|
|
|
|
501
|
|
103
|
|
|
|
|
|
|
$_[0]->load_paths->[0]; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
goto &load_paths; |
107
|
2
|
|
|
2
|
0
|
22
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
goto &load_path; |
110
|
|
|
|
|
|
|
} |
111
|
5
|
|
|
5
|
0
|
194
|
|
112
|
|
|
|
|
|
|
my $self = shift; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# it's already a fixer |
115
|
2
|
|
|
2
|
0
|
613
|
if (is_instance($_[0], 'Catmandu::Fix')) { |
116
|
|
|
|
|
|
|
return $_[0]; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
11
|
|
|
11
|
0
|
2573
|
# an array ref of fixes |
120
|
|
|
|
|
|
|
if (is_array_ref($_[0])) { |
121
|
|
|
|
|
|
|
return Catmandu::Fix->new(fixes => $_[0]); |
122
|
11
|
50
|
|
|
|
54
|
} |
123
|
0
|
|
|
|
|
0
|
|
124
|
|
|
|
|
|
|
# a single fix |
125
|
|
|
|
|
|
|
if (is_able($_[0], 'fix')) { |
126
|
|
|
|
|
|
|
return Catmandu::Fix->new(fixes => [$_[0]]); |
127
|
11
|
100
|
|
|
|
66
|
} |
128
|
2
|
|
|
|
|
23
|
|
129
|
|
|
|
|
|
|
# try to load from config |
130
|
|
|
|
|
|
|
my $key = $_[0] || $self->default_fixer; |
131
|
|
|
|
|
|
|
|
132
|
9
|
50
|
|
|
|
41
|
my $fixers = $self->fixers; |
133
|
0
|
|
|
|
|
0
|
|
134
|
|
|
|
|
|
|
$fixers->{$key} || do { |
135
|
|
|
|
|
|
|
if (my $fixes = $self->config->{fixer}{$key}) { |
136
|
|
|
|
|
|
|
return $fixers->{$key} = Catmandu::Fix->new(fixes => $fixes); |
137
|
9
|
|
66
|
|
|
41
|
} |
138
|
|
|
|
|
|
|
return Catmandu::Fix->new(fixes => [@_]); |
139
|
9
|
|
|
|
|
43
|
}; |
140
|
|
|
|
|
|
|
} |
141
|
9
|
50
|
|
|
|
52
|
|
142
|
9
|
100
|
|
|
|
62
|
my $self = shift; |
143
|
3
|
|
|
|
|
38
|
$self->_named_package('store', $self->store_namespace, |
144
|
|
|
|
|
|
|
$self->default_store, $self->default_store_package, |
145
|
6
|
|
|
|
|
99
|
$self->stores, @_); |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
my $self = shift; |
149
|
|
|
|
|
|
|
$self->_named_package('importer', $self->importer_namespace, |
150
|
29
|
|
|
29
|
0
|
202
|
$self->default_importer, $self->default_importer_package, |
151
|
29
|
|
|
|
|
300
|
undef, @_); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
my $self = shift; |
155
|
|
|
|
|
|
|
$self->_named_package('exporter', $self->exporter_namespace, |
156
|
|
|
|
|
|
|
$self->default_exporter, $self->default_exporter_package, |
157
|
41
|
|
|
41
|
0
|
629
|
undef, @_); |
158
|
41
|
|
|
|
|
365
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
my $self = shift; |
161
|
|
|
|
|
|
|
$self->_named_package( |
162
|
|
|
|
|
|
|
'validator', $self->validator_namespace, |
163
|
|
|
|
|
|
|
$self->default_validator, $self->default_validator_package, |
164
|
55
|
|
|
55
|
0
|
452
|
$self->validators, @_ |
165
|
55
|
|
|
|
|
340
|
); |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
my $self = shift; |
169
|
|
|
|
|
|
|
my $type = shift; |
170
|
|
|
|
|
|
|
my $ns = shift; |
171
|
2
|
|
|
2
|
0
|
14
|
my $default_name = shift; |
172
|
2
|
|
|
|
|
36
|
my $default_package = shift; |
173
|
|
|
|
|
|
|
my $cache = shift; |
174
|
|
|
|
|
|
|
my $name = shift; |
175
|
|
|
|
|
|
|
my $key = $name || $default_name; |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
return $name if is_instance($name) && index(ref($name), $ns) == 0; |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
# return cached instance if no arguments are given |
180
|
127
|
|
|
127
|
|
288
|
if ($cache && !@_ and my $instance = $cache->{$key}) { |
181
|
127
|
|
|
|
|
217
|
return $instance; |
182
|
127
|
|
|
|
|
192
|
} |
183
|
127
|
|
|
|
|
193
|
|
184
|
127
|
|
|
|
|
197
|
if (exists $self->config->{$type}) { |
185
|
127
|
|
|
|
|
197
|
if (my $c = $self->config->{$type}{$key}) { |
186
|
127
|
|
|
|
|
196
|
check_hash_ref($c); |
187
|
127
|
|
66
|
|
|
326
|
check_string(my $package = $c->{package} || $default_package); |
188
|
|
|
|
|
|
|
my $opts = check_hash_ref($c->{options} || {}); |
189
|
127
|
100
|
66
|
|
|
495
|
if (@_ > 1) { |
190
|
|
|
|
|
|
|
$opts = {%$opts, @_}; |
191
|
|
|
|
|
|
|
} |
192
|
126
|
100
|
100
|
|
|
565
|
elsif (@_ == 1) { |
|
|
|
100
|
|
|
|
|
193
|
12
|
|
|
|
|
65
|
$opts = {%$opts, %{$_[0]}}; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
my $instance = require_package($package, $ns)->new($opts); |
196
|
114
|
50
|
|
|
|
499
|
|
197
|
114
|
100
|
|
|
|
467
|
# cache this instance if no arguments are given |
198
|
25
|
|
|
|
|
109
|
if ($cache && !@_) { |
199
|
25
|
|
33
|
|
|
4935
|
$cache->{$key} = $instance; |
200
|
25
|
|
100
|
|
|
3793
|
} |
201
|
25
|
100
|
|
|
|
588
|
|
|
|
100
|
|
|
|
|
|
202
|
1
|
|
|
|
|
5
|
return $instance; |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
} |
205
|
6
|
|
|
|
|
20
|
|
|
6
|
|
|
|
|
18
|
|
206
|
|
|
|
|
|
|
check_string(my $package = $name || $default_package); |
207
|
25
|
|
|
|
|
105
|
require_package($package, $ns)->new(@_); |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
|
210
|
25
|
100
|
100
|
|
|
214
|
1; |
211
|
10
|
|
|
|
|
163
|
|
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=pod |
214
|
25
|
|
|
|
|
413
|
|
215
|
|
|
|
|
|
|
=head1 NAME |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
Catmandu::Env - A catmandu configuration file loader |
218
|
89
|
|
33
|
|
|
471
|
|
219
|
89
|
|
|
|
|
8090
|
=head1 SYNOPSIS |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
use Catmandu::Env; |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
my $env = Catmandu::Env->new(load_paths => [ '/etc/catmandu '] ); |
224
|
|
|
|
|
|
|
my $env = Catmandu::Env->new(load_paths => [ ':up'] ); |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
my $store = $env->store('mongodb'); |
227
|
|
|
|
|
|
|
my $importer = $env->importer('loc'); |
228
|
|
|
|
|
|
|
my $exporter = $env->exporter('europeana'); |
229
|
|
|
|
|
|
|
my $fixer = $env->fixer('my_fixes'); |
230
|
|
|
|
|
|
|
my $conf = $env->config; |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=head1 DESCRIPTION |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
This class loads the catmandu.*.pl, catmandu.*.json, catmandu.*.yml and catmandu.*.yaml file from |
235
|
|
|
|
|
|
|
all provided load_paths. Programmers are advised I<not> to use this class directly |
236
|
|
|
|
|
|
|
but use the equivalent functionality provided in the Catmandu package: |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
Catmandu->load('/etc/catmandu'); |
239
|
|
|
|
|
|
|
Catmandu->load(':up'); |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
my $store = Catmandu->store('mongodb'); |
242
|
|
|
|
|
|
|
my $importer = Catmandu->importer('loc'); |
243
|
|
|
|
|
|
|
my $exporter = Catmandu->exporter('europeana'); |
244
|
|
|
|
|
|
|
my $fixer = Catmandu->fixer('my_fixes'); |
245
|
|
|
|
|
|
|
my $conf = Catmandu->config; |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=head1 SEE ALSO |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
L<Catmandu> |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=cut |