Add recursive enumeration of images

cleaned up some comments and one typo in ARG parsing
This commit is contained in:
John Mertz 2022-07-25 22:27:47 -04:00
parent 55c2a73737
commit 922e3708c2
1 changed files with 45 additions and 6 deletions

View File

@ -42,6 +42,8 @@ are currently enabled will be set.\n
--verbose=N Define minimum log level. Counting from 1: LOG_DEBUG, LOG_INFO,
-v N LOG_NOTICE, LOG_WARNING, LOG_ERR, LOG_CRIT, LOG_ALERT, LOG_EMERG
Default: 5; If provided without a value for N then all (1).
--recursive=N Enumerate images recursively through directories in the path.
-r N N indicates the directory depth, unlimited if no N is provided.
--help This menu
-h\n
You can send SIGUSR1 to force the daemon to reload immediately. Rotation timer
@ -168,14 +170,41 @@ sub clean
}
}
sub dig_dirs
{
my $self = shift;
my $paths_ref = shift;
my $path = shift;
my $depth = shift || 0;
unless (-e $path) {
return(undef);
}
if ($path =~ m/\/\.\.?$/) {
return;
}
foreach (glob("$path/*"), glob("$path/.*")) {
if ($path =~ m/\/\.\.?$/) {
next();
} elsif (-l $_) {
push(@$paths_ref, $_);
} elsif (-d $_ && ($self->{recursive} == '-1' || $depth < $self->{recursive})) {
$self->dig_dirs($paths_ref,$_,$depth+1);
} else {
push(@$paths_ref, $_);
}
}
}
sub choose_image
{
my $self = shift;
my @w = glob($self->{'path'}."/*");
return undef unless (scalar(@w));
$self->do_log("LOG_DEBUG", "Found ".scalar(@w)." files in $self->{path}");
my @i;
my $depth = 0;
my @w;
$self->dig_dirs(\@w,$self->{path});
return undef unless (scalar(@w));
$self->do_log("LOG_DEBUG", "Found ".scalar(@w)." files in $self->{path} up to depth $self->{recursive}");
foreach (@w) {
if (-d $_) {
$self->do_log("LOG_DEBUG", "Ignoring sub-directory $_");
@ -218,10 +247,8 @@ sub crop
$x = int(rand($iw-$ow));
$y = int(rand($ih-$oh));
print "Cropping $image ${ow}x${oh}+${x}+${y}\n";
my $err = $im->Crop(geometry=>"${ow}x${oh}+${x}+${y}");
die "$err" if ($err);
print "Writing $cropped\n";
$err = $im->Write($cropped);
die "$err" if ($err);
return $cropped if ( -e $cropped );
@ -351,6 +378,7 @@ my $delay;
my $crop;
my $path;
my $verbose;
my $recursive = 0;
while (my $arg = shift(@ARGV)) {
if ($arg eq '-h' || $arg eq '--help') {
$wp->usage();
@ -376,11 +404,21 @@ while (my $arg = shift(@ARGV)) {
} elsif ($arg =~ m/^\-\-verbose=?(.+)?$/) {
die "Redundant argument '$arg'. Verbosity already set.\n" if ($verbose);
$verbose = $1 || 1;
} elsif ($arg eq '-p') {
} elsif ($arg eq '-v') {
die "Redundant argument '$arg'. Verbosity already set.\n" if ($verbose);
if (scalar(@ARGV) && $ARGV[0] =~ m/^\d$/) {
$verbose = shift(@ARGV);
}
} elsif ($arg =~ m/^\-\-recursive=?(.+)?$/) {
die "Redundant argument '$arg'. Recursive search already set.\n" unless ($recursive == 0);
$recursive = $1 || -1;
} elsif ($arg eq '-r') {
die "Redundant argument '$arg'. Recursive search already set.\n" unless ($recursive == 0);
if (scalar(@ARGV) && $ARGV[0] =~ m/^\d+$/) {
$recursive = shift(@ARGV);
} else {
$recursive = -1;
}
} elsif ($arg =~ m/^-/) {
die "Unrecognized argument: $arg\n";
} else {
@ -404,6 +442,7 @@ $wp->{path} = $path || "$ENV{HOME}/wallpapers";
$wp->{crop} = $crop || 1;
$wp->{delay} = $delay || 300;
$wp->{verbose} = $verbose || 0;
$wp->{recursive}= $recursive;
$wp->{error} = [];
################################################################################