| 1 | #!perl -w | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | mk_methods.pl - generate UltraDNS::Methods module by parsing the specification | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | mk_methods.pl NUS_API_XML.txt | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | Parses a plain text version of the UltraDNS Transaction Protocol document | ||
| 14 | (derived from http://www.ultradns.net/api/NUS_API_XML.pdf) and rewrites the | ||
| 15 | UltraDNS::Methods module with descriptions of the methods and their arguments. | ||
| 16 | |||
| 17 | You I<do not need to run this> unless you need to update the code to handle a | ||
| 18 | newer version of the API. | ||
| 19 | |||
| 20 | =head2 Converting PDF To Text | ||
| 21 | |||
| 22 | The NUS_API_XML.txt isn't included in the distribution for copyright reasons. | ||
| 23 | This is the procedure I used to generate the current UltraDNS::Methods module: | ||
| 24 | |||
| 25 | - Open http://www.ultradns.net/api/NUS_API_XML.pdf using Preview on Mac OSX | ||
| 26 | - Select and copy all the text | ||
| 27 | - Paste the text into the TextEdit application | ||
| 28 | - Select "Make Plain Text" from the Format menu | ||
| 29 | - Save as NUS_API_XML.txt | ||
| 30 | - Run mk_methods.pl NUS_API_XML.txt in the top directory of the distribution | ||
| 31 | |||
| 32 | If you use a different method to perform the conversion then it's likely that | ||
| 33 | you'll need to make slight changes to this script. | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | # TODO (maybe) | ||
| 38 | # identify methods that return results | ||
| 39 | # sigil doesn't need to be in the data struct (at least not the default '$') | ||
| 40 | |||
| 41 | use strict; | ||
| 42 | use warnings; | ||
| 43 | |||
| 44 | use Carp; | ||
| 45 | use Getopt::Long; | ||
| 46 | |||
| 47 | use XML::Simple qw(:strict); | ||
| 48 | use Data::Dumper; | ||
| 49 | |||
| 50 | GetOptions( | ||
| 51 | 'trace|t=i' => \(my $opt_trace = 0), | ||
| 52 | ) or exit 1; | ||
| 53 | |||
| 54 | my $api_spec_file = shift or die "No API file specified"; | ||
| 55 | my $pm_filename = "lib/UltraDNS/Methods.pm"; | ||
| 56 | |||
| 57 | my $api_spec_txt = `cat $api_spec_file` | ||
| 58 | or die "Couldn't read $api_spec_file\n"; | ||
| 59 | |||
| 60 | # remove page breaks | ||
| 61 | my $title = qr/NeuStar Ultra Services Transaction Protocol\s*/; | ||
| 62 | $api_spec_txt =~ s/ \n $title \n \d+ //xmg | ||
| 63 | or die "Failed to remove page breaks"; | ||
| 64 | |||
| 65 | # remove blank lines | ||
| 66 | $api_spec_txt =~ s/ \s* \n (\s* \n)* /\n/xmg | ||
| 67 | or die "Failed to remove blank lines"; | ||
| 68 | |||
| 69 | # extract method call examples | ||
| 70 | my @methods = $api_spec_txt =~ m{Syntax\s* \n (<methodCall> .*? </methodCall>) }xsg; | ||
| 71 | die "Found no methods" unless @methods; | ||
| 72 | warn "Found ".@methods." methods\n"; | ||
| 73 | |||
| 74 | # create Methods.pm and copy the prologue into it | ||
| 75 | open my $pm_fh, ">", $pm_filename ## no critic (RequireBriefOpen) | ||
| 76 | or die "Unable to open $pm_filename for writing: $!"; | ||
| 77 | my @prologue = <DATA>; | ||
| 78 | s/^:// for @prologue; | ||
| 79 | print $pm_fh @prologue; | ||
| 80 | |||
| 81 | # process each method found in the spec | ||
| 82 | # and write corresponding code into Methods.pm | ||
| 83 | my $method_spec = {}; | ||
| 84 | my %arg_type_usage; | ||
| 85 | for my $method_xml (@methods) { | ||
| 86 | |||
| 87 | # fix-up typos in the docs | ||
| 88 | $method_xml =~ s{</value\s*\n}{</value>\n}xms; | ||
| 89 | |||
| 90 | my $m = eval { XMLin($method_xml, ForceArray=>[qw(param)], KeyAttr=>[]) }; | ||
| 91 | if ($@) { | ||
| 92 | warn "Error parsing $method_xml: $@"; | ||
| 93 | next; | ||
| 94 | } | ||
| 95 | my $methname = delete $m->{methodName}; | ||
| 96 | my $params = delete $m->{params}; | ||
| 97 | warn "Unexpected remnants in $methname methodCall data: ".Dumper($m) | ||
| 98 | if keys %$m; | ||
| 99 | |||
| 100 | my $param_content = delete $params->{content}; # "..." indicating repeating param | ||
| 101 | my $param_list = delete $params->{param}; | ||
| 102 | warn "Unexpected remnants in $methname methodCall params data: ".Dumper($params) | ||
| 103 | if keys %$params; | ||
| 104 | |||
| 105 | my @mk_params; | ||
| 106 | my $last_repeats = 0; | ||
| 107 | if ($param_list) { | ||
| 108 | #warn Dumper($param_list); | ||
| 109 | for my $p (@$param_list) { | ||
| 110 | |||
| 111 | if (my $v = delete $p->{value}) { # scalar | ||
| 112 | my ($type, $example) = %$v; | ||
| 113 | $example =~ s/\n//g; | ||
| 114 | push @mk_params, { type => $type, example => $example, sigil => '$' }; | ||
| 115 | } | ||
| 116 | elsif (my $a = delete $p->{array}) { # array | ||
| 117 | my $elem_info = $a->{data}{value}; | ||
| 118 | my $elem_type = (keys %{$elem_info->[0]})[0]; | ||
| 119 | push @mk_params, { type => 'array', elem_type => $elem_type, sigil => '\@', }; | ||
| 120 | } | ||
| 121 | else { | ||
| 122 | die "I don't know how to handle $methname param ".Dumper($p); | ||
| 123 | } | ||
| 124 | die "Unexpected remnants in $methname param: ".Dumper($p) | ||
| 125 | if keys %$p; | ||
| 126 | |||
| 127 | # note which arg types are used by which methods | ||
| 128 | $arg_type_usage{ $mk_params[-1]{type} }{ $methname }++; | ||
| 129 | } | ||
| 130 | if ($param_content) { | ||
| 131 | # die if it's not an elipsis character ("...") | ||
| 132 | die "Unexpected content in $methname param section: ".Dumper($param_content) | ||
| 133 | unless $param_content eq "\n\x{2026}\n"; | ||
| 134 | $last_repeats = 1; | ||
| 135 | $mk_params[-1]{sigil} = '@'; | ||
| 136 | warn "$methname has repeating params\n" if $opt_trace; | ||
| 137 | } | ||
| 138 | warn Dumper({ $methname => \@mk_params }) if $opt_trace >= 2; | ||
| 139 | } | ||
| 140 | |||
| 141 | $method_spec->{$methname} = { | ||
| 142 | arg_info => \@mk_params, | ||
| 143 | last_arg_repeats => $last_repeats, | ||
| 144 | }; | ||
| 145 | |||
| 146 | } | ||
| 147 | |||
| 148 | # dump the data structure into the module | ||
| 149 | print $pm_fh Data::Dumper->new([$method_spec], [qw(method_spec)]) | ||
| 150 | ->Indent(1)->Sortkeys(1)->Useqq(1)->Dump; | ||
| 151 | print $pm_fh "\n\n1;\n"; | ||
| 152 | |||
| 153 | # write some docs | ||
| 154 | |||
| 155 | for my $methname (sort keys %$method_spec) { | ||
| 156 | # skip docs for some methods | ||
| 157 | next if $methname eq 'UDNS_OpenConnection'; | ||
| 158 | next if $methname eq 'UDNS_NoAutoCommit'; | ||
| 159 | |||
| 160 | my $info = $method_spec->{$methname}; | ||
| 161 | my $arg_info = $info->{arg_info}; | ||
| 162 | $methname =~ s/^UDNS_//; | ||
| 163 | |||
| 164 | my $res = ""; # XXX make "$result = " if method returns a result | ||
| 165 | my @args = map { | ||
| 166 | $_->{sigil} . $_->{type} | ||
| 167 | } @$arg_info; | ||
| 168 | my $args = (@args) ? "(". join(", ", @args). ")" : ""; | ||
| 169 | |||
| 170 | print $pm_fh "=head2 $methname\n\n $res\$udns->$methname$args;\n\n"; | ||
| 171 | |||
| 172 | if (@$arg_info) { | ||
| 173 | for my $arg (@$arg_info) { | ||
| 174 | my $example = $arg->{example}; | ||
| 175 | if ($arg->{type} eq 'array') { | ||
| 176 | $example = sprintf '[ $%s, ... ]', $arg->{elem_type}; | ||
| 177 | } | ||
| 178 | $example = "($example, ...)" if $arg->{sigil} eq '@'; | ||
| 179 | printf $pm_fh qq{ %s%s = %s\n}, | ||
| 180 | $arg->{sigil}, $arg->{type}, $example; | ||
| 181 | } | ||
| 182 | print $pm_fh "\n"; | ||
| 183 | } | ||
| 184 | |||
| 185 | } | ||
| 186 | print $pm_fh "\n=cut\n"; | ||
| 187 | |||
| 188 | # finish up | ||
| 189 | close $pm_fh or die "Error writing $pm_filename: $!"; | ||
| 190 | |||
| 191 | # sanity check the generated file | ||
| 192 | system("perl -c $pm_filename") == 0 | ||
| 193 | or die "Error in generated $pm_filename code"; | ||
| 194 | |||
| 195 | warn Dumper(\%arg_type_usage) if $opt_trace >= 2; | ||
| 196 | warn Dumper([ keys %arg_type_usage ]) if $opt_trace; | ||
| 197 | |||
| 198 | __DATA__ |