mercurial/utils/resourceutil.py
changeset 52645 4cb75772818d
parent 52640 24ee91ba9aa8
equal deleted inserted replaced
52644:e627cc25b6f3 52645:4cb75772818d
    84 
    84 
    85 except (ImportError, AttributeError):
    85 except (ImportError, AttributeError):
    86     # importlib.resources was not found (almost definitely because we're on a
    86     # importlib.resources was not found (almost definitely because we're on a
    87     # Python version before 3.7)
    87     # Python version before 3.7)
    88 
    88 
    89     def open_resource(package: bytes, name: bytes) -> "BinaryIO":
    89     def open_resource(package: bytes, name: bytes) -> BinaryIO:
    90         path = os.path.join(_package_path(package), name)
    90         path = os.path.join(_package_path(package), name)
    91         return open(path, "rb")
    91         return open(path, "rb")
    92 
    92 
    93     def is_resource(package: bytes, name: bytes) -> bool:
    93     def is_resource(package: bytes, name: bytes) -> bool:
    94         path = os.path.join(_package_path(package), name)
    94         path = os.path.join(_package_path(package), name)
    96         try:
    96         try:
    97             return os.path.isfile(pycompat.fsdecode(path))
    97             return os.path.isfile(pycompat.fsdecode(path))
    98         except OSError:
    98         except OSError:
    99             return False
    99             return False
   100 
   100 
   101     def contents(package: bytes) -> "Iterator[bytes]":
   101     def contents(package: bytes) -> Iterator[bytes]:
   102         path = pycompat.fsdecode(_package_path(package))
   102         path = pycompat.fsdecode(_package_path(package))
   103 
   103 
   104         for p in os.listdir(path):
   104         for p in os.listdir(path):
   105             yield pycompat.fsencode(p)
   105             yield pycompat.fsencode(p)
   106 
   106 
   107 else:
   107 else:
   108     from .. import encoding
   108     from .. import encoding
   109 
   109 
   110     def open_resource(package: bytes, name: bytes) -> "BinaryIO":
   110     def open_resource(package: bytes, name: bytes) -> BinaryIO:
   111         if hasattr(resources, 'files'):
   111         if hasattr(resources, 'files'):
   112             return (
   112             return (
   113                 resources.files(  # pytype: disable=module-attr
   113                 resources.files(  # pytype: disable=module-attr
   114                     pycompat.sysstr(package)
   114                     pycompat.sysstr(package)
   115                 )
   115                 )
   131         else:
   131         else:
   132             return resources.is_resource(  # pytype: disable=module-attr
   132             return resources.is_resource(  # pytype: disable=module-attr
   133                 pycompat.sysstr(package), encoding.strfromlocal(name)
   133                 pycompat.sysstr(package), encoding.strfromlocal(name)
   134             )
   134             )
   135 
   135 
   136     def contents(package: bytes) -> "Iterator[bytes]":
   136     def contents(package: bytes) -> Iterator[bytes]:
   137         if hasattr(resources, 'files'):  # Introduced in Python 3.9
   137         if hasattr(resources, 'files'):  # Introduced in Python 3.9
   138             for path in resources.files(pycompat.sysstr(package)).iterdir():
   138             for path in resources.files(pycompat.sysstr(package)).iterdir():
   139                 if path.is_file():
   139                 if path.is_file():
   140                     yield encoding.strtolocal(path.name)
   140                     yield encoding.strtolocal(path.name)
   141         else:
   141         else: