| 1 | |||||||||||||||||||||||||||||
| 2 | # config file parser and data access | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package App::GUI::Juliagraph::Config; | ||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use v5.12; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use File::HomeDir; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use App::GUI::Juliagraph::Config::Default; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | my $file_name = File::Spec->catfile( File::HomeDir->my_home, '.config', 'juliagraph'); | |||||||||||||||||||||||||||
| 12 | 1 | my $dir = ''; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 15 | 0 | my ($pkg) = @_; | |||||||||||||||||||||||||||
| 16 | 0 | ▶ | my $data = -r $file_name | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | ? load( $pkg, $file_name ) | ||||||||||||||||||||||||||||
| 18 | : $ App::GUI::Juliagraph::Config::Default::data; | ||||||||||||||||||||||||||||
| 19 | 0 | bless { path => $file_name, data => $data }; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | sub load { | ||||||||||||||||||||||||||
|
Subroutine
load: not called CC=12 SCAR=50.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | 0 | my ($self, $file) = @_; | |||||||||||||||||||||||||||
| 24 | 0 | my $data = {}; | |||||||||||||||||||||||||||
| 25 | 0 | ▶ | open my $FH, '<', $file or return "could not read $file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 0 | my $category = ''; | |||||||||||||||||||||||||||
| 27 | 0 | ▶ | while (<$FH>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 0 | chomp; | |||||||||||||||||||||||||||
| 29 | 0 | ▶ | next unless $_ or substr( $_, 0, 1) eq '#'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$_ or substr($_, 0, 1) eq "#"
MC/DC0%0/2
$_ or substr($_, 0, 1) eq "#"
$_
substr($_, 0, 1) eq "#"
Truth table0%0/3
A or B
A= $_B= substr($_, 0, 1) eq "#"
| |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | if (/^\s*(\w+):\s*$/) { $category = $1; $data->{$category} = []; } | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 31 | 0 | elsif (/^\s+-\s+(.+)\s*$/) { push @{$data->{$category}}, $1; } | |||||||||||||||||||||||||||
| 32 | elsif (/^\s+\+\s+(\w+)\s*=\s*\[\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\]/) | ||||||||||||||||||||||||||||
| 33 | 0 | ▶ | { $data->{$category} = {} if ref $data->{$category} ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 0 | $data->{$category}{$1} = [$2, $3, $4]; } | |||||||||||||||||||||||||||
| 35 | elsif (/^\s+\+\s+(\w+)\s*=\s*\[\s*(.+)\s*\]/) | ||||||||||||||||||||||||||||
| 36 | 0 | ▶ | { $data->{$category} = {} if ref $data->{$category} ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | $data->{$category}{$1} = [map {tr/ //d; $_} split /,/, $2] } | |||||||||||||||||||||||||||
| 38 | 0 | elsif (/\s*(\w+)\s*=\s*(.+)\s*$/) { $data->{$1} = $2; $category = '';} | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 0 | close $FH; | |||||||||||||||||||||||||||
| 41 | 0 | $data; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub save { | ||||||||||||||||||||||||||
|
Subroutine
save: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 46 | 0 | my $data = $self->{'data'}; | |||||||||||||||||||||||||||
| 47 | 0 | my $file = $self->{'path'}; | |||||||||||||||||||||||||||
| 48 | 0 | ▶ | open my $FH, '>', $file or return "could not write $file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | $" = ','; | |||||||||||||||||||||||||||
| 50 | 0 | for my $key (sort keys %$data){ | |||||||||||||||||||||||||||
| 51 | 0 | my $val = $data->{ $key }; | |||||||||||||||||||||||||||
| 52 | 0 | ▶ | if (ref $val eq 'ARRAY'){ | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 53 | 0 | say $FH "$key:"; | |||||||||||||||||||||||||||
| 54 | 0 | say $FH " - $_" for @$val; | |||||||||||||||||||||||||||
| 55 | } elsif (ref $val eq 'HASH'){ | ||||||||||||||||||||||||||||
| 56 | 0 | say $FH "$key:"; | |||||||||||||||||||||||||||
| 57 | 0 | say $FH " + $_ = [ @{$val->{$_}} ]" for sort keys %$val; | |||||||||||||||||||||||||||
| 58 | } elsif (not ref $val){ | ||||||||||||||||||||||||||||
| 59 | 0 | say $FH "$key = $val"; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 0 | close $FH; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | sub get_value { | ||||||||||||||||||||||||||
|
Subroutine
get_value: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | 0 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 68 | 0 | ▶ | $self->{'data'}{$key} if exists $self->{'data'}{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub set_value { | ||||||||||||||||||||||||||
|
Subroutine
set_value: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 72 | 0 | my ($self, $key, $value) = @_; | |||||||||||||||||||||||||||
| 73 | 0 | $self->{'data'}{$key} = $value; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub add_setting_file { | ||||||||||||||||||||||||||
|
Subroutine
add_setting_file: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 77 | 0 | my ($self, $file) = @_; | |||||||||||||||||||||||||||
| 78 | 0 | $file = App::GUI::Juliagraph::Settings::shrink_path( $file ); | |||||||||||||||||||||||||||
| 79 | 0 | ▶ | for my $f (@{$self->{'data'}{'last_settings'}}) { return if $f eq $file } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | push @{$self->{'data'}{'last_settings'}}, $file; | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | shift @{$self->{'data'}{'last_settings'}} if @{$self->{'data'}{'last_settings'}} > 15; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 1 | 1; | |||||||||||||||||||||||||||