Ejemplo n.º 1
0
    def gsettings_get(self):
        """ Get value from gsettings. """
        value = self.default
        try:
            # Bug in Gio, gir1.2-glib-2.0, Oneiric
            # Onboard is accumulating open file handles
            # at "/home/<user>/.config/dconf/<user>' when
            # reading from gsettings before writing.
            # Check with:
            # lsof -w -p $( pgrep gio-test ) -Fn |sort|uniq -c|sort -n|tail
            #value = self.settings[self.key]

            if self.enum:
                value = self.settings.get_enum(self.key)
            elif self.type_string:
                value = self.settings.get_value(self.key).unpack()
            else:
                _type = type(self.default)
                if _type == str:
                    value = self.settings.get_string(self.key)
                elif _type == int:
                    value = self.settings.get_int(self.key)
                elif _type == float:
                    value = self.settings.get_double(self.key)
                else:
                    value = self.settings[self.key]

        except KeyError as ex:
            _logger.error(_("Failed to get gsettings value. ") + \
                          unicode_str(ex))

        return value
Ejemplo n.º 2
0
    def load_system_defaults(self, paths):
        """
        System default settings can be optionally provided for distribution
        specific customization or branding.
        They are stored in simple ini-style files, residing in a small choice
        of directories. The last setting found in the list of paths wins.
        """
        _logger.info(_format("Looking for system defaults in {paths}",
                             paths=paths))

        filename = None
        parser = configparser.SafeConfigParser()
        try:
            if sys.version_info.major == 2:
                filename = parser.read(paths)
            else:
                filename = parser.read(paths, "UTF-8")
        except configparser.ParsingError as ex:
            _logger.error(_("Failed to read system defaults. " + \
                            unicode_str(ex)))

        if not filename:
            _logger.info(_("No system defaults found."))
        else:
            _logger.info(_format("Loading system defaults from {filename}",
                                 filename=filename))
            self._read_sysdef_section(parser)
Ejemplo n.º 3
0
    def _list_to_dict(_list, key_type = str, num_values = 2):
        """ Get dictionary from a gsettings list key """
        if sys.version_info.major == 2:
            _list = [unicode_str(x) for x in _list]  # translate to unicode

        return unpack_name_value_list(_list, key_type=key_type,
                                             num_values = num_values)
Ejemplo n.º 4
0
    def migrate_dconf_value(self, dconf_key, gskey):
        """ Copy the value of dconf_key into the given gskey """
        if not self._osk_dconf:
            self._osk_dconf = osk.DConf()
        try:
            value = self._osk_dconf.read_key(dconf_key)
        except (ValueError, TypeError) as e:
            value = None
            _logger.warning("migrate_dconf_value: {}".format(e))

        if not value is None:
            # Enums are stored as strings in dconf, convert them to int.
            if gskey.enum:
                value = gskey.enum.get(value, 0)

            # Optionally convert from gsettings value to property value.
            hook = _UNPACK_HOOK + gskey.prop
            if hasattr(self, hook):
                v = value
                value = getattr(self, hook)(value)

            _logger.info("migrate_dconf_value: {key} -> {path} {gskey}, value={value}" \
                          .format(key=dconf_key,
                                  path=self.schema,
                                  gskey=gskey.key, value=value))

            try:
                setattr(self, gskey.prop, value)
            except Exception as ex:
                _logger.error("Migrating dconf key failed: '{key}={value}'; "
                              "possibly due to incompatible dconf type; "
                              "skipping this key. "
                              "Exception: {exception}" \
                              .format(key=dconf_key, value=value,
                                      exception=unicode_str(ex)))
Ejemplo n.º 5
0
 def add_optional_child(self, type):
     """ Add child ConfigObject or None if it's schema doesn't exist. """
     try:
         co = type(self)
         self.children.append(co)
     except SchemaError as e:
         _logger.warning(unicode_str(e))
         co = None
     return co
Ejemplo n.º 6
0
    def load(filename, is_system=False):
        """ Load a color scheme and return it as a new object. """

        color_scheme = None

        f = open_utf8(filename)
        try:
            dom = minidom.parse(f).documentElement
            name = dom.attributes["name"].value

            # check layout format
            format = ColorScheme.COLOR_SCHEME_FORMAT_LEGACY
            if dom.hasAttribute("format"):
               format = Version.from_string(dom.attributes["format"].value)

            if format >= ColorScheme.COLOR_SCHEME_FORMAT_TREE:   # tree format?
                items = ColorScheme._parse_dom_node(dom, None, {})
            else:
                _logger.warning(_format( \
                    "Loading legacy color scheme format '{old_format}', "
                    "please consider upgrading to current format "
                    "'{new_format}': '{filename}'",
                    old_format = format,
                    new_format = ColorScheme.COLOR_SCHEME_FORMAT,
                    filename = filename))

                items = ColorScheme._parse_legacy_color_scheme(dom)

            if  not items is None:
                root = Root()
                root.set_items(items)

                color_scheme = ColorScheme()
                color_scheme.name = name
                color_scheme.filename = filename
                color_scheme.is_system = is_system
                color_scheme.root = root
                #print(root.dumps())
        except xml.parsers.expat.ExpatError as ex:
            _logger.error(_format("Error loading color scheme '{filename}'. "
                                  "{exception}: {cause}",
                                  filename = filename,
                                  exception = type(ex).__name__,
                                  cause = unicode_str(ex)))
        finally:
            f.close()

        return color_scheme
Ejemplo n.º 7
0
    def load(filename, is_system=False):
        """ Load a theme and return a new theme object. """

        result = None

        _file = open_utf8(filename)
        try:
            domdoc = minidom.parse(_file).documentElement
            try:
                theme = Theme()

                node = domdoc.attributes.get("format")
                format = Version.from_string(node.value) \
                         if node else Theme.THEME_FORMAT_INITIAL

                theme.name = domdoc.attributes["name"].value

                # "color_scheme" is the base file name of the color scheme
                text = utils.xml_get_text(domdoc, "color_scheme")
                if not text is None:
                    theme.color_scheme_basename = text

                # get key label overrides
                nodes = domdoc.getElementsByTagName("key_label_overrides")
                if nodes:
                    overrides = nodes[0]
                    tuples = {}
                    for override in overrides.getElementsByTagName("key"):
                        key_id = override.attributes["id"].value
                        node = override.attributes.get("label")
                        label = node.value if node else ""
                        node = override.attributes.get("group")
                        group = node.value if node else ""
                        tuples[key_id] = (label, group)
                    theme.key_label_overrides = tuples

                # read all other members
                for name, _type, _default in Theme.attributes:
                    if not name in ["color_scheme_basename",
                                    "key_label_overrides"]:
                        value = utils.xml_get_text(domdoc, name)
                        if not value is None:

                            if _type == "i":
                                value = int(value)
                            if _type == "d":
                                value = float(value)
                            if _type == "ad":
                                value = [float(s) for s in value.split(",")]

                            # upgrade to current file format
                            if format < Theme.THEME_FORMAT_1_1:
                                # direction was    0..360, ccw
                                #        is now -180..180, cw
                                if name == "key_gradient_direction":
                                    value = -(value % 360)
                                    if value <= -180:
                                        value += 360

                            setattr(theme, name, value)

                theme.filename = filename
                theme.is_system = is_system
                theme.system_exists = is_system
                result = theme
            finally:
                domdoc.unlink()

        except (Exceptions.ThemeFileError,
                xml.parsers.expat.ExpatError) as ex:
            _logger.error(_format("Error loading theme '{filename}'. "
                                  "{exception}: {cause}",
                                  filename = filename,
                                  exception = type(ex).__name__,
                                  cause = unicode_str(ex)))
            result = None
        finally:
            _file.close()

        return result
Ejemplo n.º 8
0
 def __str__(self):
     message = unicode_str(self._message) + "\n"
     if self.chained_exception:
         message += "%s: %s" % (type(self.chained_exception).__name__,
             unicode_str(self.chained_exception))
     return message