Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
else:
self.filename = os.path.join(xdg_config_dirs[0], "menus", os.path.split(self.menu.Filename)[1])
try:
self.tree = etree.parse(self.filename)
except IOError:
root = etree.fromtring("""
<menu>
Applications
%s
</menu>
""" % self.menu.Filename)
self.tree = etree.ElementTree(root)
except ParsingError:
raise ParsingError('Not a valid .menu file', self.filename)
#FIXME: is this needed with etree ?
self.__remove_whitespace_nodes(self.tree)
content[currentGroup] = {}
# key
else:
try:
key, value = line.split("=", 1)
except ValueError:
raise ParsingError("Invalid line: " + line, filename)
key = key.strip() # Spaces before/after '=' should be ignored
try:
if debug and self.hasKey(key, currentGroup):
raise DuplicateKeyError(key, currentGroup, filename)
else:
content[currentGroup][key] = value.strip()
except (IndexError, UnboundLocalError):
raise ParsingError("Parsing error on key, group missing", filename)
fd.close()
self.filename = filename
self.tainted = False
# check header
if headers:
for header in headers:
if header in content:
self.defaultGroup = header
break
else:
raise ParsingError("[%s]-Header missing" % headers[0], filename)
def _parse_apps(self):
already_added = []
database = []
# theme = Gio.Settings.new("org.gnome.desktop.interface").get_string("icon-theme")
for desktop_dir in DESKTOP_FILE_DIRS:
files = glob("{}*.desktop".format(desktop_dir))
for desktop_file in files:
try:
obj = DesktopFile(desktop_file)
icon_name = obj.getIcon()
self.emit("parse", obj.getName())
if icon_name not in already_added:
database.append(obj)
already_added.append(icon_name)
except xdg.Exceptions.ParsingError:
pass
database = sorted(database, key=lambda x: x.getName().lower())
self._db = database
self.emit("loaded")
content[currentGroup] = {}
# key
else:
try:
key, value = line.split("=", 1)
except ValueError:
raise ParsingError("Invalid line: " + line, filename)
key = key.strip() # Spaces before/after '=' should be ignored
try:
if debug and self.hasKey(key, currentGroup):
raise DuplicateKeyError(key, currentGroup, filename)
else:
content[currentGroup][key] = value.strip()
except (IndexError, UnboundLocalError):
raise ParsingError("Parsing error on key, group missing", filename)
self.filename = filename
self.tainted = False
# check header
if headers:
for header in headers:
if header in content:
self.defaultGroup = header
break
else:
raise ParsingError("[%s]-Header missing" % headers[0], filename)
def new(self, filename):
"""Make this instance into a new, blank desktop entry.
If filename has a .desktop extension, Type is set to Application. If it
has a .directory extension, Type is Directory. Other extensions will
cause :class:`~xdg.Exceptions.ParsingError` to be raised.
"""
if os.path.splitext(filename)[1] == ".desktop":
type = "Application"
elif os.path.splitext(filename)[1] == ".directory":
type = "Directory"
else:
raise ParsingError("Unknown extension", filename)
self.content = dict()
self.addGroup(self.defaultGroup)
self.set("Type", type)
self.filename = filename
# end desktop entry edit stuff
if filename and not os.path.isabs(filename):
filename = _get_menu_file_path(filename)
# use default if no filename given
if not filename:
candidate = os.environ.get('XDG_MENU_PREFIX', '') + "applications.menu"
filename = _get_menu_file_path(candidate)
if not filename:
raise ParsingError('File not found', "/etc/xdg/menus/%s" % candidate)
# check if it is a .menu file
if not filename.endswith(".menu"):
raise ParsingError('Not a .menu file', filename)
# create xml parser
try:
tree = etree.parse(filename)
except:
raise ParsingError('Not a valid .menu file', filename)
# parse menufile
self._merged_files = set()
self._directory_dirs = set()
self.cache = MenuEntryCache()
menu = self.parse_menu(tree.getroot(), filename)
menu.tree = tree
menu.filename = filename
self.handle_moves(menu)
self.post_parse(menu)
# generate the menu
self.generate_not_only_allocated(menu)
self.generate_only_allocated(menu)
def open_file(self, desktop_file):
"""
Open the specified desktop file.
"""
# TODO make sure this desktop file is selected in the list
try:
self._entry = Entry(desktop_file)
except ParsingError, e:
self.error_dialog(e)
return
self._load_desktop_entry_ui()
# validate in save
"""
try:
entry.validate()
except ValidationError, e:
self.error_dialog(e)
return
"""
def parse(self, filename=None):
"""Parse a list of recently used files.
filename defaults to ``~/.recently-used``.
"""
if not filename:
filename = os.path.join(os.getenv("HOME"), ".recently-used")
try:
doc = xml.dom.minidom.parse(filename)
except IOError:
raise ParsingError('File not found', filename)
except xml.parsers.expat.ExpatError:
raise ParsingError('Not a valid .menu file', filename)
self.filename = filename
for child in doc.childNodes:
if child.nodeType == xml.dom.Node.ELEMENT_NODE:
if child.tagName == "RecentFiles":
for recent in child.childNodes:
if recent.nodeType == xml.dom.Node.ELEMENT_NODE:
if recent.tagName == "RecentItem":
self.__parseRecentItem(recent)
self.sort()
if self.debug:
raise ParsingError('Infinite MergeFile loop detected', filename)
else:
return
self._merged_files.add(filename)
# load file
try:
tree = etree.parse(filename)
except IOError:
if self.debug:
raise ParsingError('File not found', filename)
else:
return
except:
if self.debug:
raise ParsingError('Not a valid .menu file', filename)
else:
return
root = tree.getroot()
self.parse_node(root, filename, parent)
def write(self, filename=None, trusted=False):
if not filename and not self.filename:
raise ParsingError("File not found", "")
if filename:
self.filename = filename
else:
filename = self.filename
if os.path.dirname(filename) and not os.path.isdir(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename))
with io.open(filename, 'w', encoding='utf-8') as fp:
# An executable bit signifies that the desktop file is
# trusted, but then the file can be executed. Add hashbang to
# make sure that the file is opened by something that
# understands desktop files.
if trusted: