| 1 | package Slackware::Slackget::Config; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 4 | $XML::Simple::PREFERRED_PARSER='XML::Parser'; | |||||||||||||||||||||||||||
| 7 | 4 | ▶ | use XML::Simple; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | Slackware::Slackget::Config - An interface to the configuration file | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 VERSION | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Version 1.0.1 | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =cut | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 4 | our $VERSION = '1.0.1'; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | This class is use to load a configuration file (config.xml) and the servers list file (servers.xml). It only encapsulate the XMLin() method of XML::Simple, there is no accessors or treatment method for this class. | ||||||||||||||||||||||||||||
| 24 | There is only a constructor which take only one argument : the name of the configuration file. | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | After loading you can acces to all values of the config file in the same way that with XML::Simple. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | The only purpose of this class, is to allow other class to check that the config file have been properly loaded. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | use Slackware::Slackget::Config; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | my $config = Slackware::Slackget::Config->new('/etc/slack-get/config.xml') or die "cannot load config.xml\n"; | ||||||||||||||||||||||||||||
| 33 | print "I will use the encoding: $config->{common}->{'file-encoding'}\n"; | ||||||||||||||||||||||||||||
| 34 | print "slack-getd is configured as: $config->{daemon}->{mode}\n" ; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | This module needs XML::Simple to work. | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =cut | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head2 new | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | The constructor take the config file name as argument. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | my $config = Slackware::Slackget::Config->new('/etc/slack-get/config.xml') or die "cannot load config.xml\n"; | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub new | ||||||||||||||||||||||||||||
| 51 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=38.1
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 2 | my ($class,$file) = @_ ; | |||||||||||||||||||||||||||
| 53 | 2 | ▶ | return undef unless(-e $file && -r $file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-e $file and -r $file
MC/DC0%0/2
-e $file and -r $file
-e $file
-r $file
Truth table33%1/3
A and B
A= -e $fileB= -r $file
| |||||||||||||||||||||||||||||
| 54 | 2 | ▶ | my $self= XMLin($file , ForceArray => ['li']) or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | # use Data::Dumper; | ||||||||||||||||||||||||||||
| 56 | # print "[Slackware::Slackget::Config]",Dumper($self); | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | return undef unless(defined($self->{common})); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if(exists($self->{'plugins'}->{'list'}->{'plug-in'}->{'id'}) && defined($self->{'plugins'}->{'list'}->{'plug-in'}->{'id'})) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $self->{"plugins"}{"list"}{"plug-in"}{"id"} and defined $self->{"plugins"}{"list"}{"plug-in"}{"id"}
MC/DC0%0/2
exists $self->{"plugins"}{"list"}{"plug-in"}{"id"} and defined $self->{"plugins"}{"list"}{"plug-in"}{"id"}
exists $self->{"plugins"}{"list"}{"plug-in"}{"id"}
defined $self->{"plugins"}{"list"}{"plug-in"}{"id"}
Truth table0%0/3
A and B
A= exists $self->{"plugins"}{"list"}{"plug-in"}{"id"}B= defined $self->{"plugins"}{"list"}{"plug-in"}{"id"}
| |||||||||||||||||||||||||||||
| 59 | { | ||||||||||||||||||||||||||||
| 60 | 0 | my $tmp = $self->{'plugins'}->{'list'}->{'plug-in'}; | |||||||||||||||||||||||||||
| 61 | 0 | delete($self->{'plugins'}->{'list'}->{'plug-in'}); | |||||||||||||||||||||||||||
| 62 | 0 | $self->{'plugins'}->{'list'}->{'plug-in'}->{$tmp->{'id'}} = $tmp; | |||||||||||||||||||||||||||
| 63 | 0 | delete($self->{'plugins'}->{'list'}->{'plug-in'}->{$tmp->{'id'}}->{'id'}); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 0 | ▶ | if($ENV{SG_DAEMON_DEBUG}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | require Data::Dumper; | |||||||||||||||||||||||||||
| 67 | 0 | print "[Slackware::Slackget::Config]",Data::Dumper::Dumper( $self ),"\n"; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 0 | bless($self,$class); | |||||||||||||||||||||||||||
| 70 | 0 | return $self; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =head2 get_token | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | Return the value associated to the given token. | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | Tokens are requested through a path like syntax. For example, the following XML : | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | <xml> | ||||||||||||||||||||||||||||
| 80 | <item> | ||||||||||||||||||||||||||||
| 81 | <key>value</key> | ||||||||||||||||||||||||||||
| 82 | </item> | ||||||||||||||||||||||||||||
| 83 | </xml> | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | The <key> element's value is accessed throught : | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | print $config->get_token("/item/key"); # the root key is not kept by this class | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | **WARNING** even if it could look like XPath : IT IS NOT ! | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =cut | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | sub get_token { | ||||||||||||||||||||||||||
|
Subroutine
get_token: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | 0 | my ($self,$req) = @_ ; | |||||||||||||||||||||||||||
| 97 | 0 | my @R = split(/\//,$req); | |||||||||||||||||||||||||||
| 98 | 0 | my $token; | |||||||||||||||||||||||||||
| 99 | 0 | my $ref = $self; | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | while(@R){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | $token = shift(@R); | |||||||||||||||||||||||||||
| 102 | 0 | ▶ | next if($token =~ /^\s*$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | $ref = $ref->{$token}; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 0 | return $ref; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head2 set_token | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | Following the same syntax as the get_token() method, it allows you to set a configuration token. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | $config->set_token("/item/key", "new value"); | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | The value can be anything fitting a scalar (number, strings, array ref, hash ref, etc.) | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =cut | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub set_token { | ||||||||||||||||||||||||||
|
Subroutine
set_token: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | 0 | my ($self,$req,$data) = @_ ; | |||||||||||||||||||||||||||
| 120 | 0 | my @R = split(/\//,$req); | |||||||||||||||||||||||||||
| 121 | 0 | my $token; | |||||||||||||||||||||||||||
| 122 | 0 | my $ref = $self; | |||||||||||||||||||||||||||
| 123 | 0 | my $c; | |||||||||||||||||||||||||||
| 124 | 0 | ▶ | while(@R){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | $token = shift(@R); | |||||||||||||||||||||||||||
| 126 | 0 | ▶ | next if(!defined($token) || $token =~ /^\s*$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $token or $token =~ /^\s*$/
MC/DC0%0/2
not defined $token or $token =~ /^\s*$/
not defined $token
$token =~ /^\s*$/
Truth table0%0/3
not(A) or B
A= not defined $tokenB= $token =~ /^\s*$/
| |||||||||||||||||||||||||||||
| 127 | 0 | print "$c- $token ",scalar(@R)," "; | |||||||||||||||||||||||||||
| 128 | 0 | $c .= " "; | |||||||||||||||||||||||||||
| 129 | 0 | ▶ | if(scalar(@R) >= 1){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | $ref->{$token} = {} unless( defined($ref->{$token}) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | $ref = $ref->{$token} ; | |||||||||||||||||||||||||||
| 132 | 0 | print "(not last token)"; | |||||||||||||||||||||||||||
| 133 | }else{ | ||||||||||||||||||||||||||||
| 134 | 0 | $ref->{$token} = $data; | |||||||||||||||||||||||||||
| 135 | 0 | print "(is the last token)"; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 0 | print "\n"; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | DUPUIS Arnaud, C<< <a.dupuis@infinityperl.org> >> | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head1 BUGS | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | Please report any bugs or feature requests to | ||||||||||||||||||||||||||||
| 150 | C<bug-Slackware-Slackget@rt.cpan.org>, or through the web interface at | ||||||||||||||||||||||||||||
| 151 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Slackware-Slackget>. | ||||||||||||||||||||||||||||
| 152 | I will be notified, and then you'll automatically be notified of progress on | ||||||||||||||||||||||||||||
| 153 | your bug as I make changes. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | perldoc Slackware::Slackget | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | You can also look for information at: | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =over 4 | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =item * Infinity Perl website | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | L<http://www.infinityperl.org/category/slack-get> | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =item * slack-get specific website | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | L<http://slackget.infinityperl.org> | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Slackware-Slackget> | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | L<http://annocpan.org/dist/Slackware-Slackget> | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | L<http://cpanratings.perl.org/d/Slackware-Slackget> | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | L<http://search.cpan.org/dist/Slackware-Slackget> | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =back | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | Thanks to Bertrand Dupuis (yes my brother) for his contribution to the documentation. | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | Copyright 2005 DUPUIS Arnaud, All Rights Reserved. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 203 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =cut | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 4 | 1; # End of Slackware::Slackget::Config | |||||||||||||||||||||||||||