1from os.path import join
2from glob import glob
3
4def find_gpio_base(path="/sys/class/gpio/"):
5    pattern = "gpiochip*"
6    for gc in glob(join(path, pattern)):
7        with open(join(gc, "label")) as f:
8            label = f.readline().strip()
9        if label == "1e780000.gpio":
10            with open(join(gc, "base")) as f:
11                return int(f.readline().strip())
12    # trigger a file not found exception
13    open(join(path, "gpiochip"))
14
15GPIO_BASE = find_gpio_base()
16
17def convertGpio(name):
18    offset = int(filter(str.isdigit, name))
19    port = filter(str.isalpha, name.upper())
20    a = ord(port[-1]) - ord('A')
21    if len(port) > 1:
22        a += 26
23    base = a * 8 + GPIO_BASE
24    return base + offset
25