File: perl2python.pl

package info (click to toggle)
unidecode 1.0.23-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,608 kB
  • sloc: python: 49,149; perl: 30; sh: 24; makefile: 5
file content (50 lines) | stat: -rw-r--r-- 897 bytes parent folder | download | duplicates (3)
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)
}