File Coverage

blib/lib/Finance/GeniusTrader/Conf.pm
Criterion Covered Total %
statement 6 64 9.3
branch 0 36 0.0
condition 0 8 0.0
subroutine 2 10 20.0
pod 7 7 100.0
total 15 125 12.0


line stmt bran cond sub pod time code
1             package Finance::GeniusTrader::Conf;
2              
3             # Copyright 2000-2002 Raphaël Hertzog, Fabien Fulhaber
4             # This file is distributed under the terms of the General Public License
5             # version 2 or (at your option) any later version.
6              
7             # baseline: 24 Apr 2005 2370 bytes
8             # revised based on update 01jan07
9             # $Id$
10              
11 1     1   5 use strict;
  1         2  
  1         38  
12 1     1   5 use vars qw(%conf);
  1         2  
  1         1192  
13              
14             =head1 NAME
15              
16             Finance::GeniusTrader::Conf - Manage configuration
17              
18             =head1 DESCRIPTION
19              
20             This module provides functions to manage personal GeniusTrader configuration.
21             The configuration information are stored in file ~/.gt/options by default.
22              
23             The configuration file format is similar to a perl hash, in other words, a key
24             followed by data for that key. keys are delimited from their value by whitespace.
25             key values can contain embedded whitespace.
26              
27             key value strings can be continued across multiple lines by delimiting the
28             newline with a backslash (\) (watch out for trailing whitespace after the \ and
29             before the newline).
30              
31             comments introduced with a # as the first character on a line. data lines
32             cannot contain a comment since the # character is used in many data strings.
33              
34             blank lines and lines with only whitespace are ignored.
35              
36             =head1 EXAMPLES of ~/.gt/options Entries
37              
38             # this is an example of a comment
39              
40             DB::module genericdbi
41              
42             DB::bean::dbname beancounter
43              
44             Graphic::Candle::UpBorderColor "[0,180,80]"
45              
46             Graphic::Candle::DownBorderColor "[180,0,80]"
47              
48             this example shows how continuing key values across lines can be useful.
49              
50             DB::genericdbi::prices_sql SELECT day_open, day_high, day_low, \
51             day_close, volume, date FROM stockprices WHERE symbol = '$code' ORDER \
52             BY date DESC
53              
54             the following shows why comments are not permitted on data lines:
55              
56             Aliases::Global::TFS2[] SY:TFS #1 #2 | CS:SY:TFS #1
57              
58             =over
59              
60             =head1 FUNCTIONS
61              
62             =item C<< Finance::GeniusTrader::Conf::load([ $file ]) >>
63              
64             Load the configuration from the indicated file. If the file is omitted
65             then it looks at ~/.gt/options by default.
66              
67             =cut
68             sub load {
69 0     0 1   my ($file) = @_;
70 0 0         $file = _get_home_path() . "/.gt/options" if (! defined($file));
71              
72 0 0 0       warn ("Could not find configuration file: $file") and return if (! -e $file);
73              
74             # changed from "< $file" per pg 625 programming perl 3rd ed.
75 0 0         open (FILE, "<", $file) || die "Can't open $file: $!\n";
76 0           my $buf = '';
77 0           while ()
78             {
79 0           chomp;
80              
81             # ras hack -- allow multi-line values in gt options file
82 0 0         if ( /\\$/ ) {
83 0           s/\\//; # remove \
84 0           $buf .= $_; # save line
85 0           next; # get next line
86             } else {
87 0           $_ = $buf . $_; # collect complete line into $_
88 0           $buf = ''; # reset line buffer
89             }
90              
91 0 0         next if /^\s*#/; # remove lines containing whitespace and/or comments
92 0 0         next if /^\s*$/; # remove blank lines
93 0           s/^\s*//; s/\s*$//; # remove leading and trailing whitespace
  0            
94              
95 0           tr/[ \t]/[ \t]/s; # squeeze out multiple adjacent whitespaces
96              
97 0           my ($key, $val) = split /\s+/, $_, 2;
98 0           $conf{lc($key)} = $val;
99             }
100 0           close FILE;
101              
102             # Load the various definition of aliases
103            
104 0           foreach my $kind ("Signals", "Indicators", "Systems", "CloseStrategy",
105             "MoneyManagement", "TradeFilters", "OrderFactory",
106             "Analyzers")
107             {
108 0           foreach my $file (Finance::GeniusTrader::Conf::_get_home_path()."/.gt/aliases/".lc($kind),
109             Finance::GeniusTrader::Conf::get("Path::Aliases::$kind"))
110             {
111 0 0         next unless defined $file;
112 0 0         next if not -e $file;
113 0 0         open(ALIAS, "<", "$file") || die "Can't open $file : $!\n";
114 0           while (defined($_=)) {
115 0 0         if (/^\s*(\S+)\s+(.*)$/) {
116 0           Finance::GeniusTrader::Conf::default("Aliases::$kind\::$1", $2);
117             }
118             }
119 0           close ALIAS;
120             }
121             }
122              
123             }
124              
125             =item C<< Finance::GeniusTrader::Conf::clear() >>
126              
127             Clear all the configuration.
128              
129             =cut
130 0     0 1   sub clear { %conf = () }
131              
132             =item C<< Finance::GeniusTrader::Conf::store($file) >>
133              
134             Write all the current configuration in the given file. Note: all prior
135             commentary, if any is lost.
136              
137             =cut
138             sub store {
139 0     0 1   my ($file) = @_;
140 0 0         $file = _get_home_path() . "/.gt/options" if (! defined($file));
141              
142             # changed from "> $file" per pg 625 programming perl 3rd ed.
143 0 0         open (FILE, ">", $file) || die "Can't write on $file: $!\n";
144 0           foreach (sort keys %conf)
145             {
146 0           print FILE $_ . "\t" . $conf{$_} . "\n";
147             }
148 0           close FILE;
149             }
150              
151             =item C<< Finance::GeniusTrader::Conf::get($key,$defaultValue) >>
152              
153             Return the configuration value for the given key. If the
154             key doesn't exist, it returns the optional defaultValue.
155              
156             If neither the key nor defaultValue exist, it returns undef.
157              
158             =cut
159             sub get {
160 0     0 1   my $value = $conf{lc($_[0])};
161 0 0         return $value if (defined($value));
162 0 0         return $_[1] if(defined($_[1]));
163 0           return undef;
164             }
165              
166             =item C<< Finance::GeniusTrader::Conf::set($key, $value) >>
167              
168             Set the given configuration item to the corresponding value. Replaces any
169             previous value.
170              
171             =cut
172 0     0 1   sub set { $conf{lc($_[0])} = $_[1] }
173              
174             =item C<< Finance::GeniusTrader::Conf::default($key, $value) >>
175              
176             Set a default value to the given item. Must be called by GT itself to
177             give reasonable default values to most of configurations items.
178              
179             =cut
180             sub default {
181 0     0 1   my ($key, $val) = @_;
182 0           $key = lc($key);
183 0 0         if (! defined($conf{$key}))
184             {
185 0           $conf{$key} = $val;
186             }
187             }
188              
189             =item C<< Finance::GeniusTrader::Conf::get_first($key, ...) >>
190              
191             Return the value of the first item that does have a non-zero value.
192              
193             =cut
194             sub get_first {
195 0     0 1   my (@keys) = @_;
196 0           foreach (@keys) {
197 0           my $value = get($_);
198 0 0 0       if (defined($value) && $value) {
199 0           return $value;
200             }
201             }
202 0           return "";
203             }
204              
205             #Helper function, returns the home directory
206             #This is usually defined as the environment variable HOME on Unix like
207             #systems, and HOMEDRIVE + HOMEPATH on Windows
208              
209             =item C<< Finance::GeniusTrader::Conf::=_get_home_path() >>
210              
211             Helper function, returns the home directory environment variable HOME on Unix
212             or on windows the environment variables HOMEDRIVE . HOMEPATH
213              
214             =cut
215             sub _get_home_path {
216 0     0     my $homedir = '';
217 0 0 0       if (defined($ENV{'HOME'})) {
    0          
218 0           $homedir = $ENV{'HOME'};
219             } elsif (defined($ENV{'HOMEDRIVE'}) && defined($ENV{'HOMEPATH'})) {
220 0           $homedir = $ENV{'HOMEDRIVE'} . $ENV{'HOMEPATH'};
221             } else {
222 0           warn "homedir not defined, may not be able to find configuration file";
223             }
224 0           return $homedir;
225             }
226              
227             =pod
228              
229             =back
230              
231             =cut
232             1;