[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GT] Conf.pm improvement and enhancement
gt'ers
nicholas' trouble with backtest.pl got me messin with it and aliases yet
again and naturally i'd forgotten everything i knew about them since the
last time.
anyway it was painful relearning how they are specified and reused. so
i've been looking at the conf hash and associated stuff. the attached
patch removes comments from conf key-value pair lines (some how i managed
to create many with comments even though that isn't (wasn't) supported).
now they are. the comment is removed before the key-value is inserted
in the conf hash.
in addition, there is a new method to dump the entire conf hash (sorted by key)
to stderr. (this avoids wiping out your current .gt/options file that is
repleat with useful blank lines, comments and is ordered the way you want).
meanwhile, i'm looking at GT::Tools::resolve_alias. the recent tw improvements
have helped, but there is more that can be done to make error messages more
informative and dare i say it 'helpful'.
ras
--- ../svn_repo/GT/old_Conf.pm Wed Apr 30 10:06:22 2008
+++ ../svn_repo/GT/Conf.pm Wed Apr 30 10:38:42 2008
@@ -1,12 +1,11 @@
package GT::Conf;
# Copyright 2000-2002 Raphaël Hertzog, Fabien Fulhaber
+# enhancements by ras copyright 2007-2008
# This file is distributed under the terms of the General Public License
# version 2 or (at your option) any later version.
-# baseline: 24 Apr 2005 2370 bytes
-# revised based on update 01jan07
-# $Id$
+# $Id: Conf.pm 555 2008-02-27 01:03:15Z ras $
use strict;
use vars qw(%conf);
@@ -51,9 +50,23 @@
day_close, volume, date FROM stockprices WHERE symbol = '$code' ORDER \
BY date DESC
-the following shows why comments are not permitted on data lines:
+comments are permitted on data lines provided they can be distinguished from
+positional arguments markers (e.g. #1, #2, etc). in order to do this any
+trailing data line comment marker (#) must be surrounded by whitespace.
+the code is a bit more forgiving, using this regex (\s+#[\s\D]+.$)
+
+note that the comment must follow the end of the logical data line
+and terminates at the end of the logical line. logical line means the
+line after continuation processing has completed.
+
+examples:
+
+ Aliases::Global::TFS2[] SY:TFS #1 #2 | CS:SY:TFS #1 # comment
+ graphic::positions::buycolor "[0,135,0]" # very dark green
+ graphic::buysellarrows::buycolor "[0,135,0,64]" # semitransparent dark green
-Aliases::Global::TFS2[] SY:TFS #1 #2 | CS:SY:TFS #1
+ note: configuration keys are lower cased automatically regardless of how
+ they are defined, but their values are as specified when defined
=over
@@ -71,7 +84,6 @@
warn ("Could not find configuration file: $file") and return if (! -e $file);
- # changed from "< $file" per pg 625 programming perl 3rd ed.
open (FILE, "<", $file) || die "Can't open $file: $!\n";
my $buf = '';
while (<FILE>)
@@ -94,6 +106,9 @@
tr/[ \t]/[ \t]/s; # squeeze out multiple adjacent whitespaces
+ # remove trailing comment
+ s/(\s+#[\s\D]+.*)$//;
+
my ($key, $val) = split /\s+/, $_, 2;
$conf{lc($key)} = $val;
}
@@ -117,7 +132,6 @@
my ($file) = @_;
$file = _get_home_path() . "/.gt/options" if (! defined($file));
- # changed from "> $file" per pg 625 programming perl 3rd ed.
open (FILE, ">", $file) || die "Can't write on $file: $!\n";
foreach (sort keys %conf)
{
@@ -173,10 +187,6 @@
return "";
}
-#Helper function, returns the home directory
-#This is usually defined as the environment variable HOME on Unix like
-#systems, and HOMEDRIVE + HOMEPATH on Windows
-
=item C<< GT::Conf::=_get_home_path() >>
Helper function, returns the home directory environment variable HOME on Unix
@@ -193,6 +203,23 @@
warn "homedir not defined, may not be able to find configuration file";
}
return $homedir;
+}
+
+=item C<< GT::Conf::conf_dump >>
+
+Helper function, writes the entire configure key=value pairs on stderr.
+code example: GT::Conf::conf_dump;
+
+=cut
+sub conf_dump {
+ print STDERR "gt configuration file keys and values\n";
+ printf STDERR "%s\t%-36s\t%-s\n", "item", "key", "value";
+ my $i = 0;
+ foreach (sort keys %conf)
+ {
+ printf STDERR "%3d\t%-36s\t%-s\n", $i++, $_, $conf{$_};
+ }
+ print STDERR "\n\n";
}
=pod