first parser
Some checks failed
/ test (push) Has been cancelled

This commit is contained in:
Michaël Ricart 2025-02-14 21:11:21 +01:00
parent 3bf3d65ecc
commit 28bbaf97b0
7 changed files with 57 additions and 11 deletions

View file

@ -5,6 +5,7 @@ from os.path import isfile, join
from debian import parse_debian
from freebsd import parse_freebsd
from freebox import parse_freebox
from jinja2 import Environment, FileSystemLoader
def find_os(content):
version = content.split("###### VERSION ######")[-1]
@ -19,17 +20,27 @@ def main():
mypath = "../oxidized/"
onlyfiles = [mypath + f for f in listdir(mypath) if isfile(join(mypath, f))]
env = Environment(loader = FileSystemLoader('../template'))
for file in onlyfiles:
with open(file,"r") as f:
content = f.read()
current_os = find_os(content)
if "freebsd" in current_os:
parse_freebsd(content)
infos = parse_freebsd(content)
template = env.get_template('freebsd.rst.j2')
output = template.render(**infos)
print(output)
elif "debian" in current_os:
parse_debian(content)
infos = parse_debian(content)
template = env.get_template('debian.rst.j2')
output = template.render(**infos)
print(output)
else:
parse_freebox(content)
with open("../source/devices/{host}.rst".format(host=infos["hostname"].replace("\n","")), "w") as f:
f.write(output)
if __name__ == "__main__":
main()

View file

@ -22,9 +22,9 @@ def parse_debian(content):
result[current_section] = ""
else:
if current_section:
result[current_section] += line
print(result["hostname"])
print(parse_ip(result["ip"]))
result[current_section] += line + "\n"
result["ip"] = parse_ip(result["ip"])
return result
if __name__ == "__main__":
main()

View file

@ -1,5 +1,16 @@
#!/bin/env python3
def parse_ip(block):
result = {}
current_interface = ""
for line in block.splitlines():
if not line.startswith("\t"):
current_interface= line.split(":")[0]
result[current_interface] = []
elif line.strip().startswith("inet"):
result[current_interface].append(line.strip().split(" ")[1])
return result
def parse_freebsd(content):
current_section = ""
result = {}
@ -9,9 +20,11 @@ def parse_freebsd(content):
result[current_section] = ""
else:
if current_section:
result[current_section] += line
result[current_section] += line + "\n"
from pprint import pprint
#pprint(result)
result["ip"] = parse_ip(result["interfaces"])
return result
if __name__ == "__main__":
main()

View file

@ -7,7 +7,10 @@ Doc Infra
=========
.. toctree::
:glob:
:maxdepth: 2
:caption: Contents:
devices/*
test

View file

@ -1,4 +1,4 @@
{{ "=" * (host|length) }}
{{ host }}
{{ "=" * (host|length) }}
{{ "=" * (hostname|length) }}
{{ hostname.replace("\n", "") }}
{{ "=" * (hostname|length) }}

12
template/debian.rst.j2 Normal file
View file

@ -0,0 +1,12 @@
{% include "base.rst.j2" %}
network
-------
ip
##
========= ==============
interface adresses
========= =============={% for interface in ip %}
{{ "{:<10}".format(interface["ifname"]) }} {% for ip in interface["addr_info"] %} {{ ip["local"] }}/{{ ip["prefixlen"]}}{% endfor %}
{% endfor %}========= ==============

View file

@ -1,3 +1,10 @@
{% include "base.rst.j2" %}
services
--------
network
=======
========= ==========
interface ip
========= =========={% for interface in ip %}
{{ "{:<10}".format(interface.replace("\n","")) }} {% for addr in ip[interface] %} {{ addr.replace("\n","") }}{% endfor %}{% endfor %}
========= ==========