<commit_before>""" CLass to edit XDG Menus """

from xdg.Menu import *
from xdg.BaseDirectory import *
from xdg.Exceptions import *
from xdg.DesktopEntry import *

import xml.dom.minidom
import os

# XML-Cleanups: Move / Exclude
# FIXME: pass AppDirs/DirectoryDirs around in the edit/move functions
# FIXME: copy functions
# FIXME: More Layout stuff
# FIXME: unod/redo function / remove menu...
# FIXME: Advanced MenuEditing Stuff: LegacyDir/MergeFile
#        Complex Rules/Deleted/OnlyAllocated/AppDirs/DirectoryDirs

class MenuEditor:
	def __init__(self, menu=None, filename=None):
		self.menu = None
		self.filename = None
		self.doc = None
		self.parse(menu, filename)

		# fix for creating two menus with the same name on the fly
		self.filenames = []

	def parse(self, menu=None, filename=None):
		if isinstance(menu, Menu):
			self.menu = menu
		elif isinstance(menu, unicode):
			self.menu = parse(menu)
		else:
			self.menu = parse()

		if filename:
			self.filename = filename
		elif os.access(self.menu.Filename, os.W_OK):
			self.filename = self.menu.Filename
		else:
			self.filename = os.path.join(xdg_config_dirs[0], "menus", "applications.menu")

		try:
			self.doc = xml.dom.minidom.parse(self.filename)
		except IOError:
			self.doc = xml.dom.minidom.parseString('<!DOCTYPE Menu PUBLIC "-//freedesktop//DTD Menu 1.0//EN" "http://standards.freedesktop.org/menu-spec/menu-1.0.dtd"><Menu><Name>Applications</Name><MergeFile type="parent">' + xdg_config_dirs[1] + '/menus/applications.menu</MergeFile></Menu>')
		except xml.parsers.expat.ExpatError:
			raise ParsingError('Not a valid .menu file', self.filename)

	def save(self):
		self.__saveEntries(self.menu)
		self.__saveMenu()

	def createEntry(self, parent, name, command=None, genericname=None, comment=None, icon=None, terminal=None, after=None, before=None):
		entry = MenuEntry(self.__getFileName(name, ".desktop"))
		entry = self.editEntry(entry, name, genericname, comment, command, icon, terminal)

		self.__addEntry(parent, entry, after, before)

		sort(self.menu)

		return entry

	def createMenu(self, parent, name, genericname=None, comment=None, icon=None, after=None, before=None):
		menu = Menu()
		menu = self.editMenu(menu, name, genericname, comment, icon)

		self.__addEntry(parent, menu, after, before)

		menu.Name = menu.Directory.Filename.replace(".directory", "")
		menu.Layout = parent.DefaultLayout
		menu.DefaultLayout = parent.DefaultLayout

		xml_menu = self.__getXmlMenu(menu.getPath(True, True))
		self.__addXmlTextElement(xml_menu, 'Directory', menu.Directory.Filename)

		sort(self.menu)

		return menu

	def createSeparator(self, parent, after=None, before=None):
		separator = Separator(parent)

		self.__addEntry(parent, separator, after, before)

		sort(self.menu)

		return separator

	def moveEntry(self, entry, oldparent, newparent, after=None, before=None):
		self.__deleteEntry(oldparent, entry, after, before)
		self.__addEntry(newparent, entry, after, before)

		sort(self.menu)

		return entry

	def moveMenu(self, menu, oldparent, newparent, after=None, before=None):
		self.__deleteEntry(oldparent, menu, after, before)
		self.__addEntry(newparent, menu, after, before)

		root_menu = self.__getXmlMenu(self.menu.Name)
		if oldparent.getPath(True) != newparent.getPath(True):
			self.__addXmlMove(root_menu, os.path.join(oldparent.getPath(True), menu.Name), os.path.join(newparent.getPath(True), menu.Name))

		sort(self.menu)

		return menu

	def moveSeparator(self, separator, parent, after=None, before=None):
		self.__deleteEntry(parent, separator, after, before)
		self.__addEntry(parent, separator, after, before)

		sort(self.menu)

		return separator

	def editEntry(self, entry, name=None, genericname=None, comment=None, command=None, icon=None, terminal=None, nodisplay=None):
		deskentry = entry.DesktopEntry

		if name:
			if not deskentry.hasKey("Name"):
				deskentry.set("Name", name)
			deskentry.set("Name", name, locale = True)
		if comment:
			if not deskentry.hasKey("Comment"):
				deskentry.set("Comment", comment)
			deskentry.set("Comment", comment, locale = True)
		if genericname:
			if not deskentry.hasKey("GnericNe"):
				deskentry.set("GenericName", genericname)
			deskentry.set("GenericName", genericname, locale = True)
		if command:
			deskentry.set("Exec", command)
		if icon:
			deskentry.set("Icon", icon)

		if terminal == True:
			deskentry.set("Terminal", "true")
		elif terminal == False:
			deskentry.set("Terminal", "false")

		if nodisplay == True:
			deskentry.set("NoDisplay", "true")
		elif nodisplay == False:
			deskentry.set("NoDisplay", "false")

		if entry.Type == "System":
			entry.Type = "Both"
			entry.Original = MenuEntry(entry.DesktopEntry.filename, entry.Prefix)

		if len(entry.Parents) > 0:
			sort(self.menu)

		return entry

	def editMenu(self, menu, name=None, genericname=None, comment=None, icon=None, nodisplay=None):
		# Hack for legacy dirs
		if isinstance(menu.Directory, MenuEntry) and menu.Directory.Filename == ".directory":
			xml_menu = self.__getXmlMenu(menu.getPath(True, True))
			self.__addXmlTextElement(xml_menu, 'Directory', menu.Name + ".directory")
			menu.Directory.setAttributes(menu.Name + ".directory")
		# Hack for New Entries
		elif not isinstance(menu.Directory, MenuEntry):
			menu.Directory = MenuEntry(self.__getFileName(name, ".directory").replace("/", ""))

		deskentry = menu.Directory.DesktopEntry

		if name:
			if not deskentry.hasKey("Name"):
				deskentry.set("Name", name)
			deskentry.set("Name", name, locale = True)
		if genericname:
			if not deskentry.hasKey("GenericName"):
				deskentry.set("GenericName", genericname)
			deskentry.set("GenericName", genericname, locale = True)
		if comment:
			if not deskentry.hasKey("Comment"):
				deskentry.set("Comment", comment)
			deskentry.set("Comment", comment, locale = True)
		if icon:
			deskentry.set("Icon", icon)

		if nodisplay == True:
			deskentry.set("NoDisplay", "true")
		elif nodisplay == False:
			deskentry.set("NoDisplay", "false")

		if entry.Type == "System":
			entry.Type = "Both"
			entry.Original = MenuEntry(entry.DesktopEntry.filename, entry.Prefix)

		if isinstance(entry.Parent, Menu):
			sort(self.menu)

		return menu

	def deleteEntry(self, entry):
		if "delete" in self.getActions(entry):
			self.__deleteFile(entry.DesktopEntry.filename)
			for parent in entry.Parents:
				self.__deleteEntry(parent, entry)
			sort(self.menu)
		return entry

	def revertEntry(self, entry):
		if "revert" in self.getActions(entry):
			self.__deleteFile(entry.DesktopEntry.filename)
			for parent in entry.Parents:
				index = parent.Entries.index(entry)
				parent.Entries[index] = entry.Original
				index = parent.DeskEntries.index(entry)
				parent.DeskEntries[index] = entry.Original
			sort(self.menu)
		return entry

	def deleteMenu(self, menu):
		if "delete" in self.getActions(menu):
			self.__deleteFile(menu.Directory.DesktopEntry.filename)
			self.__deleteEntry(menu.Parent, menu)
			xml_menu = self.__getXmlMenu(menu.getPath(True, True))
			xml_menu.parentNode.removeChild(xml_menu)<commit_msg>small fix<commit_after>""" CLass to edit XDG Menus """

from xdg.Menu import *
from xdg.BaseDirectory import *
from xdg.Exceptions import *
from xdg.DesktopEntry import *

import xml.dom.minidom
import os

# XML-Cleanups: Move / Exclude
# FIXME: pass AppDirs/DirectoryDirs around in the edit/move functions
# FIXME: copy functions
# FIXME: More Layout stuff
# FIXME: unod/redo function / remove menu...
# FIXME: Advanced MenuEditing Stuff: LegacyDir/MergeFile
#        Complex Rules/Deleted/OnlyAllocated/AppDirs/DirectoryDirs

class MenuEditor:
	def __init__(self, menu=None, filename=None):
		self.menu = None
		self.filename = None
		self.doc = None
		self.parse(menu, filename)

		# fix for creating two menus with the same name on the fly
		self.filenames = []

	def parse(self, menu=None, filename=None):
		if isinstance(menu, Menu):
			self.menu = menu
		elif isinstance(menu, unicode):
			self.menu = parse(menu)
		else:
			self.menu = parse()

		if filename:
			self.filename = filename
		elif os.access(self.menu.Filename, os.W_OK):
			self.filename = self.menu.Filename
		else:
			self.filename = os.path.join(xdg_config_dirs[0], "menus", "applications.menu")

		try:
			self.doc = xml.dom.minidom.parse(self.filename)
		except IOError:
			self.doc = xml.dom.minidom.parseString('<!DOCTYPE Menu PUBLIC "-//freedesktop//DTD Menu 1.0//EN" "http://standards.freedesktop.org/menu-spec/menu-1.0.dtd"><Menu><Name>Applications</Name><MergeFile type="parent">' + xdg_config_dirs[1] + '/menus/applications.menu</MergeFile></Menu>')
		except xml.parsers.expat.ExpatError:
			raise ParsingError('Not a valid .menu file', self.filename)

	def save(self):
		self.__saveEntries(self.menu)
		self.__saveMenu()

	def createEntry(self, parent, name, command=None, genericname=None, comment=None, icon=None, terminal=None, after=None, before=None):
		entry = MenuEntry(self.__getFileName(name, ".desktop"))
		entry = self.editEntry(entry, name, genericname, comment, command, icon, terminal)

		self.__addEntry(parent, entry, after, before)

		sort(self.menu)

		return entry

	def createMenu(self, parent, name, genericname=None, comment=None, icon=None, after=None, before=None):
		menu = Menu()
		menu = self.editMenu(menu, name, genericname, comment, icon)

		self.__addEntry(parent, menu, after, before)

		menu.Name = menu.Directory.Filename.replace(".directory", "")
		menu.Layout = parent.DefaultLayout
		menu.DefaultLayout = parent.DefaultLayout

		xml_menu = self.__getXmlMenu(menu.getPath(True, True))
		self.__addXmlTextElement(xml_menu, 'Directory', menu.Directory.Filename)

		sort(self.menu)

		return menu

	def createSeparator(self, parent, after=None, before=None):
		separator = Separator(parent)

		self.__addEntry(parent, separator, after, before)

		sort(self.menu)

		return separator

	def moveEntry(self, entry, oldparent, newparent, after=None, before=None):
		self.__deleteEntry(oldparent, entry, after, before)
		self.__addEntry(newparent, entry, after, before)

		sort(self.menu)

		return entry

	def moveMenu(self, menu, oldparent, newparent, after=None, before=None):
		self.__deleteEntry(oldparent, menu, after, before)
		self.__addEntry(newparent, menu, after, before)

		root_menu = self.__getXmlMenu(self.menu.Name)
		if oldparent.getPath(True) != newparent.getPath(True):
			self.__addXmlMove(root_menu, os.path.join(oldparent.getPath(True), menu.Name), os.path.join(newparent.getPath(True), menu.Name))

		sort(self.menu)

		return menu

	def moveSeparator(self, separator, parent, after=None, before=None):
		self.__deleteEntry(parent, separator, after, before)
		self.__addEntry(parent, separator, after, before)

		sort(self.menu)

		return separator

	def editEntry(self, entry, name=None, genericname=None, comment=None, command=None, icon=None, terminal=None, nodisplay=None):
		deskentry = entry.DesktopEntry

		if name:
			if not deskentry.hasKey("Name"):
				deskentry.set("Name", name)
			deskentry.set("Name", name, locale = True)
		if comment:
			if not deskentry.hasKey("Comment"):
				deskentry.set("Comment", comment)
			deskentry.set("Comment", comment, locale = True)
		if genericname:
			if not deskentry.hasKey("GnericNe"):
				deskentry.set("GenericName", genericname)
			deskentry.set("GenericName", genericname, locale = True)
		if command:
			deskentry.set("Exec", command)
		if icon:
			deskentry.set("Icon", icon)

		if terminal == True:
			deskentry.set("Terminal", "true")
		elif terminal == False:
			deskentry.set("Terminal", "false")

		if nodisplay == True:
			deskentry.set("NoDisplay", "true")
		elif nodisplay == False:
			deskentry.set("NoDisplay", "false")

		if entry.Type == "System":
			entry.Type = "Both"
			entry.Original = MenuEntry(entry.DesktopEntry.filename, entry.Prefix)

		if len(entry.Parents) > 0:
			sort(self.menu)

		return entry

	def editMenu(self, menu, name=None, genericname=None, comment=None, icon=None, nodisplay=None):
		# Hack for legacy dirs
		if isinstance(menu.Directory, MenuEntry) and menu.Directory.Filename == ".directory":
			xml_menu = self.__getXmlMenu(menu.getPath(True, True))
			self.__addXmlTextElement(xml_menu, 'Directory', menu.Name + ".directory")
			menu.Directory.setAttributes(menu.Name + ".directory")
		# Hack for New Entries
		elif not isinstance(menu.Directory, MenuEntry):
			menu.Directory = MenuEntry(self.__getFileName(name, ".directory").replace("/", ""))

		deskentry = menu.Directory.DesktopEntry

		if name:
			if not deskentry.hasKey("Name"):
				deskentry.set("Name", name)
			deskentry.set("Name", name, locale = True)
		if genericname:
			if not deskentry.hasKey("GenericName"):
				deskentry.set("GenericName", genericname)
			deskentry.set("GenericName", genericname, locale = True)
		if comment:
			if not deskentry.hasKey("Comment"):
				deskentry.set("Comment", comment)
			deskentry.set("Comment", comment, locale = True)
		if icon:
			deskentry.set("Icon", icon)

		if nodisplay == True:
			deskentry.set("NoDisplay", "true")
		elif nodisplay == False:
			deskentry.set("NoDisplay", "false")

		if menu.Directory.Type == "System":
			menu.Directory.Type = "Both"
			menu.Directory.Original = MenuEntry(entry.DesktopEntry.filename, entry.Prefix)

		if isinstance(entry.Parent, Menu):
			sort(self.menu)

		return menu

	def deleteEntry(self, entry):
		if "delete" in self.getActions(entry):
			self.__deleteFile(entry.DesktopEntry.filename)
			for parent in entry.Parents:
				self.__deleteEntry(parent, entry)
			sort(self.menu)
		return entry

	def revertEntry(self, entry):
		if "revert" in self.getActions(entry):
			self.__deleteFile(entry.DesktopEntry.filename)
			for parent in entry.Parents:
				index = parent.Entries.index(entry)
				parent.Entries[index] = entry.Original
				index = parent.DeskEntries.index(entry)
				parent.DeskEntries[index] = entry.Original
			sort(self.menu)
		return entry

	def deleteMenu(self, menu):
		if "delete" in self.getActions(menu):
			self.__deleteFile(menu.Directory.DesktopEntry.filename)
			self.__deleteEntry(menu.Parent, menu)
			xml_menu = self.__getXmlMenu(menu.getPath(True, True))
			xml_menu.parentNode.removeChild(xml_menu)
{'fraction_non_alphanumeric': 0.1322029128262858, 'fraction_numerical': 0.0009850137198339549, 'mean_word_length': 4.65168986083499, 'pattern_counts': {'":': 6, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 22}, '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': '6769275', 'n_tokens_mistral': 5174, 'n_tokens_neox': 4596, 'n_words': 1046}