Merging options

MacFH - C E Macfarlane - News news at macfh.co.uk
Mon Mar 7 06:12:47 PST 2022


I've posted before about the possibility of having command-line options 
merge with, rather than overwrite, the same options from the preferences 
file, called 'options' that lives in a user's GiP profile directory, the 
most obvious need being to be able to provide *additional* exclude 
options via the command-line to supplement those already in the 
'options' file.

For anyone interested, the following changes to a subroutine allow this 
to happen, and, in fact, because the same subroutine is used throughout 
the options hierarchy, it will apply where any option of higher 
precedence would otherwise overwrite an option of lower precedence.  The 
way it works is, if the option's value begins with '+', then the 
plus-sign is discarded and the remainder of the option's value is merged 
with any pre-existing value rather than overwriting it.

It should be noted that it's only meaningful to use it where options 
consist of comma-separated values.  I don't understand the wider options 
programming well enough to feel confident about suggesting how within 
GiP to vet against use of a plus-sign on an unsuitable option, but in 
another program which I use everyday and which calls GiP to download 
four daily programme lists  -  HDTV, SDTV, HD Radio, LD Radio (though 
this latter is rather redundant now) -  I simply check whether the 
option name is contained in a list of options where merging would be valid.

For those who are interested, the changes required to GiP v3.29 to have 
this happen are as follows:

Line 64, add the following line (you may need to use 'cpan' to install 
the module beforehand, but I had it already):

use Uniq;

Lines 2674 onwards, alter the subroutine 'sub copy_set_options_from'
to read as follows (beware possible unwanted line wrap):

# Copies values in one instance to another only if they are set with a 
value/defined
# Usage: $opt->copy_set_options_from( $opt_cmdline );
sub copy_set_options_from {
	my $this_to = shift;
	my $this_from = shift;
	#
	# CEM Change - Merge rather than overwrite
	# higher priority options whose value begins with a '+'
	#
	# Original code:
	# Merge cmdline options into $opt instance (only those options defined)
	# for ( keys %{$this_from} ) {
	# 	$this_to->{$_} = $this_from->{$_} if defined $this_from->{$_};
	# }
	#
	# Replacement code:
	for ( keys %{$this_from} ) {
		if( defined($this_from->{$_}) ) {
			# If 'from' value begins with a plus sign ...
			if( $this_from->{$_} =~ m/^\+/ ) {
				# ... its value merges with existing value
				$this_to->{$_} = join( ',', Uniq::uniq( 
sort(split(',',$this_to->{$_}),split(',',substr($this_from->{$_}, 1)) ) ) );
			} else {
				# ... else it overwrites existing value
				$this_to->{$_} = $this_from->{$_};
			}
		}
	}
}

If anyone's interested, I have a number of other patches to GiP which I 
regularly apply, some being additions to existing coding, some 
modifications or replacements of it:

3 different ones in different places which attempt try to fix the BBC's 
haphazard application of single-quotes and apostrophes by converting 
them all to the simple ASCII equivalent.  I can't help feeling that it 
shouldn't be necessary to apply the same patch in three different 
places, but GiP is so convoluted that at the time I was doing this work 
I was unable to find a single 'pressure point' at which to apply the fix 
and have it work unfailingly in all circumstances.

1 to leave genuine single quotes and apostrophes, once converted as 
above, within file names, and to replace colons with ' - '

1 to remove 'Series' and 'Episode' from programme names, leaving just 
the numbers.

1 to proper (aka camel) case file names

1 to always display the file name that will be used, aka the file-prefix



More information about the get_iplayer mailing list