Pimpd.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 117 CC 12 SCAR 31.7
CC
12
Cov
0%
config_init
56.0
mpd_init
20.0
player_cmdline
12.0
CRAP 23.8
1 package App::Pimpd;
2 use strict;
3 #use encoding 'utf8';
4 use open qw(:utf8 :std);
5  
6 BEGIN {
7 use Exporter;
8 use vars qw($VERSION @ISA @EXPORT);
9  
10 $VERSION = '0.310';
11 @ISA = qw(Exporter);
12 @EXPORT = qw(
13 @c
14 $mpd
15 %config
16  
17 &get_color_support
18 &player_cmdline
19 &abs_playlist_path
20 );
21 }
22  
23 use Audio::MPD;
24 use Config::General;
25  
26 our ($mpd, %config, @c);
27  
28 # Load the configuration file and fill %config with keys and values
29 config_init();
30 # Initialize the connection to the MPD server
31 mpd_init();
32  
33 #get_color_support();
34  
35 sub player_cmdline {
36 if(exists($config{player})) {
37 if(!exists($config{player_stream})) {
38 #chomp($config{player_stream} = <STDIN>);
39 print STDERR "No remote MPD adress specified in pimpd.conf. Exiting...\n";
40 return 1;
41 }
42 return "$config{player} $config{player_stream} $config{player_opts}";
43 }
44 return;
45 }
46  
47 #sub get_color_support {
48 # if( not($c_extended_colors) and not($c_ansi_colors) ) {
49 # # Clear the color array.
50 # @c = ();
51 # return 0;
52 # }
53 #}
54  
55 sub mpd_init {
56 my($host, $port, $password) = @_;
57 if(defined($host)) {
58 $mpd = Audio::MPD->new(
59 host => $host,
60 port => $port,
61 password => $password,
62 );
63 return;
64 }
65  
66 if( (exists($config{mpd_host})) or (exists($config{mpd_port})) ) {
67 if( (exists($config{mpd_pass}))
68 and ($config{mpd_pass} ne '')
69 and ($config{mpd_pass} ne "''")
70 and ($config{mpd_pass} ne '""')
71 ) {
72 $mpd = Audio::MPD->new(
73 host => $config{mpd_host},
74 port => $config{mpd_port},
75 password => $config{mpd_pass},
76 );
77 }
78 else {
79 $mpd = Audio::MPD->new(
80 host => $config{mpd_host},
81 port => $config{mpd_port},
82 );
83 }
84 }
85 }
86  
87 sub abs_playlist_path {
88 my $list = shift;
89 return $config{playlist_directory} . "/$list.m3u";
90 #unless(!App::Pimpd::Validate::isa_valid_playlist($list));
91 }
92  
93 sub config_init {
94 my $config;
95 if(-e "$ENV{XDG_CONFIG_HOME}/pimpd2/pimpd2.conf") {
96 $config = "$ENV{XDG_CONFIG_HOME}/pimpd2/pimpd2.conf";
97 }
98 elsif(-e "$ENV{HOME}/.config/pimpd2/pimpd2.conf") {
99 $config = "$ENV{HOME}/.config/pimpd2/pimpd2.conf";
100 }
101 elsif(-e "$ENV{HOME}/.pimpd2.conf") {
102 $config = "$ENV{HOME}/.pimpd2.conf";
103 }
104 elsif(-e "$ENV{HOME}/pimpd2.conf") {
105 $config = "$ENV{HOME}/pimpd2.conf";
106 }
107 elsif(-e './pimpd2.conf') {
108 $config = './pimpd2.conf';
109 }
110 elsif(-e '/etc/pimpd2.conf') {
111 $config = '/etc/pimpd2.conf';
112 }
113 else {
114 warn "No configuration file found.\n";
115 warn "See docs/pimpd2.conf.example for an example configuration file.\n";
116 exit 1;
117 }
118  
119 #print "Config found: $config\n";
120  
121 my $conf = Config::General->new(
122 '-ConfigFile' => $config,
123 '-AllowMultiOptions' => 1,
124 '-LowerCaseNames' => 1,
125 '-AutoTrue' => 1,
126 #'-DefaultConfig' => \%default_config,
127 '-InterPolateEnv' => 1,
128 'CComments' => 1,
129 );
130  
131 %config = $conf->getall;
132 $config{_filename} = $config;
133  
134 for my $color( sort grep{ /color/m } keys(%config) ) {
135 # color0 => green8
136 # color1 => purple14
137 push(@c, $config{$color});
138 }
139 return;
140 }
141  
142  
143 1;
144  
145 __END__