Skip to content

Commit

Permalink
Merge branch 'refactor/strings_format_and_typing' into 'main'
Browse files Browse the repository at this point in the history
Refactor/strings format and typing

See merge request espressif/idf-component-manager!359
  • Loading branch information
kumekay committed Mar 7, 2024
2 parents 1b919f9 + 90f071b commit d2ff61b
Show file tree
Hide file tree
Showing 90 changed files with 1,234 additions and 1,228 deletions.
4 changes: 0 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,3 @@ repos:
stages: [commit-msg]
args:
- --types=change,chore,ci,docs,feat,fix,refactor,remove,revert,test,feat!
- repo: https://github.com/asottile/pyupgrade
rev: v3.15.1
hooks:
- id: pyupgrade
3 changes: 2 additions & 1 deletion idf_component_manager/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import sys
import warnings
from typing import List, Optional

from idf_component_manager.utils import print_error, showwarning
from idf_component_tools.errors import FatalError
Expand All @@ -31,7 +32,7 @@ def check_required_args(args, required_field=None):
raise ValueError('--{} is required'.format(_f.replace('_', '-')))


def main(command_args=None): # type: (list[str] | None) -> None
def main(command_args: Optional[List[str]] = None) -> None:
parser = argparse.ArgumentParser(description=f'IDF component manager v{version}')
parser.add_argument('command', choices=KNOWN_ACTIONS, help='Command to execute')
parser.add_argument(
Expand Down
31 changes: 16 additions & 15 deletions idf_component_manager/cli/autocompletion.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0
import os
import subprocess # nosec
from typing import List, Optional, Union

import click

Expand All @@ -13,7 +14,7 @@
CLICK_VERSION = Version.coerce(click.__version__)


def _get_shell_completion(shell): # type: (str) -> str
def _get_shell_completion(shell: str) -> str:
if CLICK_VERSION.major == 7:
return 'source_' + shell
elif CLICK_VERSION.major > 7:
Expand All @@ -23,11 +24,11 @@ def _get_shell_completion(shell): # type: (str) -> str


def _append_text_line(
strings, # type: str | list[str]
filepath, # type: str
write_string=None, # type: str | None
dry_run=False, # type: bool
): # type: (...) -> None
strings: Union[str, List[str]],
filepath: str,
write_string: Optional[str] = None,
dry_run: bool = False,
) -> None:
if isinstance(strings, str):
strings = [strings]

Expand All @@ -48,16 +49,16 @@ def _append_text_line(
write_string = strings[-1]

if dry_run:
print_info('"{}" would be appended to {}'.format(write_string, filepath))
print_info(f'"{write_string}" would be appended to {filepath}')
else:
with open(filepath, 'ab+') as fw:
fw.write('\n{}\n'.format(write_string).encode('utf8'))
fw.write(f'\n{write_string}\n'.encode())


_COMPLETE_FILE_PATH = {
'bash': '~/.{}-complete.bash'.format(CLI_NAME),
'zsh': '~/.{}-complete.zsh'.format(CLI_NAME),
'fish': '~/.config/fish/completions/{}.fish'.format(CLI_NAME),
'bash': f'~/.{CLI_NAME}-complete.bash',
'zsh': f'~/.{CLI_NAME}-complete.zsh',
'fish': f'~/.config/fish/completions/{CLI_NAME}.fish',
}

_RC_FILE_PATH = {
Expand All @@ -67,8 +68,8 @@ def _append_text_line(
}

_SOURCING_STR = {
'bash': '. {}'.format(_COMPLETE_FILE_PATH['bash']),
'zsh': '. {}'.format(_COMPLETE_FILE_PATH['zsh']),
'bash': f". {_COMPLETE_FILE_PATH['bash']}",
'zsh': f". {_COMPLETE_FILE_PATH['zsh']}",
'fish': None, # not needed
}

Expand Down Expand Up @@ -176,7 +177,7 @@ def autocomplete(shell, install, dry_run):
os.makedirs(os.path.dirname(completion_filepath))

if dry_run:
print_info('Completion file would be created at: {}'.format(completion_filepath))
print_info(f'Completion file would be created at: {completion_filepath}')
else:
with open(completion_filepath, 'w') as fw:
fw.write(autocomplete_script_str)
Expand Down
2 changes: 1 addition & 1 deletion idf_component_manager/cli/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def clear():
Clear the cache of components and API client cache.
"""
FileCache().clear()
print_info('Successfully cleared cache at\n\t{}'.format(FileCache().path()))
print_info(f'Successfully cleared cache at\n\t{FileCache().path()}')

@cache.command()
def path():
Expand Down
6 changes: 3 additions & 3 deletions idf_component_manager/cli/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: Apache-2.0
import sys
import warnings
from typing import Any
from typing import Any, Dict

import click

Expand All @@ -22,9 +22,9 @@
from .project import init_project
from .registry import init_registry

DEFAULT_SETTINGS = {
DEFAULT_SETTINGS: Dict[str, Any] = {
'help_option_names': ['-h', '--help'],
} # type: dict[str, Any]
}

if CLICK_SUPPORTS_SHOW_DEFAULT:
DEFAULT_SETTINGS['show_default'] = True
Expand Down
16 changes: 10 additions & 6 deletions idf_component_manager/cli/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# SPDX-License-Identifier: Apache-2.0
import os
import webbrowser
from typing import List

import click
import requests
Expand Down Expand Up @@ -86,7 +87,7 @@ def login(service_profile, no_browser, description, default_namespace, registry_
raise_on_missing_profile=False,
)

auth_url = '{}/tokens/'.format(api_client.frontend_url)
auth_url = f'{api_client.frontend_url}/tokens/'

auth_params = {
'scope': 'user write:components',
Expand Down Expand Up @@ -120,9 +121,7 @@ def login(service_profile, no_browser, description, default_namespace, registry_
token_valid = True
except APIClientError as e:
# Handle 401 and 403 explicitly
print_error(
'Provided token does not seem to be working: {}\nPlease try again.'.format(e)
)
print_error(f'Provided token does not seem to be working: {e}\nPlease try again.')
continue

# Update config with token and default namespace, registry URL if they are provided
Expand Down Expand Up @@ -177,8 +176,13 @@ def logout(service_profile):
)
@click.argument('path', required=True)
def sync(
manager, service_profile, interval, component, recursive, path
): # type: (ComponentManager, str, int, list[str], bool, str) -> None
manager: ComponentManager,
service_profile: str,
interval: int,
component: List[str],
recursive: bool,
path: str,
) -> None:
manager.sync_registry(
service_profile,
path,
Expand Down
2 changes: 1 addition & 1 deletion idf_component_manager/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ def wrapper(func):

def deprecated_option(ctx, param, value):
if any(True for arg in sys.argv if arg.startswith(param.opts[0])):
warnings.warn('The option {} is deprecated.'.format(param.name), UserDeprecationWarning)
warnings.warn(f'The option {param.name} is deprecated.', UserDeprecationWarning)
48 changes: 25 additions & 23 deletions idf_component_manager/cmake_component_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

import re
from collections import OrderedDict, namedtuple
from typing import Mapping
from typing import Dict, List, Mapping, Optional
from typing import OrderedDict as OrderedDictType
from typing import Set, Union

from idf_component_tools.errors import FatalError

Expand All @@ -15,7 +17,7 @@
)


def name_without_namespace(name): # type: (str) -> str
def name_without_namespace(name: str) -> str:
name_parts = name.rsplit('__', 1)

try:
Expand All @@ -25,26 +27,26 @@ def name_without_namespace(name): # type: (str) -> str


class ComponentName:
def __init__(self, prefix, name): # type: (str, str) -> None
def __init__(self, prefix: str, name: str) -> None:
self.prefix = prefix
self.name = name

self._name_without_namespace = None # type: str | None
self._name_without_namespace: Optional[str] = None

def __eq__(self, another): # type: (object) -> bool
def __eq__(self, another: object) -> bool:
if not isinstance(another, ComponentName):
return False

return (self.prefix, self.name) == (another.prefix, another.name)

def __hash__(self): # type: () -> int
def __hash__(self) -> int:
return hash((self.prefix, self.name))

def __repr__(self): # type: () -> str
def __repr__(self) -> str:
return f'ComponentName({self.prefix}, {self.name})'

@property
def name_without_namespace(self): # type: () -> str
def name_without_namespace(self) -> str:
if self._name_without_namespace is None:
self._name_without_namespace = name_without_namespace(self.name)

Expand All @@ -55,11 +57,11 @@ class RequirementsProcessingError(FatalError):
pass


def parse_requirements_line(line): # type: (str) -> ComponentProperty
def parse_requirements_line(line: str) -> ComponentProperty:
match = REQ_RE.match(line)

if not match:
raise RequirementsProcessingError('Cannot parse CMake requirements line: %s' % line)
raise RequirementsProcessingError(f'Cannot parse CMake requirements line: {line}')

return ComponentProperty(
ComponentName(match.group('prefix'), match.group('name')),
Expand All @@ -72,7 +74,7 @@ class CMakeRequirementsManager:
def __init__(self, path):
self.path = path

def dump(self, requirements): # type: (Mapping[ComponentName, dict[str, list | str]]) -> None
def dump(self, requirements: Mapping[ComponentName, Dict[str, Union[List, str]]]) -> None:
with open(self.path, mode='w', encoding='utf-8') as f:
for name, requirement in requirements.items():
for prop, value in requirement.items():
Expand All @@ -85,8 +87,10 @@ def dump(self, requirements): # type: (Mapping[ComponentName, dict[str, list |
)
)

def load(self): # type: () -> OrderedDict[ComponentName, dict[str, list[str] | str]]
requirements = OrderedDict() # type: OrderedDict[ComponentName, dict[str, list[str] | str]]
def load(self) -> OrderedDictType[ComponentName, Dict[str, Union[List[str], str]]]:
requirements: OrderedDictType[
ComponentName, Dict[str, Union[List[str], str]]
] = OrderedDict()

with open(self.path, encoding='utf-8') as f:
for line in f:
Expand All @@ -108,18 +112,18 @@ def load(self): # type: () -> OrderedDict[ComponentName, dict[str, list[str] |


def check_requirements_name_collisions(
requirements,
): # type: (dict[ComponentName, dict[str, list[str] | str]]) -> None
requirements: Dict[ComponentName, Dict[str, Union[List[str], str]]],
) -> None:
"""
DEPRECATE: This function is deprecated since interface_version 3,
Remove it after ESP-IDF 5.1 EOL
"""
# Pay attention only to components without namespaces
name_variants = {
name_variants: Dict[str, Set[str]] = {
cmp.name: {cmp.name}
for cmp in requirements.keys()
if cmp.name == cmp.name_without_namespace
} # type: dict[str, set[str]]
}

for cmp in requirements.keys():
if cmp.name_without_namespace not in name_variants:
Expand All @@ -142,7 +146,7 @@ def check_requirements_name_collisions(
)


def _choose_component(component, known_components): # type: (str, list[str]) -> str
def _choose_component(component: str, known_components: List[str]) -> str:
if component in known_components:
return component

Expand All @@ -160,9 +164,7 @@ def _choose_component(component, known_components): # type: (str, list[str]) ->
return component


def _handle_component_reqs(
components, known_components
): # type: (list[str], list[str]) -> list[str]
def _handle_component_reqs(components: List[str], known_components: List[str]) -> List[str]:
updated_items = []
for component in components:
name_to_add = _choose_component(component, known_components)
Expand All @@ -173,8 +175,8 @@ def _handle_component_reqs(


def handle_project_requirements(
requirements,
): # type: (OrderedDict[ComponentName, dict[str, list[str] | str]]) -> None
requirements: OrderedDictType[ComponentName, Dict[str, Union[List[str], str]]],
) -> None:
"""
Use local components with higher priority.
For example if in some manifest has a dependency `namespace/component`,
Expand Down
Loading

0 comments on commit d2ff61b

Please sign in to comment.