#!/usr/bin/perl -w # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # use strict; require 5.005; use Carp qw(carp croak); use Fcntl ':flock'; # import LOCK_* constants use Getopt::Long; use Digest::MD5 qw(md5 md5_hex md5_base64); use DB_File; =head1 NAME dvt-simple2tally - create a tally sheet from manual input =cut =head1 SYNOPSIS dvt-simple2tally -c /path/to/config < input =cut =head1 DESCRIPTION Converts an input file in ad-hoc form to tally.txt =cut =head2 Internal Implementation This routine pays attention to configuration variables Tally_File, Option_* =cut sub badformat ($) { my ($m) = @_; die sprintf "bad format: stdin:%d: %s\n", STDIN->input_line_number, $m; } sub create_tally { my %params = @_; # die "Internal Error!" unless defined $params{'Configuration'}; # my $confref = $params{'Configuration'}->get_config_ref(); # my $dvt = $params{'Configuration'}; # my %Config = %{ $confref }; # my $tallyfile = $Config{'Tally_File'}; # my @valid_options = # grep {m/^Option_[[:alnum:]]+$/ && $Config{$_}; } sort keys %Config; my @valid_options = qw(D U O V FD); my %opt2ix; foreach my $ix (0..$#valid_options) { $opt2ix{$valid_options[$ix]} = $ix; } while () { s/^\s+//; s/\s+$//; next if m/^\#/; next unless m/\S/; badformat "missing voter name" unless s/^(\S+)\:\s*//; my $voter = $1; $_ = uc $_; s/\t/ /g; s/\,/ /g; while (s{\(([^()]+)\)}{ my $x = $1; $x =~ s/[ =]+/=/g; $x; }ge) { } s/[ =]*=[ =*]/=/g; s/\s+/ /g; print "# normalised $_ ($voter)\n"; my @ranks = ('-',) x @valid_options; my $rank = 1; foreach (split /\s+/) { foreach (split /=/) { my $ix = $opt2ix{$_}; defined $ix or badformat "unknown option $_ ($voter)"; $ranks[$ix] = $rank; } $rank++; } print "V: ", (map { $_ # fixme base36 } @ranks), " ", $voter, "\n" or die $!; } } #use Devotee; sub main { # my $optdesc = Devotee->Optdesc(); # GetOptions (%$optdesc); # my $dvt = Devotee->new(%::ConfOpts); my $dvt= undef; # $dvt->validate(%::ConfOpts) unless # defined $::ConfOpts{'Config File'} && -r $::ConfOpts{'Config File'}; # $dvt->lock_vote_dir(); &create_tally('Configuration' => $dvt); # $dvt->unlock_vote_dir(); } &main; exit 0; =cut =head1 BUGS None Known so far. =cut =head1 AUTHOR Ian Jackson =head1 COPYRIGHT AND LICENSE This script is a part of the Devotee package, and is Copyright (c) 2014 Ian Jackson This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA =cut 1; __END__