コード例 #1
0
ファイル: extension.py プロジェクト: tknomanzr/alan2
class Extension(OpenboxMenu):
    """ A base Extension object. Extension should subclass this. """

    extensionName = "Extension"
    structure = []

    def __init__(self, configuration=None, is_pipe=False, arguments=None):
        """ Initializes the object. """

        self.is_pipe = is_pipe

        if arguments:
            self.arguments = arguments.split(" ")
        else:
            self.arguments = None

        self.extensionId = self.__module__.replace("alan.extensions.", "")

        self.configuration = configuration
        self.settings = configuration.settings
        if "extension:%s" % self.extensionId in self.settings:
            self.extension_settings = self.settings["extension:%s" %
                                                    self.extensionId]
        else:
            self.extension_settings = {}

        if "structure" in self.extension_settings:
            self.structure = self.extension_settings["structure"].split(" ")

        if not "icons" in self.settings["alan"]:
            icons = False
        else:
            icons = self.settings["alan"]["icons"]

        self.IconPool = IconPool(icons)

        if is_pipe:
            # pipemenu, change things accordingly
            self.objectName = "openbox_pipe_menu"

        OpenboxMenu.__init__(self)

        if not is_pipe:
            if self.settings["alan"]["map_as_main"] == self.extensionId:
                self.menu = Menu("root-menu")
                self.menu.set("label", "Openbox 3")
            else:
                self.menu = Menu(self.extensionName)

                # Unfortunately in non-pipe mode Openbox gets the label from
                # the menu file and not from the id link in the main menu.
                # We need then to get the label.
                main = "extension:%s" % self.settings["alan"]["map_as_main"]
                if not main in self.configuration.sections():
                    label = "alan2"
                else:
                    self.configuration.populate_settings(main)

                    if not "%s_label" % self.extensionName in self.configuration.settings[
                            main]:
                        label = "alan2"
                    else:
                        label = self.configuration.settings[main][
                            "%s_label" % self.extensionName]

                self.menu.set("label", label)

            self.set("xmlns", "http://openbox.org/")
            self.set("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
            self.set("xsi:schemaLocation",
                     "http://openbox.org/ file:///usr/share/openbox/menu.xsd")

            self.append(self.menu)

    def generate(self):
        """ Override this method to actually do things. """

        pass

    def add(self, obj):
        """ Appends to the main menu the specified object. """

        if self.is_pipe:
            return self.append(obj)
        else:
            return self.menu.append(obj)

    def get_menu(self):
        """ Prints the resulting menu. """

        etree.ElementTree(self).write(sys.stdout)

    def write_menu(self, dest):
        """ Writes the menu in dest."""

        etree.ElementTree(self).write(dest,
                                      encoding='utf-8',
                                      xml_declaration=True)
コード例 #2
0
ファイル: extension.py プロジェクト: semplice/alan2
class Extension(OpenboxMenu):
	""" A base Extension object. Extension should subclass this. """
	
	extensionName = "Extension"
	structure = []
	
	def __init__(self, configuration=None, is_pipe=False, arguments=None):
		""" Initializes the object. """
		
		self.is_pipe = is_pipe
		
		if arguments:
			self.arguments = arguments.split(" ")
		else:
			self.arguments = None
		
		self.extensionId = self.__module__.replace("alan.extensions.", "")
		
		self.configuration = configuration
		self.settings = configuration.settings
		if "extension:%s" % self.extensionId in self.settings:
			self.extension_settings = self.settings["extension:%s" % self.extensionId]
		else:
			self.extension_settings = {}
		
		if "structure" in self.extension_settings:
			self.structure = self.extension_settings["structure"].split(" ")
		
		if not "icons" in self.settings["alan"]:
			icons = False
		else:
			icons = self.settings["alan"]["icons"]
		
		self.IconPool = IconPool(icons)
		
		if is_pipe:
			# pipemenu, change things accordingly
			self.objectName = "openbox_pipe_menu"

		OpenboxMenu.__init__(self)

		if not is_pipe:
			if self.settings["alan"]["map_as_main"] == self.extensionId:
				self.menu = Menu("root-menu")
				self.menu.set("label", "Openbox 3")
			else:
				self.menu = Menu(self.extensionName)
				
				# Unfortunately in non-pipe mode Openbox gets the label from
				# the menu file and not from the id link in the main menu.
				# We need then to get the label.
				main = "extension:%s" % self.settings["alan"]["map_as_main"]
				if not main in self.configuration.sections():
					label = "alan2"
				else:
					self.configuration.populate_settings(main)
					
					if not "%s_label" % self.extensionName in self.configuration.settings[main]:
						label = "alan2"
					else:
						label = self.configuration.settings[main]["%s_label" % self.extensionName]
				
				self.menu.set("label", label)
				
			

			self.set("xmlns", "http://openbox.org/")
			self.set("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
			self.set("xsi:schemaLocation", "http://openbox.org/ file:///usr/share/openbox/menu.xsd")
			
			self.append(self.menu)
	
	def generate(self):
		""" Override this method to actually do things. """
		
		pass
	
	def add(self, obj):
		""" Appends to the main menu the specified object. """
		
		if self.is_pipe:
			return self.append(obj)
		else:
			return self.menu.append(obj)
	
	def get_menu(self):
		""" Prints the resulting menu. """
		
		etree.ElementTree(self).write(sys.stdout)
	
	def write_menu(self, dest):
		""" Writes the menu in dest."""
		
		etree.ElementTree(self).write(dest, encoding='utf-8', xml_declaration=True)