From 5073ec0c6f2c56f9f1f5032a0912571cd4a4273f Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Mon, 28 Nov 2022 06:15:51 -0500 Subject: [PATCH] Update vendored pyxdg to 0.28 --- libs/common/xdg/BaseDirectory.py | 14 ++++++++ libs/common/xdg/IniFile.py | 59 ++++++++++++++++---------------- libs/common/xdg/Menu.py | 36 +++++++++++++------ libs/common/xdg/MenuEditor.py | 2 +- libs/common/xdg/Mime.py | 9 +++-- libs/common/xdg/RecentFiles.py | 39 ++++++++++----------- libs/common/xdg/__init__.py | 2 +- 7 files changed, 95 insertions(+), 66 deletions(-) diff --git a/libs/common/xdg/BaseDirectory.py b/libs/common/xdg/BaseDirectory.py index a7c31b1b..3c3afe89 100644 --- a/libs/common/xdg/BaseDirectory.py +++ b/libs/common/xdg/BaseDirectory.py @@ -43,6 +43,9 @@ xdg_config_dirs = [xdg_config_home] + \ xdg_cache_home = os.environ.get('XDG_CACHE_HOME') or \ os.path.join(_home, '.cache') +xdg_state_home = os.environ.get('XDG_STATE_HOME') or \ + os.path.join(_home, '.local', 'state') + xdg_data_dirs = [x for x in xdg_data_dirs if x] xdg_config_dirs = [x for x in xdg_config_dirs if x] @@ -81,6 +84,17 @@ def save_cache_path(*resource): os.makedirs(path) return path +def save_state_path(*resource): + """Ensure ``$XDG_STATE_HOME//`` exists, and return its path. + 'resource' should normally be the name of your application or a shared + resource.""" + resource = os.path.join(*resource) + assert not resource.startswith('/') + path = os.path.join(xdg_state_home, resource) + if not os.path.isdir(path): + os.makedirs(path) + return path + def load_config_paths(*resource): """Returns an iterator which gives each directory named 'resource' in the configuration search path. Information provided by earlier directories should diff --git a/libs/common/xdg/IniFile.py b/libs/common/xdg/IniFile.py index 718589f9..84be6142 100644 --- a/libs/common/xdg/IniFile.py +++ b/libs/common/xdg/IniFile.py @@ -56,38 +56,37 @@ class IniFile: return # parse file - for line in fd: - line = line.strip() - # empty line - if not line: - continue - # comment - elif line[0] == '#': - continue - # new group - elif line[0] == '[': - currentGroup = line.lstrip("[").rstrip("]") - if debug and self.hasGroup(currentGroup): - raise DuplicateGroupError(currentGroup, filename) - else: - 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) + with fd: + for line in fd: + line = line.strip() + # empty line + if not line: + continue + # comment + elif line[0] == '#': + continue + # new group + elif line[0] == '[': + currentGroup = line.lstrip("[").rstrip("]") + if debug and self.hasGroup(currentGroup): + raise DuplicateGroupError(currentGroup, filename) else: - content[currentGroup][key] = value.strip() - except (IndexError, UnboundLocalError): - raise ParsingError("Parsing error on key, group missing", filename) + content[currentGroup] = {} + # key + else: + try: + key, value = line.split("=", 1) + except ValueError: + raise ParsingError("Invalid line: " + line, filename) - fd.close() + 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 diff --git a/libs/common/xdg/Menu.py b/libs/common/xdg/Menu.py index 1d03cad5..1dd2af59 100644 --- a/libs/common/xdg/Menu.py +++ b/libs/common/xdg/Menu.py @@ -21,6 +21,7 @@ import os import locale import subprocess import ast +import sys try: import xml.etree.cElementTree as etree except ImportError: @@ -35,6 +36,17 @@ import xdg.Locale import xdg.Config +def _ast_const(name): + if sys.version_info >= (3, 4): + name = ast.literal_eval(name) + if sys.version_info >= (3, 8): + return ast.Constant(name) + else: + return ast.NameConstant(name) + else: + return ast.Name(id=name, ctx=ast.Load()) + + def _strxfrm(s): """Wrapper around locale.strxfrm that accepts unicode strings on Python 2. @@ -298,11 +310,11 @@ class Menu: entry.Show = NO_EXEC self.Visible -= 1 elif xdg.Config.windowmanager: - if (entry.DesktopEntry.OnlyShowIn != [] and ( - xdg.Config.windowmanager not in entry.DesktopEntry.OnlyShowIn + if (entry.DesktopEntry.getOnlyShowIn() != [] and ( + xdg.Config.windowmanager not in entry.DesktopEntry.getOnlyShowIn() ) ) or ( - xdg.Config.windowmanager in entry.DesktopEntry.NotShowIn + xdg.Config.windowmanager in entry.DesktopEntry.getNotShowIn() ): entry.Show = NOT_SHOW_IN self.Visible -= 1 @@ -710,11 +722,12 @@ class XMLMenuBuilder(object): inline_header=_to_bool(node.attrib.get("inline_header", True)), inline_alias=_to_bool(node.attrib.get("inline_alias", False)) ) + order = [] for child in node: tag, text = child.tag, child.text text = text.strip() if text else None if tag == "Menuname" and text: - layout.order.append([ + order.append([ "Menuname", text, _to_bool(child.attrib.get("show_empty", False)), @@ -724,14 +737,15 @@ class XMLMenuBuilder(object): _to_bool(child.attrib.get("inline_alias", False)) ]) elif tag == "Separator": - layout.order.append(['Separator']) + order.append(['Separator']) elif tag == "Filename" and text: - layout.order.append(["Filename", text]) + order.append(["Filename", text]) elif tag == "Merge": - layout.order.append([ + order.append([ "Merge", child.attrib.get("type", "all") ]) + layout.order = order return layout def parse_move(self, node): @@ -754,7 +768,7 @@ class XMLMenuBuilder(object): if expr: tree.body = expr else: - tree.body = ast.Name('False', ast.Load()) + tree.body = _ast_const('False') ast.fix_missing_locations(tree) return Rule(type, tree) @@ -781,7 +795,7 @@ class XMLMenuBuilder(object): expr = self.parse_bool_op(node, ast.Or()) return ast.UnaryOp(ast.Not(), expr) if expr else None elif tag == 'All': - return ast.Name('True', ast.Load()) + return _ast_const('True') elif tag == 'Category': category = node.text return ast.Compare( @@ -994,8 +1008,8 @@ class XMLMenuBuilder(object): menuentry = MenuEntry(directory, dir) if not menu.Directory: menu.Directory = menuentry - elif menuentry.Type == MenuEntry.TYPE_SYSTEM: - if menu.Directory.Type == MenuEntry.TYPE_USER: + elif menuentry.getType() == MenuEntry.TYPE_SYSTEM: + if menu.Directory.getType() == MenuEntry.TYPE_USER: menu.Directory.Original = menuentry if menu.Directory: break diff --git a/libs/common/xdg/MenuEditor.py b/libs/common/xdg/MenuEditor.py index 25b8e834..ee880f47 100644 --- a/libs/common/xdg/MenuEditor.py +++ b/libs/common/xdg/MenuEditor.py @@ -54,7 +54,7 @@ class MenuEditor(object): try: self.tree = etree.parse(self.filename) except IOError: - root = etree.fromtring(""" + root = etree.fromstring(""" Applications diff --git a/libs/common/xdg/Mime.py b/libs/common/xdg/Mime.py index 3bff8b26..886cb42a 100644 --- a/libs/common/xdg/Mime.py +++ b/libs/common/xdg/Mime.py @@ -749,14 +749,16 @@ def install_mime_info(application, package_file): file with the same name (if the contents are different)""" application += '.xml' - new_data = open(package_file).read() + with open(package_file) as f: + new_data = f.read() # See if the file is already installed package_dir = os.path.join('mime', 'packages') resource = os.path.join(package_dir, application) for x in BaseDirectory.load_data_paths(resource): try: - old_data = open(x).read() + with open(x) as f: + old_data = f.read() except: continue if old_data == new_data: @@ -770,7 +772,8 @@ def install_mime_info(application, package_file): new_file = os.path.join(BaseDirectory.save_data_path(package_dir), application) # Write the file... - open(new_file, 'w').write(new_data) + with open(new_file, 'w') as f: + f.write(new_data) # Update the database... command = 'update-mime-database' diff --git a/libs/common/xdg/RecentFiles.py b/libs/common/xdg/RecentFiles.py index 3038b578..7ee7ee57 100644 --- a/libs/common/xdg/RecentFiles.py +++ b/libs/common/xdg/RecentFiles.py @@ -71,28 +71,27 @@ class RecentFiles: elif not filename: filename = self.filename - f = open(filename, "w") - fcntl.lockf(f, fcntl.LOCK_EX) - f.write('\n') - f.write("\n") + with open(filename, "w") as f: + fcntl.lockf(f, fcntl.LOCK_EX) + f.write('\n') + f.write("\n") - for r in self.RecentFiles: - f.write(" \n") - f.write(" %s\n" % xml.sax.saxutils.escape(r.URI)) - f.write(" %s\n" % r.MimeType) - f.write(" %s\n" % r.Timestamp) - if r.Private == True: - f.write(" \n") - if len(r.Groups) > 0: - f.write(" \n") - for group in r.Groups: - f.write(" %s\n" % group) - f.write(" \n") - f.write(" \n") + for r in self.RecentFiles: + f.write(" \n") + f.write(" %s\n" % xml.sax.saxutils.escape(r.URI)) + f.write(" %s\n" % r.MimeType) + f.write(" %s\n" % r.Timestamp) + if r.Private == True: + f.write(" \n") + if len(r.Groups) > 0: + f.write(" \n") + for group in r.Groups: + f.write(" %s\n" % group) + f.write(" \n") + f.write(" \n") - f.write("\n") - fcntl.lockf(f, fcntl.LOCK_UN) - f.close() + f.write("\n") + fcntl.lockf(f, fcntl.LOCK_UN) def getFiles(self, mimetypes=None, groups=None, limit=0): """Get a list of recently used files. diff --git a/libs/common/xdg/__init__.py b/libs/common/xdg/__init__.py index b5a117ea..a417f61c 100644 --- a/libs/common/xdg/__init__.py +++ b/libs/common/xdg/__init__.py @@ -1,3 +1,3 @@ __all__ = [ "BaseDirectory", "DesktopEntry", "Menu", "Exceptions", "IniFile", "IconTheme", "Locale", "Config", "Mime", "RecentFiles", "MenuEditor" ] -__version__ = "0.26" +__version__ = "0.28"