Compare commits

...

3 Commits

@ -1,8 +1,37 @@
import shlex
from enum import Enum
def wrap(input: str, ws_base, max_length=70) -> str:
output: str = str()
if len(input) < max_length:
return input
temp = shlex.split(input, posix=False)
length = len(temp[0])
whitespace = str()
for _ in range(0, ws_base):
whitespace += " "
separator = " \\\n " + whitespace
for i in range(0, len(temp)):
if i < len(temp) - 1:
if length + len(temp[i]) < max_length:
output += " " + temp[i]
length += len(temp[i + 1])
else:
output += separator + temp[i]
length = len(temp[i]) + len(" \\\n\t\t")
else:
if length < max_length - len(temp[i]):
output += " " + temp[i]
else:
output += separator + temp[i]
return output
class NagObjectType(Enum):
Command = "command"
Comment = "comment"
ContactGroup = "contactgroup"
Contact = "contact"
HostDependency = "hostdependency"
@ -18,6 +47,7 @@ class NagObjectType(Enum):
class NagObject:
def __init__(self,
config_file: str = str(),
directives: list[list[str]] = list(),
@ -29,11 +59,18 @@ class NagObject:
self.m_type: NagObjectType = type
def __str__(self):
string = "{}:\n\t{}: {}\n".format(self.m_config_file, self.m_line_no,
self.m_type)
string = 'define {}'.format(self.m_type.value) + ' {\n'
ws_base = len(max([x[0] for x in self.m_directives], key=len)) + 4
for dir in self.m_directives:
string += "\t\t{}: {}\n".format(dir[0], dir[1])
return string
num_whitespace = ws_base - len(dir[0])
whitespace = str()
for _ in range(0, num_whitespace):
whitespace += " "
string += ' {}{}{}\n'.format(
dir[0], whitespace,
wrap(dir[1], ws_base).strip().strip('\\\n').strip())
return string + '}\n'
def get_type(self) -> NagObjectType:
return self.m_type

@ -4,6 +4,7 @@ from enum import Enum, auto
from nag_object import NagObject, NagObjectType
class ParserState(Enum):
COMMENT = auto()
DEFINE = auto()
DIRECTIVE = auto()
MULTILINE = auto()
@ -12,71 +13,14 @@ class ParserState(Enum):
class NagParse:
def __init__(self, config_files: list[str]):
self.m_config_files: list[str] = config_files
self.m_commands: list[NagObject] = list()
self.m_contacts: list[NagObject] = list()
self.m_contactgroups: list[NagObject] = list()
self.m_hosts: list[NagObject] = list()
self.m_hostdependencies: list[NagObject] = list()
self.m_hostescalations: list[NagObject] = list()
self.m_hostgroups: list[NagObject] = list()
self.m_services: list[NagObject] = list()
self.m_servicedependencies: list[NagObject] = list()
self.m_serviceescalations: list[NagObject] = list()
self.m_servicegroups: list[NagObject] = list()
self.m_timeperiods: list[NagObject] = list()
self.m_objects: list[NagObject] = list()
for config_file in self.m_config_files:
for object in self.parse_config(config_file):
match object.m_type:
case NagObjectType.Command:
self.m_commands.append(object)
case NagObjectType.Contact:
self.m_contacts.append(object)
case NagObjectType.ContactGroup:
self.m_contactgroups.append(object)
case NagObjectType.Host:
self.m_hosts.append(object)
case NagObjectType.HostDependency:
self.m_hostdependencies.append(object)
case NagObjectType.HostEscalation:
self.m_hostescalations.append(object)
case NagObjectType.HostGroup:
self.m_hostgroups.append(object)
case NagObjectType.Service:
self.m_services.append(object)
case NagObjectType.ServiceDependency:
self.m_servicedependencies.append(object)
case NagObjectType.ServiceEscalation:
self.m_serviceescalations.append(object)
case NagObjectType.ServiceGroup:
self.m_servicegroups.append(object)
case NagObjectType.TimePeriod:
self.m_timeperiods.append(object)
self.m_objects.append(object)
def __str__(self):
string = str()
for fragment in self.m_commands:
string += str(fragment) + "\n"
for fragment in self.m_contacts:
string += str(fragment) + "\n"
for fragment in self.m_contactgroups:
string += str(fragment) + "\n"
for fragment in self.m_hosts:
string += str(fragment) + "\n"
for fragment in self.m_hostdependencies:
string += str(fragment) + "\n"
for fragment in self.m_hostescalations:
string += str(fragment) + "\n"
for fragment in self.m_hostgroups:
string += str(fragment) + "\n"
for fragment in self.m_services:
string += str(fragment) + "\n"
for fragment in self.m_servicedependencies:
string += str(fragment) + "\n"
for fragment in self.m_serviceescalations:
string += str(fragment) + "\n"
for fragment in self.m_servicegroups:
string += str(fragment) + "\n"
for fragment in self.m_timeperiods:
string += str(fragment) + "\n"
for object in self.m_objects:
string += str(object) + "\n"
return string
def parse_config(self,config_file: str) -> list[NagObject]:

Loading…
Cancel
Save