From 3a174b42109557c2040dfbe9fd6197d2046cb8d8 Mon Sep 17 00:00:00 2001 From: Tim Pope Date: Sat, 20 Aug 2005 05:00:30 +0000 Subject: [PATCH] New options system (messy) --- perl/schedproc | 207 ++++++++++++++++++++++++++++++------------------- 1 file changed, 128 insertions(+), 79 deletions(-) diff --git a/perl/schedproc b/perl/schedproc index 9107e83..f06a778 100755 --- a/perl/schedproc +++ b/perl/schedproc @@ -10,34 +10,77 @@ use strict; use Date::Calc::Object qw(Day_of_Week Decode_Day_of_Week Decode_Month Week_of_Year Monday_of_Week Day_of_Week_Abbreviation Delta_Days Add_Delta_Days Nth_Weekday_of_Month_Year Gmtime Mktime); use Date::Calendar::Profiles qw($Profiles); use Date::Calendar::Year; +use Getopt::Long; use LWP::UserAgent; use XML::Simple; use vars qw(%opts %faculty); -my ($response); +$opts{'config'} = $ENV{HOME} . "/.schedprocrc"; -my $config = $ENV{HOME} . "/.schedprocrc"; -if (($ARGV[0] || "") eq '-F') { +if(($ARGV[0] || "") eq '-F') { shift; - $config = shift; + $opts{'config'} = shift; } -my $arg = ""; -$arg = shift if (defined($ARGV[0]) && $ARGV[0] =~ /^-\w$/); +my $arg = $ARGV[0] || ""; +if($arg eq "-x") { + shift; + $opts{'format'} = "xml"; +} elsif($arg eq "-h") { + shift; + $opts{'format'} = "html"; +} elsif($arg eq "-m") { + shift; + $opts{'format'} = "mhc"; +} elsif($arg eq "-c") { + shift; + $opts{'format'} = "csv"; +} elsif($arg eq "-v") { + shift; + $opts{'format'} = "vcs"; +} elsif($arg eq "-g") { + shift; + $opts{'format'} = "grades"; +} -if (-r $config) { - open CONFIG, $config; +Getopt::Long::Configure ("bundling", "auto_help"); +die "Invalid arguments\n" unless +GetOptions(\%opts, 'schedule=s', 'grades=s', 'name=s', 'format|f=s', 'config|F=s', 'out|o=s'); + +if (-r $opts{'config'}) { + open CONFIG, $opts{'config'}; while() { s/\#.*//; next unless m/^([^=]*)=(.*)/; - $opts{$1}=$2; + my ($l, $r) = ($1, $2); + if ($l =~ /^(schedule|grades|name)$/) { + $opts{$l}||=$r; + } else { + warn "Unknown config file option $l.\n"; + } } close CONFIG; } -my $schedurl = $ENV{HOME} . "/schedule.xml"; -$schedurl = $opts{'schedule'} if(defined($opts{'schedule'})); -my $gradeurl = $ENV{HOME} . "/grades.xml"; -$gradeurl = $opts{'grades'} if(defined($opts{'schedule'})); + +$opts{'schedule'} ||= "~/schedule.xml"; +$opts{'grades'} ||= "~/grades.xml"; + +$opts{'schedule'} =~ s/^~\//$ENV{HOME}\//; +$opts{'grades'} =~ s/^~\//$ENV{HOME}\//; + +if(!defined($opts{'out'}) && defined($ARGV[0])) { + $opts{'out'} = shift; +} + +if(!defined($opts{'format'}) && defined($opts{'out'})) { + $opts{'format'} = $opts{'out'}; + $opts{'format'} =~ s/.*\.//; + $opts{'format'} =~ s/^(.*\/|)\.?schedule$/mhc/; + undef $opts{'out'} if($opts{'out'} =~ /^(html|mhc|csv|cvs|xml|grades)$/); +} + +$opts{'format'} ||= ""; +undef $opts{'out'} if(($opts{'out'}||"") eq "-"); sub generate_id { my ($section, $number) = split("-", shift); @@ -101,79 +144,39 @@ sub capitalize { return $_; } -sub get_schedule { +sub read_fileurl { my $content; - if($schedurl =~ /:\/\//) { + my $url=shift; + if($url =~ /:\/\//) { my $ua = LWP::UserAgent->new; $ua->timeout(10); $ua->env_proxy; # $ua->cookie_jar( {} ); - $response = $ua->get("$schedurl") or die "$!"; + my $response = $ua->get("$url") or die "$!"; die $response->status_line unless $response->is_success; $content = $response->content; } else { - open(F,$schedurl) || die "$!"; + open(F,$url) || die "$!"; $content = join ("", ); close F; } + return $content; + #my $ref = XMLin($content, ForceArray => [ 'class', 'cumulative', 'off' ], KeyAttr => ""); + #return @{$ref->{'class'}}; +} + +sub get_schedule { + my $content = read_fileurl($opts{'schedule'}); my $ref = XMLin($content, ForceArray => [ 'class', 'off' ], KeyAttr => ""); return @{$ref->{'class'}}; } sub get_grades { - my $content; - if($gradeurl =~ /:\/\//) { - my $ua = LWP::UserAgent->new; - $ua->timeout(10); - $ua->env_proxy; -# $ua->cookie_jar( {} ); - $response = $ua->get("$gradeurl") or die "$!"; - die $response->status_line unless $response->is_success; - $content = $response->content; - } else { - open(F,$gradeurl) || die "$!"; - $content = join ("", ); - close F; - } + my $content = read_fileurl($opts{'grades'}); my $ref = XMLin($content, ForceArray => [ 'class', 'cumulative' ], KeyAttr => ""); return $ref; } -sub do_html_grades { - my $grades; - my @showheaders = ("Section ID", "Course Title", "Grade", "Earned
Hours", "Quality
Hours", "Quality
Points"); - #$file = shift unless (!defined $_[0] or $_[0] =~ /^\d/); - $grades = get_grades(@_); - my ($row); - my $shade = "dark"; - print ''."\n"; - print '\n"; - foreach my $ts ($grades->{'class'}) { - foreach my $row (@$ts) { - print ''; - $shade = ($shade eq "dark"?"light":"dark"); - print ''; - print ''; - print '\n"; - } - } - my $lastrow = $grades->{'cumulative'}->[scalar @{$grades->{'cumulative'}}-1]; - print '\n"; - print "
', $showheaders[0]; - print '', $showheaders[1]; - print ''; - print join('',@showheaders[2 .. 5]); - print "
', $row->{'id'}, '', $row->{'title'}, ''; - #print join('', @$row[0 .. 1]); - print join('', ($row->{'grade'},$row->{'earned'},$row->{'hours'},$row->{'points'})); - print "
Cumulative: through ', capitalize($lastrow->{'term'}); - print ''; - print $lastrow->{'gpa'}; - print ''; - print join('', ($lastrow->{'earned'}, $lastrow->{'hours'}, $lastrow->{'points'})); - print "
\n"; -} - sub get_faculty_email { my ($name, $school, $email); if((-f $ENV{'HOME'} . "/public_html/faculty.csv") && ! %faculty) { @@ -215,7 +218,7 @@ sub do_mhc_schedule { my %days = (M => "Mon", T => "Tue", W => "Wed", R => "Thu", F => "Fri", S => "Sat", U => "SU"); my @mon = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); my ($file, $current, @mhc, @schedule); - $file = shift unless (!defined $_[0] or $_[0] =~ /^\d/); + $file = $opts{'out'}; @schedule = get_schedule(@_); @mhc = get_mhc_header; if(defined($file) && (-d $file)) { @@ -290,10 +293,9 @@ sub do_csv_schedule { $| = 1; my %days = (M => "Mon", T => "Tue", W => "Wed", R => "Thu", F => "Fri", S => "Sat", U => "SU"); my @mon = qw(Jan. Feb. Mar. Apr. May June July Aug. Sept. Oct. Nov. Dec.); - my (@terms, $file, $current, @mhc, @schedule); - $file = shift unless (!defined $_[0] or $_[0] =~ /^\d/); + my ($current, @mhc, @schedule); @schedule = get_schedule(@_); - open(STDOUT, ">" . $file) if(defined($file)); + open(STDOUT, ">" . $opts{'out'}) if(defined($opts{'out'})); foreach my $row (@schedule) { map {s/\n/-/g if defined; $_} %$row; my $id=generate_id($row->{'id'}); @@ -316,7 +318,7 @@ sub do_csv_schedule { sub do_vcalendar_schedule { $| = 1; my %days = (M => "MO", T => "TU", W => "WE", R => "TH", F => "FR", S => "SA", U => "SU"); - my $file = shift if (defined $_[0] and $_[0] !~ /^\d/); + my $file = $opts{'out'}; my @schedule = get_schedule(@_); open(STDOUT, ">>" . $file) if(defined($file) && (! -d $file)); open(STDOUT, ">/dev/null") if(defined($file) && (-d $file)); @@ -363,7 +365,7 @@ sub do_vcalendar_schedule { } sub do_xml_schedule { - my $file = shift if (defined $_[0] and $_[0] !~ /^\d/); + my $file = $opts{'out'}; my $schedule = { class => [ get_schedule(@_) ] }; my $xml = XMLout($schedule, NoAttr => 1, RootName => 'schedule'); if($file) { @@ -379,6 +381,10 @@ sub do_html_schedule { my @showheaders = ("Section ID/Title", "Instructor", "Days", "Time", "Duration", "Location"); my $shade = "dark"; my @schedule = get_schedule(@_); + if($opts{'out'}) { + open FH, ">" . $opts{'out'}; + select FH; + } print ''."\n\n"; @@ -411,19 +417,62 @@ sub do_html_schedule { print "\n"; } print "
"; print join("",@showheaders); print "
\n"; + select STDOUT; +} + +sub do_html_grades { + my $grades; + my @showheaders = ("Section ID", "Course Title", "Grade", "Earned
Hours", "Quality
Hours", "Quality
Points"); + $grades = get_grades(@_); + my ($row); + my $shade = "dark"; + if($opts{'out'}) { + open FH, ">" . $opts{'out'}; + select FH; + } + print ''."\n"; + print '\n"; + foreach my $ts ($grades->{'class'}) { + foreach my $row (@$ts) { + print ''; + $shade = ($shade eq "dark"?"light":"dark"); + print ''; + print ''; + print '\n"; + } + } + my $lastrow = $grades->{'cumulative'}->[scalar @{$grades->{'cumulative'}}-1]; + print '\n"; + print "
', $showheaders[0]; + print '', $showheaders[1]; + print ''; + print join('',@showheaders[2 .. 5]); + print "
', $row->{'id'}, '', $row->{'title'}, ''; + #print join('', @$row[0 .. 1]); + print join('', ($row->{'grade'},$row->{'earned'},$row->{'hours'},$row->{'points'})); + print "
Cumulative: through ', capitalize($lastrow->{'term'}); + print ''; + print $lastrow->{'gpa'}; + print ''; + print join('', ($lastrow->{'earned'}, $lastrow->{'hours'}, $lastrow->{'points'})); + print "
\n"; + select STDOUT; } -if ($arg eq "-s" || $arg eq "-h") { + +if ($opts{'format'} eq "xml") { + do_xml_schedule(@ARGV); +} elsif ($opts{'format'} eq "html") { do_html_schedule(@ARGV); -} elsif ($arg eq "-m") { +} elsif ($opts{'format'} eq "mhc") { do_mhc_schedule(@ARGV); -} elsif ($arg eq "-c") { +} elsif ($opts{'format'} eq "csv") { do_csv_schedule(@ARGV); -} elsif ($arg eq "-v") { +} elsif ($opts{'format'} eq "vcs") { do_vcalendar_schedule(@ARGV); -} elsif ($arg eq "-x") { - do_xml_schedule(@ARGV); -} elsif ($arg eq "-g") { +} elsif ($opts{'format'} eq "grades") { do_html_grades(@ARGV); +} else { + die "Unknown format. Try specifying --format.\n" } -- 2.30.2