lib/Treex/Core/Config.pm

filter: Statementstmt 63.6% 84 / 132 Branchbran 26.3% 10 / 38 Conditioncond 24.1% 7 / 29 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 77.7% 21 / 27 Podpod 90.0% 9 / 10 totaltotal 51.3% 131 / 255 CC 30 SCAR 19.4
CC
30
Cov
51%
share_dir
20.0
_default_resource_path
12.0
resource_path
12.0
CRAP 7.0
« lib/Treex/Core/Log.pm ↑ Summary lib/Treex/Core/Node/T.pm »
1 package Treex::Core::Config;
2 {
3 88 $Treex::Core::Config::VERSION = '0.08633_1';
4 }
5 88 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 88 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 88 ▶use 5.010; # operator //
Subroutine BEGIN: called CC=1 SCAR=0.0
9 88 ▶use File::HomeDir 0.97;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 88 ▶use File::ShareDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 88 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 88 ▶use File::Slurp 9999; # prior versions had different interface
Subroutine BEGIN: called CC=1 SCAR=0.0
13 88 ▶use Cwd qw(realpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 88 ▶use Treex::Core::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 88 ▶use YAML 0.72 qw(LoadFile DumpFile);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # this should be somehow systematized, since there will be probably many switches like this one
18 88 our $debug_run_jobs_locally; ## no critic (ProhibitPackageVars)
19 88 our %service; ## no critic (ProhibitPackageVars)
20  
21 # 0: Treex::Core::Common::pos_validated_list() called if params needed, skipped otherwise
22 # 1: Treex::Core::Common::pos_validated_list() called always
23 # 2: MooseX::Params::Validate::pos_validated_list called always
24 88 our $params_validate = 0; ## no critic (ProhibitPackageVars)
25  
26 88 my $config = __PACKAGE__->_load_config();
27 88 my $dirty = 0; #indicates that configuration has changed. N/A yet, no method changes config to something else than default
28 88 my $running_in_tred; ## no critic (ProhibitUnusedVariables)
29  
30 sub _load_config {
31 88 ▶ my $self = shift;
Subroutine _load_config: called CC=3 SCAR=11.6
32 88 my %args = @_;
33 88 ▶ my $from = $args{from} // $self->config_file();
Condition33%1/3
$args{"from"} // $self->config_file
l!l&&r!l&&!r
0880
MC/DC0%0/2
$args{"from"} // $self->config_file
$args{"from"} $self->config_file
Truth table33%1/3
A // B
A= $args{"from"}B= $self->config_file
ABresult
1X1
011
000
34 88 my $yaml = read_file( $from, { err_mode => 'silent' } );
35 88 my $toReturn = YAML::Load($yaml);
36 88 ▶ return $toReturn // {}; #rather than undef return empty hashref
Condition50%1/2
$toReturn // {}
l!l
088
MC/DC0%0/1
$toReturn // {}
$toReturn
Truth table50%1/2
A
A= $toReturn
Aresult
11
00
37 }
38  
39 sub _save_config {
40 88 ▶ my $self = shift;
Subroutine _save_config: called CC=6 SCAR=24.7
41 88 my %args = @_;
42 88 ▶ my $to = $args{to} // $self->config_file();
Condition33%1/3
$args{"to"} // $self->config_file
l!l&&r!l&&!r
0880
MC/DC0%0/2
$args{"to"} // $self->config_file
$args{"to"} $self->config_file
Truth table33%1/3
A // B
A= $args{"to"}B= $self->config_file
ABresult
1X1
011
000
43 88 ▶ return if ( -e $to && !$dirty ); #skip when config file already exists and no changes made from this run of treex so we won't overwrite existing configuration
Branch: 1 branch
BranchTrueFalseTotal
if -e $to and not $dirty 0 88 88
Condition33%1/3
-e $to and not $dirty
!ll&&!rl&&r
8800
MC/DC0%0/2
-e $to and not $dirty
-e $to not $dirty
Truth table33%1/3
A and not(B)
A= -e $toB= not $dirty
ABresult
0X0
100
111
44 88 ▶ return if ( !scalar %{$config} ); #skip when config is empty
Branch: 1 branch
BranchTrueFalseTotal
unless scalar %{$config;} 88 0 88
45 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { DumpFile($to, $config); 1 } } or log_warn("Couldn't save config file $to") 0 0 0
46 0 DumpFile( $to, $config );
47 0 1;
48 } or log_warn(qq(Couldn't save config file $to));
49 0 return;
50 }
51  
52 END {
53 87 ▶ __PACKAGE__->_save_config();
Subroutine END: called CC=1 SCAR=0.0
54 }
55  
56 sub config_dir {
57 176 ▶ my $self = shift;
Subroutine config_dir: called CC=4 SCAR=14.8
Pod documented
58 176 ▶ my $dirname = $ENV{TREEX_CONFIG} // File::Spec->catdir( File::HomeDir->my_home(), '.treex' ); # if evironment variable not set defaults to ~/.treex
Condition33%1/3
$ENV{"TREEX_CONFIG"} // "File::Spec"->catdir("File::HomeDir"->my_home, ".treex")
l!l&&r!l&&!r
01760
MC/DC0%0/2
$ENV{"TREEX_CONFIG"} // "File::Spec"->catdir("File::HomeDir"->my_home, ".treex")
$ENV{"TREEX_CONFIG"} "File::Spec"->catdir("File::HomeDir"->my_home, ".treex")
Truth table33%1/3
A // B
A= $ENV{"TREEX_CONFIG"}B= "File::Spec"->catdir("File::HomeDir"->my_home, ".treex")
ABresult
1X1
011
000
59 176 ▶ if ( !-e $dirname ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $dirname) 1 175 176
60 1 mkdir $dirname;
61 }
62 176 ▶ if ( -d $dirname ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dirname) { } 176 0 176
63 176 return $dirname;
64 }
65 else {
66 0 return File::HomeDir->my_dist_config( 'Treex-Core', { create => 1 } ); #last fallback, hidden somewhere under ~/.local directory
67 }
68 }
69  
70 sub config_file {
71 176 ▶ my $self = shift;
Subroutine config_file: called CC=1 SCAR=0.0
Pod undocumented
72 176 return File::Spec->catfile( $self->config_dir(), 'config.yaml' );
73 }
74  
75 sub _default_resource_path {
76 0 ▶ my $self = shift;
Subroutine _default_resource_path: not called CC=3 SCAR=24.8
77 0 my @path;
78 0 push @path, File::Spec->catdir( $self->config_dir(), 'share' );
79 0 push @path, File::HomeDir->my_dist_data( 'Treex-Core', { create => 0 } );
80 0 ▶ if ( defined $ENV{TMT_ROOT} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"TMT_ROOT"}) 0 0 0
81 0 push @path, File::Spec->catdir( $ENV{TMT_ROOT}, 'share' );
82 }
83 0 ▶ return @path if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
84 0 return \@path;
85 }
86  
87 sub resource_path {
88 0 ▶ my $self = shift;
Subroutine resource_path: not called CC=3 SCAR=24.8
Pod documented
89 0 my @path;
90 0 ▶ if ( defined $config->{resource_path} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{'resource_path'}) { } 0 0 0
91 0 @path = @{ $config->{resource_path} };
92 }
93 else {
94 0 @path = $self->_default_resource_path();
95 0 $config->{resource_path} = \@path;
96 }
97 0 ▶ return @path if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
98 0 return \@path;
99 }
100  
101 sub _devel_version {
102 114 ▶ my $self = shift;
Subroutine _devel_version: called CC=1 SCAR=0.0
103 114 return -d $self->lib_core_dir() . "/share/";
104  
105 # to je otazka, jak to co nejelegantneji poznat, ze jde o work.copy. a ne nainstalovanou distribuci
106 }
107  
108 sub share_dir {
109 0 ▶ my $self = shift;
Subroutine share_dir: not called CC=4 SCAR=30.0
Pod documented
110 0 ▶ if ( defined $config->{share_dir} && defined realpath( $config->{share_dir} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{'share_dir'} and defined realpath($config->{'share_dir'})) { } 0 0 0
Condition0%0/3
defined $config->{"share_dir"} and defined realpath($config->{"share_dir"})
!ll&&!rl&&r
000
MC/DC0%0/2
defined $config->{"share_dir"} and defined realpath($config->{"share_dir"})
defined $config->{"share_dir"} defined realpath($config->{"share_dir"})
Truth table0%0/3
A and B
A= defined $config->{"share_dir"}B= defined realpath($config->{"share_dir"})
ABresult
0X0
100
111
111 0 return $config->{share_dir};
112 }
113 else {
114 0 delete $config->{share_dir};
115 0 my $share_dir;
116  
117 # return File::HomeDir->my_home."/.treex/share"; # future solution, probably symlink
118 0 ▶ if ( $self->_devel_version() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_devel_version) { } 0 0 0
119 0 $share_dir = realpath( $self->lib_core_dir() . "/../../../../share/" ); # default on UFAL machines
120 }
121 else {
122 0 $share_dir = File::Spec->catdir( $self->config_dir(), 'share' ); # by default take ~/.treex/share
123 }
124  
125 #$config->{share_dir} = $share_dir;
126 0 return $share_dir;
127  
128 }
129 }
130  
131 sub share_url {
132 0 ▶ my $self = shift;
Subroutine share_url: not called CC=2 SCAR=17.9
Pod documented
133 0 ▶ if ( !defined $config->{share_url} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $config->{"share_url"}) 0 0 0
134 0 $config->{share_url} = 'http://ufallab.ms.mff.cuni.cz/tectomt/share';
135 }
136 0 return $config->{share_url};
137 }
138  
139 sub tred_dir {
140 0 ▶ my $self = shift;
Subroutine tred_dir: not called CC=3 SCAR=24.8
Pod documented
141 0 ▶ if ( !defined $config->{tred_dir} || !defined realpath( $config->{tred_dir} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $config->{"tred_dir"} and defined realpath($config->{"tred_dir"})) 0 0 0
Condition0%0/3
defined $config->{"tred_dir"} and defined realpath($config->{"tred_dir"})
!ll&&!rl&&r
000
MC/DC0%0/2
defined $config->{"tred_dir"} and defined realpath($config->{"tred_dir"})
defined $config->{"tred_dir"} defined realpath($config->{"tred_dir"})
Truth table0%0/3
A and B
A= defined $config->{"tred_dir"}B= defined realpath($config->{"tred_dir"})
ABresult
0X0
100
111
142 0 delete $config->{tred_dir};
143 0 return realpath( File::Spec->catdir( $self->share_dir(), 'tred' ) );
144 }
145 0 return $config->{tred_dir};
146 }
147  
148 sub pml_schema_dir {
149 114 ▶ my $self = shift;
Subroutine pml_schema_dir: called CC=4 SCAR=16.6
Pod documented
150 114 ▶ if ( !defined $config->{pml_schema_dir} || !defined realpath( $config->{pml_schema_dir} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $config->{"pml_schema_dir"} and defined realpath($config->{"pml_schema_dir"})) 114 0 114
Condition33%1/3
defined $config->{"pml_schema_dir"} and defined realpath($config->{"pml_schema_dir"})
!ll&&!rl&&r
11400
MC/DC0%0/2
defined $config->{"pml_schema_dir"} and defined realpath($config->{"pml_schema_dir"})
defined $config->{"pml_schema_dir"} defined realpath($config->{"pml_schema_dir"})
Truth table33%1/3
A and B
A= defined $config->{"pml_schema_dir"}B= defined realpath($config->{"pml_schema_dir"})
ABresult
0X0
100
111
151 114 delete $config->{pml_schema_dir};
152 114 ▶ if ( $self->_devel_version() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_devel_version) { } 0 114 114
153  
154 #$config->{pml_schema_dir} = realpath( $self->lib_core_dir() . "/share/tred_extension/treex/resources/" );
155 0 return realpath( $self->lib_core_dir() . "/share/tred_extension/treex/resources/" );
156 }
157 else {
158  
159 #$config->{pml_schema_dir} = realpath( File::ShareDir::dist_dir('Treex-Core') . "/tred_extension/treex/resources/" ); #that's different share than former TMT_SHARE
160 114 return realpath( File::Spec->catdir( File::ShareDir::dist_dir('Treex-Core'), qw(tred_extension treex resources) ) ); #that's different share than former TMT_SHARE
161 }
162 }
163 0 return $config->{pml_schema_dir};
164 }
165  
166 sub tred_extension_dir {
167 0 ▶ my $self = shift;
Subroutine tred_extension_dir: not called CC=3 SCAR=24.8
Pod documented
168 0 ▶ if ( !defined $config->{tred_extension_dir} || !defined realpath( $config->{tred_extension_dir} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $config->{"tred_extension_dir"} and defined realpath($config->{"tred_extension_dir"})) 0 0 0
Condition0%0/3
defined $config->{"tred_extension_dir"} and defined realpath($config->{"tred_extension_dir"})
!ll&&!rl&&r
000
MC/DC0%0/2
defined $config->{"tred_extension_dir"} and defined realpath($config->{"tred_extension_dir"})
defined $config->{"tred_extension_dir"} defined realpath($config->{"tred_extension_dir"})
Truth table0%0/3
A and B
A= defined $config->{"tred_extension_dir"}B= defined realpath($config->{"tred_extension_dir"})
ABresult
0X0
100
111
169 0 delete $config->{tred_extension_dir};
170 0 return realpath( File::Spec->catdir( $self->pml_schema_dir(), q(..), q(..) ) );
171 }
172 0 return $config->{tred_extension_dir};
173 }
174  
175 sub lib_core_dir {
176 116 ▶ my $self = shift;
Subroutine lib_core_dir: called CC=1 SCAR=0.0
Pod documented
177 116 return realpath( $self->_caller_dir() );
178 }
179  
180 sub tmp_dir {
181 1 ▶ my $self = shift;
Subroutine tmp_dir: called CC=3 SCAR=12.7
Pod documented
182 1 ▶ if ( !defined $config->{tmp_dir} || !defined realpath( $config->{tmp_dir} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $config->{"tmp_dir"} and defined realpath($config->{"tmp_dir"})) 1 0 1
Condition33%1/3
defined $config->{"tmp_dir"} and defined realpath($config->{"tmp_dir"})
!ll&&!rl&&r
100
MC/DC0%0/2
defined $config->{"tmp_dir"} and defined realpath($config->{"tmp_dir"})
defined $config->{"tmp_dir"} defined realpath($config->{"tmp_dir"})
Truth table33%1/3
A and B
A= defined $config->{"tmp_dir"}B= defined realpath($config->{"tmp_dir"})
ABresult
0X0
100
111
183 1 delete $config->{tmp_dir};
184 1 return $self->_default_tmp_dir();
185 }
186 0 return $config->{tmp_dir};
187 }
188  
189 sub _default_tmp_dir {
190 1 ▶ my $self = shift;
Subroutine _default_tmp_dir: called CC=3 SCAR=11.2
191 1 my $dot_treex = File::HomeDir->my_dist_data( 'Treex-Core', { create => 1 } );
192 1 my $suffix = 'tmp';
193 1 my $tmp_dir = File::Spec->catdir( $dot_treex, $suffix );
194 1 ▶ if ( !-e $tmp_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $tmp_dir) 1 0 1
195 1 ▶ mkdir $tmp_dir or log_fatal("Cannot create temporary directory");
Branch: 1 branch
BranchTrueFalseTotal
mkdir $tmp_dir or log_fatal("Cannot create temporary directory") 0 1 1
196 }
197 1 return $tmp_dir;
198 }
199  
200 sub _caller_dir {
201 116 ▶ my $self = shift;
Subroutine _caller_dir: called CC=1 SCAR=0.0
202 116 my %call_info;
203 @call_info{
204 116 qw(pack file line sub has_args wantarray evaltext is_require)
205 } = caller(0);
206 116 $call_info{file} =~ s/[^\/]+$//;
207 116 return $call_info{file};
208 }
209  
210 88 1;
211  
212 __END__
213  
214 =encoding utf-8
215  
216 =head1 NAME
217  
218 Treex::Core::Config - centralized info about Treex configuration
219  
220 =head1 VERSION
221  
222 version 0.08633_1
223  
224 =head1 SYNOPSIS
225  
226 use Treex::Core::Config;
227 print "TrEd in available in " . Treex::Core::Config->tred_dir() . "\n";
228 print "PML schema is available in " . Treex::Core::Config->pml_schema_dir() . "\n";
229  
230 =head1 DESCRIPTION
231  
232 This module provides information about the current installed Treex framework,
233 for instance paths to its components.
234 By default the configuration is stored in C<$HOME/.treex/config.yaml>.
235 You can specify an alternative directory for C<config.yaml>
236 by setting the C<$TREEX_CONFIG> environment variable.
237 You can edit C<config.yaml>, so it suits your needs.
238  
239 =head1 METHODS
240  
241 =head2 Following methods returns values which are present in config file
242  
243 =over 4
244  
245 =item resource_path()
246  
247 return list of directories where resources will be searched
248  
249 =item tmp_dir()
250  
251 return temporary directory, should be used instead of /tmp or similar
252  
253 =item share_dir()
254  
255 returns the Treex shared directory (formerly C<$TMT_SHARE>)
256  
257 =item share_url()
258  
259 returns base url from shared data are downloaded
260  
261 =item pml_schema_dir()
262  
263 return the directory in which the PML schemata for .treex files are located
264  
265 =item tred_dir()
266  
267 the directory in which the tree editor TrEd is installed
268  
269 =item tred_extension_dir()
270  
271 the directory in which the TrEd extension for Treex files is stored
272  
273 =back
274  
275 =head2 Rest of methods is not configurable by config file
276  
277 =over 4
278  
279 =item config_dir()
280  
281 returns directory where configuration of Treex will reside (currently just F<path> file)
282  
283 =item default_resource_dir()
284  
285 returns default path for resources, it uses dist data for C<Treex-Core> and if $TMT_ROOT variable set also $TMT_ROOT/share
286  
287 =item _devel_version()
288  
289 returns C<true> iff the current Treex instance is running from the svn working copy
290 (which means that it is the development version, not installed from CPAN)
291  
292 =item lib_core_dir()
293  
294 returns the directory in which this module is located (and where
295 the other L<Treex::Core> modules are expected too)
296  
297 =back
298  
299 =head1 AUTHOR
300  
301 Zdeněk Žabokrtský <zabokrtsky@ufal.mff.cuni.cz>
302  
303 Tomáš Kraut <kraut@ufal.mff.cuni.cz>
304  
305 =head1 COPYRIGHT AND LICENSE
306  
307 Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague
308  
309 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
« lib/Treex/Core/Log.pm ↑ Summary lib/Treex/Core/Node/T.pm »