]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/alias.py
7347242fbccf283e1add4baca87d0c5e89ab2658
[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 from typing import Optional
6 import os
7 import re
8
9 def get_dts_alias(device: str) -> Optional[str]:
10     """Get the Device Tree Alias"""
11     uevent_path = "/sys/bus/platform/devices/" + device + "/uevent"
12     if os.path.exists(uevent_path):
13         with open(uevent_path, "r", encoding="utf-8") as fd:
14             pattern = r"^OF_ALIAS_0=(.*)$"
15             uevent = fd.read().split("\n")
16             for line in uevent:
17                 match = re.search(pattern, line)
18                 if match:
19                     return match.group(1).upper()
20     return None