Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/utils/resourceutil.py @ 44031:bba9149adc14
resourceutil: implement `contents()` to iterate over resources in a package
Differential Revision: https://phab.mercurial-scm.org/D7774
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Sun, 29 Dec 2019 20:35:34 -0500 |
parents | 42a897bf678c |
children | a90039b07343 |
comparison
equal
deleted
inserted
replaced
44030:42a897bf678c | 44031:bba9149adc14 |
---|---|
53 def is_resource(package, name): | 53 def is_resource(package, name): |
54 return resources.is_resource( | 54 return resources.is_resource( |
55 pycompat.sysstr(package), encoding.strfromlocal(name) | 55 pycompat.sysstr(package), encoding.strfromlocal(name) |
56 ) | 56 ) |
57 | 57 |
58 def contents(package): | |
59 for r in resources.contents(pycompat.sysstr(package)): | |
60 yield encoding.strtolocal(r) | |
61 | |
58 | 62 |
59 except (ImportError, AttributeError): | 63 except (ImportError, AttributeError): |
60 | 64 |
61 def _package_path(package): | 65 def _package_path(package): |
62 return os.path.join(_rootpath, *package.split(b'.')) | 66 return os.path.join(_rootpath, *package.split(b'.')) |
70 | 74 |
71 try: | 75 try: |
72 return os.path.isfile(pycompat.fsdecode(path)) | 76 return os.path.isfile(pycompat.fsdecode(path)) |
73 except (IOError, OSError): | 77 except (IOError, OSError): |
74 return False | 78 return False |
79 | |
80 def contents(package): | |
81 path = pycompat.fsdecode(_package_path(package)) | |
82 | |
83 for p in os.listdir(path): | |
84 yield pycompat.fsencode(p) |