##// END OF EJS Templates
typing: add type hints to `mercurial/utils/resourceutil.py`...
Matt Harbison -
r52562:32a1c922 default
parent child Browse files
Show More
@@ -11,10 +11,18
11 11 import _imp
12 12 import os
13 13 import sys
14 import typing
14 15
15 16 from .. import pycompat
16 17
17 18
19 if typing.TYPE_CHECKING:
20 from typing import (
21 BinaryIO,
22 Iterator,
23 )
24
25
18 26 def mainfrozen():
19 27 """return True if we are a frozen executable.
20 28
@@ -39,7 +47,7 if mainfrozen() and getattr(sys, "frozen
39 47 # leading "mercurial." off of the package name, so that these
40 48 # pseudo resources are found in their directory next to the
41 49 # executable.
42 def _package_path(package):
50 def _package_path(package: bytes) -> bytes:
43 51 dirs = package.split(b".")
44 52 assert dirs[0] == b"mercurial"
45 53 return os.path.join(_rootpath, *dirs[1:])
@@ -49,7 +57,7 else:
49 57 datapath = os.path.dirname(os.path.dirname(pycompat.fsencode(__file__)))
50 58 _rootpath = os.path.dirname(datapath)
51 59
52 def _package_path(package):
60 def _package_path(package: bytes) -> bytes:
53 61 return os.path.join(_rootpath, *package.split(b"."))
54 62
55 63
@@ -72,11 +80,11 except (ImportError, AttributeError):
72 80 # importlib.resources was not found (almost definitely because we're on a
73 81 # Python version before 3.7)
74 82
75 def open_resource(package, name):
83 def open_resource(package: bytes, name: bytes) -> "BinaryIO":
76 84 path = os.path.join(_package_path(package), name)
77 85 return open(path, "rb")
78 86
79 def is_resource(package, name):
87 def is_resource(package: bytes, name: bytes) -> bool:
80 88 path = os.path.join(_package_path(package), name)
81 89
82 90 try:
@@ -84,7 +92,7 except (ImportError, AttributeError):
84 92 except (IOError, OSError):
85 93 return False
86 94
87 def contents(package):
95 def contents(package: bytes) -> "Iterator[bytes]":
88 96 path = pycompat.fsdecode(_package_path(package))
89 97
90 98 for p in os.listdir(path):
@@ -94,7 +102,7 except (ImportError, AttributeError):
94 102 else:
95 103 from .. import encoding
96 104
97 def open_resource(package, name):
105 def open_resource(package: bytes, name: bytes) -> "BinaryIO":
98 106 if hasattr(resources, 'files'):
99 107 return (
100 108 resources.files( # pytype: disable=module-attr
@@ -108,12 +116,12 else:
108 116 pycompat.sysstr(package), pycompat.sysstr(name)
109 117 )
110 118
111 def is_resource(package, name):
119 def is_resource(package: bytes, name: bytes) -> bool:
112 120 return resources.is_resource( # pytype: disable=module-attr
113 121 pycompat.sysstr(package), encoding.strfromlocal(name)
114 122 )
115 123
116 def contents(package):
124 def contents(package: bytes) -> "Iterator[bytes]":
117 125 # pytype: disable=module-attr
118 126 for r in resources.contents(pycompat.sysstr(package)):
119 127 # pytype: enable=module-attr
General Comments 0
You need to be logged in to leave comments. Login now