Treex/Core/Config.pm

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