File: mate-tweak-helper

package info (click to toggle)
mate-tweak 18.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,360 kB
  • sloc: python: 1,430; sh: 65; makefile: 11
file content (163 lines) | stat: -rwxr-xr-x 6,273 bytes parent folder | download
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
163
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Copyright (C) 2015 Martin Wimpress <code@ubuntu-mate.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

import os
import subprocess
import shutil
import sys
import tempfile
from gi.repository import Gio
from subprocess import PIPE


def autostop(filename):
    desktopfile = os.path.join('/','etc','xdg','autostart', filename)
    if os.path.exists(desktopfile):
        os.remove(desktopfile)


def install_layout(filename):
    if os.path.exists(os.path.join(tempfile.gettempdir(),filename)):
        shutil.copy2(os.path.join(tempfile.gettempdir(),filename),
                     os.path.join('/','usr','share','mate-panel','layouts',filename))
        os.remove(os.path.join(tempfile.gettempdir(),filename))
    else:
        # If a .dock hint is not found but is installed, remove it.
        if filename.endswith('dock') and os.path.exists(os.path.join('/','usr','share','mate-panel','layouts',filename)):
            os.remove(os.path.join('/','usr','share','mate-panel','layouts',filename))
        print('Unable to find ' + os.path.join(tempfile.gettempdir(),filename))


def delete_layout(filename):
    if 'tweak' in filename:
        layout = os.path.join('/','usr','share','mate-panel','layouts',filename)
        if os.path.exists(layout):
            os.remove(layout)
        else:
            print('Unable to find ' + layout)
    else:
        print('WARNING! I will only delete custom layouts. Skipping ' + layout)


def backup_layout(filename):
    VALID = {'toplevel': ('expand', 'size', 'orientation'),
             'launcher': ('object-type', 'launcher-location', 'menu-path', 'toplevel-id', 'position', 'panel-right-stick', 'locked'),
             'applet': ('object-type', 'applet-iid', 'toplevel-id', 'position', 'panel-right-stick', 'locked'),
             'drawer': ('object-type', 'attached-toplevel-id', 'toplvel-id', 'position', 'panel-right-stick', 'use-custom-icon'),
             'menu-bar': ('object-type', 'applet-iid', 'toplevel-id', 'position', 'panel-right-stick', 'locked'),
             'menu': ('object-type', 'toplevel-id', 'position', 'panel-right-stick', 'locked'),
             'action': ('object-type', 'action-type', 'position', 'toplevel-id', 'panel-right-stick', 'locked'),
             'separator': ('object-type', 'toplevel-id', 'position', 'panel-right-stick', 'locked')}

    schemas = {'panel': 'org.mate.panel',
               'object': 'org.mate.panel.object',
               'toplevel':'org.mate.panel.toplevel'}

    paths = {'object': '/org/mate/panel/objects/',
             'toplevel': '/org/mate/panel/toplevels/'}

    general_settings = Gio.Settings.new(schemas['panel'])

    toplevel_ids = general_settings['toplevel-id-list']
    object_ids = general_settings['object-id-list']

    layout = []

    for toplevel in toplevel_ids:
        settings = Gio.Settings.new_with_path(
            schemas['toplevel'],
            paths['toplevel'] + toplevel + '/')

        layout.append("[Toplevel %s]\n" % toplevel)

        for key in settings.keys():
            val = settings[key]
            if str(val) == "True" or str(val) == "False":
                val = str(val).lower()

            if key in VALID['toplevel']:
                layout.append("%s=%s\n" % (key, val))
        layout.append("\n")

    for obj in object_ids:
        settings = Gio.Settings.new_with_path(
            schemas['object'],
            paths['object'] + obj + '/')

        obj_toplevel = settings['toplevel-id']
        obj_type = settings['object-type']
        obj_name = ""
        if obj_type == "applet":
            obj_name = settings['applet-iid'].split(":")[-1]
        elif obj_type == "action":
            obj_name = settings['action-type']
        else:
            obj_name = obj_type

        if not obj_toplevel in toplevel_ids:
            print("WARNING! object \"%s\" references unknown toplevel... skipped" % obj_name)
            continue

        layout.append("[Object %s]\n" % obj_name.lower())
        for key in settings.keys():
            if key in VALID[obj_type]:
                val = settings[key]
                if str(val) == "True" or str(val) == "False":
                    val = str(val).lower()

                layout.append("%s=%s\n" % (key, val))
        layout.append("\n")

    #print(layout)
    with open(os.path.join(tempfile.gettempdir(), filename + '.layout'), 'w') as f:
        f.writelines(layout)

    # Dump dconf panel
    dconf_process = subprocess.Popen(['dconf', 'dump', '/org/mate/panel/'], stdout=PIPE)
    dump = dconf_process.communicate()[0].decode("UTF-8")
    with open(os.path.join(tempfile.gettempdir(),filename + '.panel'), 'w') as f:
        f.writelines(dump)


def backup_dock(filename):
    with open(os.path.join(tempfile.gettempdir(), filename + '.dock'), 'w') as f:
        f.writelines('plank')

if __name__ == '__main__':
    if len(sys.argv) == 3:
        action = sys.argv[1]
        filename = sys.argv[2]
        if action == 'autostop':
            autostop(filename)
        elif action == 'backup':
            backup_layout(filename)
        elif action == 'dock':
            backup_dock(filename)
        elif action == 'delete':
            delete_layout(filename + '.dock')
            delete_layout(filename + '.layout')
            delete_layout(filename + '.panel')
        elif action == 'install':
            install_layout(filename + '.dock')
            install_layout(filename + '.layout')
            install_layout(filename + '.panel')
    else:
        print("ERROR! No action supplied.")
        sys.exit(1)