| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Jaipo::Config; |
|
2
|
3
|
|
|
3
|
|
45277
|
use warnings; |
|
|
3
|
|
|
|
|
3
|
|
|
|
3
|
|
|
|
|
70
|
|
|
3
|
3
|
|
|
3
|
|
9
|
use strict; |
|
|
3
|
|
|
|
|
3
|
|
|
|
3
|
|
|
|
|
56
|
|
|
4
|
3
|
|
|
3
|
|
1184
|
use Hash::Merge; |
|
|
3
|
|
|
|
|
5449
|
|
|
|
3
|
|
|
|
|
107
|
|
|
5
|
3
|
|
|
3
|
|
1197
|
use YAML::Syck; |
|
|
3
|
|
|
|
|
4004
|
|
|
|
3
|
|
|
|
|
156
|
|
|
6
|
|
|
|
|
|
|
Hash::Merge::set_behavior ('RIGHT_PRECEDENT'); |
|
7
|
3
|
|
|
3
|
|
1141
|
use Number::RecordLocator; |
|
|
3
|
|
|
|
|
86928
|
|
|
|
3
|
|
|
|
|
102
|
|
|
8
|
|
|
|
|
|
|
|
|
9
|
3
|
|
|
3
|
|
15
|
use base qw/Class::Accessor::Fast/; |
|
|
3
|
|
|
|
|
4
|
|
|
|
3
|
|
|
|
|
1238
|
|
|
10
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors (qw/stash last_sp_cnt/); |
|
11
|
|
|
|
|
|
|
|
|
12
|
3
|
|
|
3
|
|
7798
|
use vars qw/$CONFIG/; |
|
|
3
|
|
|
|
|
4
|
|
|
|
3
|
|
|
|
|
2147
|
|
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
|
15
|
1
|
|
|
1
|
1
|
378
|
my $class = shift; |
|
16
|
1
|
|
|
|
|
2
|
my %args = @_; |
|
17
|
|
|
|
|
|
|
|
|
18
|
1
|
|
|
|
|
1
|
my $self = {}; |
|
19
|
1
|
|
|
|
|
2
|
bless $self , $class; |
|
20
|
1
|
|
|
|
|
3
|
$self->stash( {} ); |
|
21
|
1
|
|
|
|
|
39
|
$self->last_sp_cnt ( 500000 + int( rand 100 ) ); |
|
22
|
1
|
|
|
|
|
5
|
$self->load; |
|
23
|
1
|
|
|
|
|
3
|
return $self; |
|
24
|
|
|
|
|
|
|
} |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub app_config_path { |
|
27
|
1
|
|
|
1
|
0
|
1
|
my $self = shift; |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# XXX: get application config path for different platform |
|
30
|
|
|
|
|
|
|
# windows , unix or ... |
|
31
|
1
|
|
|
|
|
6
|
return $ENV{HOME} . '/.jaipo.yml'; |
|
32
|
|
|
|
|
|
|
} |
|
33
|
|
|
|
|
|
|
|
|
34
|
4
|
|
|
4
|
0
|
7
|
sub app { return shift->_get( application => @_ ) } |
|
35
|
|
|
|
|
|
|
|
|
36
|
0
|
|
|
0
|
0
|
0
|
sub user { return shift->_get( user => @_ ) } |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# A teeny helper |
|
39
|
4
|
|
|
4
|
|
7
|
sub _get { return $_[0]->stash->{ $_[1] }{ $_[2] } } |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub save { |
|
43
|
1
|
|
|
1
|
0
|
1
|
my $self = shift; |
|
44
|
1
|
|
33
|
|
|
3
|
my $config_filepath = shift || $self->app_config_path; |
|
45
|
|
|
|
|
|
|
|
|
46
|
1
|
|
|
|
|
80
|
open CONFIG_FH , ">" , $config_filepath; |
|
47
|
1
|
|
|
|
|
4
|
print CONFIG_FH Dump( $self->stash ); |
|
48
|
1
|
|
|
|
|
107
|
close CONFIG_FH; |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub set_service_option { |
|
53
|
1
|
|
|
1
|
0
|
845
|
my $self = shift; |
|
54
|
1
|
|
|
|
|
2
|
my $sp_name = shift; |
|
55
|
1
|
|
|
|
|
1
|
my $opt = shift; |
|
56
|
|
|
|
|
|
|
|
|
57
|
1
|
|
|
|
|
2
|
my $new_config = $self->stash; |
|
58
|
1
|
|
|
|
|
3
|
my @sps = @{ $self->app('Services') }; |
|
|
1
|
|
|
|
|
2
|
|
|
59
|
1
|
|
|
|
|
8
|
for( my $i=0; my $sp = $sps[ $i ] ; $i++ ) { |
|
60
|
1
|
|
|
|
|
1
|
my $c_spname = join q{},keys %{ $sps[$i] } ; |
|
|
1
|
|
|
|
|
3
|
|
|
61
|
|
|
|
|
|
|
|
|
62
|
1
|
50
|
33
|
|
|
31
|
if( $c_spname eq $sp_name |
|
63
|
|
|
|
|
|
|
and $sp->{ $c_spname }->{sp_id} eq $opt->{sp_id} ) |
|
64
|
|
|
|
|
|
|
{ |
|
65
|
1
|
|
|
|
|
5
|
$new_config->{application}{Services}->[$i]->{ $c_spname } = $opt |
|
66
|
|
|
|
|
|
|
} |
|
67
|
|
|
|
|
|
|
} |
|
68
|
1
|
|
|
|
|
3
|
$self->stash( $new_config ); |
|
69
|
|
|
|
|
|
|
} |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head2 find_service_option_by_name |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Returns a config hash |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=cut |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub find_service_option_by_name { |
|
78
|
1
|
|
|
1
|
1
|
6
|
my $self = shift; |
|
79
|
1
|
|
|
|
|
1
|
my $name = shift; |
|
80
|
1
|
|
|
|
|
1
|
my @services = @{ $self->app ('Services') }; |
|
|
1
|
|
|
|
|
2
|
|
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# @services = grep { $name eq shift keys $_ }, @services; |
|
83
|
|
|
|
|
|
|
|
|
84
|
1
|
|
|
|
|
4
|
my @configs = (); |
|
85
|
1
|
|
|
|
|
2
|
map { my ($p)=keys %$_; |
|
|
1
|
|
|
|
|
2
|
|
|
86
|
1
|
50
|
|
|
|
15
|
push @configs,values %$_ |
|
87
|
|
|
|
|
|
|
if $p =~ m/\Q$name/ } @services; |
|
88
|
1
|
50
|
|
|
|
3
|
return wantarray ? @configs : $configs[0]; |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head2 find_service_option_by_trigger |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
in list context , returns an array of one or more service plugin |
|
95
|
|
|
|
|
|
|
config hash. |
|
96
|
|
|
|
|
|
|
in scalar contxt , returnes a config hash |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=cut |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub find_service_option_by_trigger { |
|
101
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
|
102
|
0
|
|
|
|
|
0
|
my $trigger = shift; |
|
103
|
0
|
|
|
|
|
0
|
my @services = @{ $self->app ('Services') }; |
|
|
0
|
|
|
|
|
0
|
|
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# @services = grep { $name eq shift keys $_ }, @services; |
|
106
|
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
0
|
my @configs = (); |
|
108
|
|
|
|
|
|
|
map { |
|
109
|
0
|
|
|
|
|
0
|
my ($v)=values %$_; |
|
|
0
|
|
|
|
|
0
|
|
|
110
|
|
|
|
|
|
|
push @configs,$v |
|
111
|
0
|
0
|
|
|
|
0
|
if $v->{trigger_name} eq $trigger; |
|
112
|
|
|
|
|
|
|
} @services; |
|
113
|
0
|
0
|
|
|
|
0
|
return wantarray ? @configs : $configs[0]; |
|
114
|
|
|
|
|
|
|
} |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head2 load |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=cut |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub load { |
|
123
|
1
|
|
|
1
|
1
|
1
|
my $self = shift; |
|
124
|
1
|
|
33
|
|
|
4
|
my $config_filepath = shift || $self->app_config_path; |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
# if we can not find yml config file |
|
127
|
|
|
|
|
|
|
# load from default function , then write back to config file |
|
128
|
1
|
|
|
|
|
1
|
my $config; |
|
129
|
|
|
|
|
|
|
|
|
130
|
1
|
50
|
|
|
|
93
|
if ( -e $config_filepath ) { # XXX: check config version |
|
131
|
0
|
|
|
|
|
0
|
$config = LoadFile( $config_filepath ); |
|
132
|
0
|
|
|
|
|
0
|
$self->stash ($config); |
|
133
|
|
|
|
|
|
|
} |
|
134
|
|
|
|
|
|
|
else { |
|
135
|
1
|
|
|
|
|
3
|
$config = $self->load_default_config; |
|
136
|
1
|
|
|
|
|
88
|
$self->stash ($config); |
|
137
|
|
|
|
|
|
|
# write back |
|
138
|
1
|
|
|
|
|
6
|
$self->save( $config_filepath ); |
|
139
|
|
|
|
|
|
|
} |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
# Canicalization |
|
142
|
1
|
|
|
|
|
5
|
$self->set_sp_id; |
|
143
|
1
|
|
|
|
|
9
|
$self->set_default_trigger_name; |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
# load user jaipo yaml config from file here |
|
146
|
1
|
|
|
|
|
3
|
return $config; |
|
147
|
|
|
|
|
|
|
} |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub set_default_trigger_name { |
|
150
|
1
|
|
|
1
|
0
|
1
|
my $self = shift; |
|
151
|
1
|
|
|
|
|
2
|
my $new_config = $self->stash; |
|
152
|
1
|
|
|
|
|
3
|
my @sps = @{ $self->app('Services') }; |
|
|
1
|
|
|
|
|
2
|
|
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
# set default trigger name for each service plugin that has no trigger name |
|
155
|
1
|
|
|
|
|
4
|
my %triggers; |
|
156
|
1
|
|
|
|
|
3
|
for( my $i=0; my $sp = $sps[$i] ; $i++ ) { |
|
157
|
1
|
|
|
|
|
1
|
my $c_spname = join q{},keys %{ $sp } ; |
|
|
1
|
|
|
|
|
2
|
|
|
158
|
|
|
|
|
|
|
|
|
159
|
1
|
|
|
|
|
5
|
my $tn = lc $c_spname; |
|
160
|
1
|
|
|
|
|
3
|
$tn .= '_' while( exists $triggers{ $tn } ); |
|
161
|
|
|
|
|
|
|
|
|
162
|
1
|
|
33
|
|
|
5
|
$new_config->{application}{Services}->[$i]->{ $c_spname }->{trigger_name} ||= $tn; |
|
163
|
1
|
|
|
|
|
3
|
$triggers{ $new_config->{application}{Services}->[$i]->{ $c_spname }->{trigger_name} } = 1; |
|
164
|
|
|
|
|
|
|
} |
|
165
|
|
|
|
|
|
|
|
|
166
|
1
|
|
|
|
|
3
|
$self->stash( $new_config ); |
|
167
|
|
|
|
|
|
|
} |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub set_sp_id { |
|
170
|
1
|
|
|
1
|
0
|
1
|
my $self = shift; |
|
171
|
1
|
|
|
|
|
3
|
my $sp_cnt = $self->last_sp_cnt; |
|
172
|
1
|
|
|
|
|
5
|
my $new_config = $self->stash; |
|
173
|
1
|
|
|
|
|
3
|
my @sps = @{ $self->app('Services') }; |
|
|
1
|
|
|
|
|
2
|
|
|
174
|
1
|
|
|
|
|
10
|
my $num_rec = Number::RecordLocator->new; |
|
175
|
1
|
|
|
|
|
1234
|
for( my $i=0; $i < scalar @sps ; $i++ ) { |
|
176
|
1
|
|
|
|
|
2
|
my $c_spname = join q{},keys %{ $sps[$i] } ; |
|
|
1
|
|
|
|
|
14
|
|
|
177
|
|
|
|
|
|
|
|
|
178
|
1
|
50
|
|
|
|
3
|
if( ! defined $new_config->{application}{Services}->[$i]->{ $c_spname }->{sp_id} ) { |
|
179
|
1
|
|
|
|
|
4
|
my $sp_id = $num_rec->encode( $sp_cnt ++ ); |
|
180
|
1
|
|
|
|
|
612
|
$new_config->{application}{Services}->[$i]->{ $c_spname }->{sp_id} = $sp_id; |
|
181
|
1
|
|
|
|
|
3
|
$self->last_sp_cnt( $sp_cnt ); |
|
182
|
|
|
|
|
|
|
} |
|
183
|
|
|
|
|
|
|
} |
|
184
|
1
|
|
|
|
|
6
|
$self->stash( $new_config ); |
|
185
|
|
|
|
|
|
|
} |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
sub load_default_config { |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
# move this to a file later |
|
190
|
2
|
|
|
2
|
0
|
235
|
my $config = <<YAML; |
|
191
|
|
|
|
|
|
|
--- |
|
192
|
|
|
|
|
|
|
application: |
|
193
|
|
|
|
|
|
|
Verbose: 1 |
|
194
|
|
|
|
|
|
|
SavePassword: 0 |
|
195
|
|
|
|
|
|
|
Services: |
|
196
|
|
|
|
|
|
|
- Twitter: |
|
197
|
|
|
|
|
|
|
enable: 1 |
|
198
|
|
|
|
|
|
|
Plugins: {} |
|
199
|
|
|
|
|
|
|
user: {} |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
YAML |
|
202
|
2
|
|
|
|
|
7
|
return Load( $config ); |
|
203
|
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
} |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
1; |