File: Config.pm

package info (click to toggle)
libhipi-perl 0.93-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 20,048 kB
  • sloc: perl: 471,917; ansic: 22; makefile: 10
file content (146 lines) | stat: -rw-r--r-- 3,908 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#########################################################################################
# Package        HiPi::Utils::Config
# Description  : Config File Wrapper
# Copyright    : Copyright (c) 2017 Mark Dootson
# License      : This is free software; you can redistribute it and/or modify it under
#                the same terms as the Perl 5 programming language system itself.
#########################################################################################

package HiPi::Utils::Config;

#########################################################################################

use strict;
use warnings;
use parent qw( HiPi::Class );
use File::Path ( );

use JSON;
use Try::Tiny;
use Storable;
use Carp;

__PACKAGE__->create_ro_accessors( qw( configclass filepath default ) );
__PACKAGE__->create_accessors( qw( config _configkey ) );

our $VERSION ='0.81';

sub new {
    my( $class, %userparams ) = @_;
    
    my %params = (
        configclass => 'hipi',
        default    => {},
    );
    
    # get user params
    foreach my $key( keys (%userparams) ) {
        $params{$key} = $userparams{$key};
    }
    
    $params{'_configkey'} = '';
    
    $params{default}->{'hipi-config-version'} = $VERSION;
        
    my $fileroot = ( $> ) ? qq($ENV{HOME}/.hipi-perl) : '/etc/hipi-perl';
    my $filename = ( $> ) ? 'user.conf' : 'global.conf';
    
    my $dirpath = qq($fileroot/$params{configclass});
    
    File::Path::make_path($dirpath , { mode => 0700 } ) unless( -d $dirpath );
    
    $params{filepath} = $dirpath . '/' . $filename;
    
    my $self = $class->SUPER::new( %params );
    
    if( -f $self->filepath ) {
        $self->read_config;
        # update any new defaults 
        my $conf = $self->config;
        my $updatedefaults = 0;
        for my $itemname ( keys %{ $params{default} } ) {
            if( !exists( $conf->{$itemname} ) || !defined($conf->{$itemname}) ) {
                $conf->{$itemname} = $params{default}->{$itemname};
                $updatedefaults = 1;
            }
        }
        $self->write_config if $updatedefaults;
        
    } else {
        $self->config( $self->default );
        $self->write_config;
    }
    
    return $self;
}

sub read_config {
    my $self = shift;
    open ( my $fh, '<:encoding(UTF-8)',  $self->filepath ) or croak( qq(failed to open config file : $!) );
    read( $fh, my $input, -s $fh);
    close( $fh );
    my $json = JSON->new;
    my $conf = try {
        my $decoded = $json->decode( $input );
        return $decoded;
    } catch {
        carp q(failed to decode configuration ) . $_;
        return { config_ok => 0 };
    };
    
    $Storable::canonical = 1;
    my $ckey = Storable::nfreeze( $conf );
    $Storable::canonical = 0;
    $self->_configkey( $ckey );
    $self->config( $conf );
    return 1;
}

sub write_config {
    my $self = shift;
    
    $Storable::canonical = 1;
        
    my $ckey = Storable::nfreeze( $self->config );
    $Storable::canonical = 0;
    if($ckey eq $self->_configkey) {
        # no need to write an unchanged config
        return 1;
    }
    
    $self->config->{epoch} = time();
    $ckey = Storable::nfreeze( $self->config );
    $self->_configkey( $ckey );
    
    open ( my $fh, '>:encoding(UTF-8)',  $self->filepath ) or croak( qq(failed to open config file : $!) );
    my $json = JSON->new;
    my $output = try {
        my $encoded = $json->pretty->canonical->encode( $self->config );
        return $encoded;
    } catch {
        carp q(failed to encode configuration ) . $_;
        return '';
    };
    if( $output ) {
        print $fh $output;
    }
    close( $fh );
    return 1;
}

sub DESTROY {
    # don't call super
    my $self = shift;
    if( $threads::threads ) {
        if( threads->tid == 0 )  {
            $self->write_config;
        }
    } else {
       $self->write_config;
    }
}


1;

__END__