MechanicalTurk/BulkSupport.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« MechanicalTurk/RowData/SubroutineRowData.pm ↑ Summary MechanicalTurk/QAPValidator.pm »
1 package Net::Amazon::MechanicalTurk::BulkSupport;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 0 ▶use Net::Amazon::MechanicalTurk::Template::ReplacementTemplate;
Subroutine BEGIN: called CC=1 SCAR=2.6
5 use Net::Amazon::MechanicalTurk::DelimitedWriter;
6  
7 our $VERSION = '1.00';
8  
9 =head1 NAME
10  
11 Net::Amazon::MechanicalTurk::BulkSupport - Common code for bulk HIT operations
12  
13 =cut
14  
15 #
16 # The purpose of these alias mappings is to support
17 # the names used by the JavaSDK which don't quite match
18 # the names actually used by the web service.
19 #
20  
21 my $CREATE_HITTYPE_PROPERTY_ALIASES = reverseLookup({
22 Title => [qw{ title }],
23 Description => [qw{ description }],
24 Keywords => [qw{ keywords }],
25 AutoApprovalDelayInSeconds => [qw{ autoapprovaldelayinseconds autoapprovaldelay }],
26 AssignmentDurationInSeconds => [qw{ assignmentdurationinseconds assignmentduration }],
27 Reward => [qw{ reward }],
28 QualificationRequirements => [qw{ qualificationrequirements }]
29 });
30  
31 my $CREATE_HIT_PROPERTY_ALIASES = reverseLookup({
32 LifetimeInSeconds => [qw{ lifetimeinseconds hitlifetime }],
33 MaxAssignments => [qw{ maxassignments assignments }],
34 RequesterAnnotation => [qw{ requesterannotation annotation }]
35 });
36  
37 sub progressBlock {
38 my ($progress) = @_;
39 if (!defined($progress)) {
40 return $progress;
41 }
42 elsif (UNIVERSAL::isa($progress, "CODE")) {
43 return $progress;
44 }
45 elsif (UNIVERSAL::isa($progress, "GLOB")) {
46 return sub {
47 print $progress @_, "\n";
48 };
49 }
50 else {
51 Carp::croak("The progress parameters should be an IO handle or a subroutine.");
52 }
53 }
54  
55 sub defaultSuccessBlock {}
56  
57 sub defaultFailBlock {
58 my %params = @_;
59 die $params{error};
60 }
61  
62 sub successBlock {
63 my ($success) = @_;
64 if (!defined($success)) {
65 return \&defaultSuccessBlock;
66 }
67 elsif (UNIVERSAL::isa($success, "CODE")) {
68 return $success;
69 }
70 else {
71 return createSuccessBlock($success);
72 }
73 }
74  
75 sub createSuccessBlock {
76 my ($file) = @_;
77 my $out;
78 my $rowNumber = 0;
79 if (UNIVERSAL::isa($file, "GLOB")) {
80 $out = Net::Amazon::MechanicalTurk::DelimitedWriter->new(
81 output => $file,
82 fieldSeparator => "\t"
83 );
84 }
85 elsif (UNIVERSAL::isa($file, "Net::Amazon::MechanicalTurk::DelimitedWriter")) {
86 $out = $file;
87 }
88 else {
89 my $fs = ($file =~ /\.csv$/i) ? "," : "\t";
90 $out = Net::Amazon::MechanicalTurk::DelimitedWriter->new(
91 lazy => 1,
92 file => $file,
93 fieldSeparator => $fs,
94 autoflush => 1
95 );
96 }
97 return sub {
98 my %params = @_;
99 if ($rowNumber++ == 0) {
100 $out->write(qw{ HITId HITTypeId });
101 }
102 $out->write($params{HITId}, $params{HITTypeId});
103 };
104 }
105  
106 sub failBlock {
107 my ($fail) = @_;
108 if (!defined($fail)) {
109 return \&defaultFailBlock;
110 }
111 elsif (UNIVERSAL::isa($fail, "CODE")) {
112 return $fail;
113 }
114 else {
115 return createFailBlock($fail);
116 }
117 }
118  
119 sub createFailBlock {
120 my ($file) = @_;
121 my $out;
122 my $rowNumber = 0;
123 if (UNIVERSAL::isa($file, "GLOB")) {
124 $out = Net::Amazon::MechanicalTurk::DelimitedWriter->new(
125 output => $file,
126 fieldSeparator => "\t"
127 );
128 }
129 elsif (UNIVERSAL::isa($file, "Net::Amazon::MechanicalTurk::DelimitedWriter")) {
130 $out = $file;
131 }
132 else {
133 my $fs = ($file =~ /\.csv$/i) ? "," : "\t";
134 $out = Net::Amazon::MechanicalTurk::DelimitedWriter->new(
135 lazy => 1,
136 file => $file,
137 fieldSeparator => $fs,
138 autoflush => 1
139 );
140 }
141 return sub {
142 my %params = @_;
143 my $fields = $params{fields};
144 if ($rowNumber++ == 0) {
145 $out->write($fields);
146 }
147 # Have to use the field names to preserve order of columns
148 my @row;
149 for (my $i=0; $i<=$#{$fields}; $i++) {
150 push(@row, $params{row}{$fields->[$i]});
151 }
152 $out->write(@row);
153 };
154 }
155  
156 sub formatDataStructure {
157 my ($ds, $indent) = @_;
158 my $text = Net::Amazon::MechanicalTurk::DataStructure->toString($ds);
159 my $out = '';
160 foreach my $line (split /\r?\n/s, $text) {
161 $out .= ' ' x $indent;
162 $out .= $line . "\n";
163 }
164 return $out;
165 }
166  
167 sub createHITType {
168 my ($mturk, $createHITTypeProperties, $properties, $progress) = @_;
169
170 my $hitTypeId = $mturk->RegisterHITType($createHITTypeProperties)->{HITTypeId}[0];
171 $progress->(" Registered HITType $hitTypeId.") if $progress;
172
173 # Properties have notification specs
174 if (exists $properties->{Notification}) {
175 $mturk->SetHITTypeNotification(
176 HITTypeId => $hitTypeId,
177 Active => 'true',
178 Notification => $properties->{Notification}
179 );
180 $progress->("SetHITTypeNotification on $hitTypeId.") if $progress;
181 }
182
183 return $hitTypeId;
184 }
185  
186 sub getCreateHITTypeProperties {
187 my ($properties) = @_;
188 my $createHITTypeProperties = extractAliasProperties($properties, $CREATE_HITTYPE_PROPERTY_ALIASES);
189 # Special handling for reward specified as only a dollar amount in properties file
190 if (exists $createHITTypeProperties->{Reward} and
191 !ref($createHITTypeProperties->{Reward}))
192 {
193 $createHITTypeProperties->{Reward} = {
194 Amount => $createHITTypeProperties->{Reward},
195 CurrencyCode => 'USD'
196 };
197 }
198 return $createHITTypeProperties;
199 }
200  
201 sub getCreateHITProperties {
202 my ($properties) = @_;
203 # Some of the properties for a hit may have values that come from the input data.
204 my $createHITProperties = extractAliasProperties($properties, $CREATE_HIT_PROPERTY_ALIASES);
205 foreach my $key (keys %$createHITProperties) {
206 $createHITProperties->{$key} = Net::Amazon::MechanicalTurk::Template::ReplacementTemplate->new(
207 templateSource => $createHITProperties->{$key}
208 );
209 }
210 return $createHITProperties;
211 }
212  
213 sub extractAliasProperties {
214 my ($properties, $aliases) = @_;
215 my $result = {};
216 while (my ($prop,$value) = each %$properties) {
217 $prop = $aliases->{lc($prop)};
218 if (defined($prop)) {
219 $result->{$prop} = $value;
220 }
221 }
222 return $result;
223 }
224  
225 sub reverseLookup {
226 my $inHash = shift;
227 my $outHash = {};
228 while (my ($key,$array) = each %$inHash) {
229 foreach my $value (@$array) {
230 $outHash->{lc($value)} = $key;
231 }
232 }
233 return $outHash;
234 }
235  
236 return 1;
« MechanicalTurk/RowData/SubroutineRowData.pm ↑ Summary MechanicalTurk/QAPValidator.pm »