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