-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwkdiary
executable file
·190 lines (141 loc) · 3.31 KB
/
wkdiary
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/perl
# edit diary entries
use warnings;
use strict;
use Getopt::Long;
use DateTime;
use WkDB;
use WkDB::Jaufs::Diary;
# TODO: POD
# TODO: add option to use existing file
my $wk = WkDB->new;
my $skip;
my $opt_athlete = $wk->config('athlete');
my $wanthelp;
my $needhelp;
GetOptions(
'athlete=s' => \$opt_athlete,
'help!' => \$wanthelp,
'skip!' => \$skip,
) or ++$needhelp;
if( $wanthelp ){
print <<EOF;
usage: $0 [<datespec>]
edit wkdb diary entries.
Options:
--skip prepare existing entries for skip
--help this message
datespecs:
today default
yesterday
-x today - x days
yyyy-mm-dd specified date
[from]:[to] range of datespecs
EOF
exit 0;
}
my( $from, $to );
my $spec = shift;
if( $spec && $spec =~ /:/ ){
my @spec = split(/:/,$spec);
$from = &parse_datespec( shift @spec );
$to = &parse_datespec( shift @spec );
} else {
$from = $to = &parse_datespec( $spec );
}
if( ! $from ){
print STDERR "missing/invalid 'from' datespec\n";
++$needhelp;
}
if( ! $to ){
print STDERR "missing/invalid 'to' datespec\n";
++$needhelp;
}
if( $from && $to && $from > $to ){
print STDERR "invalid datespec range\n";
++$needhelp;
}
if( $needhelp ){
print STDERR "use --help for usage info\n";
exit 1;
}
my $db = $wk->db;
$opt_athlete ||= $wk->config( 'athlete' );
my $athlete = $db->resultset('Athlete')->find( $opt_athlete );
if( ! $athlete ){
print STDERR "no such athlete: $opt_athlete\n";
exit 1;
}
# write textfile
print $athlete->name, ": preparing entries from ", $from->ymd ," to ", $to->ymd, "...\n";
my $list = &build_list( $from, $to );
my $jaufs = WkDB::Jaufs::Diary->new({
wk => $wk,
});
$jaufs->edit( $list, \&process_list );
sub build_list {
my( $from, $to ) = @_;
my @list;
for( my $date = $from->clone; $date <= $to; $date->add( days => 1 ) ){
if( my $row = $athlete->search_related( 'diary', {
day => $date->ymd,
})->first ){
push @list, {
$row->get_inflated_columns,
action => $skip ? 'skip' : 'save',
};
} else {
push @list, {
athlete => $athlete,
day => $date->clone,
action => 'save',
};
}
}
\@list;
}
sub process_list {
my( $list ) = @_;
foreach my $ent ( @$list ){
if( ! $ent->{action} || $ent->{action} eq 'skip' ){
print "skipping ", $ent->{day}->ymd,"...\n";
# do nothing
} elsif( $ent->{action} eq 'delete' ){
print "deleting ", $ent->{day}->ymd,"...\n";
$athlete->search_related( 'diary', {
day => $ent->{day}->ymd,
})->delete;
} elsif( $ent->{action} eq 'save' ){
my %dat = %$ent;
delete $dat{action};
if( my $row = $ent->{athlete}->search_related( 'diary', {
day => $ent->{day}->ymd,
})->first ){
print "updating ", $ent->{day}->ymd,"...\n";
$row->update(\%dat);
} else {
print "adding ", $ent->{day}->ymd,"...\n";
$ent->{athlete}->add_to_diary(\%dat);
}
}
}
1;
}
sub parse_datespec {
my $spec = shift;
if( ! defined $spec || $spec =~ /^\s*(|today)\s*$/i ){
return DateTime->today;
} elsif( $spec =~ /^\s*yesterday\s*$/i ){
return DateTime->today->add( days => -1 );
} elsif( $spec =~ /^\s*(-\d+)\s*$/i ){
return DateTime->today->add( days => $1 );
} elsif( $spec =~ /^\s*(\d+)-(\d+)-(\d+)\s*$/i ){
return DateTime->new(
year => $1,
month => $2,
day => $3,
time_zone => 'local',
);
}
return;
}