Update git submodules
[LibreOffice.git] / bin / generate-bash-completion.py
blobdb6f49e814b579d93e84fb5a1bd6d0eecf6f8567
1 #!/usr/bin/env python3
2 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
4 # This file is part of the LibreOffice project.
6 # This Source Code Form is subject to the terms of the Mozilla Public
7 # License, v. 2.0. If a copy of the MPL was not distributed with this
8 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 """
12 Script to generate LibreOffice bash_completion file for the main applications
13 """
15 import argparse
16 import sys
18 MASTERDOCS = ["sxg", "odm", "sgl"]
20 BASEDOCS = ["odb"]
22 CALCDOCS = ["sxc", "stc", "dif", "dbf", "xls", "xlw", "xlt", "rtf", "sdc", "vor",
23 "slk", "txt", "htm", "html", "wk1", "wks", "123", "xml", "ods", "ots",
24 "fods", "csv", "xlsb", "xlsm", "xlsx", "xltm", "xltx"]
26 DRAWDOCS = ["sxd", "std", "dxf", "emf", "eps", "met", "pct", "sgf", "sgv", "sda",
27 "sdd", "vor", "svm", "wmf", "bmp", "gif", "jpg", "jpeg", "jfif", "fif",
28 "jpe", "pcd", "pcx", "pgm", "png", "ppm", "psd", "ras", "tga", "tif",
29 "tiff", "xbm", "xpm", "odg", "otg", "fodg", "odc", "odi", "sds",
30 "wpg", "svg", "vdx", "vsd", "vsdm", "vsdx", "pdf"]
32 IMPRESSDOCS = ["sxi", "sti", "ppt", "pps", "pot", "sxd", "sda", "sdd", "sdp",
33 "vor", "cgm", "odp", "otp", "fodp", "ppsm", "ppsx", "pptm", "pptx",
34 "potm", "potx"]
36 MATHDOCS = ["sxm", "smf", "mml", "odf"]
38 WEBDOCS = ["htm", "html", "stw", "txt", "vor", "oth"]
40 WRITERDOCS = ["doc", "dot", "rtf", "sxw", "stw", "sdw", "vor", "txt", "htm?",
41 "xml", "wp", "wpd", "wps", "odt", "ott", "fodt", "docm", "docx",
42 "dotm", "dotx"]
44 TEMPLATES = ["stw", "dot", "vor", "stc", "xlt", "sti", "pot", "std", "stw",
45 "dotm", "dotx", "potm", "potx", "xltm", "xltx"]
47 ALLDOCS = MASTERDOCS + BASEDOCS + CALCDOCS + DRAWDOCS + IMPRESSDOCS + MATHDOCS + WEBDOCS + WRITERDOCS + TEMPLATES
49 EXTENSIONS = ["oxt"]
52 class App(object):
53 def __init__(self, name, compat_name, suffix_list):
54 self.name = name
55 self.compat_name = compat_name
56 self.suffix_list = suffix_list
59 class SetAppCompatName(argparse.Action):
60 def __call__(self, parser, namespace, values, option_string=None):
61 setattr(namespace, self.dest, True)
62 for app in APPS.values():
63 app.name = app.compat_name
66 class SetAppName(argparse.Action):
67 def __call__(self, parser, namespace, values, option_string=None):
68 APPS[self.dest].name = values
71 # default names of lowrappers
72 # use "" for name if you want to disable any wrapper
73 APPS = {
74 'office': App("libreoffice", 'openoffice', ALLDOCS), # libreoffice should contain all
75 'office_short': App("loffice", 'ooffice', ALLDOCS), # libreoffice should contain all
76 'master': App("", '', MASTERDOCS),
77 'base': App("lobase", 'oobase', BASEDOCS),
78 'calc': App("localc", 'oocalc', CALCDOCS),
79 'draw': App("lodraw", 'oodraw', DRAWDOCS),
80 'impress': App("loimpress", 'ooimpress', IMPRESSDOCS),
81 'math': App("lomath", 'oomath', MATHDOCS),
82 'template': App("lofromtemplate", 'oofromtemplate', TEMPLATES),
83 'unopkg': App("unopkg", 'unopkg', EXTENSIONS), # unopkg is a standalone tool
84 'web': App("loweb", 'ooweb', WEBDOCS),
85 'writer': App("lowriter", 'oowriter', WRITERDOCS + MASTERDOCS)
89 def check_open(filename, mode):
90 try:
91 with open(filename, mode):
92 pass
93 except OSError as e:
94 mode = 'reading' if mode == 'r' else 'writing'
95 sys.exit("Error: can't open %s for %s: %s" % (filename, mode, e))
98 def print_app_suffixes_check(out, app):
99 if not app.suffix_list:
100 sys.exit('Error: No suffix defined for %s' % app.name)
102 suffix_str = '|'.join(['%s|%s' % (s, s.upper()) for s in app.suffix_list])
103 out.write(" %s)\t\te=\'!*.+(%s)\' ;;\n" % (app.name, suffix_str))
106 def print_suffixes_check(out):
107 for app in APPS.values():
108 if not app.name: # skip the disabled wrapper
109 continue
110 print_app_suffixes_check(out, app)
113 def main():
114 parser = argparse.ArgumentParser(description='Script to Generate bash completion for LO wrappers',
115 epilog='The other options allows to redefine the wrapper names.\n'
116 'The value "" can be used to disable any wrapper.',
117 formatter_class=argparse.RawDescriptionHelpFormatter)
118 parser.add_argument('input_file')
119 parser.add_argument('output_file')
120 parser.add_argument('--binsuffix', metavar='suffix',
121 help='defines a suffix that is added after each wrapper')
122 parser.add_argument('--compat-oowrappers', metavar='', nargs=0, action=SetAppCompatName, default=False,
123 help='set wrapper names to the old default oo* wrapper names')
124 for app in APPS:
125 parser.add_argument('--%s' % app, metavar='wrapper_name', action=SetAppName)
127 args = parser.parse_args()
129 check_open(args.input_file, 'r')
130 check_open(args.output_file, 'w')
132 # add binsuffix
133 if args.binsuffix:
134 for app in APPS.values():
135 if app.name:
136 app.name += args.binsuffix
138 if args.compat_oowrappers:
139 office_shell_function = '_ooexp_'
140 else:
141 office_shell_function = '_loexp_'
143 # the last app will be printed without the final backslash
144 apps_to_print = ' \\\n'.join(['\t\t\t\t\t%s' % app.name for app in APPS.values() if app.name])
146 with open(args.input_file, 'r') as in_fh, open(args.output_file, 'w') as out_fh:
147 for line in in_fh:
148 line = line.replace('@OFFICE_SHELL_FUNCTION@', office_shell_function)
149 if '@BASH_COMPLETION_SUFFIXES_CHECKS@' in line:
150 print_suffixes_check(out_fh)
151 elif '@BASH_COMPLETION_OOO_APPS@' in line:
152 if not apps_to_print:
153 sys.exit('Error: No LO wrapper was selected')
154 out_fh.write('%s\n' % apps_to_print)
155 else:
156 out_fh.write(line)
159 if __name__ == '__main__':
160 main()
162 # vim: set shiftwidth=4 softtabstop=4 expandtab: