| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Net::Amazon::MechanicalTurk::Properties; | 
| 2 | 19 |  |  | 19 |  | 96 | use strict; | 
|  | 19 |  |  |  |  | 35 |  | 
|  | 19 |  |  |  |  | 567 |  | 
| 3 | 19 |  |  | 19 |  | 119 | use warnings; | 
|  | 19 |  |  |  |  | 35 |  | 
|  | 19 |  |  |  |  | 419 |  | 
| 4 | 19 |  |  | 19 |  | 99 | use IO::File; | 
|  | 19 |  |  |  |  | 71 |  | 
|  | 19 |  |  |  |  | 2887 |  | 
| 5 | 19 |  |  | 19 |  | 95 | use Carp; | 
|  | 19 |  |  |  |  | 69 |  | 
|  | 19 |  |  |  |  | 14214 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | our $VERSION = '1.00'; | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | sub toProperties { | 
| 10 | 0 |  |  | 0 | 0 |  | my ($class, $properties) = @_; | 
| 11 | 0 | 0 |  |  |  |  | if (UNIVERSAL::isa($properties, "HASH")) { | 
| 12 | 0 |  |  |  |  |  | return $properties; | 
| 13 |  |  |  |  |  |  | } | 
| 14 |  |  |  |  |  |  | else { | 
| 15 | 0 |  |  |  |  |  | return Net::Amazon::MechanicalTurk::Properties->readNestedData($properties); | 
| 16 |  |  |  |  |  |  | } | 
| 17 |  |  |  |  |  |  | } | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | sub write { | 
| 20 | 0 |  |  | 0 | 0 |  | my ($class, $settings, $file, $header) = @_; | 
| 21 | 0 |  |  |  |  |  | my $out = IO::File->new($file, "w"); | 
| 22 | 0 | 0 |  |  |  |  | if (!$out) { | 
| 23 | 0 |  |  |  |  |  | Carp::croak("Could not open file $file - $!."); | 
| 24 |  |  |  |  |  |  | } | 
| 25 | 0 | 0 |  |  |  |  | if ($header) { | 
| 26 | 0 |  |  |  |  |  | foreach my $line (split /\r?\n/, $header) { | 
| 27 | 0 |  |  |  |  |  | printf $out "# %s\n", $line; | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  | } | 
| 30 | 0 |  |  |  |  |  | foreach my $key (sort keys %$settings) { | 
| 31 | 0 |  |  |  |  |  | my $value = $settings->{$key}; | 
| 32 | 0 |  |  |  |  |  | printf $out "%s: %s\n", $key, $value; | 
| 33 |  |  |  |  |  |  | } | 
| 34 | 0 |  |  |  |  |  | $out->close; | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | # Reads a properties file into a nested data structure | 
| 38 |  |  |  |  |  |  | sub readNestedData { | 
| 39 | 0 |  |  | 0 | 0 |  | my ($class, $in) = @_; | 
| 40 | 0 |  |  |  |  |  | return Net::Amazon::MechanicalTurk::DataStructure->fromProperties($class->read($in)); | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | # Reads a file into key value pairs and returns a hash. | 
| 44 |  |  |  |  |  |  | # The file should be similar to that of a Java properties file. | 
| 45 |  |  |  |  |  |  | # Backslash escaping is not supported though. | 
| 46 |  |  |  |  |  |  | # Both = and : can be used as key/value separators. | 
| 47 |  |  |  |  |  |  | sub read { | 
| 48 | 0 |  |  | 0 | 0 |  | my ($class, $in) = @_; | 
| 49 | 0 |  |  |  |  |  | my $file = ""; | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 0 | 0 |  |  |  |  | if (!UNIVERSAL::isa($in, "GLOB")) { | 
| 52 | 0 |  |  |  |  |  | $file = $in; | 
| 53 | 0 |  |  |  |  |  | $in = IO::File->new($file, "r"); | 
| 54 | 0 | 0 |  |  |  |  | if (!$in) { | 
| 55 | 0 |  |  |  |  |  | Carp::croak("Could not read file $file - $!."); | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 0 |  |  |  |  |  | my $props = {}; | 
| 60 | 0 |  |  |  |  |  | my $lineno = 0; | 
| 61 | 0 |  |  |  |  |  | while (my $line = <$in>) { | 
| 62 | 0 | 0 | 0 |  |  |  | next if ($line =~ /^\s*[#!]/ or $line =~ /^\s*$/); | 
| 63 | 0 | 0 |  |  |  |  | if ($line =~ /^([^:=]+)[=:](.*)/) { | 
| 64 | 0 |  |  |  |  |  | my ($key,$val) = ($1,$2); | 
| 65 | 0 |  |  |  |  |  | $key =~ s/^\s+//; | 
| 66 | 0 |  |  |  |  |  | $key =~ s/\s+$//; | 
| 67 | 0 |  |  |  |  |  | $val =~ s/^\s+//; | 
| 68 | 0 |  |  |  |  |  | $val =~ s/\s+$//; | 
| 69 | 0 |  |  |  |  |  | $props->{$key} = $val; | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  | else { | 
| 72 | 0 |  |  |  |  |  | warn "Unknown format at $file:$lineno."; | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 0 |  |  |  |  |  | return $props; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | return 1; |