1 # (c) 2007 by Ralf Ertzinger <ralf@camperquake.de>
2 # licensed under GNU GPL v2
4 package xmlrtorrent::Base;
11 my $self = {'_DEBUG' => 0, '_OUT' => sub {print shift}};
15 $self->_prepare_parameters();
24 $t = sprintf(shift(@_), @_);
33 $data[0] = "DEBUG: " . $data[0];
34 if ($self->{'_DEBUG'} != 0) {$self->error(@data)};
42 return $self->{'_CONFIG'} unless defined($c);
44 foreach $o (keys(%{$c->{'option'}})) {
45 if (exists($self->{'_CONFIG'}->{'option'}->{$o})) {
46 $self->{'_CONFIG'}->{'option'}->{$o}->{'content'} = $c->{'option'}->{$o}->{'content'};
50 return $self->{'_CONFIG'};
53 sub _prepare_parameters {
57 $self->{'_CONFIG'} = {'option' => {'enabled' => {'content' => '1'}}};
59 foreach $p (keys(%{$self->{'_PARAMS'}})) {
60 $self->{'_CONFIG'}->{'option'}->{$p}->{'content'} = $self->{'_PARAMS'}->{$p}->[0];
69 $val = $self->{'_CONFIG'}->{'option'}->{$key}->{'content'};
70 $self->debug('Returning %s=%s', $key, $val);
80 if (exists($self->{'_CONFIG'}->{'option'}->{$key})) {
81 $self->{'_CONFIG'}->{'option'}->{$key}->{'content'} = $val;
83 $self->error('Module %s does not have a parameter named %s', $self->{'NAME'}, $key);
91 $self->{'_OUT'} = $io;
96 my $s = 'Options for ' . $self->{'NAME'} . ":\n";
100 foreach $k (keys(%{$self->{'_CONFIG'}->{'option'}})) {
101 $p = $self->{'_CONFIG'}->{'option'}->{$k}->{'content'};
103 $s .= sprintf(" %s: %s", $k, $p);
104 if ($self->{'_CONFIG'}->{'option'}->{$k}->{'content'} eq $self->{'_PARAMS'}->{$k}->[0]) {
105 $s .= " (default)\n";
114 # Return a list of the parameters supported by the module
115 # Does not return the 'enabled' parameter
120 return grep {$_ ne 'enabled' && /^$word/} keys %{$self->{'_PARAMS'}};
123 # Return a list of valid parameter values, if the parameter has
130 unless(exists($self->{'_PARAMS'}->{$param}->[2])) {
133 return grep {/^$word/} keys %{$self->{'_PARAMS'}->{$param}->[2]};
140 my $s = 'Help for ' . $self->{'NAME'} . ":\n";
144 if (exists($self->{'DESC'})) {
145 $s .= "Description:\n " . $self->{'DESC'} . "\n";
149 foreach $k (keys(%{$self->{'_CONFIG'}->{'option'}})) {
150 $p = $self->{'_PARAMS'}->{$k}->[0];
152 if (exists($self->{'_PARAMS'}->{$k}->[2])) {
153 # The parameter has a list of allowed values. Add the keys and their help
154 $s .= sprintf(" %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
155 foreach (sort keys(%{$self->{'_PARAMS'}->{$k}->[2]})) {
156 $s .= sprintf(" %s: %s\n", $_, $self->{'_PARAMS'}->{$k}->[2]->{$_});
159 # The parameter just has a default value and a help text
160 $s .= sprintf(" %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
170 $self->{'_DEBUG'} = shift;