kateb/LocalData.pm untested

filter: Statementstmt 0.0% 0 / 85 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 154 CC 8 SCAR 33.1
CC
8
Cov
0%
_prepare
72.0
new
2.0
write_data
2.0
CRAP 27.5
1 package kateb::LocalData;
2 $kateb::LocalData::VERSION = '1.2.1';
3  
4 use strict;
5 use warnings;
6 use 5.012;
7  
8 use kateb::FontInfo;
9 use JSON::PP;
10 use File::Temp;
11 use File::Path qw(make_path rmtree);
12 use File::Spec;
13 use File::Spec::Functions qw(catdir catfile tmpdir);
14  
15 sub new {
16 my $class = shift;
17 my $self = _prepare();
18 bless $self, $class;
19  
20 return $self;
21 }
22  
23 sub _prepare {
24 my $execname = "kateb";
25  
26 #----------------------- GLOBAL VARIABLES -----------------------#
27 # create empty envs
28 my $json_file;
29  
30 # Home directory
31 my $home_dir = $ENV{HOME} || $ENV{LOGDIR} || (getpwuid $<)[7] || `echo -n ~`;
32  
33 # Configuration directory
34 my $config_dir = catdir($ENV{XDG_CONFIG_HOME} || catdir($home_dir, '.config'), $execname);
35  
36 # download fonts directory
37 my $cache_font_dir = catdir($config_dir, 'fonts');
38  
39 # user font directory [target directory]
40 my $root_font_dir = $^O eq 'darwin'
41 ? catdir('/', 'Library', 'Fonts')
42 : catdir('/', 'usr', 'share', 'fonts', 'truetype', 'farsifreefont');
43 my $user_font_dir = $^O eq 'darwin'
44 ? catdir($home_dir, 'Library', 'Fonts')
45 : catdir($home_dir, '.local', 'share', 'fonts', 'farsifreefont');
46 my $target_dir = $> == 0 ? $root_font_dir : $user_font_dir;
47  
48 # temp dir for downloads
49 my $temp_dir = File::Temp->newdir();
50 $temp_dir = catdir($temp_dir, 'kateb');
51 make_path $temp_dir;
52  
53 # Configuration file
54 # my $config_file = catfile($config_dir, "$execname.conf");
55  
56 # json Database file
57 my $json_data_file = catfile($config_dir, "$execname.json");
58  
59 # check for exist config dir
60 if (not -d $config_dir)
61 {
62 make_path($config_dir)
63 or note(":: Unable to create directory <<$config_dir>>: {!$!!}");
64 }
65  
66 # check for exist taget directory
67 if (not -d $target_dir)
68 {
69 make_path($target_dir)
70 or note(":: Unable to create directory <<$target_dir>>: {!$!!}");
71 }
72  
73 # check json data base file
74 my $installed_versions;
75  
76 if (not -e $json_data_file)
77 {
78 _reset_json_file($json_data_file);
79 $json_file = _read_file($json_data_file);
80 $installed_versions = decode_json($json_file);
81 } else
82 {
83 $json_file = _read_file($json_data_file);
84 eval
85 {
86 $installed_versions = decode_json($json_file)
87 }; if ($@)
88 {
89 _reset_json_file($json_data_file);
90 $json_file = _read_file($json_file);
91 $installed_versions = decode_json($json_file);
92 }
93 }
94 return
95 {
96 homeDir => $home_dir,
97 configDir => $config_dir,
98 jsonFile => $json_data_file,
99 cacheDir => $cache_font_dir,
100 targetDir => $target_dir,
101 tempDir => $temp_dir,
102 installedVersions => $installed_versions,
103 };
104 }
105  
106 sub write_data {
107 shift; #$self
108 my $local_data = shift;
109 my $json_file = shift;
110 my $installed_versions = encode_json($local_data);
111 _write_file($json_file, $installed_versions);
112 }
113  
114 sub _read_file {
115 my $entry = shift;
116 open my $fh, "<:encoding(UTF-8)", $entry or die $!;
117 local $/ = undef;
118 my $content = <$fh>;
119 close $fh;
120 return $content;
121 }
122  
123 sub _write_file {
124 my $file = shift;
125 my $json = shift;
126 open my $fh, '>:encoding(UTF-8)', $file || die;
127 print $fh $json;
128 close $fh;
129 }
130  
131 sub _reset_json_file {
132 my $file = shift;
133 my $info = kateb::FontInfo->new;
134 my %hash = ();
135 foreach my $font_name (keys %{$info})
136 {
137 $hash{$font_name} = '';
138 }
139  
140 my $json = encode_json( \%hash );
141 _write_file($file, $json);
142 }
143  
144 1;