mercurial/utils/resourceutil.py
changeset 47860 08f16b3331df
parent 47858 df94c13ddf60
child 48008 c0588d389c5f
equal deleted inserted replaced
47859:155a2ec8a9dc 47860:08f16b3331df
    57 try:
    57 try:
    58     # importlib.resources exists from Python 3.7; see fallback in except clause
    58     # importlib.resources exists from Python 3.7; see fallback in except clause
    59     # further down
    59     # further down
    60     from importlib import resources
    60     from importlib import resources
    61 
    61 
    62     from .. import encoding
       
    63 
       
    64     # Force loading of the resources module
    62     # Force loading of the resources module
    65     resources.open_binary  # pytype: disable=module-attr
    63     resources.open_binary  # pytype: disable=module-attr
    66 
       
    67     def open_resource(package, name):
       
    68         return resources.open_binary(  # pytype: disable=module-attr
       
    69             pycompat.sysstr(package), pycompat.sysstr(name)
       
    70         )
       
    71 
       
    72     def is_resource(package, name):
       
    73         return resources.is_resource(  # pytype: disable=module-attr
       
    74             pycompat.sysstr(package), encoding.strfromlocal(name)
       
    75         )
       
    76 
       
    77     def contents(package):
       
    78         # pytype: disable=module-attr
       
    79         for r in resources.contents(pycompat.sysstr(package)):
       
    80             # pytype: enable=module-attr
       
    81             yield encoding.strtolocal(r)
       
    82 
       
    83 
    64 
    84 except (ImportError, AttributeError):
    65 except (ImportError, AttributeError):
    85     # importlib.resources was not found (almost definitely because we're on a
    66     # importlib.resources was not found (almost definitely because we're on a
    86     # Python version before 3.7)
    67     # Python version before 3.7)
    87 
    68 
   100     def contents(package):
    81     def contents(package):
   101         path = pycompat.fsdecode(_package_path(package))
    82         path = pycompat.fsdecode(_package_path(package))
   102 
    83 
   103         for p in os.listdir(path):
    84         for p in os.listdir(path):
   104             yield pycompat.fsencode(p)
    85             yield pycompat.fsencode(p)
       
    86 
       
    87 
       
    88 else:
       
    89     from .. import encoding
       
    90 
       
    91     def open_resource(package, name):
       
    92         return resources.open_binary(  # pytype: disable=module-attr
       
    93             pycompat.sysstr(package), pycompat.sysstr(name)
       
    94         )
       
    95 
       
    96     def is_resource(package, name):
       
    97         return resources.is_resource(  # pytype: disable=module-attr
       
    98             pycompat.sysstr(package), encoding.strfromlocal(name)
       
    99         )
       
   100 
       
   101     def contents(package):
       
   102         # pytype: disable=module-attr
       
   103         for r in resources.contents(pycompat.sysstr(package)):
       
   104             # pytype: enable=module-attr
       
   105             yield encoding.strtolocal(r)