GeniusTrader/Conf.pm

filter: Statementstmt 10.7% 7 / 65 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 20.0% 2 / 10 Podpod 100% 7 / 7 totaltotal 11.5% 16 / 139 CC 28 SCAR 48.3
CC
28
Cov
6%
load
272.0
store
20.0
get_first
20.0
CRAP 125.6
« GeniusTrader/Report.pm ↑ Summary GeniusTrader/DB/MetaStock.pm »
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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use vars qw(%conf);
Subroutine BEGIN: called CC=1 SCAR=0.0
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 0 ▶sub load {
Subroutine load: not called CC=16 SCAR=56.1
Pod documented
69 0 my ($file) = @_;
70 0 ▶ $file = _get_home_path() . "/.gt/options" if (! defined($file));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 0 0
71  
72 0 ▶ warn ("Could not find configuration file: $file") and return if (! -e $file);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 0 0
Condition0%0/5
-e $file || warn("Could not find configuration file: $file") && (return)
l!l&&r!l&&!r
000
warn "Could not find configuration file: $file" and return
!ll
00
MC/DC0%0/2
-e $file || warn("Could not find configuration file: $file") && (return)
-e $file warn "Could not find configuration file: $file"
Truth table0%0/3
A || B
A= -e $fileB= warn "Could not find configuration file: $file"
ABresult
1X1
011
000
73  
74 # changed from "< $file" per pg 625 programming perl 3rd ed.
75 0 ▶ open (FILE, "<", $file) || die "Can't open $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<", $file or die "Can't open ${file}: $!\n" 0 0 0
76 0 my $buf = '';
77 0 ▶ while (<FILE>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 0 0 0
78 {
79 0 chomp;
80  
81 # ras hack -- allow multi-line values in gt options file
82 0 ▶ if ( /\\$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\\$/) { } 0 0 0
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 ▶ next if /^\s*#/; # remove lines containing whitespace and/or comments
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*#/ 0 0 0
92 0 ▶ next if /^\s*$/; # remove blank lines
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 0 0 0
93 0 s/^\s*//; s/\s*$//; # remove leading and trailing whitespace
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 ▶ next unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 0 0
112 0 ▶ next if not -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 0 0
113 0 ▶ open(ALIAS, "<", "$file") || die "Can't open $file : $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open ALIAS, "<", "$file" or die "Can't open $file : $!\n" 0 0 0
114 0 ▶ while (defined($_=<ALIAS>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline ALIAS)) 0 0 0
115 0 ▶ if (/^\s*(\S+)\s+(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*(\S+)\s+(.*)$/) 0 0 0
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 ▶sub clear { %conf = () }
Subroutine clear: not called CC=1 SCAR=6.9
Pod documented
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 0 ▶sub store {
Subroutine store: not called CC=4 SCAR=30.0
Pod documented
139 0 my ($file) = @_;
140 0 ▶ $file = _get_home_path() . "/.gt/options" if (! defined($file));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 0 0
141  
142 # changed from "> $file" per pg 625 programming perl 3rd ed.
143 0 ▶ open (FILE, ">", $file) || die "Can't write on $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, ">", $file or die "Can't write on ${file}: $!\n" 0 0 0
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 0 ▶sub get {
Subroutine get: not called CC=3 SCAR=24.8
Pod documented
160 0 my $value = $conf{lc($_[0])};
161 0 ▶ return $value if (defined($value));
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
162 0 ▶ return $_[1] if(defined($_[1]));
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 0 0 0
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 ▶sub set { $conf{lc($_[0])} = $_[1] }
Subroutine set: not called CC=1 SCAR=6.9
Pod documented
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 0 ▶sub default {
Subroutine default: not called CC=2 SCAR=17.9
Pod documented
181 0 my ($key, $val) = @_;
182 0 $key = lc($key);
183 0 ▶ if (! defined($conf{$key}))
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $Finance::GeniusTrader::Conf::conf{$key}) 0 0 0
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 0 ▶sub get_first {
Subroutine get_first: not called CC=4 SCAR=30.0
Pod documented
195 0 my (@keys) = @_;
196 0 foreach (@keys) {
197 0 my $value = get($_);
198 0 ▶ if (defined($value) && $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and $value) 0 0 0
Condition0%0/3
defined $value and $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and $value
defined $value $value
Truth table0%0/3
A and B
A= defined $valueB= $value
ABresult
0X0
100
111
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 0 ▶sub _get_home_path {
Subroutine _get_home_path: not called CC=4 SCAR=30.0
216 0 my $homedir = '';
217 0 ▶ if (defined($ENV{'HOME'})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $ENV{'HOME'}) { } 0 0 0
elsif (defined $ENV{'HOMEDRIVE'} and defined $ENV{'HOMEPATH'}) { } 0 0 0
218 0 $homedir = $ENV{'HOME'};
219 ▶ } elsif (defined($ENV{'HOMEDRIVE'}) && defined($ENV{'HOMEPATH'})) {
Condition0%0/3
defined $ENV{"HOMEDRIVE"} and defined $ENV{"HOMEPATH"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ENV{"HOMEDRIVE"} and defined $ENV{"HOMEPATH"}
defined $ENV{"HOMEDRIVE"} defined $ENV{"HOMEPATH"}
Truth table0%0/3
A and B
A= defined $ENV{"HOMEDRIVE"}B= defined $ENV{"HOMEPATH"}
ABresult
0X0
100
111
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 1;
« GeniusTrader/Report.pm ↑ Summary GeniusTrader/DB/MetaStock.pm »