# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
import sys
from dogtail.procedural import focus, FocusError

"""
dogtail-detect session

This script checks for some main pieces of a running GNOME session,
specifically gnome-panel and metacity.

It checks to see that the gnome-panel node has at least some child nodes.
For example, the main gnome-panel node by default has two direct descendents:
the top panel, and the bottom panel.
Metacity's existence is also checked. However, metacity currently never has
any child nodes.

If a proper session is running, the scripts exits with a status of 0.
If no session is found, a non-zero exit code is returned.

Author: Zack Cerza <zcerza@redhat.com>
"""

__author__ = "Zack Cerza <zcerza@redhat.com>"


def GNOME():
    """
    "Is an accessibility-enabled GNOME session running?"
    """

    running = False
    try:
        assert focus.desktop
        assert focus.desktop.children

        focus.application("gnome-panel")
        assert focus.application.children

        focus.application("metacity")
        print(focus.application.node)
        assert focus.application.node
        running = True
        print("GNOME Session detected.")

    except (AttributeError or AssertionError or FocusError):
        print("ERROR: No session found.")

    return running


def KDE():
    """
    "Is an accessibility-enabled KDE session running?"
    """

    running = False
    return running


def JustSomeApps():
    """
    "Is at least one accessibility-enabled application running?"
    """

    assert focus.desktop
    assert focus.desktop.children


if GNOME() or KDE() or JustSomeApps():
    sys.exit()
else:
    sys.exit(1)
