#! /usr/bin/python

import json
import os
import subprocess
import tempfile
import traceback

from ansible.module_utils.basic import *


def get_extension_state(name):
    result = subprocess.check_output([
        'gsettings', 'get', 'org.gnome.shell', 'enabled-extensions'
    ])
    if result.startswith('@as'):
        result = result.replace('@as', '')

    result = json.loads(result.replace("'", '"'))
    return 'enabled' if name in result else 'disabled'


def clone_git_repo(uri, target_path):
    subprocess.check_output([
        'git', 'clone', uri, target_path,
    ])


def toggle_extension(name, state='enable'):
    assert state in ('enable', 'disable')
    subprocess.check_output([
        'gnome-shell-extension-tool', '--{}-extension={}'.format(state, name)
    ])


def disable_extension(uri, name):
    toggle_extension(name, state='disable')


def enable_extension(uri, name, subfolder):
    if uri is not None:
        path = os.path.expanduser('~/.local/share/gnome-shell/extensions/{}'.format(name))
        if not os.path.exists(path):
            install_extension(uri, name, subfolder)

    toggle_extension(name, state='enable')


def install_extension(uri, name, subfolder):
    temp_dir = tempfile.mkdtemp()
    clone_git_repo(uri, temp_dir)

    if subfolder:
        src_path = '{}/{}'.format(temp_dir, subfolder)
    else:
        src_path = temp_dir

    extensions_path = os.path.expanduser('~/.local/share/gnome-shell/extensions')
    if not os.path.exists(extensions_path):
        os.makedirs(extensions_path)

    os.rename(
        src_path,
        '{}/{}'.format(extensions_path, name),
    )


def main():
    module = AnsibleModule(
        argument_spec={
            'state': {'choices': ['enabled', 'disabled'], 'default': 'enabled'},
            'uri': {'required': False},
            'name': {'required': True},
            'subfolder': {'default': '', 'required': False},
        },
        supports_check_mode=True,
    )

    uri = module.params['uri']
    name = module.params.get('name')
    state = module.params['state']
    subfolder = module.params['subfolder']

    try:
        old_state = get_extension_state(name)
        changed = old_state != state

        if changed and not module.check_mode:
            if state == 'enabled':
                enable_extension(uri, name, subfolder)
            else:
                disable_extension(uri, name)
    except Exception as e:
        module.fail_json(msg=traceback.format_exc())
    else:
        module.exit_json(
            changed=changed,
            state=state,
            uri=uri,
            old_state=old_state,
        )

if __name__ == '__main__':
    main()

25de9f73c4f28ac0ebaec671295546607bc3f5b5
<mask token>


def clone_git_repo(uri, target_path):
    subprocess.check_output(['git', 'clone', uri, target_path])


<mask token>


def main():
    module = AnsibleModule(argument_spec={'state': {'choices': ['enabled',
        'disabled'], 'default': 'enabled'}, 'uri': {'required': False},
        'name': {'required': True}, 'subfolder': {'default': '', 'required':
        False}}, supports_check_mode=True)
    uri = module.params['uri']
    name = module.params.get('name')
    state = module.params['state']
    subfolder = module.params['subfolder']
    try:
        old_state = get_extension_state(name)
        changed = old_state != state
        if changed and not module.check_mode:
            if state == 'enabled':
                enable_extension(uri, name, subfolder)
            else:
                disable_extension(uri, name)
    except Exception as e:
        module.fail_json(msg=traceback.format_exc())
    else:
        module.exit_json(changed=changed, state=state, uri=uri, old_state=
            old_state)


<mask token>

<mask token>


def get_extension_state(name):
    result = subprocess.check_output(['gsettings', 'get', 'org.gnome.shell',
        'enabled-extensions'])
    if result.startswith('@as'):
        result = result.replace('@as', '')
    result = json.loads(result.replace("'", '"'))
    return 'enabled' if name in result else 'disabled'


def clone_git_repo(uri, target_path):
    subprocess.check_output(['git', 'clone', uri, target_path])


<mask token>


def enable_extension(uri, name, subfolder):
    if uri is not None:
        path = os.path.expanduser('~/.local/share/gnome-shell/extensions/{}'
            .format(name))
        if not os.path.exists(path):
            install_extension(uri, name, subfolder)
    toggle_extension(name, state='enable')


<mask token>


def main():
    module = AnsibleModule(argument_spec={'state': {'choices': ['enabled',
        'disabled'], 'default': 'enabled'}, 'uri': {'required': False},
        'name': {'required': True}, 'subfolder': {'default': '', 'required':
        False}}, supports_check_mode=True)
    uri = module.params['uri']
    name = module.params.get('name')
    state = module.params['state']
    subfolder = module.params['subfolder']
    try:
        old_state = get_extension_state(name)
        changed = old_state != state
        if changed and not module.check_mode:
            if state == 'enabled':
                enable_extension(uri, name, subfolder)
            else:
                disable_extension(uri, name)
    except Exception as e:
        module.fail_json(msg=traceback.format_exc())
    else:
        module.exit_json(changed=changed, state=state, uri=uri, old_state=
            old_state)


<mask token>

<mask token>


def get_extension_state(name):
    result = subprocess.check_output(['gsettings', 'get', 'org.gnome.shell',
        'enabled-extensions'])
    if result.startswith('@as'):
        result = result.replace('@as', '')
    result = json.loads(result.replace("'", '"'))
    return 'enabled' if name in result else 'disabled'


def clone_git_repo(uri, target_path):
    subprocess.check_output(['git', 'clone', uri, target_path])


def toggle_extension(name, state='enable'):
    assert state in ('enable', 'disable')
    subprocess.check_output(['gnome-shell-extension-tool',
        '--{}-extension={}'.format(state, name)])


def disable_extension(uri, name):
    toggle_extension(name, state='disable')


def enable_extension(uri, name, subfolder):
    if uri is not None:
        path = os.path.expanduser('~/.local/share/gnome-shell/extensions/{}'
            .format(name))
        if not os.path.exists(path):
            install_extension(uri, name, subfolder)
    toggle_extension(name, state='enable')


def install_extension(uri, name, subfolder):
    temp_dir = tempfile.mkdtemp()
    clone_git_repo(uri, temp_dir)
    if subfolder:
        src_path = '{}/{}'.format(temp_dir, subfolder)
    else:
        src_path = temp_dir
    extensions_path = os.path.expanduser(
        '~/.local/share/gnome-shell/extensions')
    if not os.path.exists(extensions_path):
        os.makedirs(extensions_path)
    os.rename(src_path, '{}/{}'.format(extensions_path, name))


def main():
    module = AnsibleModule(argument_spec={'state': {'choices': ['enabled',
        'disabled'], 'default': 'enabled'}, 'uri': {'required': False},
        'name': {'required': True}, 'subfolder': {'default': '', 'required':
        False}}, supports_check_mode=True)
    uri = module.params['uri']
    name = module.params.get('name')
    state = module.params['state']
    subfolder = module.params['subfolder']
    try:
        old_state = get_extension_state(name)
        changed = old_state != state
        if changed and not module.check_mode:
            if state == 'enabled':
                enable_extension(uri, name, subfolder)
            else:
                disable_extension(uri, name)
    except Exception as e:
        module.fail_json(msg=traceback.format_exc())
    else:
        module.exit_json(changed=changed, state=state, uri=uri, old_state=
            old_state)


<mask token>

<mask token>


def get_extension_state(name):
    result = subprocess.check_output(['gsettings', 'get', 'org.gnome.shell',
        'enabled-extensions'])
    if result.startswith('@as'):
        result = result.replace('@as', '')
    result = json.loads(result.replace("'", '"'))
    return 'enabled' if name in result else 'disabled'


def clone_git_repo(uri, target_path):
    subprocess.check_output(['git', 'clone', uri, target_path])


def toggle_extension(name, state='enable'):
    assert state in ('enable', 'disable')
    subprocess.check_output(['gnome-shell-extension-tool',
        '--{}-extension={}'.format(state, name)])


def disable_extension(uri, name):
    toggle_extension(name, state='disable')


def enable_extension(uri, name, subfolder):
    if uri is not None:
        path = os.path.expanduser('~/.local/share/gnome-shell/extensions/{}'
            .format(name))
        if not os.path.exists(path):
            install_extension(uri, name, subfolder)
    toggle_extension(name, state='enable')


def install_extension(uri, name, subfolder):
    temp_dir = tempfile.mkdtemp()
    clone_git_repo(uri, temp_dir)
    if subfolder:
        src_path = '{}/{}'.format(temp_dir, subfolder)
    else:
        src_path = temp_dir
    extensions_path = os.path.expanduser(
        '~/.local/share/gnome-shell/extensions')
    if not os.path.exists(extensions_path):
        os.makedirs(extensions_path)
    os.rename(src_path, '{}/{}'.format(extensions_path, name))


def main():
    module = AnsibleModule(argument_spec={'state': {'choices': ['enabled',
        'disabled'], 'default': 'enabled'}, 'uri': {'required': False},
        'name': {'required': True}, 'subfolder': {'default': '', 'required':
        False}}, supports_check_mode=True)
    uri = module.params['uri']
    name = module.params.get('name')
    state = module.params['state']
    subfolder = module.params['subfolder']
    try:
        old_state = get_extension_state(name)
        changed = old_state != state
        if changed and not module.check_mode:
            if state == 'enabled':
                enable_extension(uri, name, subfolder)
            else:
                disable_extension(uri, name)
    except Exception as e:
        module.fail_json(msg=traceback.format_exc())
    else:
        module.exit_json(changed=changed, state=state, uri=uri, old_state=
            old_state)


if __name__ == '__main__':
    main()

#! /usr/bin/python

import json
import os
import subprocess
import tempfile
import traceback

from ansible.module_utils.basic import *


def get_extension_state(name):
    result = subprocess.check_output([
        'gsettings', 'get', 'org.gnome.shell', 'enabled-extensions'
    ])
    if result.startswith('@as'):
        result = result.replace('@as', '')

    result = json.loads(result.replace("'", '"'))
    return 'enabled' if name in result else 'disabled'


def clone_git_repo(uri, target_path):
    subprocess.check_output([
        'git', 'clone', uri, target_path,
    ])


def toggle_extension(name, state='enable'):
    assert state in ('enable', 'disable')
    subprocess.check_output([
        'gnome-shell-extension-tool', '--{}-extension={}'.format(state, name)
    ])


def disable_extension(uri, name):
    toggle_extension(name, state='disable')


def enable_extension(uri, name, subfolder):
    if uri is not None:
        path = os.path.expanduser('~/.local/share/gnome-shell/extensions/{}'.format(name))
        if not os.path.exists(path):
            install_extension(uri, name, subfolder)

    toggle_extension(name, state='enable')


def install_extension(uri, name, subfolder):
    temp_dir = tempfile.mkdtemp()
    clone_git_repo(uri, temp_dir)

    if subfolder:
        src_path = '{}/{}'.format(temp_dir, subfolder)
    else:
        src_path = temp_dir

    extensions_path = os.path.expanduser('~/.local/share/gnome-shell/extensions')
    if not os.path.exists(extensions_path):
        os.makedirs(extensions_path)

    os.rename(
        src_path,
        '{}/{}'.format(extensions_path, name),
    )


def main():
    module = AnsibleModule(
        argument_spec={
            'state': {'choices': ['enabled', 'disabled'], 'default': 'enabled'},
            'uri': {'required': False},
            'name': {'required': True},
            'subfolder': {'default': '', 'required': False},
        },
        supports_check_mode=True,
    )

    uri = module.params['uri']
    name = module.params.get('name')
    state = module.params['state']
    subfolder = module.params['subfolder']

    try:
        old_state = get_extension_state(name)
        changed = old_state != state

        if changed and not module.check_mode:
            if state == 'enabled':
                enable_extension(uri, name, subfolder)
            else:
                disable_extension(uri, name)
    except Exception as e:
        module.fail_json(msg=traceback.format_exc())
    else:
        module.exit_json(
            changed=changed,
            state=state,
            uri=uri,
            old_state=old_state,
        )

if __name__ == '__main__':
    main()
