tools: Add python types hints

This commit is contained in:
simon.chupin
2022-06-03 14:46:56 +02:00
parent b20aa0612b
commit 44f3c19fa9
12 changed files with 210 additions and 217 deletions
+11 -7
View File
@@ -6,13 +6,17 @@ import os
import re
import sys
from distutils.dir_util import copy_tree
from typing import Dict
import click
from idf_py_actions.tools import PropertyDict
def get_type(action):
def get_type(action: str) -> str:
return action.split('-')[1]
def replace_in_file(filename, pattern, replacement):
def replace_in_file(filename: str, pattern: str, replacement: str) -> None:
with open(filename, 'r+') as f:
content = f.read()
overwritten_content = re.sub(pattern, replacement, content, flags=re.M)
@@ -21,7 +25,7 @@ def replace_in_file(filename, pattern, replacement):
f.truncate()
def is_empty_and_create(path, action):
def is_empty_and_create(path: str, action: str) -> None:
abspath = os.path.abspath(path)
if not os.path.exists(abspath):
os.makedirs(abspath)
@@ -35,7 +39,7 @@ def is_empty_and_create(path, action):
sys.exit(3)
def create_project(target_path, name):
def create_project(target_path: str, name: str) -> None:
copy_tree(os.path.join(os.environ['IDF_PATH'], 'examples', 'get-started', 'sample_project'), target_path)
main_folder = os.path.join(target_path, 'main')
os.rename(os.path.join(main_folder, 'main.c'), os.path.join(main_folder, '.'.join((name, 'c'))))
@@ -44,7 +48,7 @@ def create_project(target_path, name):
os.remove(os.path.join(target_path, 'README.md'))
def create_component(target_path, name):
def create_component(target_path: str, name: str) -> None:
copy_tree(os.path.join(os.environ['IDF_PATH'], 'tools', 'templates', 'sample_component'), target_path)
os.rename(os.path.join(target_path, 'main.c'), os.path.join(target_path, '.'.join((name, 'c'))))
os.rename(os.path.join(target_path, 'include', 'main.h'),
@@ -54,8 +58,8 @@ def create_component(target_path, name):
replace_in_file(os.path.join(target_path, 'CMakeLists.txt'), 'main', name)
def action_extensions(base_actions, project_path):
def create_new(action, ctx, global_args, **action_args):
def action_extensions(base_actions: Dict, project_path: str) -> Dict:
def create_new(action: str, ctx: click.core.Context, global_args: PropertyDict, **action_args: str) -> Dict:
target_path = action_args.get('path') or os.path.join(project_path, action_args['name'])
is_empty_and_create(target_path, action)