comparison mercurial/utils/resourceutil.py @ 44030: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
comparison
equal deleted inserted replaced
44029:52f0140c2604 44030:42a897bf678c
38 _rootpath = os.path.dirname(datapath) 38 _rootpath = os.path.dirname(datapath)
39 39
40 try: 40 try:
41 from importlib import resources 41 from importlib import resources
42 42
43 from .. import encoding
44
43 # Force loading of the resources module 45 # Force loading of the resources module
44 resources.open_binary # pytype: disable=module-attr 46 resources.open_binary # pytype: disable=module-attr
45 47
46 def open_resource(package, name): 48 def open_resource(package, name):
47 return resources.open_binary( # pytype: disable=module-attr 49 return resources.open_binary( # pytype: disable=module-attr
48 pycompat.sysstr(package), pycompat.sysstr(name) 50 pycompat.sysstr(package), pycompat.sysstr(name)
51 )
52
53 def is_resource(package, name):
54 return resources.is_resource(
55 pycompat.sysstr(package), encoding.strfromlocal(name)
49 ) 56 )
50 57
51 58
52 except (ImportError, AttributeError): 59 except (ImportError, AttributeError):
53 60
55 return os.path.join(_rootpath, *package.split(b'.')) 62 return os.path.join(_rootpath, *package.split(b'.'))
56 63
57 def open_resource(package, name): 64 def open_resource(package, name):
58 path = os.path.join(_package_path(package), name) 65 path = os.path.join(_package_path(package), name)
59 return open(path, 'rb') 66 return open(path, 'rb')
67
68 def is_resource(package, name):
69 path = os.path.join(_package_path(package), name)
70
71 try:
72 return os.path.isfile(pycompat.fsdecode(path))
73 except (IOError, OSError):
74 return False