From 2ec088f4b8134f68a1851c1b524c4996c2dfc05d Mon Sep 17 00:00:00 2001 From: Dmitriy Yefremov Date: Sat, 7 Oct 2017 23:24:56 +0300 Subject: [PATCH] parsing lamedb for services --- main/eparser/lamedb.py | 47 +++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/main/eparser/lamedb.py b/main/eparser/lamedb.py index 111eca9f..fb08107e 100644 --- a/main/eparser/lamedb.py +++ b/main/eparser/lamedb.py @@ -1,19 +1,46 @@ from collections import namedtuple -Channel = namedtuple("Channel", ["name", "service", "freq", "fec"]) +Channel = namedtuple("Channel", ["service", "package", "freq", "fec"]) -FILE_PATH = "files/lamedb_example" - -with open(FILE_PATH) as file: - lines = file.readlines() +FILE_PATH = "../data/lamedb_example" -for l in lines: - l.split() +def parse(path): + with open(path, "r") as file: + data = str(file.read()) + services, sep, transponders = data.partition("services") # 1 step + transponders, sep, services = transponders.partition("transponders") # 2 step + transponders, sep, services = services.partition("end") # 3 step + services = services.split("\n") -print(lines) + while len(services) % 3 == 0: + services.append("\n") + + srv = [] + tmp = [] + for i, line in enumerate(services): + tmp.append(line) + if i % 3 == 0: + srv.append(tuple(tmp)) + tmp.clear() + + if srv[0][0] == "": # remove first empty element + srv.remove(srv[0]) + + if srv[0][0] == "services": # building one element from first and last if necessary! + first = srv.pop(0) + last = srv.pop() + srv.insert(0, (last[0], first[1], first[2])) + + channels = [] + for ch in srv: + pack = str(ch[0]) + pack = pack[2:pack.find(",")] + channels.append(Channel(ch[2], pack, None, None)) + + for ch in channels: + print(ch) if __name__ == "__main__": - pass - + parse(FILE_PATH)