tools.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. # SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Apache-2.0
  3. import asyncio
  4. import os
  5. import re
  6. import subprocess
  7. import sys
  8. from asyncio.subprocess import Process
  9. from io import open
  10. from types import FunctionType
  11. from typing import Any, Dict, List, Match, Optional, TextIO, Tuple, Union
  12. import click
  13. import yaml
  14. from .constants import GENERATORS
  15. from .errors import FatalError
  16. def executable_exists(args: List) -> bool:
  17. try:
  18. subprocess.check_output(args)
  19. return True
  20. except Exception:
  21. return False
  22. def realpath(path: str) -> str:
  23. """
  24. Return the cannonical path with normalized case.
  25. It is useful on Windows to comparision paths in case-insensitive manner.
  26. On Unix and Mac OS X it works as `os.path.realpath()` only.
  27. """
  28. return os.path.normcase(os.path.realpath(path))
  29. def _idf_version_from_cmake() -> Optional[str]:
  30. version_path = os.path.join(os.environ['IDF_PATH'], 'tools/cmake/version.cmake')
  31. regex = re.compile(r'^\s*set\s*\(\s*IDF_VERSION_([A-Z]{5})\s+(\d+)')
  32. ver = {}
  33. try:
  34. with open(version_path) as f:
  35. for line in f:
  36. m = regex.match(line)
  37. if m:
  38. ver[m.group(1)] = m.group(2)
  39. return 'v%s.%s.%s' % (ver['MAJOR'], ver['MINOR'], ver['PATCH'])
  40. except (KeyError, OSError):
  41. sys.stderr.write('WARNING: Cannot find ESP-IDF version in version.cmake\n')
  42. return None
  43. def get_target(path: str, sdkconfig_filename: str='sdkconfig') -> Optional[str]:
  44. path = os.path.join(path, sdkconfig_filename)
  45. return get_sdkconfig_value(path, 'CONFIG_IDF_TARGET')
  46. def idf_version() -> Optional[str]:
  47. """Print version of ESP-IDF"""
  48. # Try to get version from git:
  49. try:
  50. version: Optional[str] = subprocess.check_output([
  51. 'git',
  52. '--git-dir=%s' % os.path.join(os.environ['IDF_PATH'], '.git'),
  53. '--work-tree=%s' % os.environ['IDF_PATH'],
  54. 'describe', '--tags', '--dirty', '--match', 'v*.*',
  55. ]).decode('utf-8', 'ignore').strip()
  56. except (subprocess.CalledProcessError, UnicodeError):
  57. # if failed, then try to parse cmake.version file
  58. sys.stderr.write('WARNING: Git version unavailable, reading from source\n')
  59. version = _idf_version_from_cmake()
  60. return version
  61. def color_print(message: str, color: str, newline: Optional[str]='\n') -> None:
  62. """ Print a message to stderr with colored highlighting """
  63. ansi_normal = '\033[0m'
  64. sys.stderr.write('%s%s%s%s' % (color, message, ansi_normal, newline))
  65. sys.stderr.flush()
  66. def yellow_print(message: str, newline: Optional[str]='\n') -> None:
  67. ansi_yellow = '\033[0;33m'
  68. color_print(message, ansi_yellow, newline)
  69. def red_print(message: str, newline: Optional[str]='\n') -> None:
  70. ansi_red = '\033[1;31m'
  71. color_print(message, ansi_red, newline)
  72. def print_hints(*filenames: str) -> None:
  73. """Getting output files and printing hints on how to resolve errors based on the output."""
  74. with open(os.path.join(os.path.dirname(__file__), 'hints.yml'), 'r') as file:
  75. hints = yaml.safe_load(file)
  76. for file_name in filenames:
  77. with open(file_name, 'r') as file:
  78. output = ' '.join(line.strip() for line in file if line.strip())
  79. for hint in hints:
  80. variables_list = hint.get('variables')
  81. hint_list, hint_vars, re_vars = [], [], []
  82. match: Optional[Match[str]] = None
  83. try:
  84. if variables_list:
  85. for variables in variables_list:
  86. hint_vars = variables['re_variables']
  87. re_vars = variables['hint_variables']
  88. regex = hint['re'].format(*re_vars)
  89. if re.compile(regex).search(output):
  90. try:
  91. hint_list.append(hint['hint'].format(*hint_vars))
  92. except KeyError as e:
  93. red_print('Argument {} missing in {}. Check hints.yml file.'.format(e, hint))
  94. sys.exit(1)
  95. else:
  96. match = re.compile(hint['re']).search(output)
  97. except KeyError as e:
  98. red_print('Argument {} missing in {}. Check hints.yml file.'.format(e, hint))
  99. sys.exit(1)
  100. except re.error as e:
  101. red_print('{} from hints.yml have {} problem. Check hints.yml file.'.format(hint['re'], e))
  102. sys.exit(1)
  103. if hint_list:
  104. for message in hint_list:
  105. yellow_print('HINT:', message)
  106. elif match:
  107. extra_info = ', '.join(match.groups()) if hint.get('match_to_output', '') else ''
  108. try:
  109. yellow_print(' '.join(['HINT:', hint['hint'].format(extra_info)]))
  110. except KeyError as e:
  111. red_print('Argument {} missing in {}. Check hints.yml file.'.format(e, hint))
  112. sys.exit(1)
  113. def fit_text_in_terminal(out: str) -> str:
  114. """Fit text in terminal, if the string is not fit replace center with `...`"""
  115. space_for_dots = 3 # Space for "..."
  116. terminal_width, _ = os.get_terminal_size()
  117. if terminal_width <= space_for_dots:
  118. # if the wide of the terminal is too small just print dots
  119. return '.' * terminal_width
  120. if len(out) >= terminal_width:
  121. elide_size = (terminal_width - space_for_dots) // 2
  122. # cut out the middle part of the output if it does not fit in the terminal
  123. return '...'.join([out[:elide_size], out[len(out) - elide_size:]])
  124. return out
  125. class RunTool:
  126. def __init__(self, tool_name: str, args: List, cwd: str, env: Dict=None, custom_error_handler: FunctionType=None, build_dir: str=None,
  127. hints: bool=True, force_progression: bool=False, interactive: bool=False) -> None:
  128. self.tool_name = tool_name
  129. self.args = args
  130. self.cwd = cwd
  131. self.env = env
  132. self.custom_error_handler = custom_error_handler
  133. # build_dir sets by tools that do not use build directory as cwd
  134. self.build_dir = build_dir or cwd
  135. self.hints = hints
  136. self.force_progression = force_progression
  137. self.interactive = interactive
  138. def __call__(self) -> None:
  139. def quote_arg(arg: str) -> str:
  140. """ Quote the `arg` with whitespace in them because it can cause problems when we call it from a subprocess."""
  141. if re.match(r"^(?![\'\"]).*\s.*", arg):
  142. return ''.join(["'", arg, "'"])
  143. return arg
  144. self.args = [str(arg) for arg in self.args]
  145. display_args = ' '.join(quote_arg(arg) for arg in self.args)
  146. print('Running %s in directory %s' % (self.tool_name, quote_arg(self.cwd)))
  147. print('Executing "%s"...' % str(display_args))
  148. env_copy = dict(os.environ)
  149. env_copy.update(self.env or {})
  150. process: Union[Process, subprocess.CompletedProcess[bytes]]
  151. if self.hints:
  152. process, stderr_output_file, stdout_output_file = asyncio.run(self.run_command(self.args, env_copy))
  153. else:
  154. process = subprocess.run(self.args, env=env_copy, cwd=self.cwd)
  155. stderr_output_file, stdout_output_file = None, None
  156. if process.returncode == 0:
  157. return
  158. if self.custom_error_handler:
  159. self.custom_error_handler(process.returncode, stderr_output_file, stdout_output_file)
  160. return
  161. if stderr_output_file and stdout_output_file:
  162. print_hints(stderr_output_file, stdout_output_file)
  163. raise FatalError('{} failed with exit code {}, output of the command is in the {} and {}'.format(self.tool_name, process.returncode,
  164. stderr_output_file, stdout_output_file))
  165. raise FatalError('{} failed with exit code {}'.format(self.tool_name, process.returncode))
  166. async def run_command(self, cmd: List, env_copy: Dict) -> Tuple[Process, Optional[str], Optional[str]]:
  167. """ Run the `cmd` command with capturing stderr and stdout from that function and return returncode
  168. and of the command, the id of the process, paths to captured output """
  169. log_dir_name = 'log'
  170. try:
  171. os.mkdir(os.path.join(self.build_dir, log_dir_name))
  172. except FileExistsError:
  173. pass
  174. # Note: we explicitly pass in os.environ here, as we may have set IDF_PATH there during startup
  175. # limit was added for avoiding error in idf.py confserver
  176. try:
  177. p = await asyncio.create_subprocess_exec(*cmd, env=env_copy, limit=1024 * 256, cwd=self.cwd, stdout=asyncio.subprocess.PIPE,
  178. stderr=asyncio.subprocess.PIPE)
  179. except NotImplementedError:
  180. sys.exit(f'ERROR: {sys.executable} doesn\'t support asyncio. The issue can be worked around by re-running idf.py with the "--no-hints" argument.')
  181. stderr_output_file = os.path.join(self.build_dir, log_dir_name, f'idf_py_stderr_output_{p.pid}')
  182. stdout_output_file = os.path.join(self.build_dir, log_dir_name, f'idf_py_stdout_output_{p.pid}')
  183. if p.stderr and p.stdout: # it only to avoid None type in p.std
  184. await asyncio.gather(
  185. self.read_and_write_stream(p.stderr, stderr_output_file, sys.stderr),
  186. self.read_and_write_stream(p.stdout, stdout_output_file))
  187. await p.wait() # added for avoiding None returncode
  188. return p, stderr_output_file, stdout_output_file
  189. async def read_and_write_stream(self, input_stream: asyncio.StreamReader, output_filename: str,
  190. output_stream: TextIO=sys.stdout) -> None:
  191. """read the output of the `input_stream` and then write it into `output_filename` and `output_stream`"""
  192. def delete_ansi_escape(text: str) -> str:
  193. ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])')
  194. return ansi_escape.sub('', text)
  195. def prepare_for_print(out: bytes) -> str:
  196. # errors='ignore' is here because some chips produce some garbage bytes
  197. result = out.decode(errors='ignore')
  198. if not output_stream.isatty():
  199. # delete escape sequence if we printing in environments where ANSI coloring is disabled
  200. return delete_ansi_escape(result)
  201. return result
  202. def print_progression(output: str) -> None:
  203. # Print a new line on top of the previous line
  204. sys.stdout.write('\x1b[K')
  205. print('\r', end='')
  206. print(fit_text_in_terminal(output.strip('\n\r')), end='', file=output_stream)
  207. try:
  208. with open(output_filename, 'w') as output_file:
  209. while True:
  210. if self.interactive:
  211. out = await input_stream.read(1)
  212. else:
  213. out = await input_stream.readline()
  214. if not out:
  215. break
  216. output = prepare_for_print(out)
  217. output_file.write(output)
  218. # print output in progression way but only the progression related (that started with '[') and if verbose flag is not set
  219. if self.force_progression and output[0] == '[' and '-v' not in self.args and output_stream.isatty():
  220. print_progression(output)
  221. else:
  222. output_stream.write(output)
  223. output_stream.flush()
  224. except (RuntimeError, EnvironmentError) as e:
  225. yellow_print('WARNING: The exception {} was raised and we can\'t capture all your {} and '
  226. 'hints on how to resolve errors can be not accurate.'.format(e, output_stream.name.strip('<>')))
  227. def run_tool(*args: Any, **kwargs: Any) -> None:
  228. # Added in case someone uses run_tool externally in idf.py extensions
  229. return RunTool(*args, **kwargs)()
  230. def run_target(target_name: str, args: 'PropertyDict', env: Optional[Dict]=None,
  231. custom_error_handler: FunctionType=None, force_progression: bool=False, interactive: bool=False) -> None:
  232. """Run target in build directory."""
  233. if env is None:
  234. env = {}
  235. generator_cmd = GENERATORS[args.generator]['command']
  236. env.update(GENERATORS[args.generator]['envvar'])
  237. if args.verbose:
  238. generator_cmd += [GENERATORS[args.generator]['verbose_flag']]
  239. RunTool(generator_cmd[0], generator_cmd + [target_name], args.build_dir, env, custom_error_handler, hints=not args.no_hints,
  240. force_progression=force_progression, interactive=interactive)()
  241. def _strip_quotes(value: str, regexp: re.Pattern=re.compile(r"^\"(.*)\"$|^'(.*)'$|^(.*)$")) -> Optional[str]:
  242. """
  243. Strip quotes like CMake does during parsing cache entries
  244. """
  245. matching_values = regexp.match(value)
  246. return [x for x in matching_values.groups() if x is not None][0].rstrip() if matching_values is not None else None
  247. def _parse_cmakecache(path: str) -> Dict:
  248. """
  249. Parse the CMakeCache file at 'path'.
  250. Returns a dict of name:value.
  251. CMakeCache entries also each have a "type", but this is currently ignored.
  252. """
  253. result = {}
  254. with open(path, encoding='utf-8') as f:
  255. for line in f:
  256. # cmake cache lines look like: CMAKE_CXX_FLAGS_DEBUG:STRING=-g
  257. # groups are name, type, value
  258. m = re.match(r'^([^#/:=]+):([^:=]+)=(.*)\n$', line)
  259. if m:
  260. result[m.group(1)] = m.group(3)
  261. return result
  262. def _new_cmakecache_entries(cache_path: str, new_cache_entries: List) -> bool:
  263. if not os.path.exists(cache_path):
  264. return True
  265. if new_cache_entries:
  266. current_cache = _parse_cmakecache(cache_path)
  267. for entry in new_cache_entries:
  268. key, value = entry.split('=', 1)
  269. current_value = current_cache.get(key, None)
  270. if current_value is None or _strip_quotes(value) != current_value:
  271. return True
  272. return False
  273. def _detect_cmake_generator(prog_name: str) -> Any:
  274. """
  275. Find the default cmake generator, if none was specified. Raises an exception if no valid generator is found.
  276. """
  277. for (generator_name, generator) in GENERATORS.items():
  278. if executable_exists(generator['version']):
  279. return generator_name
  280. raise FatalError("To use %s, either the 'ninja' or 'GNU make' build tool must be available in the PATH" % prog_name)
  281. def ensure_build_directory(args: 'PropertyDict', prog_name: str, always_run_cmake: bool=False) -> None:
  282. """Check the build directory exists and that cmake has been run there.
  283. If this isn't the case, create the build directory (if necessary) and
  284. do an initial cmake run to configure it.
  285. This function will also check args.generator parameter. If the parameter is incompatible with
  286. the build directory, an error is raised. If the parameter is None, this function will set it to
  287. an auto-detected default generator or to the value already configured in the build directory.
  288. """
  289. project_dir = args.project_dir
  290. # Verify the project directory
  291. if not os.path.isdir(project_dir):
  292. if not os.path.exists(project_dir):
  293. raise FatalError('Project directory %s does not exist' % project_dir)
  294. else:
  295. raise FatalError('%s must be a project directory' % project_dir)
  296. if not os.path.exists(os.path.join(project_dir, 'CMakeLists.txt')):
  297. raise FatalError('CMakeLists.txt not found in project directory %s' % project_dir)
  298. # Verify/create the build directory
  299. build_dir = args.build_dir
  300. if not os.path.isdir(build_dir):
  301. os.makedirs(build_dir)
  302. # Parse CMakeCache, if it exists
  303. cache_path = os.path.join(build_dir, 'CMakeCache.txt')
  304. cache = _parse_cmakecache(cache_path) if os.path.exists(cache_path) else {}
  305. # Validate or set IDF_TARGET
  306. _guess_or_check_idf_target(args, prog_name, cache)
  307. args.define_cache_entry.append('CCACHE_ENABLE=%d' % args.ccache)
  308. if always_run_cmake or _new_cmakecache_entries(cache_path, args.define_cache_entry):
  309. if args.generator is None:
  310. args.generator = _detect_cmake_generator(prog_name)
  311. try:
  312. cmake_args = [
  313. 'cmake',
  314. '-G',
  315. args.generator,
  316. '-DPYTHON_DEPS_CHECKED=1',
  317. '-DESP_PLATFORM=1',
  318. ]
  319. if args.cmake_warn_uninitialized:
  320. cmake_args += ['--warn-uninitialized']
  321. if args.define_cache_entry:
  322. cmake_args += ['-D' + d for d in args.define_cache_entry]
  323. cmake_args += [project_dir]
  324. hints = not args.no_hints
  325. RunTool('cmake', cmake_args, cwd=args.build_dir, hints=hints)()
  326. except Exception:
  327. # don't allow partially valid CMakeCache.txt files,
  328. # to keep the "should I run cmake?" logic simple
  329. if os.path.exists(cache_path):
  330. os.remove(cache_path)
  331. raise
  332. # need to update cache so subsequent access in this method would reflect the result of the previous cmake run
  333. cache = _parse_cmakecache(cache_path) if os.path.exists(cache_path) else {}
  334. try:
  335. generator = cache['CMAKE_GENERATOR']
  336. except KeyError:
  337. generator = _detect_cmake_generator(prog_name)
  338. if args.generator is None:
  339. args.generator = (generator) # reuse the previously configured generator, if none was given
  340. if generator != args.generator:
  341. raise FatalError("Build is configured for generator '%s' not '%s'. Run '%s fullclean' to start again." %
  342. (generator, args.generator, prog_name))
  343. try:
  344. home_dir = cache['CMAKE_HOME_DIRECTORY']
  345. if realpath(home_dir) != realpath(project_dir):
  346. raise FatalError(
  347. "Build directory '%s' configured for project '%s' not '%s'. Run '%s fullclean' to start again." %
  348. (build_dir, realpath(home_dir), realpath(project_dir), prog_name))
  349. except KeyError:
  350. pass # if cmake failed part way, CMAKE_HOME_DIRECTORY may not be set yet
  351. def merge_action_lists(*action_lists: Dict) -> Dict:
  352. merged_actions: Dict = {
  353. 'global_options': [],
  354. 'actions': {},
  355. 'global_action_callbacks': [],
  356. }
  357. for action_list in action_lists:
  358. merged_actions['global_options'].extend(action_list.get('global_options', []))
  359. merged_actions['actions'].update(action_list.get('actions', {}))
  360. merged_actions['global_action_callbacks'].extend(action_list.get('global_action_callbacks', []))
  361. return merged_actions
  362. def get_sdkconfig_value(sdkconfig_file: str, key: str) -> Optional[str]:
  363. """
  364. Return the value of given key from sdkconfig_file.
  365. If sdkconfig_file does not exist or the option is not present, returns None.
  366. """
  367. assert key.startswith('CONFIG_')
  368. if not os.path.exists(sdkconfig_file):
  369. return None
  370. # keep track of the last seen value for the given key
  371. value = None
  372. # if the value is quoted, this excludes the quotes from the value
  373. pattern = re.compile(r"^{}=\"?([^\"]*)\"?$".format(key))
  374. with open(sdkconfig_file, 'r') as f:
  375. for line in f:
  376. match = re.match(pattern, line)
  377. if match:
  378. value = match.group(1)
  379. return value
  380. def is_target_supported(project_path: str, supported_targets: List) -> bool:
  381. """
  382. Returns True if the active target is supported, or False otherwise.
  383. """
  384. return get_target(project_path) in supported_targets
  385. def _guess_or_check_idf_target(args: 'PropertyDict', prog_name: str, cache: Dict) -> None:
  386. """
  387. If CMakeCache.txt doesn't exist, and IDF_TARGET is not set in the environment, guess the value from
  388. sdkconfig or sdkconfig.defaults, and pass it to CMake in IDF_TARGET variable.
  389. Otherwise, cross-check the three settings (sdkconfig, CMakeCache, environment) and if there is
  390. mismatch, fail with instructions on how to fix this.
  391. """
  392. # Default locations of sdkconfig files.
  393. # FIXME: they may be overridden in the project or by a CMake variable (IDF-1369).
  394. # These are used to guess the target from sdkconfig, or set the default target by sdkconfig.defaults.
  395. idf_target_from_sdkconfig = get_target(args.project_dir)
  396. idf_target_from_sdkconfig_defaults = get_target(args.project_dir, 'sdkconfig.defaults')
  397. idf_target_from_env = os.environ.get('IDF_TARGET')
  398. idf_target_from_cache = cache.get('IDF_TARGET')
  399. if not cache and not idf_target_from_env:
  400. # CMakeCache.txt does not exist yet, and IDF_TARGET is not set in the environment.
  401. guessed_target = idf_target_from_sdkconfig or idf_target_from_sdkconfig_defaults
  402. if guessed_target:
  403. if args.verbose:
  404. print("IDF_TARGET is not set, guessed '%s' from sdkconfig" % (guessed_target))
  405. args.define_cache_entry.append('IDF_TARGET=' + guessed_target)
  406. elif idf_target_from_env:
  407. # Let's check that IDF_TARGET values are consistent
  408. if idf_target_from_sdkconfig and idf_target_from_sdkconfig != idf_target_from_env:
  409. raise FatalError("Project sdkconfig was generated for target '{t_conf}', but environment variable IDF_TARGET "
  410. "is set to '{t_env}'. Run '{prog} set-target {t_env}' to generate new sdkconfig file for target {t_env}."
  411. .format(t_conf=idf_target_from_sdkconfig, t_env=idf_target_from_env, prog=prog_name))
  412. if idf_target_from_cache and idf_target_from_cache != idf_target_from_env:
  413. raise FatalError("Target settings are not consistent: '{t_env}' in the environment, '{t_cache}' in CMakeCache.txt. "
  414. "Run '{prog} fullclean' to start again."
  415. .format(t_env=idf_target_from_env, t_cache=idf_target_from_cache, prog=prog_name))
  416. elif idf_target_from_cache and idf_target_from_sdkconfig and idf_target_from_cache != idf_target_from_sdkconfig:
  417. # This shouldn't happen, unless the user manually edits CMakeCache.txt or sdkconfig, but let's check anyway.
  418. raise FatalError("Project sdkconfig was generated for target '{t_conf}', but CMakeCache.txt contains '{t_cache}'. "
  419. "To keep the setting in sdkconfig ({t_conf}) and re-generate CMakeCache.txt, run '{prog} fullclean'. "
  420. "To re-generate sdkconfig for '{t_cache}' target, run '{prog} set-target {t_cache}'."
  421. .format(t_conf=idf_target_from_sdkconfig, t_cache=idf_target_from_cache, prog=prog_name))
  422. class TargetChoice(click.Choice):
  423. """
  424. A version of click.Choice with two special features:
  425. - ignores hyphens
  426. - not case sensitive
  427. """
  428. def __init__(self, choices: List) -> None:
  429. super(TargetChoice, self).__init__(choices, case_sensitive=False)
  430. def convert(self, value: Any, param: click.Parameter, ctx: click.Context) -> Any:
  431. def normalize(string: str) -> str:
  432. return string.lower().replace('-', '')
  433. saved_token_normalize_func = ctx.token_normalize_func
  434. ctx.token_normalize_func = normalize
  435. try:
  436. return super(TargetChoice, self).convert(value, param, ctx)
  437. finally:
  438. ctx.token_normalize_func = saved_token_normalize_func
  439. class PropertyDict(dict):
  440. def __getattr__(self, name: str) -> Any:
  441. if name in self:
  442. return self[name]
  443. else:
  444. raise AttributeError("'PropertyDict' object has no attribute '%s'" % name)
  445. def __setattr__(self, name: str, value: Any) -> None:
  446. self[name] = value
  447. def __delattr__(self, name: str) -> None:
  448. if name in self:
  449. del self[name]
  450. else:
  451. raise AttributeError("'PropertyDict' object has no attribute '%s'" % name)