Compare commits
No commits in common. "47198413e2a3dc89461d80fd2d0322f99834881c" and "c2ee6e7a6603a9b67b2679e78e984d4c3400256f" have entirely different histories.
47198413e2
...
c2ee6e7a66
2 changed files with 65 additions and 46 deletions
|
@ -1,37 +1,8 @@
|
||||||
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"
|
||||||
|
@ -47,7 +18,6 @@ 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(),
|
||||||
|
@ -59,18 +29,11 @@ class NagObject:
|
||||||
self.m_type: NagObjectType = type
|
self.m_type: NagObjectType = type
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
string = 'define {}'.format(self.m_type.value) + ' {\n'
|
string = "{}:\n\t{}: {}\n".format(self.m_config_file, self.m_line_no,
|
||||||
ws_base = len(max([x[0] for x in self.m_directives], key=len)) + 4
|
self.m_type)
|
||||||
for dir in self.m_directives:
|
for dir in self.m_directives:
|
||||||
num_whitespace = ws_base - len(dir[0])
|
string += "\t\t{}: {}\n".format(dir[0], dir[1])
|
||||||
|
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,7 +4,6 @@ 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()
|
||||||
|
@ -13,14 +12,71 @@ 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_objects: list[NagObject] = list()
|
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()
|
||||||
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):
|
||||||
self.m_objects.append(object)
|
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)
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
string = str()
|
string = str()
|
||||||
for object in self.m_objects:
|
for fragment in self.m_commands:
|
||||||
string += str(object) + "\n"
|
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"
|
||||||
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