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