~happyaron/ubiquity/lp-1465530

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
# -*- coding: utf-8; Mode: Python; indent-tabs-mode: nil; tab-width: 4 -*-

# Copyright (C) 2011 Canonical Ltd.
# Written by Stephane Graber <stgraber@ubuntu.com>.
#
# 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

from ubiquity import misc, osextras


# I think it's clearer to keep our 'set' method, and it doesn't cause a
# builtin-shadowing problem in practice unless you use 'from
# ubiquity.gsettings import set' (so don't do that).
__pychecker__ = 'no-shadowbuiltin'

_cached_gsettings_exists = None


def _gsettings_exists():
    global _cached_gsettings_exists
    if _cached_gsettings_exists is not None:
        return _cached_gsettings_exists

    _cached_gsettings_exists = osextras.find_on_path('gsettings')
    return _cached_gsettings_exists


def get(schema, key, user=None):
    if not _gsettings_exists():
        return

    if not user:
        user = os.getenv("SUDO_USER", os.getenv("USER", "root"))

    subp = subprocess.Popen(
        ['sudo', '-H', '-u', user, 'gsettings', 'get', schema, key],
        stdout=subprocess.PIPE, stderr=subprocess.PIPE,
        preexec_fn=misc.drop_all_privileges, universal_newlines=True)
    value = subp.communicate()[0].rstrip('\n')

    if not value:
        return

    # If it's a list, it should be accessed through get_list()
    if value.startswith('['):
        return value

    # Parse strings
    if value.startswith('\'') and value.endswith('\''):
        return value[1:-1]

    # Parse ints
    if value.isdigit():
        return int(value)

    # Parse booleans
    if value == 'false':
        return False
    if value == 'true':
        return True


def get_list(schema, key, user=None):
    if not _gsettings_exists():
        return

    value = get(schema, key, user)
    if not value or not value.startswith("[") or not value.endswith("]"):
        return

    try:
        # This only works reliably with int and strings
        elements = eval(value, None, None)
        return elements
    except:
        return


def set(schema, key, value, user=None):
    if not _gsettings_exists():
        return

    if not user:
        user = os.getenv("SUDO_USER", os.getenv("USER", "root"))

    # Convert booleans
    if isinstance(value, bool):
        value = "true" if value else "false"

    subprocess.call(
        ['sudo', '-H', '-u', user, 'gsettings', 'set', schema, key,
         str(value)], preexec_fn=misc.drop_all_privileges)


def set_list(schema, key, values, user=None):
    if not _gsettings_exists():
        return

    value = str(values)
    set(schema, key, value, user)


def unset(schema, key, user=None):
    if not _gsettings_exists():
        return

    if not user:
        user = os.getenv("SUDO_USER", os.getenv("USER", "root"))

    subprocess.call(
        ['sudo', '-H', '-u', user, 'gsettings', 'reset', schema, key],
        preexec_fn=misc.drop_all_privileges)