changeset 44028:42a897bf678c

resourceutil: implement `is_resource()` This will be needed when iterating resources. Differential Revision: https://phab.mercurial-scm.org/D7773
author Matt Harbison <matt_harbison@yahoo.com>
date Sun, 29 Dec 2019 20:32:56 -0500
parents 52f0140c2604
children bba9149adc14
files mercurial/utils/resourceutil.py
diffstat 1 files changed, 15 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/utils/resourceutil.py	Sat Dec 28 23:35:13 2019 -0500
+++ b/mercurial/utils/resourceutil.py	Sun Dec 29 20:32:56 2019 -0500
@@ -40,6 +40,8 @@
 try:
     from importlib import resources
 
+    from .. import encoding
+
     # Force loading of the resources module
     resources.open_binary  # pytype: disable=module-attr
 
@@ -48,6 +50,11 @@
             pycompat.sysstr(package), pycompat.sysstr(name)
         )
 
+    def is_resource(package, name):
+        return resources.is_resource(
+            pycompat.sysstr(package), encoding.strfromlocal(name)
+        )
+
 
 except (ImportError, AttributeError):
 
@@ -57,3 +64,11 @@
     def open_resource(package, name):
         path = os.path.join(_package_path(package), name)
         return open(path, 'rb')
+
+    def is_resource(package, name):
+        path = os.path.join(_package_path(package), name)
+
+        try:
+            return os.path.isfile(pycompat.fsdecode(path))
+        except (IOError, OSError):
+            return False