forked from espressif/esp-idf
tools: fix some type annotations in idf_tools.py
This commit is contained in:
@@ -59,7 +59,7 @@ except RuntimeError as e:
|
|||||||
print(e)
|
print(e)
|
||||||
raise SystemExit(1)
|
raise SystemExit(1)
|
||||||
|
|
||||||
from typing import IO, Any, Callable, Optional, Tuple, Union # noqa: F401
|
from typing import IO, Any, Callable, Dict, List, Optional, Set, Tuple, Union # noqa: F401
|
||||||
from urllib.error import ContentTooShortError
|
from urllib.error import ContentTooShortError
|
||||||
from urllib.request import urlopen
|
from urllib.request import urlopen
|
||||||
# the following is only for typing annotation
|
# the following is only for typing annotation
|
||||||
@@ -206,7 +206,7 @@ def info(text, f=None, *args): # type: (str, Optional[IO[str]], str) -> None
|
|||||||
|
|
||||||
|
|
||||||
def run_cmd_check_output(cmd, input_text=None, extra_paths=None):
|
def run_cmd_check_output(cmd, input_text=None, extra_paths=None):
|
||||||
# type: (list[str], Optional[str], Optional[list[str]]) -> bytes
|
# type: (List[str], Optional[str], Optional[List[str]]) -> bytes
|
||||||
# If extra_paths is given, locate the executable in one of these directories.
|
# If extra_paths is given, locate the executable in one of these directories.
|
||||||
# Note: it would seem logical to add extra_paths to env[PATH], instead, and let OS do the job of finding the
|
# Note: it would seem logical to add extra_paths to env[PATH], instead, and let OS do the job of finding the
|
||||||
# executable for us. However this does not work on Windows: https://bugs.python.org/issue8557.
|
# executable for us. However this does not work on Windows: https://bugs.python.org/issue8557.
|
||||||
@@ -242,14 +242,14 @@ def run_cmd_check_output(cmd, input_text=None, extra_paths=None):
|
|||||||
return stdout + stderr
|
return stdout + stderr
|
||||||
|
|
||||||
|
|
||||||
def to_shell_specific_paths(paths_list): # type: (list[str]) -> list[str]
|
def to_shell_specific_paths(paths_list): # type: (List[str]) -> List[str]
|
||||||
if sys.platform == 'win32':
|
if sys.platform == 'win32':
|
||||||
paths_list = [p.replace('/', os.path.sep) if os.path.sep in p else p for p in paths_list]
|
paths_list = [p.replace('/', os.path.sep) if os.path.sep in p else p for p in paths_list]
|
||||||
|
|
||||||
return paths_list
|
return paths_list
|
||||||
|
|
||||||
|
|
||||||
def get_env_for_extra_paths(extra_paths): # type: (list[str]) -> dict[str, str]
|
def get_env_for_extra_paths(extra_paths): # type: (List[str]) -> Dict[str, str]
|
||||||
"""
|
"""
|
||||||
Return a copy of environment variables dict, prepending paths listed in extra_paths
|
Return a copy of environment variables dict, prepending paths listed in extra_paths
|
||||||
to the PATH environment variable.
|
to the PATH environment variable.
|
||||||
@@ -476,7 +476,8 @@ class IDFToolVersion(object):
|
|||||||
return set(self.downloads.keys())
|
return set(self.downloads.keys())
|
||||||
|
|
||||||
|
|
||||||
OPTIONS_LIST = ['version_cmd',
|
IDFToolOptions = namedtuple('IDFToolOptions', [
|
||||||
|
'version_cmd',
|
||||||
'version_regex',
|
'version_regex',
|
||||||
'version_regex_replace',
|
'version_regex_replace',
|
||||||
'export_paths',
|
'export_paths',
|
||||||
@@ -485,9 +486,7 @@ OPTIONS_LIST = ['version_cmd',
|
|||||||
'info_url',
|
'info_url',
|
||||||
'license',
|
'license',
|
||||||
'strip_container_dirs',
|
'strip_container_dirs',
|
||||||
'supported_targets']
|
'supported_targets'])
|
||||||
|
|
||||||
IDFToolOptions = namedtuple('IDFToolOptions', OPTIONS_LIST) # type: ignore
|
|
||||||
|
|
||||||
|
|
||||||
class IDFTool(object):
|
class IDFTool(object):
|
||||||
@@ -498,17 +497,17 @@ class IDFTool(object):
|
|||||||
|
|
||||||
def __init__(self, name, description, install, info_url, license, version_cmd, version_regex, supported_targets, version_regex_replace=None,
|
def __init__(self, name, description, install, info_url, license, version_cmd, version_regex, supported_targets, version_regex_replace=None,
|
||||||
strip_container_dirs=0):
|
strip_container_dirs=0):
|
||||||
# type: (str, str, str, str, str, list[str], str, list[str], Optional[str], int) -> None
|
# type: (str, str, str, str, str, List[str], str, List[str], Optional[str], int) -> None
|
||||||
self.name = name
|
self.name = name
|
||||||
self.description = description
|
self.description = description
|
||||||
self.versions = OrderedDict() # type: dict[str, IDFToolVersion]
|
self.versions = OrderedDict() # type: Dict[str, IDFToolVersion]
|
||||||
self.version_in_path = None # type: Optional[str]
|
self.version_in_path = None # type: Optional[str]
|
||||||
self.versions_installed = [] # type: list[str]
|
self.versions_installed = [] # type: List[str]
|
||||||
if version_regex_replace is None:
|
if version_regex_replace is None:
|
||||||
version_regex_replace = VERSION_REGEX_REPLACE_DEFAULT
|
version_regex_replace = VERSION_REGEX_REPLACE_DEFAULT
|
||||||
self.options = IDFToolOptions(version_cmd, version_regex, version_regex_replace,
|
self.options = IDFToolOptions(version_cmd, version_regex, version_regex_replace,
|
||||||
[], OrderedDict(), install, info_url, license, strip_container_dirs, supported_targets) # type: ignore
|
[], OrderedDict(), install, info_url, license, strip_container_dirs, supported_targets) # type: ignore
|
||||||
self.platform_overrides = [] # type: list[dict[str, str]]
|
self.platform_overrides = [] # type: List[Dict[str, str]]
|
||||||
self._platform = CURRENT_PLATFORM
|
self._platform = CURRENT_PLATFORM
|
||||||
self._update_current_options()
|
self._update_current_options()
|
||||||
|
|
||||||
@@ -538,11 +537,11 @@ class IDFTool(object):
|
|||||||
assert(version in self.versions)
|
assert(version in self.versions)
|
||||||
return os.path.join(self.get_path(), version)
|
return os.path.join(self.get_path(), version)
|
||||||
|
|
||||||
def get_export_paths(self, version): # type: (str) -> list[str]
|
def get_export_paths(self, version): # type: (str) -> List[str]
|
||||||
tool_path = self.get_path_for_version(version)
|
tool_path = self.get_path_for_version(version)
|
||||||
return [os.path.join(tool_path, *p) for p in self._current_options.export_paths] # type: ignore
|
return [os.path.join(tool_path, *p) for p in self._current_options.export_paths] # type: ignore
|
||||||
|
|
||||||
def get_export_vars(self, version): # type: (str) -> dict[str, str]
|
def get_export_vars(self, version): # type: (str) -> Dict[str, str]
|
||||||
"""
|
"""
|
||||||
Get the dictionary of environment variables to be exported, for the given version.
|
Get the dictionary of environment variables to be exported, for the given version.
|
||||||
Expands:
|
Expands:
|
||||||
@@ -557,7 +556,7 @@ class IDFTool(object):
|
|||||||
result[k] = v_repl
|
result[k] = v_repl
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def check_version(self, extra_paths=None): # type: (Optional[list[str]]) -> str
|
def check_version(self, extra_paths=None): # type: (Optional[List[str]]) -> str
|
||||||
"""
|
"""
|
||||||
Execute the tool, optionally prepending extra_paths to PATH,
|
Execute the tool, optionally prepending extra_paths to PATH,
|
||||||
extract the version string and return it as a result.
|
extract the version string and return it as a result.
|
||||||
@@ -593,7 +592,7 @@ class IDFTool(object):
|
|||||||
def compatible_with_platform(self): # type: () -> bool
|
def compatible_with_platform(self): # type: () -> bool
|
||||||
return any([v.compatible_with_platform() for v in self.versions.values()])
|
return any([v.compatible_with_platform() for v in self.versions.values()])
|
||||||
|
|
||||||
def get_supported_platforms(self): # type: () -> set[str]
|
def get_supported_platforms(self): # type: () -> Set[str]
|
||||||
result = set()
|
result = set()
|
||||||
for v in self.versions.values():
|
for v in self.versions.values():
|
||||||
result.update(v.get_supported_platforms())
|
result.update(v.get_supported_platforms())
|
||||||
@@ -746,7 +745,7 @@ class IDFTool(object):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_json(cls, tool_dict): # type: (dict[str, Union[str, list[str], dict[str, str]]]) -> IDFTool
|
def from_json(cls, tool_dict): # type: (Dict[str, Union[str, List[str], Dict[str, str]]]) -> IDFTool
|
||||||
# json.load will return 'str' types in Python 3 and 'unicode' in Python 2
|
# json.load will return 'str' types in Python 3 and 'unicode' in Python 2
|
||||||
expected_str_type = type(u'')
|
expected_str_type = type(u'')
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user