diff options
author | Jussi Pakkanen <jpakkane@gmail.com> | 2020-02-22 21:36:13 +0200 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2020-03-06 12:31:33 +0100 |
commit | 6ec05ef6fef789f5646b773495285e845e155d77 (patch) | |
tree | 0ecef7c4fb62eacd26bb6f258da5cde0ce8dd372 /oox/source/token | |
parent | 1391b1b3eb3d28c7a606a3f0357eef6ca71267e7 (diff) |
tdf#130911: convert some token generation scripts from Perl to Python.
Change-Id: Id655b6a0cee7bdfe4804941f20fe358af8f3185e
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/89477
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Reviewed-by: Michael Stahl <michael.stahl@cib.de>
Diffstat (limited to 'oox/source/token')
-rw-r--r-- | oox/source/token/namespaces.pl | 99 | ||||
-rw-r--r-- | oox/source/token/namespaces.py | 85 | ||||
-rw-r--r-- | oox/source/token/properties.pl | 58 | ||||
-rw-r--r-- | oox/source/token/properties.py | 50 |
4 files changed, 135 insertions, 157 deletions
diff --git a/oox/source/token/namespaces.pl b/oox/source/token/namespaces.pl deleted file mode 100644 index abbb70814255..000000000000 --- a/oox/source/token/namespaces.pl +++ /dev/null @@ -1,99 +0,0 @@ -# -# This file is part of the LibreOffice project. -# -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. -# -# This file incorporates work covered by the following license notice: -# -# Licensed to the Apache Software Foundation (ASF) under one or more -# contributor license agreements. See the NOTICE file distributed -# with this work for additional information regarding copyright -# ownership. The ASF licenses this file to you under the Apache -# License, Version 2.0 (the "License"); you may not use this file -# except in compliance with the License. You may obtain a copy of -# the License at http://www.apache.org/licenses/LICENSE-2.0 . -# - -$ARGV0 = shift @ARGV; -$ARGV1 = shift @ARGV; -$ARGV2 = shift @ARGV; -$ARGV3 = shift @ARGV; -$ARGV4 = shift @ARGV; -$ARGV5 = shift @ARGV; - -# parse input file - -open( INFILE, $ARGV0 ) or die "cannot open input file: $!"; -my %namespaces; -while( <INFILE> ) -{ - # trim newline - chomp( $_ ); - # trim leading/trailing whitespace - $_ =~ s/^\s*//g; - $_ =~ s/\s*$//g; - # trim comments - $_ =~ s/^#.*//; - # skip empty lines - if( $_ ) - { - # check for valid characters - $_ =~ /^([a-zA-Z][a-zA-Z0-9]*)\s+([a-zA-Z0-9-.:\/]+)\s*$/ or die "Error: invalid character in input data"; - $namespaces{$1} = $2; - } -} -close( INFILE ); - -# OOXML strict namespaces - -open( INFILE_STRICT, $ARGV4 ) or die "cannot open input file: $!"; -my %namespaces_strict; -while( <INFILE_STRICT> ) -{ - # trim newline - chomp( $_ ); - # trim leading/trailing whitespace - $_ =~ s/^\s*//g; - $_ =~ s/\s*$//g; - # trim comments - $_ =~ s/^#.*//; - # skip empty lines - if( $_ ) - { - # check for valid characters - $_ =~ /^([a-zA-Z][a-zA-Z0-9]*)\s+([a-zA-Z0-9-.:\/]+)\s*$/ or die "Error: invalid character in input data"; - $namespaces_strict{$1} = $2; - } -} -close( INFILE_STRICT ); - -# generate output files - -open( IDFILE, ">$ARGV1" ) or die "Error: cannot open output file: $!"; -open( NAMEFILE, ">$ARGV2" ) or die "Error: cannot open output file: $!"; -open( TXTFILE, ">$ARGV3" ) or die "Error: cannot open output file: $!"; -open( NAMEFILE_STRICT, ">$ARGV5" ) or die "Error: cannot open output file: $!"; - -# number of bits to shift the namespace identifier -$shift = 16; - -print ( IDFILE "const size_t NMSP_SHIFT = $shift;\n" ); - -$i = 1; -foreach( sort( keys( %namespaces ) ) ) -{ - print( IDFILE "const sal_Int32 NMSP_$_ = $i << NMSP_SHIFT;\n" ); - $id = $i << $shift; - print( NAMEFILE "{ $id, \"$namespaces{$_}\" },\n" ); - print( NAMEFILE_STRICT "{ $id, \"$namespaces_strict{$_}\" },\n" ); - print( TXTFILE "$id $_ $namespaces{$_}\n" ); - print( TXTFILE "$id $_ $namespaces_strict{$_}\n" ); - ++$i; -} - -close( IDFILE ); -close( NAMEFILE ); -close( NAMEFILE_STRICT ); -close( TXTFILE ); diff --git a/oox/source/token/namespaces.py b/oox/source/token/namespaces.py new file mode 100644 index 000000000000..1244e88e857f --- /dev/null +++ b/oox/source/token/namespaces.py @@ -0,0 +1,85 @@ +# This file is part of the LibreOffice project. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# This file incorporates work covered by the following license notice: +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed +# with this work for additional information regarding copyright +# ownership. The ASF licenses this file to you under the Apache +# License, Version 2.0 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.apache.org/licenses/LICENSE-2.0 . +# + +import sys, re + +infile_name = sys.argv[1] +id_out_name = sys.argv[2] +name_out_name = sys.argv[3] +txt_out_name = sys.argv[4] +instrict_name = sys.argv[5] +strict_out_name = sys.argv[6] + +# parse input file + +namespaces = {} + +with open(infile_name) as infile: + for line in infile: + line = line.strip() + # trim comments + line = line.split('#')[0] + # skip empty lines + if line: + # check for valid characters + m = re.match(r'([a-zA-Z][a-zA-Z0-9]*)\s+([a-zA-Z0-9-.:\/]+)\s*$', line) + if not m: + sys.exit('Invalid character in input data: ' + line) + namespaces[m.group(1)] = m.group(2) + +# OOXML strict namespaces + +namespaces_strict = {} +with open(instrict_name) as infile_strict: + for line in infile_strict: + line = line.strip() + # trim comments + line = line.split('#')[0] + # skip empty lines + if line: + # check for valid characters + m = re.match(r'([a-zA-Z][a-zA-Z0-9]*)\s+([a-zA-Z0-9-.:\/]+)\s*$', line) + if not m: + sys.exit("Error: invalid character in input data: " + line) + namespaces_strict[m.group(1)] = m.group(2) + +# generate output files + +idfile = open(id_out_name, 'w') +namefile = open(name_out_name, 'w') +txtfile = open(txt_out_name, 'w') +namefile_strict = open(strict_out_name, 'w') + +# number of bits to shift the namespace identifier +shift = 16 + +idfile.write("const size_t NMSP_SHIFT = {};\n".format(shift)) + +i = 1; +for token in sorted(namespaces.keys()): + idfile.write("const sal_Int32 NMSP_{} = {} << NMSP_SHIFT;\n".format(token, i)) + cur_id = i << shift + namefile.write("{{ {}, \"{}\" }},\n".format(cur_id, namespaces[token])) + namefile_strict.write("{{ {}, \"{}\" }},\n".format(cur_id, namespaces_strict[token])) + txtfile.write("{} {} {}\n".format(cur_id, token, namespaces[token])) + txtfile.write("{} {} {}\n".format(cur_id, token, namespaces_strict[token])) + i += 1 + +idfile.close() +namefile.close() +namefile_strict.close() +txtfile.close() diff --git a/oox/source/token/properties.pl b/oox/source/token/properties.pl deleted file mode 100644 index d2a2d80153ef..000000000000 --- a/oox/source/token/properties.pl +++ /dev/null @@ -1,58 +0,0 @@ -# -# This file is part of the LibreOffice project. -# -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. -# -# This file incorporates work covered by the following license notice: -# -# Licensed to the Apache Software Foundation (ASF) under one or more -# contributor license agreements. See the NOTICE file distributed -# with this work for additional information regarding copyright -# ownership. The ASF licenses this file to you under the Apache -# License, Version 2.0 (the "License"); you may not use this file -# except in compliance with the License. You may obtain a copy of -# the License at http://www.apache.org/licenses/LICENSE-2.0 . -# - -$ARGV0 = shift @ARGV; -$ARGV1 = shift @ARGV; -$ARGV2 = shift @ARGV; - -# parse input file - -open( INFILE, $ARGV0 ) or die "Error: cannot open input file: $!"; -my %props; -while( <INFILE> ) -{ - # trim newline - chomp( $_ ); - # trim leading/trailing whitespace - $_ =~ s/^\s*//g; - $_ =~ s/\s*$//g; - # check for valid characters - $_ =~ /^[A-Z][a-zA-Z0-9]*$/ or die "Error: invalid character in property '$_'"; - $id = "PROP_$_"; - $props{$_} = $id; -} -close( INFILE ); - -# generate output files - -open( IDFILE, ">$ARGV1" ) or die "Error: cannot open output file: $!"; -open( NAMEFILE, ">$ARGV2" ) or die "Error: cannot open output file: $!"; - -$i = 0; -foreach( sort( keys( %props ) ) ) -{ - print( IDFILE "const sal_Int32 $props{$_} = $i;\n" ); - print( NAMEFILE "/* $i */ \"$_\",\n" ); - ++$i; -} - -print( IDFILE "const sal_Int32 PROP_COUNT = $i;\n" ); -print( IDFILE "const sal_Int32 PROP_INVALID = -1;\n" ); - -close( IDFILE ); -close( NAMEFILE ); diff --git a/oox/source/token/properties.py b/oox/source/token/properties.py new file mode 100644 index 000000000000..8f8bc437b358 --- /dev/null +++ b/oox/source/token/properties.py @@ -0,0 +1,50 @@ +# This file is part of the LibreOffice project. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# This file incorporates work covered by the following license notice: +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed +# with this work for additional information regarding copyright +# ownership. The ASF licenses this file to you under the Apache +# License, Version 2.0 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.apache.org/licenses/LICENSE-2.0 . +# + +import sys, re + +infile_name = sys.argv[1] +id_out_name = sys.argv[2] +name_out_name = sys.argv[3] + +# parse input file + +props = {} +with open(infile_name) as infile: + for line in infile: + line = line.strip() + # check for valid characters + if not re.match(r'[A-Z][a-zA-Z0-9]*$', line): + sys.exit("Error: invalid character in property '{}'".format(line)) + props[line] = "PROP_" + line + +# generate output files + +idfile = open(id_out_name, 'w') +namefile = open(name_out_name, 'w') + +i = 0; +for token in sorted(props.keys()): + idfile.write("const sal_Int32 {} = {};\n".format(props[token], i)) + namefile.write("/* {} */ \"{}\",\n".format(i, token)) + i += 1 + +idfile.write("const sal_Int32 PROP_COUNT = {};\n".format(i)) +idfile.write("const sal_Int32 PROP_INVALID = -1;\n" ) + +idfile.close() +namefile.close() |