lib/App/DrivePlayer/Config.pm

filter: Statementstmt 95.0% 77 / 81 Branchbran 69.2% 18 / 26 Conditioncond 37.1% 13 / 35 MC/DCmcdc 10.5% 2 / 19 Subroutinesub 82.6% 19 / 23 Podpod 84.6% 11 / 13 totaltotal 71.0% 140 / 197 CC 31 SCAR 15.4
CC
31
Cov
76%
_expand_paths
9.5
_build_data
7.4
restapi_log_level
6.0
CRAP 4.7
« lib/App/DrivePlayer/DB.pm ↑ Summary lib/App/DrivePlayer/Schema.pm »
1 package App::DrivePlayer::Config;
2  
3 2 ▶use App::DrivePlayer::Setup;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use File::Basename qw( dirname );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use File::Path qw( make_path );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use YAML::XS qw( LoadFile DumpFile );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 my $DEFAULT_CONFIG_DIR = "$ENV{HOME}/.config/drive_player";
9 2 my $DEFAULT_CONFIG_FILE = "$DEFAULT_CONFIG_DIR/config.yaml";
10 2 my $DEFAULT_DATA_DIR = "$ENV{HOME}/.local/share/drive_player";
11 2 my $DEFAULT_DB_PATH = "$DEFAULT_DATA_DIR/music.db";
12 2 my $DEFAULT_LOG_FILE = "$DEFAULT_DATA_DIR/drive_player.log";
13  
14 has config_file => (
15 is => 'ro',
16 isa => Str,
17 0 ▶ default => sub { $DEFAULT_CONFIG_FILE },
Subroutine __ANON__: not called CC=1 SCAR=6.9
18 2 );
19  
20 # Internal: the parsed YAML data hash
21 2 has _data => (
22 is => 'lazy',
23 isa => HashRef,
24 builder => '_build_data',
25 );
26  
27 12 ▶sub _build_data {
Subroutine _build_data: called CC=7 SCAR=20.0
28 12 my ($self) = @_;
29 12 my $data = $self->_defaults();
30 12 ▶ if (-f $self->config_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->config_file) 5 7 12
31 5 my $file = LoadFile($self->config_file);
32 # Migrate legacy root-level auth key into google_restapi.auth
33 5 ▶ if ($file->{auth} && !($file->{google_restapi} && $file->{google_restapi}{auth})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->{"auth"} and not $file->{"google_restapi"} && $file->{"google_restapi"}{"auth"}) 3 2 5
Condition50%3/6
$file->{"auth"} and not $file->{"google_restapi"} && $file->{"google_restapi"}{"auth"}
!ll&&!rl&&r
203
$file->{'google_restapi'} && $file->{'google_restapi'}{'auth'}
!ll&&!rl&&r
300
MC/DC33%1/3
$file->{"auth"} and not $file->{"google_restapi"} && $file->{"google_restapi"}{"auth"}
$file->{"auth"} $file->{'google_restapi'} $file->{'google_restapi'}{'auth'}
Truth table50%2/4
A and not(B && C)
A= $file->{"auth"}B= $file->{'google_restapi'}C= $file->{'google_restapi'}{'auth'}
ABCresult
0XX0
1110
10X1
1101
34 3 ▶ $file->{google_restapi} //= {};
Condition50%1/2
$file->{"google_restapi"} //= {}
l!l
03
MC/DC0%0/1
$file->{"google_restapi"} //= {}
$file->{"google_restapi"}
Truth table50%1/2
A
A= $file->{"google_restapi"}
Aresult
11
00
35 3 $file->{google_restapi}{auth} = delete $file->{auth};
36 }
37 # 'class' belongs under google_restapi.auth, never as a sibling of it.
38 # A stray copy at the google_restapi level makes Google::RestApi->new
39 # reject the config ("Unrecognized parameter: class"), so drop it.
40 delete $file->{google_restapi}{class}
41 5 ▶ if ref $file->{google_restapi} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $file->{"google_restapi"} eq "HASH" 5 0 5
42 5 _merge($data, $file);
43 }
44 12 _expand_paths($data, dirname($self->config_file));
45 12 return $data;
46 }
47  
48 # Recursively merge $src over $dst (scalar/array values in $src win).
49 19 ▶sub _merge {
Subroutine _merge: called CC=4 SCAR=13.9
50 19 my ($dst, $src) = @_;
51 19 for my $key (keys %{ $src }) {
52 56 ▶ if (ref $src->{$key} eq 'HASH' && ref $dst->{$key} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $src->{$key} eq 'HASH' and ref $dst->{$key} eq 'HASH') { } 14 42 56
Condition66%2/3
ref $src->{$key} eq "HASH" and ref $dst->{$key} eq "HASH"
!ll&&!rl&&r
42014
MC/DC50%1/2
ref $src->{$key} eq "HASH" and ref $dst->{$key} eq "HASH"
ref $src->{$key} eq "HASH" ref $dst->{$key} eq "HASH"
Truth table66%2/3
A and B
A= ref $src->{$key} eq "HASH"B= ref $dst->{$key} eq "HASH"
ABresult
0X0
100
111
53 14 _merge($dst->{$key}, $src->{$key});
54 } else {
55 42 $dst->{$key} = $src->{$key};
56 }
57 }
58 }
59  
60 12 ▶sub _defaults {
Subroutine _defaults: called CC=1 SCAR=0.0
61 return {
62 12 google_restapi => {
63 auth => {
64 class => 'OAuth2Client',
65 client_id => '',
66 client_secret => '',
67 token_file => "$DEFAULT_CONFIG_DIR/token.dat",
68 scope => ['https://www.googleapis.com/auth/drive.readonly'],
69 },
70 },
71 music_folders => [],
72 database => { path => $DEFAULT_DB_PATH },
73 log_level => 'WARN',
74 restapi_log_level => 'WARN',
75 log_file => $DEFAULT_LOG_FILE,
76 acoustid_key => '',
77 sheet_id => '',
78 };
79 }
80  
81 12 ▶sub _expand_paths {
Subroutine _expand_paths: called CC=7 SCAR=22.5
82 12 my ($data, $config_dir) = @_;
83 12 for my $key (qw( log_file )) {
84 12 ▶ $data->{$key} = _abs_path($data->{$key}, $config_dir) if defined $data->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $data->{$key} 12 0 12
85 }
86 $data->{database}{path} = _abs_path($data->{database}{path}, $config_dir)
87 12 ▶ if defined $data->{database}{path};
Branch: 1 branch
BranchTrueFalseTotal
if defined $data->{"database"}{"path"} 12 0 12
88  
89 # Support auth under google_restapi.auth (preferred) or legacy root auth key
90 12 ▶ my $auth = $data->{google_restapi}{auth} // $data->{auth};
Condition33%1/3
$data->{"google_restapi"}{"auth"} // $data->{"auth"}
l!l&&r!l&&!r
1200
MC/DC0%0/2
$data->{"google_restapi"}{"auth"} // $data->{"auth"}
$data->{"google_restapi"}{"auth"} $data->{"auth"}
Truth table33%1/3
A // B
A= $data->{"google_restapi"}{"auth"}B= $data->{"auth"}
ABresult
1X1
011
000
91 12 ▶ if ($auth && defined $auth->{token_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth and defined $auth->{"token_file"}) 12 0 12
Condition33%1/3
$auth and defined $auth->{"token_file"}
!ll&&!rl&&r
0012
MC/DC0%0/2
$auth and defined $auth->{"token_file"}
$auth defined $auth->{"token_file"}
Truth table33%1/3
A and B
A= $authB= defined $auth->{"token_file"}
ABresult
0X0
100
111
92 12 $auth->{token_file} = _abs_path($auth->{token_file}, $config_dir);
93 }
94 }
95  
96 36 ▶sub _abs_path {
Subroutine _abs_path: called CC=2 SCAR=6.9
97 36 my ($path, $config_dir) = @_;
98 36 $path =~ s|^~|$ENV{HOME}|;
99 36 ▶ return File::Spec->rel2abs($path, $config_dir) unless File::Spec->file_name_is_absolute($path);
Branch: 1 branch
BranchTrueFalseTotal
unless "File::Spec"->file_name_is_absolute($path) 1 35 36
100 35 return $path;
101 }
102  
103 2 ▶sub save {
Subroutine save: called CC=2 SCAR=7.0
Pod documented
104 2 my ($self) = @_;
105 2 my $dir = dirname($self->config_file);
106 2 ▶ make_path($dir) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 2 2
107 2 DumpFile($self->config_file, $self->_data);
108 }
109  
110 1 ▶sub ensure_dirs {
Subroutine ensure_dirs: called CC=5 SCAR=17.8
Pod documented
111 1 my ($self) = @_;
112 1 for my $path ($self->db_path, $self->log_file, $self->token_file) {
113 3 ▶ next unless defined $path && $path ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and $path ne "" 0 3 3
Condition33%1/3
defined $path and $path ne ""
!ll&&!rl&&r
003
MC/DC0%0/2
defined $path and $path ne ""
defined $path $path ne ""
Truth table33%1/3
A and B
A= defined $pathB= $path ne ""
ABresult
0X0
100
111
114 3 my $dir = dirname($path);
115 3 ▶ make_path($dir) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 3 0 3
116 }
117 }
118  
119 # Auth config hashref suitable for Google::RestApi->new(auth => ...)
120 # Prefers google_restapi.auth; falls back to legacy root-level auth key.
121 8 ▶sub auth_config {
Subroutine auth_config: called CC=3 SCAR=13.1
Pod documented
122 8 my ($self) = @_;
123 8 ▶ return $self->_data->{google_restapi}{auth} // $self->_data->{auth} // {};
Condition40%2/5
($self->_data->{"google_restapi"}{"auth"} // $self->_data->{"auth"}) // {}
l!l
80
$self->_data->{"google_restapi"}{"auth"} // $self->_data->{"auth"}
l!l&&r!l&&!r
800
MC/DC0%0/2
($self->_data->{"google_restapi"}{"auth"} // $self->_data->{"auth"}) // {}
$self->_data->{"google_restapi"}{"auth"} $self->_data->{"auth"}
Truth table33%1/3
A // B
A= $self->_data->{"google_restapi"}{"auth"}B= $self->_data->{"auth"}
ABresult
1X1
011
000
124 }
125  
126 # Music folders: arrayref of { id => '...', name => '...' }
127 12 ▶sub music_folders {
Subroutine music_folders: called CC=3 SCAR=11.1
Pod documented
128 12 my ($self, $folders) = @_;
129 12 ▶ $self->_data->{music_folders} = $folders if defined $folders;
Branch: 1 branch
BranchTrueFalseTotal
if defined $folders 1 11 12
130 12 ▶ return $self->_data->{music_folders} // [];
Condition50%1/2
$self->_data->{"music_folders"} // []
l!l
120
MC/DC0%0/1
$self->_data->{"music_folders"} // []
$self->_data->{"music_folders"}
Truth table50%1/2
A
A= $self->_data->{"music_folders"}
Aresult
11
00
131 }
132  
133 6 ▶sub add_music_folder {
Subroutine add_music_folder: called CC=2 SCAR=7.0
Pod documented
134 6 my ($self, $id, $name) = @_;
135 6 ▶ return if grep { $_->{id} eq $id } @{ $self->_data->{music_folders} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_->{"id"} eq $id;} @{$self->_data->{"music_folders"};} 0 6 6
136 6 push @{ $self->_data->{music_folders} }, { id => $id, name => $name };
137 }
138  
139 1 ▶sub remove_music_folder {
Subroutine remove_music_folder: called CC=1 SCAR=0.0
Pod documented
140 1 my ($self, $id) = @_;
141 $self->_data->{music_folders} = [
142 1 grep { $_->{id} ne $id } @{ $self->_data->{music_folders} }
143 ];
144 }
145  
146 4 ▶sub db_path { $_[0]->_data->{database}{path} }
Subroutine db_path: called CC=1 SCAR=0.0
Pod documented
147 3 ▶sub log_level { $_[0]->_data->{log_level} // 'WARN' }
Subroutine log_level: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
$_[0]->_data->{"log_level"} // "WARN"
l!l
30
MC/DC0%0/1
$_[0]->_data->{"log_level"} // "WARN"
$_[0]->_data->{"log_level"}
Truth table50%1/2
A
A= $_[0]->_data->{"log_level"}
Aresult
11
00
148 0 ▶sub restapi_log_level { $_[0]->_data->{restapi_log_level} // 'WARN' }
Subroutine restapi_log_level: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$_[0]->_data->{"restapi_log_level"} // "WARN"
l!l
00
MC/DC0%0/1
$_[0]->_data->{"restapi_log_level"} // "WARN"
$_[0]->_data->{"restapi_log_level"}
Truth table0%0/2
A
A= $_[0]->_data->{"restapi_log_level"}
Aresult
11
00
149 3 ▶sub log_file { $_[0]->_data->{log_file} }
Subroutine log_file: called CC=1 SCAR=0.0
Pod documented
150 3 ▶sub token_file { $_[0]->auth_config->{token_file} }
Subroutine token_file: called CC=1 SCAR=0.0
Pod documented
151 0 ▶sub acoustid_key { $_[0]->_data->{acoustid_key} // '' }
Subroutine acoustid_key: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/2
$_[0]->_data->{"acoustid_key"} // ""
l!l
00
MC/DC0%0/1
$_[0]->_data->{"acoustid_key"} // ""
$_[0]->_data->{"acoustid_key"}
Truth table0%0/2
A
A= $_[0]->_data->{"acoustid_key"}
Aresult
11
00
152 0 ▶sub sheet_id { $_[0]->_data->{sheet_id} // '' }
Subroutine sheet_id: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/2
$_[0]->_data->{"sheet_id"} // ""
l!l
00
MC/DC0%0/1
$_[0]->_data->{"sheet_id"} // ""
$_[0]->_data->{"sheet_id"}
Truth table0%0/2
A
A= $_[0]->_data->{"sheet_id"}
Aresult
11
00
153  
154 2 1;
155  
156 __END__
« lib/App/DrivePlayer/DB.pm ↑ Summary lib/App/DrivePlayer/Schema.pm »