summaryrefslogtreecommitdiff
path: root/bin/generate-bash-completion.py
blob: 0702a3635774bc4e80d10476a1830cb694d3201a (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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: