示例#1
0
def set_subpixel_order(direction):
    '''
    Sets the text subpixel anti-alias order.

    :param tps.Direction direction: New direction
    :returns: None
    '''
    if tps.has_program('xfconf-query'):
        try:
            tps.check_call(['xfconf-query', '-c', 'xsettings', '-p',
                            '/Xft/RGBA', '-s', direction.subpixel], logger)
        except subprocess.CalledProcessError as e:
            logger.error(e)

    elif tps.has_program('gsettings'):
        try:
            schemas = tps.check_output(
                ['gsettings', 'list-schemas'], logger).decode().split('\n')
            schema = 'org.gnome.settings-daemon.plugins.xsettings'
            if schema in schemas:
                tps.check_call(['gsettings', 'set', schema, 'rgba-order',
                                direction.subpixel], logger)
            else:
                logger.warning('gsettings is installed, but the "{}" schema '
                               'is not available'.format(schema))
        except subprocess.CalledProcessError as e:
            logger.error(e)
    else:
        logger.warning('neither xfconf-query nor gsettings is installed')
示例#2
0
def set_subpixel_order(direction):
    '''
    Sets the text subpixel anti-alias order.

    :param tps.Direction direction: New direction
    :returns: None
    '''
    if tps.has_program('xfconf-query'):
        try:
            tps.check_call([
                'xfconf-query', '-c', 'xsettings', '-p', '/Xft/RGBA', '-s',
                direction.subpixel
            ], logger)
        except subprocess.CalledProcessError as e:
            logger.error(e)

    elif tps.has_program('gsettings'):
        try:
            schemas = tps.check_output(['gsettings', 'list-schemas'],
                                       logger).decode().split('\n')
            schema = 'org.gnome.settings-daemon.plugins.xsettings'
            if schema in schemas:
                tps.check_call([
                    'gsettings', 'set', schema, 'rgba-order',
                    direction.subpixel
                ], logger)
            else:
                logger.warning('gsettings is installed, but the "{}" schema '
                               'is not available'.format(schema))
        except subprocess.CalledProcessError as e:
            logger.error(e)
    else:
        logger.warning('neither xfconf-query nor gsettings is installed')
示例#3
0
def get_ethernet_con_name():
    '''
    Gets the lexicographically first ethernet connection name from nmcli.

    :returns: str
    :raises tps.network.MissingEthernetException:
    '''
    if not tps.has_program('nmcli'):
        logger.warning('nmcli is not installed')
        return

    if get_nmcli_version() >= (0, 9, 10):
        command = ['nmcli', '--terse', '--fields', 'NAME,TYPE', 'con', 'show']
    else:
        command = ['nmcli', '--terse', '--fields', 'NAME,TYPE', 'con', 'list']
    lines = tps.check_output(command, logger).decode()
    ethernet_cons = []
    for line in lines.split('\n'):
        if line.strip():
            name, type = parse_terse_line(line)
            if 'ethernet' in type.lower():
                ethernet_cons.append(name)
    if ethernet_cons:
        return sorted(ethernet_cons)[0]
    else:
        raise MissingEthernetException('No configured Ethernet connections.')
def toggle(program, state):
    '''
    Toggles the running state of the given progam.

    If state is true, the program will be spawned.

    :param str program: Name of the program
    :param bool state: Desired state
    :returns: None
    '''
    if state:
        try:
            tps.check_output(['pgrep', program], logger)
        except subprocess.CalledProcessError:
            if tps.has_program(program):
                logger.debug(program)
                subprocess.Popen([program])
            else:
                logger.warning('{} is not installed'.format(program))
    else:
        try:
            tps.check_output(['pgrep', program], logger)
            tps.check_call(['killall', program], logger)
        except subprocess.CalledProcessError:
            pass
示例#5
0
def get_ethernet_con_name():
    '''
    Gets the lexicographically first ethernet connection name from nmcli.

    :returns: str
    :raises tps.network.MissingEthernetException:
    '''
    if not tps.has_program('nmcli'):
        logger.warning('nmcli is not installed')
        return

    if get_nmcli_version() >= (0, 9, 10):
        command = ['nmcli', '--terse', '--fields', 'NAME,TYPE', 'con', 'show']
    else:
        command = ['nmcli', '--terse', '--fields', 'NAME,TYPE', 'con', 'list']
    lines = tps.check_output(command, logger).decode()
    ethernet_cons = []
    for line in lines.split('\n'):
        if line.strip():
            name, type = parse_terse_line(line)
            if 'ethernet' in type.lower():
                ethernet_cons.append(name)
    if ethernet_cons:
        return sorted(ethernet_cons)[0]
    else:
        raise MissingEthernetException('No configured Ethernet connections.')
示例#6
0
def toggle(program, state):
    '''
    Toggles the running state of the given progam.

    If state is true, the program will be spawned.

    :param str program: Name of the program
    :param bool state: Desired state
    :returns: None
    '''
    if state:
        try:
            tps.check_output(['pgrep', program], logger)
        except subprocess.CalledProcessError:
            if tps.has_program(program):
                logger.debug(program)
                subprocess.Popen([program])
            else:
                logger.warning('{} is not installed'.format(program))
    else:
        try:
            tps.check_output(['pgrep', program], logger)
            tps.check_call(['killall', program], logger)
        except subprocess.CalledProcessError:
            pass
示例#7
0
def postdock(state, config):
    '''
    Executes postdock hook if it exists.

    :param bool state: Whether new state is on
    :param configparser.ConfigParser config: Global config
    :returns: None
    '''
    hook = os.path.expanduser(config['hooks']['postdock'])
    if tps.has_program(hook):
        tps.call([hook, 'on' if state else 'off'], logger)
示例#8
0
def postrotate(direction, config):
    '''
    Executes postrotate hook if it exists.

    :param tps.Direction direction: Desired direction
    :param configparser.ConfigParser config: Global config
    :returns: None
    '''
    hook = os.path.expanduser(config['hooks']['postrotate'])
    if tps.has_program(hook):
        tps.call([hook, direction.xrandr], logger)
示例#9
0
def postrotate(direction, config):
    '''
    Executes postrotate hook if it exists.

    :param tps.Direction direction: Desired direction
    :param configparser.ConfigParser config: Global config
    :returns: None
    '''
    hook = os.path.expanduser(config['hooks']['postrotate'])
    if tps.has_program(hook):
        tps.call([hook, direction.xrandr], logger)
示例#10
0
def postdock(state, config):
    '''
    Executes postdock hook if it exists.

    :param bool state: Whether new state is on
    :param configparser.ConfigParser config: Global config
    :returns: None
    '''
    hook = os.path.expanduser(config['hooks']['postdock'])
    if tps.has_program(hook):
        tps.call([hook, 'on' if state else 'off'], logger)
示例#11
0
def set_volume(loudness):
    '''
    Sets the volume to the given loudness.

    :param str loudness: Loudness value as string with percent
    '''
    if not tps.has_program('pactl'):
        logger.warning('pactl is not installed')
        return

    tps.check_call(['pactl', 'set-sink-volume', '0', loudness], logger)
示例#12
0
def set_brightness(brightness):
    '''
    Sets the brightness with ``xbacklight``.

    :param str brightness: Percent value of brightness, e. g. ``60%``
    :returns: None
    '''
    if not tps.has_program('xbacklight'):
        logger.warning('xbacklight is not installed')
        return

    tps.check_call(['xbacklight', '-set', brightness], logger)
示例#13
0
def restart(connection):
    '''
    Disables and enables the given connection if it exists.

    :param str connection: Name of the connection
    :returns: None
    '''
    if not tps.has_program('nmcli'):
        logger.warning('nmcli is not installed')
        return

    tps.check_call(['nmcli', 'con', 'up', 'id', connection], logger)
示例#14
0
def set_brightness(brightness):
    '''
    Sets the brightness with ``xbacklight``.

    :param str brightness: Percent value of brightness, e. g. ``60%``
    :returns: None
    '''
    if not tps.has_program('xbacklight'):
        logger.warning('xbacklight is not installed')
        return

    tps.check_call(['xbacklight', '-set', brightness], logger)
示例#15
0
def restart(connection):
    '''
    Disables and enables the given connection if it exists.

    :param str connection: Name of the connection
    :returns: None
    '''
    if not tps.has_program('nmcli'):
        logger.warning('nmcli is not installed')
        return

    tps.check_call(['nmcli', 'con', 'up', 'id', connection], logger)
示例#16
0
def unmute(loudness):
    '''
    Unmutes the speakers and sets them to the given loudness.

    :param str loudness: Loudness value as string with percent
    '''
    if not tps.has_program('pactl'):
        logger.warning('pactl is not installed')
        return

    tps.check_call(['pactl', 'set-sink-volume', '0', loudness], logger)
    tps.check_call(['pactl', 'set-sink-mute', '0', '0'], logger)
示例#17
0
def set_brightness(brightness):
    '''
    Sets the brightness with ``xbacklight``.

    :param str brightness: Percent value of brightness, e. g. ``60%``
    :returns: None
    '''
    if not tps.has_program('xbacklight'):
        logger.warning('xbacklight is not installed')
        return

    status = tps.call(['xbacklight', '-set', brightness], logger)
    if status != 0:
        logger.warning('`xbacklight` cannot set the brightness, this is ignored.')
示例#18
0
def set_brightness(brightness):
    '''
    Sets the brightness with ``xbacklight``.

    :param str brightness: Percent value of brightness, e. g. ``60%``
    :returns: None
    '''
    if not tps.has_program('xbacklight'):
        logger.warning('xbacklight is not installed')
        return

    status = tps.call(['xbacklight', '-set', brightness], logger)
    if status != 0:
        logger.warning(
            '`xbacklight` cannot set the brightness, this is ignored.')
示例#19
0
def get_pulseaudio_sinks():
    '''
    Retrieves the available PulseAudio sinks on the current system
    and returns them in a set of strings

    :returns: List of sinks. If ``pactl`` is not installed, an empty list is
    returned instead.
    :rtype: list of str
    '''
    if not tps.has_program('pactl'):
        logger.warning('pactl is not installed')
        return []

    output = tps.check_output(['pactl', 'list', 'sinks'], logger).decode()
    sinks = re.findall('^Sink #(\d+)$', output, flags=re.MULTILINE)
    return sinks
示例#20
0
def set_wifi(state):
    '''
    Sets the wifi hardware to the given state.

    :param bool state: Desired state
    :returns: None
    '''
    if not tps.has_program('nmcli'):
        logger.warning('nmcli is not installed')
        return

    if get_nmcli_version() >= (0, 9, 10):
        command = ['nmcli', 'radio', 'wifi', 'on' if state else 'off']
    else:
        command = ['nmcli', 'nm', 'wifi', 'on' if state else 'off']
    tps.check_call(command, logger)
示例#21
0
def get_nmcli_version():
    '''
    Gets the version of nmcli, removing trailing zeroes.

    :returns: tuple, e.g. (0, 9, 10) for version 0.9.10.0
    '''
    if not tps.has_program('nmcli'):
        logger.warning('nmcli is not installed')
        return

    response = tps.check_output(['nmcli', '--version'], logger).decode()
    version_str = re.search(r'\d+(\.\d+)*', response).group(0)
    version_list = [int(n) for n in version_str.split('.')]
    while version_list[-1] == 0:
        version_list.pop()
    return tuple(version_list)
示例#22
0
def get_pulseaudio_sinks():
    '''
    Retrieves the available PulseAudio sinks on the current system
    and returns them in a set of strings

    :returns: List of sinks. If ``pactl`` is not installed, an empty list is
    returned instead.
    :rtype: list of str
    '''
    if not tps.has_program('pactl'):
        logger.warning('pactl is not installed')
        return []

    output = tps.check_output(['pactl', 'list', 'sinks'], logger).decode()
    sinks = re.findall('^Sink #(\d+)$', output, flags=re.MULTILINE)
    return sinks
示例#23
0
def get_nmcli_version():
    '''
    Gets the version of nmcli, removing trailing zeroes.

    :returns: tuple, e.g. (0, 9, 10) for version 0.9.10.0
    '''
    if not tps.has_program('nmcli'):
        logger.warning('nmcli is not installed')
        return

    response = tps.check_output(['nmcli', '--version'], logger).decode()
    version_str = re.search(r'\d+(\.\d+)*', response).group(0)
    version_list = [int(n) for n in version_str.split('.')]
    while version_list[-1] == 0:
        version_list.pop()
    return tuple(version_list)
示例#24
0
def set_wifi(state):
    '''
    Sets the wifi hardware to the given state.

    :param bool state: Desired state
    :returns: None
    '''
    if not tps.has_program('nmcli'):
        logger.warning('nmcli is not installed')
        return

    if get_nmcli_version() >= (0, 9, 10):
        command = ['nmcli', 'radio', 'wifi', 'on' if state else 'off']
    else:
        command = ['nmcli', 'nm', 'wifi', 'on' if state else 'off']
    tps.check_call(command, logger)
示例#25
0
def set_launcher(autohide):
    '''
    Sets the autohide property of the Unity launcher.

    In the back, this uses ``dconf``. If that is not installed, this just fails
    with a warning.

    :param bool autohide: True if autohide is desired
    '''
    if not tps.has_program('dconf'):
        logger.warning('dconf is not installed')
        return

    set_to = '1' if autohide else '0'
    tps.check_call(['dconf', 'write',
                    '/org/compiz/profiles/unity/plugins/unityshell/launcher-hide-mode',
                    set_to], logger)
示例#26
0
def set_launcher(autohide):
    '''
    Sets the autohide property of the Unity launcher.

    In the back, this uses ``dconf``. If that is not installed, this just fails
    with a warning.

    :param bool autohide: True if autohide is desired
    '''
    if not tps.has_program('dconf'):
        logger.warning('dconf is not installed')
        return

    set_to = '1' if autohide else '0'
    tps.check_call([
        'dconf', 'write',
        '/org/compiz/profiles/unity/plugins/unityshell/launcher-hide-mode',
        set_to
    ], logger)