]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/alias.py
Added missing module docstring and ran pre-commit
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / alias.py
1 # SPDX-FileCopyrightText: 2023 Steve Jeong for Hardkernel
2 #
3 # SPDX-License-Identifier: MIT
4
5 """
6 Device Tree Alias Functions
7 """
8
9 from typing import Optional
10 import os
11 import re
12
13
14 def get_dts_alias(device: str) -> Optional[str]:
15     """Get the Device Tree Alias"""
16     uevent_path = "/sys/bus/platform/devices/" + device + "/uevent"
17     if os.path.exists(uevent_path):
18         with open(uevent_path, "r", encoding="utf-8") as fd:
19             pattern = r"^OF_ALIAS_0=(.*)$"
20             uevent = fd.read().split("\n")
21             for line in uevent:
22                 match = re.search(pattern, line)
23                 if match:
24                     return match.group(1).upper()
25     return None