diff options
| author | fukachan <fukachan> | 2001-11-23 02:52:24 +0000 |
|---|---|---|
| committer | fukachan <fukachan> | 2001-11-23 02:52:24 +0000 |
| commit | 00e81465e23ca40cd830645f6bb4c134ffb5ab3d (patch) | |
| tree | ed27293cc1a3df94e9ba5d5c8012750d964af594 /fml | |
| parent | abd1f36b3bcf92f4b16225dfb615bec38ea5bcbe (diff) | |
| download | fml8-00e81465e23ca40cd830645f6bb4c134ffb5ab3d.tar.gz fml8-00e81465e23ca40cd830645f6bb4c134ffb5ab3d.tar.bz2 fml8-00e81465e23ca40cd830645f6bb4c134ffb5ab3d.zip | |
clean up fmlsch
FNF-ify
handle options by getopt() in Process::Scheduler not TinySchedule
make "fmlsch help" work
fix entry duplication bug in parser
print_specific_month() can handle (month, year) pair
define set_mode()
use env var BROWSER ("w3m" by default)
Diffstat (limited to 'fml')
| -rw-r--r-- | fml/lib/FML/Process/Scheduler.pm | 84 | ||||
| -rw-r--r-- | fml/lib/TinyScheduler.pm | 189 |
2 files changed, 208 insertions, 65 deletions
diff --git a/fml/lib/FML/Process/Scheduler.pm b/fml/lib/FML/Process/Scheduler.pm index 93340589..2b2087b2 100644 --- a/fml/lib/FML/Process/Scheduler.pm +++ b/fml/lib/FML/Process/Scheduler.pm @@ -4,7 +4,7 @@ # Copyright (C) 2000 Ken'ichi Fukamachi # All rights reserved. # -# $FML: Scheduler.pm,v 1.3 2001/04/05 08:31:44 fukachan Exp $ +# $FML: Scheduler.pm,v 1.4 2001/06/28 09:06:44 fukachan Exp $ # package FML::Process::Scheduler; @@ -62,51 +62,93 @@ sub new } -sub prepare { ; } +sub prepare { 1; } +sub verify_request { 1; } +sub finish { 1; } sub run { my ($self, $args) = @_; - + my $argv = $args->{ argv }; + my $option = $args->{ options }; + my $mode = 'text'; + + if (defined $option->{ h }) { return $self->help();} + use FileHandle; use TinyScheduler; - my $schedule = new TinyScheduler $args; - my $tmp = $schedule->tmpfile; - my $fh = new FileHandle $tmp, "w"; - - # show three calender for this month, next month, last month - my $show_3_month = defined($args->{ options }->{ a }) ? 1 : 0; - if ($show_3_month) { - for my $n ('this', 'next', 'last') { - $schedule->print_specific_month($fh, $n); + # prepare new() argument + $mode = $option->{ m } if defined $option->{ m }; + my $schedule_dir = $option->{ D } if defined $option->{ D }; + my $schedule_file = $option->{ F } if defined $option->{ F }; + my $schargs = { + schedule_dir => $schedule_dir, + schedule_file => undef, + }; + my $schedule = new TinyScheduler $schargs; + + # prepare output channel + my $tmpf = $schedule->tmpfile; + my $wh = new FileHandle $tmpf, "w"; + + # set output mode + $schedule->set_mode( $mode ); + + # -a option: show three calender for this, next and last month. + if (defined($option->{ a })) { + for my $month ('this', 'next', 'last') { + $schedule->print_specific_month($wh, $month); } } + elsif (defined(@$argv) && @$argv) { + # ($month, $yeer) = @$argv; + $schedule->print_specific_month($wh, @$argv); + } else { - $schedule->print_specific_month($fh, 'this'); + $schedule->print_specific_month($wh, 'this'); } - $fh->close; + $wh->close; - my $mode = $args->{ options }->{ m } || 'text'; if ($mode eq 'text') { - system "w3m -dump $tmp"; + system "w3m -dump $tmpf"; } else { - system "cat $tmp"; + system "cat $tmpf"; } - unlink $tmp; + unlink $tmpf if -f $tmpf; } -# dummy to avoid the error ( undefined function ) -sub AUTOLOAD + +sub help { - ; + my ($self) = @_; + + my $name = $0; + eval q{ use File::Basename; $name = basename($0);}; + +print <<"_EOF_"; + +Usage: $name [-a] [-m mode] [month] [year] + + show this month if not specified. + +-h show this help +-a show calender at this, next and last month +-m mode mode is 'text' or 'html' +-D DIR alternative of ~/.schedule/ +-F FILE specify schedule file to read + +--debug debug mode on + +_EOF_ } + =head1 AUTHOR Ken'ichi Fukamachi diff --git a/fml/lib/TinyScheduler.pm b/fml/lib/TinyScheduler.pm index defbe39a..f0ab0f0e 100644 --- a/fml/lib/TinyScheduler.pm +++ b/fml/lib/TinyScheduler.pm @@ -4,7 +4,7 @@ # All rights reserved. This program is free software; you can # redistribute it and/or modify it under the same terms as Perl itself. # -# $FML: TinyScheduler.pm,v 1.7 2001/06/28 09:06:43 fukachan Exp $ +# $FML: TinyScheduler.pm,v 1.8 2001/09/22 13:17:10 fukachan Exp $ # package TinyScheduler; @@ -14,50 +14,78 @@ use Carp; =head1 NAME -TinyScheduler - what is this +TinyScheduler - scheduler with minimal functions =head1 SYNOPSIS + use TinyScheduler; + my $schedule = new TinyScheduler; + + $schedule->parse; + + # show table by w3m :-) + my $tmp = $schedule->tmpfile; + my $fh = new FileHandle $tmp, "w"; + $schedule->print($fh); + $fh->close; + + system "w3m -dump $tmp"; + unlink $tmp; + +See the debug code in this module. + =head1 DESCRIPTION +demonstration module to show how to use and build up modules. + +It parses files in ~/.schedule/ and output schedule of this month as +HTML TABLE by default. + +To see it, you need WWW browser e.g. "w3m". + =head1 METHODS -=head2 C<new()> +=head2 new() =cut -# $args == parameters from CGI.pm if fmlsci.cgi uses. -# $args == libexec/loaders's $args if fmlsch uses. + +# Descriptions: usual constructor. $args is optional, which comes from +# parameters from CGI.pm if fmlsci.cgi uses. +# OR +# libexec/loaders's $args if fmlsch uses. +# Arguments: $self $args +# Side Effects: none +# Return Value: object sub new { my ($self, $args) = @_; - my ($type) = ref($self) || $self; - my $me = {}; - - # fmlsch.cgi options - my $user = $args->{ user } || $ENV{'USER'}; + my ($type) = ref($self) || $self; + my $me = {}; + my $user = defined $args->{ user } ? $args->{ user } : $ENV{'USER'}; - # fmlsch options - my $options = $args->{ options }; - - # directory + # default directory to hold schdule file(s): ~/.schedule/ by default use User::pwent; - my $pw = getpwnam($user); - my $home_dir = $pw->dir; - my $dir = $options->{'D'} || "$home_dir/.tsch"; - - # ~/.schedule/ by default + my $pw = getpwnam($user); + my $home_dir = $pw->dir; $me->{ _user } = $user; - $me->{ _schedule_dir } = $dir; - $me->{ _schedule_file } = $options->{'F'} || ''; + $me->{ _schedule_dir } = "$home_dir/.schedule"; # ~/.schedule/ by default + $me->{ _schedule_file } = undef; - # attribute - $me->{ _mode } = $options->{ 'm' } || 'text'; + for my $key ('schedule_dir', 'schedule_file', 'mode') { + if (defined $args->{ $key }) { + $me->{ "_$key" } = $args->{ $key }; + } + } return bless $me, $type; } +# Descriptions: determine template file location +# Arguments: $self $args +# Side Effects: none +# Return Value: STR(filename) sub tmpfile { my ($self, $args) = @_; @@ -72,8 +100,11 @@ sub tmpfile croak("Hmm, unsafe ? I cannot write $dir, stop\n"); } - use File::Spec; - $self->{ _tmpfile } = File::Spec->catfile($tmpdir,"$$.html"); + eval q{ + use File::Spec; + $self->{ _tmpfile } = File::Spec->catfile($tmpdir, ".tmp.$$.html"); + }; + croak($@) if $@; return $self->{ _tmpfile }; } @@ -82,31 +113,35 @@ sub tmpfile =cut + +# Descriptions: parse file(s) +# Arguments: $self $args +# Side Effects: update calender entries in $self object +# (actually by _add_entry() calleded here) +# Return Value: none sub parse { my ($self, $args) = @_; my ($sec,$min,$hour,$mday,$month,$year,$wday) = localtime(time); # get the date to show - $year = $args->{ year } || (1900 + $year); - $month = $args->{ month } || ($month + 1); + $year = defined $args->{ year } ? $args->{ year } : (1900 + $year); + $month = defined $args->{ month } ? $args->{ month } : ($month + 1); # schedule file my $data_dir = $self->{ _schedule_dir }; my $data_file = $self->{ _schedule_file }; - # pattern to match + # pick up line matched with this pattern my @pat = ( - sprintf("^%04d%02d(\\d{1,2})\\s+(.*)", $year, $month), + sprintf("^%04d%02d(\\d{1,2})\\s+(.*)", $year, $month), sprintf("^%04d/%02d/(\\d{1,2})\\s+(.*)", $year, $month), - sprintf("^%04d/%d/(\\d{1,2})\\s+(.*)", $year, $month), + sprintf("^%04d/%d/(\\d{1,2})\\s+(.*)", $year, $month), sprintf("^%02d(\\d{1,2})\\s+(.*)", $month), sprintf("^%02d/(\\d{1,2})\\s+(.*)", $month), ); - # use HTML::CalendarMonthSimple; - my $cal = new HTML::CalendarMonthSimple('year'=> $year, 'month'=> $month); if (defined $cal) { @@ -131,7 +166,11 @@ sub parse if (defined $dh) { while (defined($_ = $dh->read)) { next if $_ =~ /~$/; - $self->_analyze("$data_dir/$_", \@pat); + next if $_ =~ /^\./; + my $schedule_file = "$data_dir/$_"; + if (-f $schedule_file) { + $self->_analyze($schedule_file, \@pat); + } } } } @@ -141,6 +180,11 @@ sub parse } +# Descriptions: open, read specified $file +# analyze the line which matches $pattern. +# Arguments: OBJ($self) STR($file) STR($pattern) +# Side Effects: update $self object by _add_entry() +# Return Value: none sub _analyze { my ($self, $file, $pattern) = @_; @@ -149,16 +193,18 @@ sub _analyze my $fh = new FileHandle $file; if (defined $fh) { + FILE: while (<$fh>) { for my $pat (@$pattern) { if (/$pat(.*)/) { - $self->_parse($1, $2); + $self->_add_entry($1, $2); + next FILE; } } # for example, "*/24 something" if (/^\*\/(\d+)\s+(.*)/) { - $self->_parse($1, $2); + $self->_add_entry($1, $2); } } close($fh); @@ -166,11 +212,16 @@ sub _analyze } -sub _parse +# Descriptions: add calender entry to $self object +# Arguments: OBJ($self) STR($day) STR($buf) +# Side Effects: update $self object +# Return Value: none +sub _add_entry { my ($self, $day, $buf) = @_; my $cal = $self->{ _schedule }; $day =~ s/^0//; + $cal->addcontent($day, "<p>". $buf); } @@ -183,6 +234,10 @@ You can specify the output channel by C<$fd>. =cut +# Descriptions: print calender by HTML::CalenderMonthSimple::as_HTML() method +# Arguments: OBJ($self) HANDLE($fd) +# Side Effects: none +# Return Value: none sub print { my ($self, $fd) = @_; @@ -198,20 +253,64 @@ C<$n> is one of C<this>, C<next> and C<last>. =cut + +# Descriptions: print calender for specific month as HTML +# Arguments: OBJ($self) HANDLE($fd) STR($month) [STR($year)] +# Side Effects: none +# Return Value: none sub print_specific_month { - my ($self, $fh, $n) = @_; - my ($sec,$min,$hour,$mday,$month,$year,$wday) = localtime(time); - my $thismonth = $month + 1; - $thismonth++ if $n eq 'next'; - $thismonth-- if $n eq 'last'; + my ($self, $fh, $month, $year) = @_; + my ($month_now, $year_now) = (localtime(time))[4,5]; + my $default_year = 1900 + $year_now; + my $default_month = $month_now + 1; + my ($thismonth, $thisyear) = ($default_month, $default_year); + + if ($month =~ /^\d+$/) { + $thismonth = $month; + $thisyear = $year if defined $year; + } + else { + if ($default_month == 1) { + $thismonth = 2 if $month eq 'next'; + $thismonth = 12 if $month eq 'last'; + } + elsif ($default_month == 12) { + $thismonth = 1 if $month eq 'next'; + $thismonth = 11 if $month eq 'last'; + } + else { + $thismonth++ if $month eq 'next'; + $thismonth-- if $month eq 'last'; + } + } - print $fh "<A NAME=\"$n\">\n"; - $self->parse( { month => $thismonth } ); + print $fh "<A NAME=\"$month\">\n"; + $self->parse( { month => $thismonth, year => $thisyear } ); $self->print($fh); } +=head2 set_mode( $mode ) + +=cut + + +# Descriptions: overwrite $mode +# Arguments: OBJ($self) STR($mode) +# Side Effects: update $self object +# Return Value: none +sub set_mode +{ + my ($self, $mode) = @_; + $self->{ _mode } = $mode; +} + + +# +# debug +# + if ($0 eq __FILE__) { eval q{ my %options; @@ -234,8 +333,10 @@ if ($0 eq __FILE__) { my $fh = new FileHandle $tmp, "w"; $schedule->print($fh); $fh->close; + + my $formater = defined $ENV{'BROWSER'} ? $ENV{'BROWSER'} : 'w3m'; - system "w3m -dump $tmp"; + system "$formater -dump $tmp"; unlink $tmp; }; |
