Commits

Palmer, 2E0EOL  committed 3ed44da

EXPERIMENTAL: Turn the main feed data into a TagProcessor object

  • Participants
  • Parent commits 9231ac5
  • Branches develop

Comments (0)

Files changed (2)

 my $Debug = 0;
 my $Quiet = 0;
 my $File = '';
-my $globalTagProcessor = new Dlpodget::TagProcessor;
 
 sub fileFromURI($) {
 	my $Url = shift;
 	my $http_resp;
 	my $local_length = 0;
 	my ( $Feeds, $entry, $Name ) = @_;
-	my $rsleep = $Feeds->{$Name}->{rsleep} || $Feeds->{main}->{RSLEEP};
-	my $retries = $Feeds->{$Name}->{retries} || $Feeds->{main}->{RETRIES};
+	my $rsleep = $Feeds->{$Name}->{rsleep}   || $Feeds->{main}->value('rsleep');
+	my $retries = $Feeds->{$Name}->{retries} || $Feeds->{main}->value('retries');
 	my $local_podcast = $Feeds->{$Name}->{localpath} . '/' . $entry->{filename};
 	if ( -f $local_podcast ) {
 		$local_length = (stat($local_podcast))[7];
 	);
 	printf(STDERR "Downloading %s ... ", $entry->{uri}) if ( !$is_interactive && !$Quiet );
 
-	unless ( $Feeds->{main}->{NOOP} ) {
+	unless ( $Feeds->{main}->value('noop') ) {
 		if ( open($f, '>>:raw', $local_podcast) ) {
 			binmode($f);
 		} else {
 			%get_params,
 			':content_cb' => sub {
 				my ( $chunk ) = @_;
-				print $f $chunk unless ( $Feeds->{main}->{NOOP} );
+				print $f $chunk unless ( $Feeds->{main}->value('noop') );
 			}
 		);
 		$paul->rSleep($rsleep) if ( ($tries-1) && $http_resp->code() != 200 );
 	} while ( $http_resp->code() != 200 && --$tries );
 
-	close($f) if ( !$Feeds->{main}->{NOOP} );
+	close($f) if ( !$Feeds->{main}->value('noop') );
 	printf(STDERR "done.\n") if ( !$is_interactive && !$Quiet );
 }
 
-sub processTagsOld($$) {
-	my ( $Feeds, $V ) = @_; # Feeds deprecated in this function
-	my $tagRx = qr/^\$([A-Z0-9]+)/o;
-	my $avoid = 0;
-	while ( (my $idx = index($V, '$', $avoid)) > -1 ) { # Find remaining user-variable references
-		my $var = substr($V, $idx);
-		if ( $var =~ $tagRx ) {
-			my $v = $Feeds->{main}->{ uc($1) };
-			warn(sprintf('%s -> %s', $1, $v || '(undef)')) if ( $Debug );
-			if ( !defined($v) ) {
-				$avoid = $idx+1;
-				next;
-			}
-			substr($V, $idx, length($1)+1, $v);
-		} else {
-			$avoid++;
-		}
-	}
-	return $V;
-}
-
-sub processTags($$) {
-	my ( undef, $V ) = @_;
-
-	return $globalTagProcessor->result($V);
-}
+# @Deprecated because $Feeds->{'main'} should be itself a Dlpodget::TagProcessor object!
+#sub processTags($$) {
+#	my ( undef, $V ) = @_; # Feeds deprecated in this function
+#	my $tagRx = qr/^\$([A-Z0-9]+)/o;
+#	my $avoid = 0;
+#	while ( (my $idx = index($V, '$', $avoid)) > -1 ) { # Find remaining user-variable references
+#		my $var = substr($V, $idx);
+#		if ( $var =~ $tagRx ) {
+#			#my $v = $Feeds->{main}->{ uc($1) };
+#			#my $v = $globalTagProcessor->value(uc($1));
+#			warn(sprintf('%s -> %s', $1, $v || '(undef)')) if ( 1 || $Debug );
+#			if ( !defined($v) ) {
+#				$avoid = $idx+1;
+#				next;
+#			}
+#			substr($V, $idx, length($1)+1, $v);
+#		} else {
+#			$avoid++;
+#		}
+#	}
+#	return $V;
+#}
 
 sub child($$$$) {
 	my ( $DB, $FeedKey, $Feeds, $Feed ) = @_;
 			$conf = undef;
 		}
 	}
+
+	$feeds{'main'} = new Dlpodget::TagProcessor;
 	if ( $conf ) {
 		my %reserveSec = map {  $_ => 1 } ( 'main', 'paths' ); # Reserved section names
 		my ( @sections, $secC );
 		# First load the generation configuration information
 		# Include environment variables.
 		foreach my $ek ( keys(%ENV) ) {
-			$feeds{main}->{ uc($ek) } = $ENV{$ek};
+			$feeds{'main'}->assoc(uc($ek), $ENV{$ek});
 		}
 		if ( scalar(@defKeys) ) {
 			foreach my $mk ( @defKeys ) {
+				#$globalTagProcessor->assoc($mk, $globalTagProcessor->result($conf->val('main', $mk)));
 				#$feeds{main}->{ uc($mk) } = processTags(\%feeds, $conf->val('main', $mk));
-				$globalTagProcessor->assoc(uc($mk), $globalTagProcessor->result($conf->val('main', $mk)));
+				#$globalTagProcessor->assoc(uc($mk), $globalTagProcessor->result($conf->val('main', $mk)));
 			}
 			# Set debug flag via config if it existed.
-			if ( $feeds{main}->{DEBUG} ) {
-				$Debug = $feeds{main}->{DEBUG};
+			if ( $feeds{main}->value('DEBUG') ) {
+				$Debug = $feeds{main}->value('DEBUG');
 				printf(STDERR "Set Debug via config: %s\n", $Debug);
 			}
 		}
 			my @secKeys;
 			if ( (@secKeys = $conf->Parameters('paths')) ) {
 				foreach my $pk ( @secKeys ) {
-					$feeds{main}->{ uc($pk) } = $conf->val('paths', $pk);
+					$feeds{'main'}->assoc(uc($pk), $conf->val('paths', $pk));
 				}
 				# For backward compatibility, set localpfx with [paths] root
-				$feeds{main}->{'LOCALPFX'} = $feeds{main}->{'ROOT'}
-					if ( $feeds{main}->{'ROOT'} );
+				if ( $feeds{'main'}->value('root') ) {
+					$feeds{'main'}->assoc('localpfx', $feeds{'main'}->value('root'));
+				}
 			}
 		}
 
 			foreach ( my $keyI = 0; $keyI < $keyC; $keyI++ ) {
 				next if ( $reserveSec{$sections[$secI]} ); # Skip reserved sections
 				my $v = $conf->val($sections[$secI], $secKeys[$keyI]);
-				$v = processTags(\%feeds, $v);
+				#$v = processTags(\%feeds, $v);
 				printf(
 					STDERR
 					'[%s] %s -> %s'."\n",
 		my $max_children_opt = int($opts{'c'});
 		if ( $Debug ) {
 			my $msg;
-			if ( defined($feeds{main}->{MAXCHILDREN}) ) {
+			if ( defined($feeds{'main'}->value('maxchildren')) ) {
 				$msg = sprintf(
 					"Overriding config maxchildren %u via -c %u\n",
-					$feeds{main}->{MAXCHILDREN}, $max_children_opt
+					$feeds{main}->value('maxchildren'), $max_children_opt
 				);
 			} else {
 				$msg = sprintf("Setting maxchildren via -c %u\n", $max_children_opt);
 			}
 			print(STDERR $msg);
 		}
-		$feeds{main}->{MAXCHILDREN} = $max_children_opt;
+		$feeds{'main'}->assoc('maxchildren', $max_children_opt);
 	}
 
-	$0 = "$proctitle [MASTER]" if ( $feeds{main}->{MAXCHILDREN} != 0 );
+	$0 = "$proctitle [MASTER]" if ( $feeds{main}->value('maxchildren') != 0 );
 	my $db = db();
 	foreach my $feedName ( keys(%feeds) ) {
 		my $feed_key;

File lib/Dlpodget/TagProcessor.pm

 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 # POSSIBILITY OF SUCH DAMAGE.
 
+# Question: Would it have been better to implement this as a tie hash container?
 package Dlpodget::TagProcessor;
-# TODO: Should derive from LocalBase or it's successor.
+
 use Moose;
 use strict;
 use warnings;
 sub assoc($$$) {
 	my ($self, $k, $v) = @_;
 
+	$k = uc($k); # All keys are uppercase
 	if ( exists($self->mappings->{$k}) ) {
 		my $old = $self->mappings->{$k};
 		$old = '(undef)' unless (defined($old));
 
 sub value($$) {
 	my ( $self, $k ) = @_;
+
+	$k = uc($k); # All keys are uppercase
 	if ( exists($self->mappings->{$k}) ) {
 		return $self->mappings->{$k};
 	}