diff options
author | Andy Shevchenko <andy@smile.org.ua> | 2012-07-28 15:42:27 +0000 |
---|---|---|
committer | Andy Shevchenko <andy@smile.org.ua> | 2012-07-28 15:42:27 +0000 |
commit | 299ff0c83e4c2e6b8aad050f4fb080fddc037093 (patch) | |
tree | 137e919f4a5c8a0ba456c2b705484aebf1d95a6f | |
parent | f71b5c9b7335427ad805b5d2c1d7d12d40d5d81d (diff) | |
download | renrot-299ff0c83e4c2e6b8aad050f4fb080fddc037093.tar.gz renrot-299ff0c83e4c2e6b8aad050f4fb080fddc037093.tar.bz2 |
util, rpm: move several functions to Image::RenRot::Util
Move and rename several functions:
trim_value() -> trim()
boolConv() -> bool2str()
boolConverter() -> str2bool()
isTherePackage() -> loadpkg()
This patch also introduces the basic support of the perl modules in the
specfile.
Signed-off-by: Andy Shevchenko <andy.shevchenko@gmail.com>
git-svn-id: file:///svnroot/renrot/trunk@608 fe2816f4-e837-0410-b10a-f608c9d244a1
-rw-r--r-- | MANIFEST | 1 | ||||
-rw-r--r-- | lib/Image/RenRot/Util.pm | 84 | ||||
-rwxr-xr-x | renrot | 112 | ||||
-rw-r--r-- | renrot.spec | 5 |
4 files changed, 115 insertions, 87 deletions
@@ -13,5 +13,6 @@ etc/colors.conf Colorization scheme etc/copyright.tag Text to put to Copyright tag etc/renrot.conf Sample of configuration file etc/tags.conf Strings to fill appropriate tags +lib/Image/RenRot/Util.pm sample/dotkeywords Sample of a .keywords file sample/dotrank Sample of a .rank file diff --git a/lib/Image/RenRot/Util.pm b/lib/Image/RenRot/Util.pm new file mode 100644 index 0000000..6e1e790 --- /dev/null +++ b/lib/Image/RenRot/Util.pm @@ -0,0 +1,84 @@ +package Image::RenRot::Util; + +# +# vim: ts=2 sw=2 et : +# + +######################################################################################## +### COMMON HELPERS ### +######################################################################################## + +use strict; +use warnings; +require 5.006; +require Exporter; + +use vars qw(@ISA @EXPORT); + +@ISA = qw(Exporter); +@EXPORT = qw(trim bool2str str2bool loadpkg); + +######################################################################################## +# Usage : trim($value) +# Purpose : removes heading and trailing spaces +# Returns : trimmed $value +# Parameters : $value string +# Throws : no exceptions +# Comments : none +# See Also : N/A +sub trim($) { + my $value = shift; + $value =~ s/^\s*//; # heading + $value =~ s/\s*$//; # trailing + return $value; +} + +######################################################################################## +# Usage : bool2str($var) +# Purpose : converts boolean value to human readable string +# Returns : string "Yes" or "No" +# Parameters : 0 or 1 +# Throws : no exceptions +# Comments : none +# See Also : str2bool() +sub bool2str($) { + if (shift == 0) { + return "No"; + } else { + return "Yes"; + } +} + +######################################################################################## +# Usage : str2bool($var) +# Purpose : converts given string to a boolean value +# Returns : number 1 or 0 +# Parameters : one of "1", "Yes", "True", "On", "0", "No", "False" or "Off" +# Throws : no exceptions +# Comments : none +# See Also : bool2str() +sub str2bool($) { + my $value = trim(shift); + if ($value =~ m/^(0|No|False|Off|Disable)$/i) { + return 0; + } elsif ($value =~ m/^(1|Yes|True|On|Enable)$/i) { + return 1; + } + return $value; +} + +######################################################################################## +# Usage : loadpkg($pkg) +# Purpose : checks availability of given package (renrot could be depend of it) +# Returns : nothing in case the package available and undef if not +# Parameters : $pkg - string with package name +# Throws : no exceptions +# Comments : none +# See Also : n/a +sub loadpkg($) { + my $pkg = shift; + return undef unless eval "require $pkg"; +} + +######################################################################################## +1; # end @@ -21,6 +21,15 @@ use File::Spec; use File::Path; use Term::ANSIColor; +# add our 'lib' directory to the include list +my $exeDir; +BEGIN { + $exeDir = ($0 =~ /(.*)[\\\/]/) ? $1 : '.'; # get exe directory + unshift @INC, "$exeDir/lib"; # add lib directory at start of include path +} + +use Image::RenRot::Util; + $Term::ANSIColor::AUTORESET = 1; $Term::ANSIColor::EACHLINE = "\n"; $ENV{ANSI_COLORS_DISABLED} = 1; @@ -384,8 +393,8 @@ sub ldbg3opts { $value = $hash->{$option}{$key}{value}; $default = $hash->{$option}{$key}{default}; } else { - $value = boolConv($hash->{$option}{$key}{value}); - $default = boolConv($hash->{$option}{$key}{default}); + $value = bool2str($hash->{$option}{$key}{value}); + $default = bool2str($hash->{$option}{$key}{default}); } if (not defined $default) { ldbg3("--> '$option $key': $value"); @@ -396,10 +405,6 @@ sub ldbg3opts { } ######################################################################################## -### COMMON HELPERS ### -######################################################################################## - -######################################################################################## # Usage : die_renrot() # Purpose : dies differently depend on verbose level # Returns : nothing @@ -415,54 +420,6 @@ sub die_renrot { } ######################################################################################## -# Usage : boolConv($var) -# Purpose : converts boolean value to human readable string -# Returns : string "Yes" or "No" -# Parameters : 0 or 1 -# Throws : no exceptions -# Comments : none -# See Also : boolConverter() -sub boolConv { - my $var = shift; - if ($var == 0) { - return "No"; - } else { - return "Yes"; - } -} - -######################################################################################## -# Usage : boolConverter($var) -# Purpose : converts given string to a boolean value -# Returns : number 1 or 0 -# Parameters : one of "1", "Yes", "True", "On", "0", "No", "False" or "Off" -# Throws : no exceptions -# Comments : none -# See Also : boolConv() -sub boolConverter { - my $value = trim_value(shift); - if ($value =~ m/^(0|No|False|Off|Disable)$/i) { - return 0; - } elsif ($value =~ m/^(1|Yes|True|On|Enable)$/i) { - return 1; - } - return $value; -} - -######################################################################################## -# Usage : isTherePackage($pkg) -# Purpose : checks availability of given package (renrot could be depend of it) -# Returns : nothing in case the package available and undef if not -# Parameters : $pkg - string with package name -# Throws : no exceptions -# Comments : none -# See Also : n/a -sub isTherePackage { - my $pkg = shift; - return undef unless eval "require $pkg"; -} - -######################################################################################## ### GETTINNG OPTIONS FROM CLI ### ######################################################################################## @@ -593,23 +550,23 @@ sub getOptions { assign_hash_values($config_opts, 'general', \%h_g_opts); ldbg3opts($config_opts, 'general'); - ldbg3("--backup: ", boolConv($backup)); + ldbg3("--backup: ", bool2str($backup)); ldbg3("--comment-file: $comfile") if (defined $comfile); ldbg3("--config-file: $configFile") if (defined $configFile); ldbg3("--counter-start: $countStart", " --counter-step: $countStep", - " --counter-fixed-field: ", boolConv($countFF)); - ldbg3("--dry-run: ", boolConv($dryRun)) if (defined $dryRun); + " --counter-fixed-field: ", bool2str($countFF)); + ldbg3("--dry-run: ", bool2str($dryRun)) if (defined $dryRun); ldbg3("--exclude:\n", join("\n", @excludeList)) if (scalar(@excludeList) > 0); ldbg3("--extension: '$extToProcess'"); - ldbg3("--generate-thumb: ", boolConv($gen_thm)); + ldbg3("--generate-thumb: ", bool2str($gen_thm)); - ldbg3("--no-rename: ", boolConv($noRename), - " --no-rotate: ", boolConv($noRotation), - " --no-tags: ", boolConv($noTags), - " --no-renrot: ", boolConv($noRenRoTagMtm)); - ldbg3("--only-orientation: ", boolConv($orientTag)); + ldbg3("--no-rename: ", bool2str($noRename), + " --no-rotate: ", bool2str($noRotation), + " --no-tags: ", bool2str($noTags), + " --no-renrot: ", bool2str($noRenRoTagMtm)); + ldbg3("--only-orientation: ", bool2str($orientTag)); ldbg3("--rotate-angle: $rotateAngle") if (defined $rotateAngle); ldbg3("--rotate-thumb: $rotateThumbnail") if (defined $rotateThumbnail); ldbg3("--sub-fileset: $subFileSet") if ($subFileSet ne ""); @@ -682,21 +639,6 @@ sub getOptions { ######################################################################################## ######################################################################################## -# Usage : trim_value($value) -# Purpose : removes heading and trailing spaces -# Returns : trimmed $value -# Parameters : $value string -# Throws : no exceptions -# Comments : none -# See Also : N/A -sub trim_value { - my $value = shift; - $value =~ s/^\s*//; # heading - $value =~ s/\s*$//; # trailing - return $value; -} - -######################################################################################## # Usage : parsePair() gets (key, value) pair from the string like # : [multiword] key = "value" # Purpose : parses pairs of data @@ -709,11 +651,11 @@ sub parsePair { my $str = shift; my ($key, $value) = (undef, shift); if ($str =~ m/^([^=]+)=(.+)/) { - ($key, $value) = (trim_value($1), trim_value($2)); + ($key, $value) = (trim($1), trim($2)); $value =~ s/^[\'\"](.+)[\'\"]/$1/; # trim quotes dbgmsg (4, "Parsed: '$key' <- '$value'\n"); } elsif ($str =~ m/^([^=]+)=$/) { - $key = trim_value($1); + $key = trim($1); dbgmsg (4, "Parsed empty '$key', applying default value: ", defined $value ? "'$value'" : "undef", "\n"); } return ($key, $value); @@ -757,7 +699,7 @@ sub parse_many_pairs { warnmsg ("Invalid key '$key' in '$sect'. Skiping...\n"); next; } - $value = boolConverter($value) if ($hash->{$sect}{$key}{type} eq "!"); + $value = str2bool($value) if ($hash->{$sect}{$key}{type} eq "!"); update_config_option_value($hash->{$sect}{$key}, $value); dbgmsg (4, "Parsed: '$sect' -> '$key': '$value'\n"); } @@ -842,7 +784,7 @@ sub getConfig { } $key .= sprintf("#%d#%d", $fc, $i) if (grep (/^$key$/, @multOpts)); $key .= ' ' . 'enabled' if (grep (/^$key$/, @config_sections)); - $hash->{$key} = boolConverter($value); + $hash->{$key} = str2bool($value); dbgmsg (3, "Parsed line($i): '$key' <- '$hash->{$key}'\n"); } else { warnmsg ("Unparsed line $i in configuration file.\n"); @@ -919,7 +861,7 @@ sub apply_config_values { my ($sect, $key) = undef; foreach my $s (@config_sections) { if ($option =~ m/^$s(.*)/) { - ($sect, $key) = ($s, trim_value($1)); + ($sect, $key) = ($s, trim($1)); last; } } @@ -973,7 +915,7 @@ sub keywordizer { my @keywordArr = getFileDataLines($file); for (my $i = 0; $i < scalar(@keywordArr); $i++) { $keywordArr[$i] =~ s/\r?\n$//; # remove CR and LF symbols - push (@result, trim_value($keywordArr[$i])) if ($keywordArr[$i] !~ m/^\s*$/); + push (@result, trim($keywordArr[$i])) if ($keywordArr[$i] !~ m/^\s*$/); } return @result; } @@ -2401,7 +2343,7 @@ sub template2name { # # Check for modules -$isThereIM = 1 if (defined isTherePackage("Image::Magick")); +$isThereIM = 1 if (defined loadpkg("Image::Magick")); getOptions(); parseConfig($configFile, $config_opts); diff --git a/renrot.spec b/renrot.spec index efbc64e..201214b 100644 --- a/renrot.spec +++ b/renrot.spec @@ -33,11 +33,11 @@ configuration file. %setup -q -n %{name}-%{version}%{?rcver} %build -%{__perl} Makefile.PL PREFIX=%{_prefix} +%{__perl} Makefile.PL INSTALLDIRS=vendor make %install -make install PREFIX=$RPM_BUILD_ROOT%{_prefix} +make pure_install PERL_INSTALL_ROOT=$RPM_BUILD_ROOT # Fix renrot permissions chmod 755 $RPM_BUILD_ROOT%{_bindir}/renrot @@ -69,6 +69,7 @@ fi %config(noreplace) %{_sysconfdir}/%{name}/copyright.tag %config(noreplace) %{_sysconfdir}/%{name}/renrot.conf %config(noreplace) %{_sysconfdir}/%{name}/tags.conf +%{perl_vendorlib}/Image/ %changelog * Sun Jul 15 2012 Andy Shevchenko <andy.shevchenko@gmail.com> |