<commit_before># This file is part of the GOsa framework.
#
#  http://gosa-project.org
#
# Copyright:
#  (C) 2016 GONICUS GmbH, Germany, http://www.gonicus.de
#
# See the LICENSE file in the project's top-level directory for details.

"""
.. _dbus-cups:

GOsa D-Bus Environment Plugin
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This plugin allows to configure a user environment (e.g. application menu).

"""
import glob
import logging
import dbus.service

from gosa.common import Environment
from gosa.common.components import Plugin
from gosa.common.components.jsonrpc_utils import Binary
from gosa.common.gjson import loads
from gosa.dbus import get_system_bus
import shutil
import xdg.DesktopEntry
import xdg.Menu
from bs4 import BeautifulSoup

from xml.dom.minidom import getDOMImplementation
import os

XDG_CONFIG_DIR = "/etc/xdg/xdg-gnome"
XDG_MENU_PREFIX = "gnome-"
GLOBAL_MENU = XDG_CONFIG_DIR + '/menus/' + XDG_MENU_PREFIX + 'applications.menu'


class DBusEnvironmentHandler(dbus.service.Object, Plugin):
    """
    This dbus plugin is able to configure a users environment.

    """
    add_other_menu = False
    home_dir = None
    local_menu = os.path.join(".config", "menus", XDG_MENU_PREFIX + 'applications.menu')
    local_applications = os.path.join(".local", "share", "applications")
    local_applications_scripts = os.path.join(".local", "share", "goto", "applications-scripts")
    local_icons = os.path.join(".local", "share", "icons")
    local_directories = os.path.join(".local", "share", "desktop-directories")
    __initialized_dirs = []

    def __init__(self):
        conn = get_system_bus()
        dbus.service.Object.__init__(self, conn, '/org/gosa/environment')
        self.env = Environment.getInstance()
        self.log = logging.getLogger(__name__)

    @dbus.service.method('org.gosa', in_signature='ss', out_signature='')
    def configureUserMenu(self, user, user_menu):
        """ configure a users application menu """
        user_menu = loads(user_menu)
        self.__initialized_dirs = []
        self.home_dir = os.path.expanduser('~%s' % user)
        self.init_directories(user_menu)
        self.init_applications(user_menu)
        self.init_menu(user_menu)
        if self.env.config.get("user.chown-menu", default="false") == "true":
            self.chown_dirs(user)

    def chown_dirs(self, user):
        self.log.debug("chown %s dirs to user %s" % (self.__initialized_dirs, user))
        for dir in self.__initialized_dirs:
            self.__chown(dir, user=user)

    def __chown(self, path, user):
        shutil.chown(path, user=user)
        for item in glob.glob(path+'/*'):
            cur_path = os.path.join(path, item)
            if os.path.isdir(item):
                self.__chown(cur_path, user)
            else:
                shutil.chown(cur_path, user=user)

    def init_dir(self, dir):
        if not os.path.exists(dir):
            os.makedirs(dir)
        shutil.rmtree(dir)
        os.makedirs(dir)

        self.__initialized_dirs.append(dir)

    def init_applications(self, user_menu):
        app_dir = os.path.join(self.home_dir, self.local_applications)
        self.init_dir(app_dir)
        scripts_dir = os.path.join(self.home_dir, self.local_applications_scripts)
        self.init_dir(scripts_dir)

        def get_appname(item):
            result = []

            if 'apps' in item:
                for cn, app_entry in item['apps'].items():
                    result.append(app_entry)

            if 'menus' in item:
                for menu_entry in item['menus']:
                    result.extend(get_appname(item['menus'][menu_entry]))
<commit_msg>chown group too<commit_after># This file is part of the GOsa framework.
#
#  http://gosa-project.org
#
# Copyright:
#  (C) 2016 GONICUS GmbH, Germany, http://www.gonicus.de
#
# See the LICENSE file in the project's top-level directory for details.

"""
.. _dbus-cups:

GOsa D-Bus Environment Plugin
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This plugin allows to configure a user environment (e.g. application menu).

"""
import glob
import logging
from subprocess import check_output

import dbus.service

from gosa.common import Environment
from gosa.common.components import Plugin
from gosa.common.components.jsonrpc_utils import Binary
from gosa.common.gjson import loads
from gosa.dbus import get_system_bus
import shutil
import xdg.DesktopEntry
import xdg.Menu
from bs4 import BeautifulSoup

from xml.dom.minidom import getDOMImplementation
import os

XDG_CONFIG_DIR = "/etc/xdg/xdg-gnome"
XDG_MENU_PREFIX = "gnome-"
GLOBAL_MENU = XDG_CONFIG_DIR + '/menus/' + XDG_MENU_PREFIX + 'applications.menu'


class DBusEnvironmentHandler(dbus.service.Object, Plugin):
    """
    This dbus plugin is able to configure a users environment.

    """
    add_other_menu = False
    home_dir = None
    local_menu = os.path.join(".config", "menus", XDG_MENU_PREFIX + 'applications.menu')
    local_applications = os.path.join(".local", "share", "applications")
    local_applications_scripts = os.path.join(".local", "share", "goto", "applications-scripts")
    local_icons = os.path.join(".local", "share", "icons")
    local_directories = os.path.join(".local", "share", "desktop-directories")
    __initialized_dirs = []

    def __init__(self):
        conn = get_system_bus()
        dbus.service.Object.__init__(self, conn, '/org/gosa/environment')
        self.env = Environment.getInstance()
        self.log = logging.getLogger(__name__)

    @dbus.service.method('org.gosa', in_signature='ss', out_signature='')
    def configureUserMenu(self, user, user_menu):
        """ configure a users application menu """
        user_menu = loads(user_menu)
        primary_group = None
        try:
            groups = check_output(['id', '-gn', user]).split()
            if len(groups) > 0:
                primary_group = groups[0].decode("utf-8")
        except:
            pass
        self.__initialized_dirs = []
        self.home_dir = os.path.expanduser('~%s' % user)
        self.init_directories(user_menu)
        self.init_applications(user_menu)
        self.init_menu(user_menu)
        if self.env.config.get("user.chown-menu", default="false") == "true":
            self.chown_dirs(user, primary_group)

    def chown_dirs(self, user, primary_group):
        self.log.debug("chown %s dirs to %s:%s" % (self.__initialized_dirs, user, primary_group))
        for dir in self.__initialized_dirs:
            self.__chown(dir, user=user, group=primary_group)

    def __chown(self, path, user, group=None):
        shutil.chown(path, user=user, group=group)
        for item in glob.glob(path+'/*'):
            cur_path = os.path.join(path, item)
            if os.path.isdir(item):
                self.__chown(cur_path, user, group=group)
            else:
                shutil.chown(cur_path, user=user, group=group)

    def init_dir(self, dir):
        if not os.path.exists(dir):
            os.makedirs(dir)
        shutil.rmtree(dir)
        os.makedirs(dir)

        self.__initialized_dirs.append(dir)

    def init_applications(self, user_menu):
        app_dir = os.path.join(self.home_dir, self.local_applications)
        self.init_dir(app_dir)
        scripts_dir = os.path.join(self.home_dir, self.local_applications_scripts)
        self.init_dir(scripts_dir)

        def get_appname(item):
            result = []

            if 'apps' in item:
                for cn, app_entry in item['apps'].items():
                    result.append(app_entry)

            if 'menus' in item:
                for menu_entry in item['menus']:
                    result.extend(get_appname(item['menus'][menu_entry]))

{'fraction_non_alphanumeric': 0.12675689744924518, 'fraction_numerical': 0.001691827173347215, 'mean_word_length': 2.9149261334691796, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2003805', 'n_tokens_mistral': 2505, 'n_tokens_neox': 2426, 'n_words': 531}