chiark / gitweb /
wip
[appendix-a6.git] / dvt-simple2tally
1 #!/usr/bin/perl -w
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
18 use strict;
19
20 require 5.005;
21 use Carp qw(carp croak);
22 use Fcntl ':flock';             # import LOCK_* constants
23 use Getopt::Long;
24 use Digest::MD5  qw(md5 md5_hex md5_base64);
25 use DB_File;
26
27 =head1 NAME
28
29 dvt-simple2tally - create a tally sheet from manual input
30
31 =cut
32
33 =head1 SYNOPSIS
34
35 dvt-simple2tally -c /path/to/config < input
36
37 =cut
38
39 =head1 DESCRIPTION
40
41 Converts an input file in ad-hoc form to tally.txt
42
43 =cut
44
45
46 =head2 Internal Implementation
47
48 This routine pays attention to configuration variables Tally_File,
49 Option_*
50
51 =cut
52
53 sub badformat ($) {
54   my ($m) = @_;
55   die sprintf "bad format: stdin:%d: %s\n", STDIN->input_line_number, $m;
56 }
57
58 sub create_tally {
59   my %params   = @_;
60 #  die "Internal Error!" unless defined $params{'Configuration'};
61 #  my $confref = $params{'Configuration'}->get_config_ref();
62 #  my $dvt = $params{'Configuration'};
63 #  my %Config = %{ $confref };
64
65 #  my $tallyfile   = $Config{'Tally_File'};
66
67 #  my @valid_options =
68 #      grep {m/^Option_[[:alnum:]]+$/ && $Config{$_}; } sort keys %Config;
69
70 my @valid_options = qw(D U O V FD);
71   my %opt2ix;
72
73   foreach my $ix (0..$#valid_options) {
74     $opt2ix{$valid_options[$ix]} = $ix;
75   }
76
77   while (<STDIN>) {
78     s/^\s+//;
79     s/\s+$//;
80     next if m/^\#/;
81     next unless m/\S/;
82     badformat "missing voter name" unless s/^(\S+)\:\s*//;
83     my $voter = $1;
84     $_ = uc $_;
85     s/\t/ /g;
86     s/\,/ /g;
87     while (s{\(([^()]+)\)}{
88         my $x = $1; $x =~ s/[ =]+/=/g; $x;
89     }ge) { }
90     s/[ =]*=[ =*]/=/g;
91     s/\s+/ /g;
92     print "# normalised $_ ($voter)\n";
93
94     my @ranks = ('-',) x @valid_options;
95     my $rank = 1;
96     foreach (split /\s+/) {
97       foreach (split /=/) {
98         my $ix = $opt2ix{$_};
99         defined $ix or badformat "unknown option $_ ($voter)";
100         $ranks[$ix] = $rank;
101       }
102       $rank++;
103     }
104     print "V: ", (map {
105       $_      # fixme  base36
106     } @ranks), " ", $voter, "\n"
107         or die $!;
108   }
109 }
110
111
112 #use Devotee;
113 sub main {
114 #  my $optdesc = Devotee->Optdesc();
115 #  GetOptions (%$optdesc);
116 #  my $dvt = Devotee->new(%::ConfOpts);
117 my $dvt= undef;
118 #  $dvt->validate(%::ConfOpts) unless 
119 #    defined $::ConfOpts{'Config File'} && -r $::ConfOpts{'Config File'};
120 #  $dvt->lock_vote_dir();
121   &create_tally('Configuration' => $dvt);
122 #  $dvt->unlock_vote_dir();
123 }
124
125 &main;
126
127 exit 0;
128
129
130 =cut
131
132 =head1 BUGS
133
134 None Known so far.
135
136 =cut
137
138 =head1 AUTHOR
139
140 Ian Jackson <ijackson@chiark.greenend.org.uk>
141
142 =head1 COPYRIGHT AND LICENSE
143
144 This script is a part of the Devotee package, and is 
145
146 Copyright (c) 2014 Ian Jackson <ijackson@chiark.greenend.org.uk>
147
148 This program is free software; you can redistribute it and/or modify
149 it under the terms of the GNU General Public License as published by
150 the Free Software Foundation; either version 2 of the License, or
151 (at your option) any later version.
152
153 This program is distributed in the hope that it will be useful,
154 but WITHOUT ANY WARRANTY; without even the implied warranty of
155 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
156 GNU General Public License for more details.
157
158 You should have received a copy of the GNU General Public License
159 along with this program; if not, write to the Free Software
160 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
161
162 =cut
163
164
165
166 1;
167
168 __END__
169