File Coverage

blib/lib/Treex/Core/Config.pm
Criterion Covered Total %
statement 61 132 46.2
branch 8 48 16.6
condition 5 33 15.1
subroutine 18 30 60.0
pod 9 12 75.0
total 101 255 39.6


line stmt bran cond sub pod time code
1             package Treex::Core::Config;
2             $Treex::Core::Config::VERSION = '2.20150928';
3 29     29   20567 use strict;
  29         48  
  29         775  
4 29     29   206 use warnings;
  29         54  
  29         702  
5              
6 29     29   1007 use 5.010; # operator //
  29         90  
7 29     29   30552 use File::HomeDir 0.97;
  29         177302  
  29         1718  
8 29     29   20710 use File::ShareDir;
  29         169423  
  29         1774  
9 29     29   204 use File::Spec;
  29         60  
  29         723  
10 29     29   23989 use File::Slurp 9999; # prior versions had different interface
  29         379707  
  29         2364  
11 29     29   248 use Cwd qw(realpath);
  29         62  
  29         1216  
12 29     29   718 use Treex::Core::Log;
  29         62  
  29         2065  
13 29     29   17137 use Treex::Core::Loader 'load_module';
  29         78  
  29         1767  
14 29     29   19740 use YAML 0.72 qw(LoadFile DumpFile);
  29         211692  
  29         41927  
15              
16             # this should be somehow systematized, since there will be probably many switches like this one
17             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             our $params_validate = 0; ## no critic (ProhibitPackageVars)
23              
24             my $config = __PACKAGE__->_load_config();
25             my $dirty = 0; #indicates that configuration has changed. N/A yet, no method changes config to something else than default
26             my $running_in_tred; ## no critic (ProhibitUnusedVariables)
27              
28             sub _load_config {
29 29     29   63 my $self = shift;
30 29         93 my %args = @_;
31 29   33     217 my $from = $args{from} // $self->config_file(); #/
32 29 50       436 return {} if !-e $from;
33 0         0 my $yaml = read_file( $from, { err_mode => 'quiet' } );
34 0         0 my $toReturn = YAML::Load($yaml);
35 0   0     0 return $toReturn // {}; #rather than undef return empty hashref
36             }
37              
38             sub _save_config {
39 29     29   101 my $self = shift;
40 29         99 my %args = @_;
41 29   33     323 my $to = $args{to} // $self->config_file(); #/
42 29 50 33     491 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
43 29 50       78 return if ( !scalar %{$config} ); #skip when config is empty
  29         256  
44 0 0       0 eval {
45 0         0 DumpFile( $to, $config );
46 0         0 1;
47             } or log_warn(qq(Couldn't save config file $to));
48 0         0 return;
49             }
50              
51             END {
52 29     29   74492 __PACKAGE__->_save_config();
53             }
54              
55             sub config_dir {
56 58     58 1 118 my $self = shift;
57 58   33     737 my $dirname = $ENV{TREEX_CONFIG} // File::Spec->catdir( File::HomeDir->my_home(), '.treex' ); #/ # if evironment variable not set defaults to ~/.treex
58 58 50       4607 if ( !-e $dirname ) {
59 0         0 mkdir $dirname;
60             }
61 58 50       815 if ( -d $dirname ) {
62 58         760 return $dirname;
63             }
64             else {
65 0         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 58     58 0 137 my $self = shift;
71 58         219 return File::Spec->catfile( $self->config_dir(), 'config.yaml' );
72             }
73              
74             sub _default_resource_path {
75 0     0   0 my $self = shift;
76 0         0 my @path;
77 0         0 push @path, File::Spec->catdir( $self->config_dir(), 'share' );
78 0         0 push @path, File::HomeDir->my_dist_data( 'Treex-Core', { create => 0 } );
79 0 0       0 if ( defined $ENV{TMT_ROOT} ) {
80 0         0 push @path, File::Spec->catdir( $ENV{TMT_ROOT}, 'share' );
81             }
82 0 0       0 return @path if wantarray;
83 0         0 return \@path;
84             }
85              
86             sub resource_path {
87 0     0 1 0 my $self = shift;
88 0         0 my @path;
89 0 0       0 if ( defined $config->{resource_path} ) {
90 0         0 @path = @{ $config->{resource_path} };
  0         0  
91             }
92             else {
93 0         0 @path = $self->_default_resource_path();
94 0         0 $config->{resource_path} = \@path;
95             }
96 0 0       0 return @path if wantarray;
97 0         0 return \@path;
98             }
99              
100             sub _devel_version {
101 0     0   0 my $self = shift;
102 0         0 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     0 1 0 my $self = shift;
109 0 0 0     0 if ( defined $config->{share_dir} && defined realpath( $config->{share_dir} ) ) {
110 0         0 return $config->{share_dir};
111             }
112             else {
113 0         0 delete $config->{share_dir};
114 0         0 my $share_dir;
115              
116             # return File::HomeDir->my_home."/.treex/share"; # future solution, probably symlink
117 0 0       0 if ( $self->_devel_version() ) {
118 0         0 $share_dir = realpath( $self->lib_core_dir() . "/../../../../share/" ); # default on UFAL machines
119             }
120             else {
121 0         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         0 return $share_dir;
126              
127             }
128             }
129              
130             sub share_url {
131 0     0 1 0 my $self = shift;
132 0 0       0 if ( !defined $config->{share_url} ) {
133 0         0 $config->{share_url} = 'http://ufallab.ms.mff.cuni.cz/tectomt/share';
134             }
135 0         0 return $config->{share_url};
136             }
137              
138             sub tred_dir {
139 0     0 1 0 my $self = shift;
140 0 0 0     0 if ( !defined $config->{tred_dir} || !defined realpath( $config->{tred_dir} ) ) {
141 0         0 delete $config->{tred_dir};
142 0         0 return realpath( File::Spec->catdir( $self->share_dir(), 'tred' ) );
143             }
144 0         0 return $config->{tred_dir};
145             }
146              
147             sub pml_schema_dir {
148 0     0 1 0 my $self = shift;
149 0 0 0     0 if ( !defined $config->{pml_schema_dir} || !defined realpath( $config->{pml_schema_dir} ) ) {
150 0         0 delete $config->{pml_schema_dir};
151 0 0       0 if ( $self->_devel_version() ) {
152              
153             #$config->{pml_schema_dir} = realpath( $self->lib_core_dir() . "/share/tred_extension/treex/resources/" );
154 0         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 0         0 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         0 return $config->{pml_schema_dir};
163             }
164              
165             sub tred_extension_dir {
166 0     0 1 0 my $self = shift;
167 0 0 0     0 if ( !defined $config->{tred_extension_dir} || !defined realpath( $config->{tred_extension_dir} ) ) {
168 0         0 delete $config->{tred_extension_dir};
169 0         0 return realpath( File::Spec->catdir( $self->pml_schema_dir(), q(..), q(..) ) );
170             }
171 0         0 return $config->{tred_extension_dir};
172             }
173              
174             sub lib_core_dir {
175 0     0 1 0 my $self = shift;
176 0         0 return realpath( $self->_caller_dir() );
177             }
178              
179             sub tmp_dir {
180 1     1 1 15 my $self = shift;
181 1 50 33     6 if ( !defined $config->{tmp_dir} || !defined realpath( $config->{tmp_dir} ) ) {
182 1         2 delete $config->{tmp_dir};
183 1         5 return $self->_default_tmp_dir();
184             }
185 0         0 return $config->{tmp_dir};
186             }
187              
188             sub treex_server_url {
189             return exists $ENV{TREEX_SERVER_URL} ?
190 0 0 0 0 0 0 $ENV{TREEX_SERVER_URL} : $config->{treex_server_url} || 'tcp://127.0.0.1:7338';
191             }
192              
193             sub use_services {
194 0 0   0 0 0 return $ENV{USE_SERVICES} if defined $ENV{USE_SERVICES};
195              
196 0 0       0 if ($config->{use_services}) {
197 0 0       0 $config->{use_services} = 0 unless load_module('Treex::Service::Role');
198             }
199              
200 0   0     0 return $config->{use_services} || 0;
201             }
202              
203             sub _default_tmp_dir {
204 1     1   2 my $self = shift;
205 1         14 my $dot_treex = File::HomeDir->my_dist_data( 'Treex-Core', { create => 1 } );
206 1         597 my $suffix = 'tmp';
207 1         7 my $tmp_dir = File::Spec->catdir( $dot_treex, $suffix );
208 1 50       21 if ( !-e $tmp_dir ) {
209 1 50       49 mkdir $tmp_dir or log_fatal("Cannot create temporary directory");
210             }
211 1         5 return $tmp_dir;
212             }
213              
214             sub _caller_dir {
215 0     0     my $self = shift;
216 0           my %call_info;
217             @call_info{
218 0           qw(pack file line sub has_args wantarray evaltext is_require)
219             } = caller(0);
220 0           $call_info{file} =~ s/[^\/]+$//;
221 0           return $call_info{file};
222             }
223              
224             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.20150928
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.