[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GT] Bug fixes in GT::Conf
- To: devel <devel
AT
geniustrader.org>
- Subject: [GT] Bug fixes in GT::Conf
- From: Nick Fantes Huege <nfhuege
AT
gmail.com>
- Date: Fri, 23 Jul 2010 22:18:02 +0300
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:date:message-id :subject:from:to:content-type; bh=1kI/LW3ubbP8ipNWmGK19siBQBQddBgRm1Ah9fVQUA4=; b=cNThKmGx3K+WTB+Ewu45JSyjEkCZvKR6pYVBdmt9qjfQs49fkK8PkV/2xW2ymTC9nD PjdgBX6NYS6J0sh7CzKgRlhwXoj2GGxgEFrWCxWy7G/6ogYsTWCfVEcGsfewZR1PS8Tj HCfgXQPX+l++fUmJFGEOTLYaQG1OXzEV60q38=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=W85VFPviE6auYIgT395jkP1WTPGaTAjcNnddLoHWbHCBr/yAkcDJeStNAIWJMDRQ+S MmWF8JnDy0LyEp4LO7J0JCEruBZbCN0Csm+pD5a7rMmTG7F+B4DZzzkuHH0DO6YOAhtx nvK7iM5ucNQtNGt7i5CXleVW7V7YGZrT49uv0=
- Message-id: <AANLkTiksk3XDsf5qUR5rb38hvvpyVUyAr48QKMu2JcC+@mail.gmail.com> (sfid-20100723_211923_692925_6D7B0F1B)
- Reply-to: devel
AT
geniustrader.org
When GT/Conf.pm reads the configuration file, it assumes that
statements that end in a backslash continue on the next line. It also
allows for inline comments behind a pound sign.
Currently multi-line statements have to have the \ symbol as their
very last character, otherwise they won't concatenate the next line.
So, for example if you have a couple of spaces after the ending \, it
won't work.
Similar problem with the inline comments. If you have a # Comment
right behind the \ sign, it won't work properly.
Attached is a fixed version of Conf.pm, with this and other minor
modifications. Here is also a Pastebin http://pastebin.com/XZvzWnjY
This module is completely backwards compatible with the old one.
The changes are:
1. Fixed multi-line statements with inline comments
2. Improved _get_home_path function (for Windows)
3. Added sub vars which returns a hash_ref to all config options.
Sometimes it's easier to use $conf->{'db::module'} instead
>::Conf::get('DB::module'), because the former can be interpolated
in strings.
4. Minor code reorders and improvements.
Note that I have removed all comments and pod from the module, because
for some reason they mess up my debugger.
If you find this patch to be useful, please feel free to add all the
commends and pod back.
Best regards,
Nick
package GT::Conf;
use strict;
use vars qw(%conf);
BEGIN {
$conf{'HOMEPATH'} = $ENV{'HOME'} || $ENV{'USERPROFILE'} || '';
}
sub load {
my $file = $_[0] || "$conf{'HOMEPATH'}/.gt/options";
if ( !-r $file ) {
warn("Could not find configuration file: $file");
return;
}
open( FILE, "<$file" ) || die "Can't open $file: $!\n";
my $line = '';
while (<FILE>) {
chop while /(\n|\r|\s)$/;
s/^\s+//;
s/\s*#\D+.*$//;
next if !$_;
$line .= $_;
next if ( $line =~ s/\\$/ / );
$line =~ tr/[ \t]/[ \t]/s;
my ( $key, $val ) = split /\s+/, $line, 2;
$conf{ lc($key) } = $val;
$line = '';
}
close FILE;
foreach my $kind (
"Signals", "Indicators",
"Systems", "CloseStrategy",
"MoneyManagement", "TradeFilters",
"OrderFactory", "Analyzers"
)
{
foreach my $file (
"$conf{'HOMEPATH'}/.gt/aliases/" . lc($kind),
GT::Conf::get("Path::Aliases::$kind")
)
{
next unless defined $file;
next if not -e $file;
open( ALIAS, "<$file" ) || die "Can't open $file : $!\n";
while (<ALIAS>) {
if (/^\s*(\S+)\s+(.*)$/) {
GT::Conf::default( "Aliases::$kind\::$1", $2 );
}
}
close ALIAS;
}
}
}
sub store {
my $file = $_[0] || "$conf{'HOMEPATH'}/.gt/options";
open( FILE, ">$file" ) || die "Can't write $file: $!\n";
for ( sort keys %conf ) { printf FILE "$_\t$conf{$_}\n" }
close FILE;
}
sub clear { %conf = () }
sub get { return $conf{ lc( $_[0] ) } || $_[1] || undef }
sub set { $conf{ lc( $_[0] ) } = $_[1] }
sub vars { return \%conf }
sub default {
my ( $key, $val ) = @_;
$key = lc($key);
if ( !defined( $conf{$key} ) ) {
$conf{$key} = $val;
}
}
sub get_first {
my (@keys) = @_;
foreach (@keys) {
my $value = &get($_);
return $value if ( defined($value) && $value );
}
return '';
}
sub _get_home_path { return $conf{'HOMEPATH'} }
sub conf_dump {
my $regex = $_[0] || '.*';
print STDERR
"gt configuration file keys and values filtered using \"$regex\"\n";
printf STDERR "%s\t%-36s\t%-s\n", "item", "key", "value";
my $i = 0;
foreach ( sort keys %conf ) {
( grep m/$regex/i, $_ )
? printf STDERR "%3d\t%-36s\t%-s\n", $i, $_, $conf{$_}
: ();
++$i;
}
print STDERR "\n\n";
}
sub get_gt_root { return &get('GT::Root') || $ENV{'GT_ROOT'} || '' }
1;