Compare commits
3 commits
c2ee6e7a66
...
47198413e2
Author | SHA1 | Date | |
---|---|---|---|
|
47198413e2 | ||
|
472667f1f1 | ||
|
c7bfb5d2db |
2 changed files with 46 additions and 65 deletions
|
@ -1,8 +1,37 @@
|
||||||
|
import shlex
|
||||||
from enum import Enum
|
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):
|
class NagObjectType(Enum):
|
||||||
Command = "command"
|
Command = "command"
|
||||||
|
Comment = "comment"
|
||||||
ContactGroup = "contactgroup"
|
ContactGroup = "contactgroup"
|
||||||
Contact = "contact"
|
Contact = "contact"
|
||||||
HostDependency = "hostdependency"
|
HostDependency = "hostdependency"
|
||||||
|
@ -18,6 +47,7 @@ class NagObjectType(Enum):
|
||||||
|
|
||||||
|
|
||||||
class NagObject:
|
class NagObject:
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
config_file: str = str(),
|
config_file: str = str(),
|
||||||
directives: list[list[str]] = list(),
|
directives: list[list[str]] = list(),
|
||||||
|
@ -29,11 +59,18 @@ class NagObject:
|
||||||
self.m_type: NagObjectType = type
|
self.m_type: NagObjectType = type
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
string = "{}:\n\t{}: {}\n".format(self.m_config_file, self.m_line_no,
|
string = 'define {}'.format(self.m_type.value) + ' {\n'
|
||||||
self.m_type)
|
ws_base = len(max([x[0] for x in self.m_directives], key=len)) + 4
|
||||||
for dir in self.m_directives:
|
for dir in self.m_directives:
|
||||||
string += "\t\t{}: {}\n".format(dir[0], dir[1])
|
num_whitespace = ws_base - len(dir[0])
|
||||||
return string
|
|
||||||
|
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:
|
def get_type(self) -> NagObjectType:
|
||||||
return self.m_type
|
return self.m_type
|
||||||
|
|
|
@ -4,6 +4,7 @@ from enum import Enum, auto
|
||||||
from nag_object import NagObject, NagObjectType
|
from nag_object import NagObject, NagObjectType
|
||||||
|
|
||||||
class ParserState(Enum):
|
class ParserState(Enum):
|
||||||
|
COMMENT = auto()
|
||||||
DEFINE = auto()
|
DEFINE = auto()
|
||||||
DIRECTIVE = auto()
|
DIRECTIVE = auto()
|
||||||
MULTILINE = auto()
|
MULTILINE = auto()
|
||||||
|
@ -12,71 +13,14 @@ class ParserState(Enum):
|
||||||
class NagParse:
|
class NagParse:
|
||||||
def __init__(self, config_files: list[str]):
|
def __init__(self, config_files: list[str]):
|
||||||
self.m_config_files: list[str] = config_files
|
self.m_config_files: list[str] = config_files
|
||||||
self.m_commands: list[NagObject] = list()
|
self.m_objects: 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()
|
|
||||||
for config_file in self.m_config_files:
|
for config_file in self.m_config_files:
|
||||||
for object in self.parse_config(config_file):
|
for object in self.parse_config(config_file):
|
||||||
match object.m_type:
|
self.m_objects.append(object)
|
||||||
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)
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
string = str()
|
string = str()
|
||||||
for fragment in self.m_commands:
|
for object in self.m_objects:
|
||||||
string += str(fragment) + "\n"
|
string += str(object) + "\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"
|
|
||||||
return string
|
return string
|
||||||
|
|
||||||
def parse_config(self,config_file: str) -> list[NagObject]:
|
def parse_config(self,config_file: str) -> list[NagObject]:
|
||||||
|
|
Loading…
Add table
Reference in a new issue