summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArkadiy Illarionov <qarkai@gmail.com>2017-06-21 23:15:21 +0300
committerSamuel Mehrbrodt <Samuel.Mehrbrodt@cib.de>2017-06-27 08:48:26 +0200
commit344a5dbc5c35255994a9e31df83cf964e6709246 (patch)
treecebf32a171d24a328323f70ee2cc61d4aa8c8f77
parentc937c6828784260f3536a37f8e297dc721a7d974 (diff)
Ported bin/generate-bash-completion from Perl to Python
Change-Id: Ie1b5cd0a1f64198f8e6362a02484055c5ae6b26e Reviewed-on: https://gerrit.libreoffice.org/39077 Reviewed-by: Samuel Mehrbrodt <Samuel.Mehrbrodt@cib.de> Tested-by: Samuel Mehrbrodt <Samuel.Mehrbrodt@cib.de>
-rwxr-xr-xbin/distro-install-desktop-integration4
-rwxr-xr-xbin/generate-bash-completion240
-rwxr-xr-xbin/generate-bash-completion.py162
3 files changed, 164 insertions, 242 deletions
diff --git a/bin/distro-install-desktop-integration b/bin/distro-install-desktop-integration
index b335dd4d814b..1d2484b324d0 100755
--- a/bin/distro-install-desktop-integration
+++ b/bin/distro-install-desktop-integration
@@ -178,10 +178,10 @@ test -f $DESTDIR/gid_Module_Root_Brand && echo "$PREFIXDIR/bin/soffice" >>$DESTD
# create bash completion
mkdir -p $DESTDIR/usr/share/bash-completion/completions
-"${SRCDIR?}"/bin/generate-bash-completion bin/bash-completion.in $DESTDIR/usr/share/bash-completion/completions/$INSTALLDIRNAME.sh
+"${SRCDIR?}"/bin/generate-bash-completion.py bin/bash-completion.in $DESTDIR/usr/share/bash-completion/completions/$INSTALLDIRNAME.sh
test -f $DESTDIR/gid_Module_Root_Brand && echo "/usr/share/bash-completion/completions/$INSTALLDIRNAME.sh" >>$DESTDIR/gid_Module_Root_Brand
if test "$WITH_COMPAT_OOWRAPPERS" = "TRUE" ; then
- "${SRCDIR?}"/bin/generate-bash-completion --compat-oowrappers bin/bash-completion.in $DESTDIR/usr/share/bash-completion/completions/ooffice.sh
+ "${SRCDIR?}"/bin/generate-bash-completion.py --compat-oowrappers bin/bash-completion.in $DESTDIR/usr/share/bash-completion/completions/ooffice.sh
test -f $DESTDIR/gid_Module_Root_Brand && echo "/usr/share/bash-completion/completions/ooffice.sh" >>$DESTDIR/gid_Module_Root_Brand
fi
diff --git a/bin/generate-bash-completion b/bin/generate-bash-completion
deleted file mode 100755
index 41ad2004b5ac..000000000000
--- a/bin/generate-bash-completion
+++ /dev/null
@@ -1,240 +0,0 @@
-#!/usr/bin/env perl
-# script to generate LibreOffice bash_completion file for the main applications
-# written by Rene Engelhard <rene@debian.org>, Public Domain
-# updated for libreoffice-build by Petr Mladek <pmladek@suse.cz>, Public Domain
-# yes, this script probably is not real good code :) but still easier
-# to maintain than adding those entries statically many times in
-# a file...
-
-use strict;
-
-my @DRAWDOCS=("sxd", "std", "dxf", "emf", "eps", "met", "pct", "sgf", "sgv", "sda",
- "sdd", "vor", "svm", "wmf", "bmp", "gif", "jpg", "jpeg", "jfif", "fif",
- "jpe", "pcd", "pcx", "pgm", "png", "ppm", "psd", "ras", "tga", "tif",
- "tiff", "xbm", "xpm", "odg", "otg", "fodg", "odc", "odi", "sds",
- "wpg", "svg", "vdx", "vsd", "vsdm", "vsdx");
-
-my @IMPRESSDOCS=("sxi", "sti", "ppt", "pps", "pot", "sxd", "sda", "sdd", "sdp",
- "vor", "cgm", "odp", "otp", "fodp", "ppsm", "ppsx", "pptm", "pptx",
- "potm", "potx");
-
-my @TEMPLATES=("stw", "dot", "vor", "stc", "xlt", "sti", "pot", "std", "stw",
- "dotm", "dotx", "potm", "potx", "xltm", "xltx");
-
-my @MATHDOCS=("sxm", "smf", "mml", "odf");
-
-my @MASTERDOCS=("sxg", "odm", "sgl");
-
-my @WRITERDOCS=("doc", "dot", "rtf", "sxw", "stw", "sdw", "vor", "txt", "htm?",
- "xml", "wp", "wpd", "wps", "odt", "ott", "fodt", "docm", "docx",
- "dotm", "dotx");
-
-my @WEBDOCS=("htm", "html", "stw", "txt", "vor", "oth");
-
-my @BASEDOCS=("odb");
-
-my @CALCDOCS=("sxc", "stc", "dif", "dbf", "xls", "xlw", "xlt", "rtf", "sdc", "vor",
- "slk", "txt", "htm", "html", "wk1", "wks", "123", "xml", "ods", "ots",
- "fods", "csv", "xlsb", "xlsm", "xlsx", "xltm", "xltx");
-
-my @EXTENSIONS=("oxt");
-
-# default names of lowrappers
-# use "" if you want to disable any wrapper
-my %APPS = (
- office => "libreoffice",
- office_short => "loffice",
- master => "",
- base => "lobase",
- calc => "localc",
- draw => "lodraw",
- impress => "loimpress",
- math => "lomath",
- template => "lofromtemplate",
- unopkg => "unopkg",
- web => "loweb",
- writer => "lowriter",
-);
-
-my $office_shell_function = "_loexp_";
-
-sub usage()
-{
- print "Script to Generate bash completion for LO wrappers\n\n";
-
- print "Usage: $0 --help\n";
- print " $0 [--binsuffix=suffix]\n";
- print "\t\t[--compat-oowrappers]\n";
- print "\t\t[--office=wrapper_name]\n";
- print "\t\t[--office-short=wrapper_name]\n";
- print "\t\t[--master=wrapper_name]\n";
- print "\t\t[--base=wrapper_name]\n";
- print "\t\t[--calc=wrapper_name]\n";
- print "\t\t[--draw=wrapper_name]\n";
- print "\t\t[--impress=wrapper_name]\n";
- print "\t\t[--math=wrapper_name]\n";
- print "\t\t[--template=wrapper_name]\n";
- print "\t\t[--unopkg=wrapper_name]\n";
- print "\t\t[--web=wrapper_name]\n";
- print "\t\t[--writer=wrapper_name]\n";
- print "\t\tinput_file\n";
- print "\t\toutput_file\n\n";
-
- print "Options:\n";
- print "\t--help\t\tprint this help\n";
- print "\t--binsuffix\tdefines a suffix that is added after each wrapper\n";
- print "\t--compat-oowrappers\tset wrapper names to the old default oo* wrapper names\n";
-
- print "The other options allows to redefine the wrapper names.\n";
- print "The value \"\" can be used to disable any wrapper.\n\n";
-}
-
-my $infilename;
-my $outfilename;
-my $binsuffix = '';
-
-my $opt;
-foreach my $arg (@ARGV) {
- if ( $arg =~ /--help/ ) {
- usage();
- exit 0;
- } elsif ( $arg =~ /--compat-oowrappers/ ) {
- $APPS{'office'} = "openoffice";
- $APPS{'office_short'} = "ooffice";
- $APPS{'master'} = "";
- $APPS{'base'} = "oobase";
- $APPS{'calc'} = "oocalc";
- $APPS{'draw'} = "oodraw";
- $APPS{'impress'} = "ooimpress";
- $APPS{'math'} = "oomath";
- $APPS{'template'} = "oofromtemplate";
- $APPS{'unopkg'} = "unopkg";
- $APPS{'web'} = "ooweb";
- $APPS{'writer'} = "oowriter";
- $office_shell_function = "_ooexp_";
- } elsif ( $arg =~ /--binsuffix=(.*)/ ) {
- $binsuffix = "$1";
- } elsif ( $arg =~ /--office=(.*)/ ) {
- $APPS{'office'} = "$1";
- } elsif ( $arg =~ /--office-short=(.*)/ ) {
- $APPS{'office_short'} = "$1";
- } elsif ( $arg =~ /--master=(.*)/ ) {
- $APPS{'master'} = "$1";
- } elsif ( $arg =~ /--base=(.*)/ ) {
- $APPS{'base'} = "$1";
- } elsif ( $arg =~ /--calc=(.*)/ ) {
- $APPS{'calc'} = "$1";
- } elsif ( $arg =~ /--draw=(.*)/ ) {
- $APPS{'draw'} = "$1";
- } elsif ( $arg =~ /--impress=(.*)/ ) {
- $APPS{'impress'} = "$1"
- } elsif ( $arg =~ /--math=(.*)/ ) {
- $APPS{'math'} = "$1";
- } elsif ( $arg =~ /--template=(.*)/ ) {
- $APPS{'template'} = "$1";
- } elsif ( $arg =~ /--unopkg=(.*)/ ) {
- $APPS{'unopkg'} = "$1";
- } elsif ( $arg =~ /--web=(.*)/ ) {
- $APPS{'web'} = "$1";
- } elsif ( $arg =~ /--writer=(.*)/ ) {
- $APPS{'writer'} = "$1"
- } elsif ( $arg =~ /^-.*/ ) {
- printf STDERR "Error: invalid option \"$arg\", try --help\n";
- exit 1;
- } elsif ( $outfilename ) {
- printf STDERR "Error: too much arguments, try --help\n";
- exit 1;
- } else {
- if ($infilename) {
- $outfilename = "$arg";
- } else {
- $infilename = "$arg";
- }
- }
-}
-
-unless ( $infilename ) {
- printf STDERR "Error: undefined input file, try --help\n";
- exit 1;
-}
-
-unless ( $outfilename ) {
- printf STDERR "Error: undefined output file, try --help\n";
- exit 1;
-}
-
-#add binsuffix
-foreach my $app (keys %APPS) {
- $APPS{$app} .= "$binsuffix" unless ( "$APPS{$app}" eq "" );
-}
-
-sub print_suffixes_check {
- my $app = shift(@_);
- my $first_suffix = shift(@_);
-
- ($first_suffix) || die "Error: No suffix defined for $app\n";
-
- print BCOUTFILE " $app)\t\te=\'!*.+(" . $first_suffix . "|" . uc($first_suffix);
- foreach my $suffix (@_) {
- print BCOUTFILE "|" . $suffix;
- print BCOUTFILE "|" . uc($suffix);
- }
- print BCOUTFILE ")\' ;;\n";
-}
-
-sub print_suffixes_checks {
- foreach my $app (keys %APPS) {
- # skip the disabled wrapper
- next if ( $APPS{$app} eq "" );
-
- if ($app eq "draw" ) { print_suffixes_check ($APPS{$app}, @DRAWDOCS); }
- if ($app eq "writer") { print_suffixes_check ($APPS{$app}, @WRITERDOCS, @MASTERDOCS); }
- if ($app eq "web") { print_suffixes_check ($APPS{$app}, @WEBDOCS); }
- if ($app eq "math") { print_suffixes_check ($APPS{$app}, @MATHDOCS); }
- if ($app eq "impress") { print_suffixes_check ($APPS{$app}, @IMPRESSDOCS); }
- if ($app eq "base") { print_suffixes_check ($APPS{$app}, @BASEDOCS); }
- if ($app eq "calc") { print_suffixes_check ($APPS{$app}, @CALCDOCS); }
- if ($app eq "master") { print_suffixes_check ($APPS{$app}, @MASTERDOCS); }
- if ($app eq "template") { print_suffixes_check ($APPS{$app}, @TEMPLATES); }
- # libreoffice should contain all...
- if (($app eq "office") || ($app eq "office_short"))
- { print_suffixes_check ($APPS{$app}, @DRAWDOCS, @WRITERDOCS, @MATHDOCS, @IMPRESSDOCS, @BASEDOCS, @CALCDOCS, @MASTERDOCS, @TEMPLATES, @WEBDOCS); }
- # unopkg is a standalone tool
- if ($app eq "unopkg") { print_suffixes_check ($APPS{$app}, @EXTENSIONS); }
- }
-}
-
-sub print_apps {
- my $app_to_print;
- foreach my $app (keys %APPS) {
- # skip the disabled wrapper
- next if ( $APPS{$app} eq "" );
-
- print BCOUTFILE "\t\t\t\t\t$app_to_print \\\n" if ($app_to_print);
- $app_to_print = $APPS{$app};
- }
- # the last app will be printed without the final backslash
- ($app_to_print) || die "Error: No LO wrapper was selected\n";
- print BCOUTFILE "\t\t\t\t\t$app_to_print\n";
-}
-
-
-open (BCINFILE, "$infilename") || die "Error: can't open $infilename for reading: $!\n";
-open (BCOUTFILE, "> $outfilename") || die "Error: can't open $outfilename for writing: $!\n";
-
-while (my $line = <BCINFILE>) {
- chomp $line;
-
- $line =~ s/\@OFFICE_SHELL_FUNCTION\@/$office_shell_function/;
-
- if ($line =~ m/\@BASH_COMPLETION_SUFFIXES_CHECKS\@/) {
- print_suffixes_checks();
- } elsif ($line =~ m/\@BASH_COMPLETION_OOO_APPS\@/) {
- print_apps();
- } else {
- print BCOUTFILE "$line\n";
- }
-}
-
-close (BCINFILE);
-close (BCOUTFILE);
diff --git a/bin/generate-bash-completion.py b/bin/generate-bash-completion.py
new file mode 100755
index 000000000000..0702a3635774
--- /dev/null
+++ b/bin/generate-bash-completion.py
@@ -0,0 +1,162 @@
+#!/usr/bin/env python3
+# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
+#
+# 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/.
+#
+
+"""
+Script to generate LibreOffice bash_completion file for the main applications
+"""
+
+import argparse
+import sys
+
+MASTERDOCS = ["sxg", "odm", "sgl"]
+
+BASEDOCS = ["odb"]
+
+CALCDOCS = ["sxc", "stc", "dif", "dbf", "xls", "xlw", "xlt", "rtf", "sdc", "vor",
+ "slk", "txt", "htm", "html", "wk1", "wks", "123", "xml", "ods", "ots",
+ "fods", "csv", "xlsb", "xlsm", "xlsx", "xltm", "xltx"]
+
+DRAWDOCS = ["sxd", "std", "dxf", "emf", "eps", "met", "pct", "sgf", "sgv", "sda",
+ "sdd", "vor", "svm", "wmf", "bmp", "gif", "jpg", "jpeg", "jfif", "fif",
+ "jpe", "pcd", "pcx", "pgm", "png", "ppm", "psd", "ras", "tga", "tif",
+ "tiff", "xbm", "xpm", "odg", "otg", "fodg", "odc", "odi", "sds",
+ "wpg", "svg", "vdx", "vsd", "vsdm", "vsdx"]
+
+IMPRESSDOCS = ["sxi", "sti", "ppt", "pps", "pot", "sxd", "sda", "sdd", "sdp",
+ "vor", "cgm", "odp", "otp", "fodp", "ppsm", "ppsx", "pptm", "pptx",
+ "potm", "potx"]
+
+MATHDOCS = ["sxm", "smf", "mml", "odf"]
+
+WEBDOCS = ["htm", "html", "stw", "txt", "vor", "oth"]
+
+WRITERDOCS = ["doc", "dot", "rtf", "sxw", "stw", "sdw", "vor", "txt", "htm?",
+ "xml", "wp", "wpd", "wps", "odt", "ott", "fodt", "docm", "docx",
+ "dotm", "dotx"]
+
+TEMPLATES = ["stw", "dot", "vor", "stc", "xlt", "sti", "pot", "std", "stw",
+ "dotm", "dotx", "potm", "potx", "xltm", "xltx"]
+
+ALLDOCS = MASTERDOCS + BASEDOCS + CALCDOCS + DRAWDOCS + IMPRESSDOCS + MATHDOCS + WEBDOCS + WRITERDOCS + TEMPLATES
+
+EXTENSIONS = ["oxt"]
+
+
+class App(object):
+ def __init__(self, name, compat_name, suffix_list):
+ self.name = name
+ self.compat_name = compat_name
+ self.suffix_list = suffix_list
+
+
+class SetAppCompatName(argparse.Action):
+ def __call__(self, parser, namespace, values, option_string=None):
+ setattr(namespace, self.dest, True)
+ for app in APPS.values():
+ app.name = app.compat_name
+
+
+class SetAppName(argparse.Action):
+ def __call__(self, parser, namespace, values, option_string=None):
+ APPS[self.dest].name = values
+
+
+# default names of lowrappers
+# use "" for name if you want to disable any wrapper
+APPS = {
+ 'office': App("libreoffice", 'openoffice', ALLDOCS), # libreoffice should contain all
+ 'office_short': App("loffice", 'ooffice', ALLDOCS), # libreoffice should contain all
+ 'master': App("", '', MASTERDOCS),
+ 'base': App("lobase", 'oobase', BASEDOCS),
+ 'calc': App("localc", 'oocalc', CALCDOCS),
+ 'draw': App("lodraw", 'oodraw', DRAWDOCS),
+ 'impress': App("loimpress", 'ooimpress', IMPRESSDOCS),
+ 'math': App("lomath", 'oomath', MATHDOCS),
+ 'template': App("lofromtemplate", 'oofromtemplate', TEMPLATES),
+ 'unopkg': App("unopkg", 'unopkg', EXTENSIONS), # unopkg is a standalone tool
+ 'web': App("loweb", 'ooweb', WEBDOCS),
+ 'writer': App("lowriter", 'oowriter', WRITERDOCS + MASTERDOCS)
+}
+
+
+def check_open(filename, mode):
+ try:
+ with open(filename, mode):
+ pass
+ except OSError as e:
+ mode = 'reading' if mode == 'r' else 'writing'
+ sys.exit("Error: can't open %s for %s: %s" % (filename, mode, e))
+
+
+def print_app_suffixes_check(out, app):
+ if not app.suffix_list:
+ sys.exit('Error: No suffix defined for %s' % app.name)
+
+ suffix_str = '|'.join(['%s|%s' % (s, s.upper()) for s in app.suffix_list])
+ out.write(" %s)\t\te=\'!*.+(%s)\' ;;\n" % (app.name, suffix_str))
+
+
+def print_suffixes_check(out):
+ for app in APPS.values():
+ if not app.name: # skip the disabled wrapper
+ continue
+ print_app_suffixes_check(out, app)
+
+
+def main():
+ parser = argparse.ArgumentParser(description='Script to Generate bash completion for LO wrappers',
+ epilog='The other options allows to redefine the wrapper names.\n'
+ 'The value "" can be used to disable any wrapper.',
+ formatter_class=argparse.RawDescriptionHelpFormatter)
+ parser.add_argument('input_file')
+ parser.add_argument('output_file')
+ parser.add_argument('--binsuffix', metavar='suffix',
+ help='defines a suffix that is added after each wrapper')
+ parser.add_argument('--compat-oowrappers', metavar='', nargs=0, action=SetAppCompatName, default=False,
+ help='set wrapper names to the old default oo* wrapper names')
+ for app in APPS:
+ parser.add_argument('--%s' % app, metavar='wrapper_name', action=SetAppName)
+
+ args = parser.parse_args()
+
+ check_open(args.input_file, 'r')
+ check_open(args.output_file, 'w')
+
+ # add binsuffix
+ if args.binsuffix:
+ for app in APPS.values():
+ if app.name:
+ app.name += args.binsuffix
+
+ if args.compat_oowrappers:
+ office_shell_function = '_ooexp_'
+ else:
+ office_shell_function = '_loexp_'
+
+ # the last app will be printed without the final backslash
+ apps_to_print = ' \\\n'.join(['\t\t\t\t\t%s' % app.name for app in APPS.values() if app.name])
+
+ with open(args.input_file, 'r') as in_fh, open(args.output_file, 'w') as out_fh:
+ for line in in_fh:
+ line = line.replace('@OFFICE_SHELL_FUNCTION@', office_shell_function)
+ if '@BASH_COMPLETION_SUFFIXES_CHECKS@' in line:
+ print_suffixes_check(out_fh)
+ elif '@BASH_COMPLETION_OOO_APPS@' in line:
+ if not apps_to_print:
+ sys.exit('Error: No LO wrapper was selected')
+ out_fh.write('%s\n' % apps_to_print)
+ else:
+ out_fh.write(line)
+
+
+if __name__ == '__main__':
+ main()
+
+# vim: set shiftwidth=4 softtabstop=4 expandtab: