| 1 | package App::JobLog::Config; | ||
| 2 | $App::JobLog::Config::VERSION = '1.042'; | ||
| 3 | # ABSTRACT: central depot for App::JobLog configuration parameters and controller allowing their modification | ||
| 4 | |||
| 5 | |||
| 6 | use Exporter 'import'; | ||
| 7 | our @EXPORT_OK = qw( | ||
| 8 | columns | ||
| 9 | day_length | ||
| 10 | dir | ||
| 11 | editor | ||
| 12 | hidden_columns | ||
| 13 | init_file | ||
| 14 | is_hidden | ||
| 15 | is_workday | ||
| 16 | log | ||
| 17 | merge | ||
| 18 | pay_period_length | ||
| 19 | precision | ||
| 20 | readme | ||
| 21 | start_pay_period | ||
| 22 | sunday_begins_week | ||
| 23 | time_zone | ||
| 24 | _tz | ||
| 25 | vacation | ||
| 26 | workdays | ||
| 27 | DAYS | ||
| 28 | DIRECTORY | ||
| 29 | HIDABLE_COLUMNS | ||
| 30 | HOURS | ||
| 31 | MERGE | ||
| 32 | NONE_COLUMN | ||
| 33 | PERIOD | ||
| 34 | PRECISION | ||
| 35 | SUNDAY_BEGINS_WEEK | ||
| 36 | TIME_ZONE | ||
| 37 | WORKDAYS | ||
| 38 | ); | ||
| 39 | |||
| 40 | use Class::Autouse qw{ | ||
| 41 | File::HomeDir | ||
| 42 | File::Spec | ||
| 43 | Config::Tiny | ||
| 44 | FileHandle | ||
| 45 | App::JobLog::Command::info | ||
| 46 | }; | ||
| 47 | use autouse 'File::Path' => qw(mkpath); | ||
| 48 | use autouse 'Cwd' => qw(abs_path); | ||
| 49 | use autouse 'Term::ReadKey' => qw(GetTerminalSize); | ||
| 50 | use Modern::Perl; | ||
| 51 | |||
| 52 | # default precision | ||
| 53 | use constant PRECISION => 2; | ||
| 54 | |||
| 55 | # default pay period | ||
| 56 | use constant PERIOD => 14; | ||
| 57 | |||
| 58 | # hours worked in day | ||
| 59 | use constant HOURS => 8; | ||
| 60 | |||
| 61 | # whether Sunday is the first day of the week | ||
| 62 | # otherwise it's Monday, as in DateTime | ||
| 63 | use constant SUNDAY_BEGINS_WEEK => 1; | ||
| 64 | |||
| 65 | # environment variables | ||
| 66 | |||
| 67 | # identifies directory to write files into | ||
| 68 | use constant DIRECTORY => 'JOB_LOG_DIRECTORY'; | ||
| 69 | |||
| 70 | # expected abbreviations for working days in week | ||
| 71 | use constant WORKDAYS => 'MTWHF'; | ||
| 72 | |||
| 73 | # expected abbreviations for weekdays | ||
| 74 | use constant DAYS => 'S' . WORKDAYS . 'A'; | ||
| 75 | |||
| 76 | # default level of merging | ||
| 77 | use constant MERGE => 'adjacent same tags'; | ||
| 78 | |||
| 79 | # name of hide nothing "column" | ||
| 80 | use constant NONE_COLUMN => 'none'; | ||
| 81 | |||
| 82 | # array of hidable columns | ||
| 83 | use constant HIDABLE_COLUMNS => [ | ||
| 84 | NONE_COLUMN, qw( | ||
| 85 | date | ||
| 86 | description | ||
| 87 | duration | ||
| 88 | tags | ||
| 89 | time | ||
| 90 | ) | ||
| 91 | ]; | ||
| 92 | |||
| 93 | # default time zone; necessary because Cygwin doesn't support local | ||
| 94 | use constant TIME_ZONE => $^O eq 'cygwin' ? 'floating' : 'local'; | ||
| 95 | |||
| 96 | |||
| 97 | sub init_file { | ||
| 98 | my ($path) = @_; | ||
| 99 | unless ( -e $path ) { | ||
| 100 | my ( $volume, $directories, $file ) = File::Spec->splitpath($path); | ||
| 101 | my $dir = File::Spec->catfile( $volume, $directories ); | ||
| 102 | mkpath( $dir, { verbose => 0, mode => 0711 } ) unless -d $dir; | ||
| 103 | unless ( -e readme() ) { | ||
| 104 | my $fh = FileHandle->new( readme(), 'w' ) | ||
| 105 | or die 'could not create file ' . readme(); | ||
| 106 | my $executable = abs_path($0); | ||
| 107 | |||
| 108 | # to protect against refactoring | ||
| 109 | my $command = App::JobLog::Command::info->name; | ||
| 110 | print $fh <<END; | ||
| 111 | |||
| 112 | Job Log | ||
| 113 | |||
| 114 | This directory holds files used by Job Log to maintain | ||
| 115 | a work log. For more details type | ||
| 116 | |||
| 117 | $executable $command | ||
| 118 | |||
| 119 | on the command line. | ||
| 120 | |||
| 121 | END | ||
| 122 | $fh->close; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | my $dir; | ||
| 129 | |||
| 130 | sub dir { | ||
| 131 | $dir ||= $ENV{ DIRECTORY() }; | ||
| 132 | $dir ||= File::Spec->catfile( File::HomeDir->my_home, '.joblog' ); | ||
| 133 | return $dir; | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | my $log; | ||
| 138 | |||
| 139 | sub log { | ||
| 140 | $log ||= File::Spec->catfile( dir(), 'log' ); | ||
| 141 | return $log; | ||
| 142 | } | ||
| 143 | |||
| 144 | |||
| 145 | my $readme; | ||
| 146 | |||
| 147 | sub readme { | ||
| 148 | $readme ||= File::Spec->catfile( dir(), 'README' ); | ||
| 149 | return $readme; | ||
| 150 | } | ||
| 151 | |||
| 152 | # configuration file for basic parameters | ||
| 153 | my $config_file; | ||
| 154 | |||
| 155 | sub _config_file { | ||
| 156 | $config_file ||= File::Spec->catfile( dir(), 'config.ini' ); | ||
| 157 | return $config_file; | ||
| 158 | } | ||
| 159 | |||
| 160 | |||
| 161 | my $vacation_file; | ||
| 162 | |||
| 163 | sub vacation { | ||
| 164 | $vacation_file ||= File::Spec->catfile( dir(), 'vacation' ); | ||
| 165 | return $vacation_file; | ||
| 166 | } | ||
| 167 | |||
| 168 | # configuration object and whether any changes need to be written to this file | ||
| 169 | my ( $config, $config_changed ); | ||
| 170 | |||
| 171 | END { | ||
| 172 | if ($config_changed) { | ||
| 173 | init_file( _config_file() ); | ||
| 174 | $config->write( _config_file() ); | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | # construct configuration object as necessary | ||
| 179 | sub _config { | ||
| 180 | unless ($config) { | ||
| 181 | my $f = _config_file(); | ||
| 182 | $config = -e $f ? Config::Tiny->read($f) : Config::Tiny->new; | ||
| 183 | } | ||
| 184 | return $config; | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | sub precision { | ||
| 189 | my ($value) = @_; | ||
| 190 | return _param( 'precision', PRECISION, 'summary', $value ); | ||
| 191 | } | ||
| 192 | |||
| 193 | sub merge { | ||
| 194 | my ($value) = @_; | ||
| 195 | return _param( 'merge', MERGE, 'summary', $value ); | ||
| 196 | } | ||
| 197 | |||
| 198 | |||
| 199 | sub day_length { | ||
| 200 | my ($value) = @_; | ||
| 201 | return _param( 'day-length', HOURS, 'time', $value ); | ||
| 202 | } | ||
| 203 | |||
| 204 | |||
| 205 | sub pay_period_length { | ||
| 206 | my ($value) = @_; | ||
| 207 | return _param( 'pay-period-length', PERIOD, 'time', $value ); | ||
| 208 | } | ||
| 209 | |||
| 210 | |||
| 211 | sub sunday_begins_week { | ||
| 212 | my ($value) = @_; | ||
| 213 | return _param( 'sunday-begins-week', SUNDAY_BEGINS_WEEK, 'time', $value ); | ||
| 214 | } | ||
| 215 | |||
| 216 | |||
| 217 | sub start_pay_period { | ||
| 218 | my ($value) = @_; | ||
| 219 | require DateTime; | ||
| 220 | if ( ref $value eq 'DateTime' ) { | ||
| 221 | $value = sprintf '%d %d %d', $value->year, $value->month, $value->day; | ||
| 222 | } | ||
| 223 | $value = _param( 'start-pay-period', undef, 'time', $value ); | ||
| 224 | if ($value) { | ||
| 225 | my @parts = split / /, $value; | ||
| 226 | return DateTime->new( | ||
| 227 | year => $parts[0], | ||
| 228 | month => $parts[1], | ||
| 229 | day => $parts[2], | ||
| 230 | time_zone => _tz(), | ||
| 231 | ); | ||
| 232 | } | ||
| 233 | return; | ||
| 234 | } | ||
| 235 | |||
| 236 | # abstracts out code for maintaining config file | ||
| 237 | sub _param { | ||
| 238 | my ( $param, $default, $section, $new_value ) = @_; | ||
| 239 | $section ||= 'main'; | ||
| 240 | my $config = _config(); | ||
| 241 | my $value = $config->{$section}->{$param}; | ||
| 242 | if ( defined $new_value ) { | ||
| 243 | if ( defined $default && $new_value eq $default && !defined $value ) { | ||
| 244 | return $new_value; | ||
| 245 | } | ||
| 246 | return $value if defined $value && $value eq $new_value; | ||
| 247 | $config_changed = 1; | ||
| 248 | return $config->{$section}->{$param} = $new_value; | ||
| 249 | } | ||
| 250 | else { | ||
| 251 | return defined $value ? $value : $default; | ||
| 252 | } | ||
| 253 | } | ||
| 254 | |||
| 255 | |||
| 256 | sub editor { | ||
| 257 | my ($value) = @_; | ||
| 258 | $value = _param( 'editor', undef, 'external', $value ); | ||
| 259 | return $value; | ||
| 260 | } | ||
| 261 | |||
| 262 | |||
| 263 | sub columns { | ||
| 264 | my ($cols) = GetTerminalSize; | ||
| 265 | $cols ||= 76; | ||
| 266 | return $cols; | ||
| 267 | } | ||
| 268 | |||
| 269 | |||
| 270 | sub workdays { | ||
| 271 | my ($value) = @_; | ||
| 272 | return _param( 'workdays', WORKDAYS, 'time', $value ); | ||
| 273 | } | ||
| 274 | |||
| 275 | |||
| 276 | my %workdays; | ||
| 277 | |||
| 278 | sub is_workday { | ||
| 279 | my ($date) = @_; | ||
| 280 | |||
| 281 | # initialize map | ||
| 282 | unless (%workdays) { | ||
| 283 | my @days = split //, DAYS; | ||
| 284 | |||
| 285 | # move Sunday into DateTime's expected position | ||
| 286 | push @days, shift @days; | ||
| 287 | my %day_map; | ||
| 288 | for ( 0 .. $#days ) { | ||
| 289 | $day_map{ $days[$_] } = $_ + 1; | ||
| 290 | } | ||
| 291 | for ( split //, workdays() ) { | ||
| 292 | $workdays{ $day_map{$_} } = 1; | ||
| 293 | } | ||
| 294 | } | ||
| 295 | return $workdays{ $date->day_of_week }; | ||
| 296 | } | ||
| 297 | |||
| 298 | |||
| 299 | sub hidden_columns { | ||
| 300 | my ($value) = @_; | ||
| 301 | return _param( 'hidden_columns', NONE_COLUMN, 'summary', $value ); | ||
| 302 | } | ||
| 303 | |||
| 304 | |||
| 305 | my %hidden_columns; | ||
| 306 | |||
| 307 | sub is_hidden { | ||
| 308 | my ($value) = @_; | ||
| 309 | unless (%hidden_columns) { | ||
| 310 | %hidden_columns = map { $_ => 1 } split / /, hidden_columns(); | ||
| 311 | } | ||
| 312 | return $hidden_columns{$value}; | ||
| 313 | } | ||
| 314 | |||
| 315 | |||
| 316 | sub time_zone { | ||
| 317 | my ($value) = @_; | ||
| 318 | return _param( 'time_zone', TIME_ZONE, 'time', $value ); | ||
| 319 | } | ||
| 320 | |||
| 321 | our $tz; | ||
| 322 | |||
| 323 | # removed from App::JobLog::Time to prevent dependency cycle | ||
| 324 | sub _tz { | ||
| 325 | if ( !defined $tz ) { | ||
| 326 | require DateTime::TimeZone; | ||
| 327 | eval { $tz = DateTime::TimeZone->new( name => time_zone() ) }; | ||
| 328 | if ($@) { | ||
| 329 | print STDERR 'DateTime::TimeZone doesn\'t like the time zone ' | ||
| 330 | . time_zone() | ||
| 331 | . "\nreverting to floating time\n full error: $@"; | ||
| 332 | $tz = DateTime::TimeZone->new( name => 'floating' ); | ||
| 333 | } | ||
| 334 | } | ||
| 335 | return $tz; | ||
| 336 | } | ||
| 337 | |||
| 338 | 1; | ||
| 339 | |||
| 340 | __END__ |