File Coverage

lib/Kephra/Config/File.pm
Criterion Covered Total %
statement 6 58 10.3
branch 0 36 0.0
condition 0 8 0.0
subroutine 2 12 16.6
pod 0 9 0.0
total 8 123 6.5


line stmt bran cond sub pod time code
1             package Kephra::Config::File;
2             our $VERSION = '0.13';
3            
4 1     1   1036 use strict;
  1         3  
  1         36  
5 1     1   6 use warnings;
  1         1  
  1         907  
6             #
7             # internal
8             #
9             sub _get_type {
10 0     0     my $name = shift;
11 0 0         if (not defined $name) {
12             #$main::logger->error("
13 0           return;
14             }
15 0 0         return unless $name;
16 0 0         return 'conf' if $name =~ /\.conf$/;
17 0 0         return 'conf' if $name =~ /\.cfg$/;
18 0 0         return 'yaml' if $name =~ /\.yaml$/;
19 0 0         return 'yaml' if $name =~ /\.yml$/;
20            
21 0           return;
22             # TODO: log or throw exception if no or invalid file given
23            
24             # make the extension checking stricter?
25             # accept only .yml .yaml and .conf extension?
26             }
27            
28             #
29             # API 2 App
30             #
31             sub load_from_node_data {
32 0     0 0   my $node = shift;
33 0 0 0       return unless defined $node->{file} and $node->{node};
34 0           load_node( Kephra::Config::filepath( $node->{file} ), $node->{node} );
35             }
36            
37            
38             sub load_node{
39 0     0 0   my $file_name = shift;
40 0           my $start_node = shift;
41 0           my $config_tree = load($file_name);
42 0 0         return defined $start_node
43             ? Kephra::Config::Tree::get_subtree( $config_tree, $start_node )
44             : $config_tree;
45             }
46            
47            
48             # !!! -NI
49             sub store_node{
50 0     0 0   my $file_name = shift;
51 0           my $start_node = shift;
52             }
53            
54            
55             sub load {
56 0     0 0   my $file_name = shift;
57 0 0         return unless -e $file_name;
58 0           my $type = _get_type($file_name);
59 0 0         return unless $type;
60 0 0         if ($type eq 'conf') { load_conf($file_name) }
  0 0          
61 0           elsif ($type eq 'yaml') { load_yaml($file_name) }
62             }
63            
64            
65             sub store {
66 0     0 0   my $file_name = shift;
67 0           my $config = shift;
68            
69             # if want to write into nonexisting dir, create it
70 0 0         unless (-w $file_name){
71 0           my ($volume,$dir,$file) = File::Spec->splitpath( $file_name );
72 0           $dir = File::Spec->catdir( $volume, $dir );
73 0 0         mkdir $dir unless -e $dir;
74             }
75            
76 0           my $type = _get_type($file_name);
77 0 0         if ($type eq 'conf') { store_conf($file_name, $config) }
  0 0          
78 0           elsif ($type eq 'yaml') { store_yaml($file_name, $config) }
79             }
80             #
81             # API 2 YAML
82             #
83 0     0 0   sub load_yaml { &YAML::Tiny::LoadFile
84             #if ($^O=~/(?:linux|darwin)/i) {
85             #YAML::Tiny::Load( Kephra::File::IO::lin_load_file($_[0]) )
86             #} else { }
87             }
88 0     0 0   sub store_yaml { &YAML::Tiny::DumpFile }
89             #
90             # API 2 General::Config
91             #
92            
93             sub load_conf {
94 0     0 0   my $configfilename = shift;
95 0   0       my $utf = shift || 0;
96 0           my %config;
97 0           my $error_msg = Kephra::Config::Localisation::strings()->{dialog}{error};
98 0           my %opt = (
99             -AutoTrue => 1,
100             -UseApacheInclude => 1,
101             -IncludeRelative => 1,
102             -InterPolateVars => 0,
103             -AllowMultiOptions => 1,
104             -MergeDuplicateOptions => 0,
105             -MergeDuplicateBlocks => 0,
106             -SplitPolicy => 'equalsign',
107             -SaveSorted => 1,
108             -UTF8 => $utf,
109             );
110 0           $opt{'-ConfigFile'} = $configfilename;
111             #if ($^O=~/(?:linux|darwin)/i) {$opt{'-String'} = Kephra::File::IO::lin_load_file($configfilename);}else {}
112 0           $Kephra::app{config}{parser}{conf} = Config::General->new(%opt);
113 0 0         if ( -e $configfilename ) {
114 0           eval { %config = $Kephra::app{config}{parser}{conf}->getall };
  0            
115 0 0 0       Kephra::Dialog::warning_box
116             ("$configfilename: \n $@", $error_msg->{config_read})
117             if $@ or !%config;
118             } else {
119 0           Kephra::Dialog::warning_box
120             ($error_msg->{config_read}."-".$configfilename, $error_msg->{file});
121             }
122 0           \%config;
123             }
124            
125             sub store_conf {
126 0     0 0   my ( $configfilename, $config ) = @_;
127 0           $Kephra::app{config}{parser}{conf}->save_file( $configfilename, $config );
128             }
129            
130             1;
131            
132             =head1 NAME
133            
134             Kephra::Config::File - IO of config files
135            
136             =head1 DESCRIPTION
137            
138             =cut