1 package PackageInfo; 2 # 3 # $RCSfile: PackageInfo.pm,v $ 4 # $Date: 2015/02/28 20:47:18 $ 5 # $Revision: 1.9 $ 6 # 7 # Author: Manish Sud <msud@san.rr.com> 8 # 9 # Copyright (C) 2015 Manish Sud. All rights reserved. 10 # 11 # This file is part of MayaChemTools. 12 # 13 # MayaChemTools is free software; you can redistribute it and/or modify it under 14 # the terms of the GNU Lesser General Public License as published by the Free 15 # Software Foundation; either version 3 of the License, or (at your option) any 16 # later version. 17 # 18 # MayaChemTools is distributed in the hope that it will be useful, but without 19 # any warranty; without even the implied warranty of merchantability of fitness 20 # for a particular purpose. See the GNU Lesser General Public License for more 21 # details. 22 # 23 # You should have received a copy of the GNU Lesser General Public License 24 # along with MayaChemTools; if not, see <http://www.gnu.org/licenses/> or 25 # write to the Free Software Foundation Inc., 59 Temple Place, Suite 330, 26 # Boston, MA, 02111-1307, USA. 27 # 28 29 use strict; 30 use Exporter; 31 use Carp; 32 use Text::ParseWords; 33 use TextUtil; 34 use FileUtil; 35 36 use vars qw($AUTOLOAD @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); 37 38 @ISA = qw(Exporter); 39 @EXPORT = qw(GetPackageKeyValue SetPackageKeyValue IsPackageKeyNameAvailable); 40 @EXPORT_OK = qw(); 41 %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); 42 43 # 44 # Load package data... 45 # 46 my(%PackageDataMap); 47 _LoadPackageData(); 48 49 # Return value of a specific key... 50 # 51 sub GetPackageKeyValue { 52 my($KeyName) = @_; 53 54 return exists $PackageDataMap{$KeyName} ? $PackageDataMap{$KeyName} : 'Not Available'; 55 } 56 57 # Set value of a specific key... 58 # 59 sub SetPackageKeyValue { 60 my($KeyName, $KeyValue) = @_; 61 62 $PackageDataMap{$KeyName} = $KeyValue; 63 } 64 65 # Check availability of a package key name... 66 # 67 sub IsPackageKeyNameAvailable { 68 my($KeyName) = @_; 69 70 return exists $PackageDataMap{$KeyName} ? 1 : 0; 71 } 72 73 # Implements Set<KeyName> and Get<KeyName> functions... 74 # 75 sub AUTOLOAD { 76 my($KeyValue) = @_; 77 my($PackageName, $FunctionName, $KeyName); 78 79 ($PackageName, $FunctionName) = $AUTOLOAD =~ /^(.*?)::(.*?)$/; 80 81 if (!($FunctionName =~ /^Get/ || $FunctionName =~ /^Set/)) { 82 croak "Error: Can't locate function \"$FunctionName\" via package \"$PackageName\": This function is not automatically implemented by AUTOLOAD: Only Get<KeyName> and Set<KeyName> functions are implemented via AUTOLOAD..."; 83 } 84 85 ($KeyName) = $FunctionName =~ /^[SG]et(.*?)$/; 86 87 if ($FunctionName =~ /^Set/ && !defined($KeyValue)) { 88 carp "Warning: ${PackageName}::${FunctionName}: Didn't set value for key $KeyName: Key value for must be specified...\n"; 89 return undef; 90 } 91 92 if ($FunctionName =~ /^Get/) { 93 return GetPackageKeyValue($KeyName); 94 } 95 elsif ($FunctionName =~ /^Set/) { 96 return SetPackageKeyValue($KeyName, $KeyValue); 97 } 98 99 } 100 101 # Load PackageInfo.csv files from <MayaChemTools>/lib directory... 102 # 103 sub _LoadPackageData { 104 my($PackageDataFile, $MayaChemToolsLibDir, $Key, $Value, $Line, $InDelim, @LineWords); 105 106 $MayaChemToolsLibDir = GetMayaChemToolsLibDirName(); 107 $PackageDataFile = "$MayaChemToolsLibDir" . "/data/PackageInfo.csv"; 108 109 if (! -e "$PackageDataFile") { 110 croak "Error: MayaChemTools package file, $PackageDataFile, is missing: Possible installation problems..."; 111 } 112 113 # 114 # Format: 115 # 116 # "Key","Value" 117 # "PackageName","MayaChemTools" 118 # ... ... 119 # 120 121 %PackageDataMap = (); 122 $InDelim = "\,"; 123 124 open PACKAGEDATAFILE, "$PackageDataFile" or croak "Couldn't open $PackageDataFile: $! ..."; 125 126 # Skip lines up to column labels... 127 LINE: while ($Line = GetTextLine(\*PACKAGEDATAFILE)) { 128 if ($Line !~ /^#/) { 129 last LINE; 130 } 131 } 132 133 # Process key/value pairs... 134 LINE: while ($Line = GetTextLine(\*PACKAGEDATAFILE)) { 135 if ($Line =~ /^#/) { 136 next LINE; 137 } 138 @LineWords = (); 139 @LineWords = quotewords($InDelim, 0, $Line); 140 if (@LineWords != 2) { 141 croak "Error: The number of data fields, @LineWords, in $PackageDataFile must be 2.\nLine: $Line..."; 142 } 143 ($Key, $Value) = @LineWords; 144 145 if (exists $PackageDataMap{$Key}) { 146 carp "Warning: Multiple entries for key, $Key, in $PackageDataFile. Ignoring current line.\nLine: $Line..."; 147 next LINE; 148 } 149 150 $PackageDataMap{$Key} = $Value; 151 } 152 close PACKAGEDATAFILE; 153 } 154