Update of /cvsroot/woc/woc/data/wml
In directory usw-pr-cvs1:/tmp/cvs-serv28392
Added Files:
mkwml
Log Message:
Some final clean ups.
--- NEW FILE: mkwml ---
#!/usr/local/bin/perl -w
use strict;
use check_digit;
# location of the item files and the templates #
my $wmldatadir = "/vol/www/woc/data/wml";
my $templatedir = "$wmldatadir/templates";
### MAIN ###
my $flag;
my $type = "short"; # use chemical template if no type is given #
my $item;
my $cas = "";
my $template;
&process_arguments();
print "Creating new item $wmldatadir/$item.xml with ID=";
my $wocnumber = &get_next_wocnumber;
print "$wocnumber...$/";
$template = &read_template ("$templatedir/$type.xml");
$template = &expand_variables ($template, ("item" => $item,
"wocnumber" => $wocnumber,
"cas" => $cas,
"code" => $item));
&save_item ($template, "$wmldatadir/$item.xml");
chmod 0775, "$wmldatadir/$item.xml";
`chgrp wwwwoc "$wmldatadir/$item.xml"`;
print "Done!$/";
### END ###
sub process_arguments {
if (@ARGV < 1) {
print "Usage: $0 [-cas <casnumber>] [-type (group|chemical|short|term)] <item>$/";
print " -cas may be shortened to -c$/";
print " -type may be shortened to -t$/";
print " group may be shortened to g$/";
print " chemical may be shortened to c$/";
print " short may be shortened to s$/";
print " element may be shortened to e$/";
print " word may be shortened to w$/";
die " term may be shortened to t$/";
} elsif (@ARGV == 1) {
$item = $ARGV[0];
} elsif (@ARGV == 3) {
$flag = $ARGV[0];
$type = $ARGV[1] if ($flag =~ /^-t/i);
$cas = $ARGV[1] if ($flag =~ /^-c/i);
$item = $ARGV[2];
die "Incorrect flag $flag! (type '$0' for usage)$/" if ($flag !~ /^-[tc]/i);
} elsif (@ARGV == 5) {
if (($ARGV[0] =~ /^-[tc]/i) && ($ARGV[2] =~ /^-[tc]/i)) {
if ($ARGV[0] =~ /^-t/i) {
$type = $ARGV[1];
$flag = $ARGV[2];
$cas = $ARGV[3];
$item = $ARGV[4];
die "Incorrect flag $flag! (type '$0' for usage)$/" if ($flag !~ /^-c/i);
} elsif ($ARGV[0] =~ /^-c/i) {
$cas = $ARGV[1];
$flag = $ARGV[2];
$type = $ARGV[3];
$item = $ARGV[4];
die "Incorrect flag $flag! (type '$0' for usage)$/" if ($flag !~ /^-t/i);
}
} else {
die "Incorrect usage of arguments! (type '$0' for usage)$/";
}
} else {
die "Incorrect number of arguments! (type '$0' for usage)$/";
}
die "Must provide an item! (type '$0' for usage)$/" if ($item =~ /^-/i);
die "Must be a new item! (type '$0' for usage)$/" if (-e "$wmldatadir/$item.xml");
if ($cas) {
my $check = &check_digit ($cas);
if (!$check) {
die "CAS NUMBER: Wrong check digit or typing error in $cas!$/";
} elsif ($check =~ /Error/i) {
die "CAS NUMBER $check in $cas$/";
} elsif ($check !~ /Correct/i) {
print "CAS NUMBER: Missing check digit in $cas! ";
$cas = &check_digit ($cas);
print "Using $cas instead...$/";
}
my $match = &already_exists (("CAS-NUMBER" => ">$cas<"));
die "CAS NUMBER: Already found in$/\t$match$/" if ($match);
}
if ($type =~ /^c/i) {
$type = "chemical";
} elsif ($type =~ /^g/i) {
$type = "group";
} elsif ($type =~ /^s/i) {
$type = "short";
} elsif ($type =~ /^t/i) {
$type = "term";
} elsif ($type =~ /^e/i) {
$type = "element";
} elsif ($type =~ /^w/i) {
$type = "word";
} else {
die "Unknown type $type! (type '$0' for usage)$/";
}
}
sub get_next_wocnumber {
my @item_start_elements = `grep -i "ITEM\.*ID=" $wmldatadir/*.xml`;
# make a list of current ID's
my @idlist;
foreach (@item_start_elements) {
/ID=\"WOC(.*?)\".*/i;
push (@idlist, $1);
}
# sort thislist
@idlist = sort @idlist;
# what is the highest ID?
my $highestid = $idlist[-1];
my $nextfree = sprintf "WOC%08i", $highestid + 1;
return ($nextfree);
}
sub read_template {
my $file = shift;
my $template = "";
open (FILE, $file) || die "$file: $!$?";
while (<FILE>) {
$template = "$template$_";
}
close (FILE) || die "$file: $!$?";
return ($template);
}
sub expand_variables {
my $template = shift;
my %variables = @_;
foreach my $key (keys %variables) {
$template =~ s/\$$key/$variables{$key}/ig;
}
return ($template);
}
sub save_item {
my $template = shift;
my $file = shift;
open (FILE, ">$file") || die "$file: $!$?";
print FILE $template;
close (FILE) || die "$file: $!$?";
}
sub already_exists {
my %filters = @_;
foreach my $key (keys %filters) {
my $out = `grep -i "$key" $wmldatadir/*.xml | grep -i "$filters{$key}"`;
chomp ($out);
return "$out" if $out;
}
return "";
}
|