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 foreach $p (keys(%{$self->{'_PARAMS'}})) {
58 $self->{'_CONFIG'}->{'option'}->{$p}->{'content'} = $self->{'_PARAMS'}->{$p}->[0];
67 $val = $self->{'_CONFIG'}->{'option'}->{$key}->{'content'};
68 $self->debug('Returning %s=%s', $key, $val);
78 if (exists($self->{'_CONFIG'}->{'option'}->{$key})) {
79 $self->{'_CONFIG'}->{'option'}->{$key}->{'content'} = $val;
81 $self->error('Module %s does not have a parameter named %s', $self->{'NAME'}, $key);
89 $self->{'_OUT'} = $io;
94 my $s = 'Options for ' . $self->{'NAME'} . ":\n";
98 foreach $k (keys(%{$self->{'_CONFIG'}->{'option'}})) {
99 $p = $self->{'_CONFIG'}->{'option'}->{$k}->{'content'};
101 $s .= sprintf(" %s: %s", $k, $p);
102 if ($self->{'_CONFIG'}->{'option'}->{$k}->{'content'} eq $self->{'_PARAMS'}->{$k}->[0]) {
103 $s .= " (default)\n";
112 # Return a list of the parameters supported by the module
113 # Does not return the 'enabled' parameter
118 return grep {$_ ne 'enabled' && /^$word/} keys %{$self->{'_PARAMS'}};
121 # Return a list of valid parameter values, if the parameter has
128 unless(exists($self->{'_PARAMS'}->{$param}->[2])) {
131 return grep {/^$word/} keys %{$self->{'_PARAMS'}->{$param}->[2]};
138 my $s = 'Help for ' . $self->{'NAME'} . ":\n";
142 if (exists($self->{'DESC'})) {
143 $s .= "Description:\n " . $self->{'DESC'} . "\n";
147 foreach $k (keys(%{$self->{'_CONFIG'}->{'option'}})) {
148 $p = $self->{'_PARAMS'}->{$k}->[0];
150 if (exists($self->{'_PARAMS'}->{$k}->[2])) {
151 # The parameter has a list of allowed values. Add the keys and their help
152 $s .= sprintf(" %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
153 foreach (sort keys(%{$self->{'_PARAMS'}->{$k}->[2]})) {
154 $s .= sprintf(" %s: %s\n", $_, $self->{'_PARAMS'}->{$k}->[2]->{$_});
157 # The parameter just has a default value and a help text
158 $s .= sprintf(" %s: %s (default: %s)\n", $k, $self->{'_PARAMS'}->{$k}->[1], $p);
168 $self->{'_DEBUG'} = shift;