Просмотр исходного кода

Optimisation: use scandir rather than walk to find config files

This reduces the time for running the device_config unit tests from
7.3s to 5.9s. More importantly, aiofile has a version of this, while it
does not have a version of the older less efficient walk, so we can convert
the file I/O to async in future.
Jason Rumney 1 год назад
Родитель
Сommit
9f3c01bf8a
1 измененных файлов с 4 добавлено и 5 удалено
  1. 4 5
      custom_components/tuya_local/helpers/device_config.py

+ 4 - 5
custom_components/tuya_local/helpers/device_config.py

@@ -8,7 +8,7 @@ from collections.abc import Sequence
 from datetime import datetime
 from fnmatch import fnmatch
 from numbers import Number
-from os import walk
+from os import scandir
 from os.path import dirname, exists, join, splitext
 
 from homeassistant.util import slugify
@@ -1051,10 +1051,9 @@ def available_configs():
     """List the available config files."""
     _CONFIG_DIR = dirname(config_dir.__file__)
 
-    for path, dirs, files in walk(_CONFIG_DIR):
-        for basename in sorted(files):
-            if fnmatch(basename, "*.yaml"):
-                yield basename
+    for direntry in scandir(_CONFIG_DIR):
+        if direntry.is_file() and fnmatch(direntry.name, "*.yaml"):
+            yield direntry.name
 
 
 def possible_matches(dps, product_ids=None):