comparison mayachemtools/lib/PackageInfo.pm @ 0:73ae111cf86f draft

Uploaded
author deepakjadmin
date Wed, 20 Jan 2016 11:55:01 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:73ae111cf86f
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
155 1;
156
157 __END__
158
159 =head1 NAME
160
161 PackageInfo
162
163 =head1 SYNOPSIS
164
165 use PackageInfo;
166
167 use PackageInfo qw(:all);
168
169 =head1 DESCRIPTION
170
171 B<PackageInfo> module provides the following functions:
172
173 GetPackageKeyValue, IsPackageKeyNameAvailable, SetPackageKeyValue
174
175 The functions to set and get package keyvalues not explicitly defined in this module
176 are implemented using Perl's AUTOLOAD functionality. These methods are generated
177 on-the-fly for a specified key:
178
179 Set<KeyName>(<KeyValue>);
180 $KeyValue = Get<KeyName>();
181
182 B<PackageInfo> module provides functionality to retrieve information about MayaChemTools
183 package from PackagaInfo.csv which contains the following types key name and values:
184
185 "KeyName","KeyValue"
186 "PackageName","MayaChemTools"
187 "ReleaseDate","Oct 21, 2010"
188 "VersionNumber","7.4"
189 "DevSoftwareEnvironment","Cygwin on Windows XP"
190 ... ...
191 ... ...
192
193 =head1 FUNCTIONS
194
195 =over 4
196
197 =item B<GetPackageKeyValue>
198
199 $KeyValue = GetPackageKeyValue($KeyName);
200
201 Returns B<KeyValue> for a specified I<KeyName>.
202
203 =item B<IsPackageKeyNameAvailable>
204
205 $Status = IsPackageKeyNameAvailable($KeyName);
206
207 Returns 1 or 0 based on whether I<KeyName> is available in package info file.
208
209 =item B<SetPackageKeyValue>
210
211 SetPackageKeyValue($KeyName, $KeyValue);
212
213 Sets I<KeyValue> for a I<KeyName>. No data is written to package info file.
214
215 =back
216
217 =head1 AUTHOR
218
219 Manish Sud <msud@san.rr.com>
220
221 =head1 COPYRIGHT
222
223 Copyright (C) 2015 Manish Sud. All rights reserved.
224
225 This file is part of MayaChemTools.
226
227 MayaChemTools is free software; you can redistribute it and/or modify it under
228 the terms of the GNU Lesser General Public License as published by the Free
229 Software Foundation; either version 3 of the License, or (at your option)
230 any later version.
231
232 =cut