package My::parser::config; use strict; use warnings; use Getopt::Long; sub new { my $class = shift; my $file = shift; my $self = {}; bless ($self, $class); my $test = 0; my $parse; GetOptions( "cf=s" => \$file, #string, config file from options "test=i" => \$test, #integer, test mode (no writing to db, no blacklisting) "parse=s" => \$parse, #string, parser testing. Parse only the submitted line, test=1 needed ); $self->{'file'} = $file; $self->parse if($self->load); $self->{'config'}->{'test'} = $test; $self->{'config'}->{'parse'} = $parse; return $self; } sub load { my $self = shift; my $file = $self->{'file'}; open(FH, "<", "$file") || die "Could not open $file for reading: ".$!; while() { my $line = $_; chomp($line); next unless($line =~ m/\S/); push(@{$self->{'conf_file'}},$line) unless($line =~ m/^\#/); } close(FH); return 1; } sub parse { my $self = shift; foreach(@{$self->{'conf_file'}}) { my ($opt,$arg) = split("="); $opt =~ s/^\s+//; $opt =~ s/\s+$//; $arg =~ s/^\s+//; $arg =~ s/\s+$//; my @argarr = split("','",$arg); foreach my $argie(@argarr) { $argie =~ s/^\'//; $argie =~ s/\'$//; push(@{$self->{'config'}->{$opt}},$argie); } } delete $self->{'conf_file'}; return 1; } sub get_modules { my $self = shift; my $type = $self->{'config'}->{'modules'}; return $type; } sub get_parsers { my $self = shift; my $return = $self->{'parsers'}; return $return; } sub get_fetchers { my $self = shift; my $return = $self->{'fetchers'}; return $return; } sub get_parser_info { my $self = shift; my $m = shift; my $toret = {}; if(defined($self->{'config'}->{$m})) { for(my $i = 0; $i < @{$self->{'config'}->{$m}}; $i++) { my $val; $val = 'type' if($i == 0); $val = 'source' if($i == 1); $val = 'program' if($i == 2); $toret->{$val} = @{$self->{'config'}->{$m}}[$i] } } else { return; } return $toret; } sub get_fetcher { my $self = shift; my $m = shift; my $toret; if(defined($self->get_parser_info($m))) { my $type = $self->get_parser_info($m)->{'type'}; if ($toret = $self->{'config'}->{'fetchers'}->{$type}->{$m}) { return $toret; } } return; } sub get_fetcher_module { my $self = shift; my $m = shift; my $toret; if(defined($self->{'fetcher'}->{$m})) { my $toret = $self->{'fetcher'}->{$m}; return $toret; } else { return; } } sub get_dbh { my $self = shift; my $dbh = $self->{'config'}->{'fetchers'}->{'db'}->{'dbh'}; return $dbh; } sub set_dbh { my $self = shift; my $dbh = shift; $self->{'config'}->{'fetchers'}->{'db'}->{'dbh'} = $dbh; return 1; } sub set_fetcher { my $self = shift; my $type = shift; ## Type of source my $m = shift; ## m for module? name of service my $fetcher = shift; ## hash for reaching fetcher if($type && $m) { $self->{'config'}->{'fetchers'}->{$type}->{$m} = $fetcher; #print localtime(time).": Fetcher set to $type($fetcher) for service $m\n"; FIXME add debug option in config? return 1; } return 0; } sub set { my $self = shift; my $node = shift || undef; my $key = shift || undef; my $value = shift || undef; if(defined($node) && defined($key) && defined($value)) { $self->{$node}->{$key} = $value; return 1; } elsif (defined($node) && defined($key)) { $self->{$node} = $key; return 1; } return 0; } sub get { my $self = shift; my $node = shift || undef; my $key = shift || undef; if(defined($node) && defined($key)) { my $toret = $self->{$node}->{$key}; return $toret; } elsif(defined($node)) { my $toret = $self->{$node}; return $toret; } return 0; } sub get_as_single_val { my $self = shift; my $node = shift || undef; my $key = shift || undef; my $toret; if(defined($node) && defined($key)) { $toret = exists $self->{$node}->{$key} ? $self->{$node}->{$key} : undef; } elsif(defined($node)) { $toret = exists $self->{$key} ? $self->{$key} : undef; } if($toret) { if(scalar(@{$toret})) { $toret = join('',@{$toret}); } return $toret; } return; } sub print { my $self = shift; foreach my $key(keys %{$self->{'config'}}) { $self->{'config'}->{'logger'}->log("$key: "); my $tolog; foreach my $arg(@{$self->{'config'}->{$key}}) { $tolog .= "$arg "; } $self->{'config'}->{'logger'}->log($tolog); } return 1; } 1;