-
Notifications
You must be signed in to change notification settings - Fork 62
/
perl2python.pl
50 lines (35 loc) · 897 Bytes
/
perl2python.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# usage:
#
# perl perl2python.pl --input=Text-Unidecode-1.00_01/lib/Text/Unidecode --output unidecode
use Getopt::Long;
my $input = ".";
my $output = ".";
$result = GetOptions("input=s" => \$input,
"output=s" => \$output);
sub python_escape {
my $x = shift;
return '' unless defined($x);
$x =~ s/\\/\\\\/gs;
$x =~ s/'/\\'/gs;
$x =~ s/([\x00-\x1f])/sprintf("\\x%02x", ord($1))/ges;
return $x;
}
# print "$input\n";
push(@INC, $input);
mkdir $output;
my $n;
for($n = 0; $n < 256; $n++) {
eval( sprintf("require x%02x;\n", $n) );
next unless( $#{$Text::Unidecode::Char[$n]} >= 0 );
# print "$n\n";
open(PYTHON, sprintf(">%s/x%03x.py", $output, $n));
print PYTHON "data = (\n";
my $m = 0;
for my $t (@{$Text::Unidecode::Char[$n]}) {
print PYTHON "'", &python_escape($t), "', # ";
printf PYTHON "0x%02x\n", $m;
$m++;
}
print PYTHON ")\n";
close(PYTHON)
}