diff --git a/usr/lib/co2monitor/python/co2monitor/service.py b/usr/lib/co2monitor/python/co2monitor/service.py index 4c4cdc3..e9e818e 100644 --- a/usr/lib/co2monitor/python/co2monitor/service.py +++ b/usr/lib/co2monitor/python/co2monitor/service.py @@ -14,6 +14,7 @@ from gi.repository import GLib from . import device +from . import utils # define names CO2MONITOR_BUSNAME = "de.nobodyinperson.co2monitor" diff --git a/usr/lib/co2monitor/python/co2monitor/utils.py b/usr/lib/co2monitor/python/co2monitor/utils.py new file mode 100644 index 0000000..b6f6c43 --- /dev/null +++ b/usr/lib/co2monitor/python/co2monitor/utils.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +import os +import re + +# split a path into its components +def splitpath(path): + res = [] + # split the path + while True: + base, subject = os.path.split(path) + if path == base: + break + else: + if subject: res.append(subject) + path = base + res.reverse() + return res + + +# convert a device file path to a DBus object name last part +def devicefile2objectname(devicefile): + p = [x.replace("-","--") for x in splitpath(devicefile)] + return "-".join(p) + + +# # convert a DBus object name last part to a device file path +# def objectname2devicefile(objectname): +# s = objectname.split("-") +# print("full split of -: {}".format(s)) +# res = ["/"] +# c = False +# for e in s: +# print("element is '{}'".format(e)) +# if e: +# if c: +# print("appending element '{}' to last element of res {}".format(e,res[len(res)-1],res)) +# res[len(res)-1] = "{}{}".format(res[len(res)-1],e) +# c = False +# else: +# print("appending element '{}' to res {}".format(e,res)) +# res.append(e) +# else: +# print("appending a '-' to last element '{}' of res {}".format(res[len(res)-1],res)) +# res[len(res)-1] = "{}-".format(res[len(res)-1]) +# c = True +# print("---------------") +# return os.path.join(*res)