Exemple #1
0
    def set_prop_from_string(self, key, value):

        value = str(value)  # to stay compatible with sensors :(
        key = key.replace("_", "-")
        try:
            datatype = self.get_datatype_of_property(key)
        except KeyError:
            datatype = TYPE_ANY

        from utils import typeconverter
        self.set_prop(key, typeconverter.str2type(datatype, value))
    def endElement(self, name):

        oname, attrs = self.__nesting_stack.pop()
        state = self.__state[-1]
        if (not name == oname):
            # nesting errors in XML are detected by the SAX parser; if we
            # get here, it means our parser is buggy, not the XML input
            log("Nesting error: expected %s, got %s." % (oname, name))
            return


        elif (state == _STATE_DISPLAY):
            settings = self.__create_settings(attrs)
            # if there is no ID given, guess a unique one
            ident = attrs.get("id", Display.make_id())
            settings["id"] = ident

            # build the tree of children from bottom up
            children = self.__children_stack.pop()
            self.__children_stack[-1].append((name, settings, children))

        elif (state == _STATE_META):
            self.__metadata = attrs

        elif (state == _STATE_SCRIPT):
            # Some distros ship a broken SAX parser and
            # their users are too lazy to reportbug. (doesn't have __contains__)
            # So we have to use has_key()
            # if ((name == "script") and ("uri" in attrs)):
            if (name == "script" and attrs.has_key("uri")):
                path = attrs["uri"]
                filename = os.path.join(self.__path, path)
                try:
                    self.__script += "\n" + vfs.read_entire_file(filename)
                except:
                    log("File doesn't exist or couldn't be loaded: %s"
                        % (path,))
                    dialog.warning(_("File doesn't exist or couldn't be "
                                     "loaded"),
                                   _("A file which contains a script isn't "
                                     "available for further execution."))
            else:
                filename = self.__filename

            # collect script by script to be able to execute them one by one
            # later; we would get problems with different indentation otherwise
            self.__scripts.append((self.__script, filename))

        # deprecated sensor stuff
        elif (state == _STATE_SENSOR):
            from utils import typeconverter
            ident = attrs["id"]
            moduledata = typeconverter.str2type(TYPE_LIST, attrs["module"])
            module = moduledata[0]
            args = moduledata[1:]
            sensorctrl = self.__control_factory.get_control(_SENSOR_CONTROL)

            if (sensorctrl):
                sensorctrl.sensor = (module, args)
                sensorctrl.config_id = self.__id + ident
                self.__sensors.append((ident, sensorctrl))
            else:
                raise RuntimeError(_("Could not load sensor"))

        elif (state == _STATE_CONTROL):
            ident = attrs["id"]
            interface = attrs["interface"]
            
            if not attrs.has_key("length"):
                attrs["length"] = "0"
            length = int(attrs["length"])

            script = "%s = get_control('%s', %d)" % \
                     (ident, interface, length)
            self.__scripts.append((script, self.__filename))

        elif (state == _STATE_PREFS):
            if (name == "prefs"):
                pass
            else:
                if (name in ("boolean", "color", "date", "enum", "float", "font",
                             "integer", "list", "radio", "string", "toggle", "uri")
                    and not attrs.has_key("bind")):
                    dialog.warning(_("<%s> needs a bind attribute") % \
                                                                      (name,),
                                   _("The <prefs> section of this "
                                     "desklet file is broken."))
                    raise AttributeError("<%s> needs a bind attribute" % \
                                                                      (name,))

                # use global prefs callback if no callback was given
                if (self.__prefs_callback and not attrs.has_key("callback")):
                    attrs["callback"] = self.__prefs_callback

                self.__config_items.append((name, attrs))


        if (name in ("display", "sensor", "control", "meta",
                     "prefs", "script")):
            self.__state.pop()