diff options
author | Jacob Appelbaum <jacob@appelbaum.net> | 2011-08-17 18:03:43 +0200 |
---|---|---|
committer | Jacob Appelbaum <jacob@appelbaum.net> | 2011-08-17 18:03:43 +0200 |
commit | bb4a0df747092bdc0fac0baa0658bf1d314a9de8 (patch) | |
tree | 7c41a9eb66549ad17c0e7e4c06cae19c1ac54916 /packages/torouter-web/src/tui/utils/parsing.py | |
parent | 24b92ab9b034d6c957631a034a717ba41809a335 (diff) | |
download | torouter-bb4a0df747092bdc0fac0baa0658bf1d314a9de8.tar.gz torouter-bb4a0df747092bdc0fac0baa0658bf1d314a9de8.zip |
Move source into src dir
Diffstat (limited to 'packages/torouter-web/src/tui/utils/parsing.py')
-rw-r--r-- | packages/torouter-web/src/tui/utils/parsing.py | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/packages/torouter-web/src/tui/utils/parsing.py b/packages/torouter-web/src/tui/utils/parsing.py new file mode 100644 index 0000000..e9ccc83 --- /dev/null +++ b/packages/torouter-web/src/tui/utils/parsing.py @@ -0,0 +1,83 @@ +# These functions are for parsing /etc/network/interface +# files, they will be used inside torouter to visualize +# and edit configuration +import os + +class interfaces: + def __init__(self,filename): + self.fp = open(filename, "r") + self.wifi = {} + self.eth1 = {} + self.eth0 = {} + + def parse_line(self, line, iface): + name = line.split(" ")[0] + values = " ".join(line.split(" ")[1:]).rstrip() + if iface == "uap0": + if self.wifi.has_key(name): + if type(self.wifi[name]) is list: + self.wifi[name].append(values) + else: + self.wifi[name] = [self.wifi[name],values] + else: + self.wifi.update({name : values}) + elif iface == "eth1": + if self.eth1.has_key(name): + if type(self.eth1[name]) is list: + self.eth1[name].append(values) + else: + self.eth1[name] = [self.eth1[name],values] + else: + self.eth1.update({name : values}) + elif iface == "eth0": + if self.eth0.has_key(name): + if type(self.eth0[name]) is list: + self.eth0[name].append(values) + else: + self.eth0[name] = [self.eth0[name],values] + else: + self.eth0.update({name : values}) + + def parse(self): + iface = None + for line in self.fp.readlines(): + line = line.lstrip() + if line.startswith("#") or line == "": + continue + if line.startswith("iface"): + iface = line.split(" ")[1] + if iface: + self.parse_line(line, iface) + + def html_output(self, data): + output = "<h3>Interface %s</h3>\n" % data['iface'].split(" ")[0] + output += "<table class=\"interface\" id=\"%s\">\n" % data['iface'].split(" ")[0] + + for item in data.items(): + if item[0] != "iface": + if type(item[1]) is list: + for i in item[1]: + output += "<tr><td>%s</td><td>%s</td></tr>\n" % (item[0], i) + else: + output += "<tr><td>%s</td><td>%s</td></tr>\n" % (item[0],item[1]) + output += "</table>" + print output + return output + + def output(self, data): + print "iface %s" % data['iface'] + for item in data.items(): + if item[0] != "iface": + if type(item[1]) is list: + for i in item[1]: + print "%s %s" % (item[0], i) + else: + print "%s %s" % (item[0],item[1]) + +#interfaces_file = os.getcwd() + "/../../../torouter-prep/configs/interfaces" +#itfc = interfaces(interfaces_file) +#itfc.parse() +#itfc.html_output(itfc.wifi) +#itfc.html_output(itfc.eth1) +#itfc.html_output(itfc.eth0) + |