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" HostEscalation = "hostescalation" HostGroup = "hostgroup" Host = "host" ServiceDependency = "servicedependency" ServiceEscalation = "serviceescalation" ServiceGroup = "servicegroup" Service = "service" TimePeriod = "timeperiod" Nil = "nil" class NagObject: def __init__(self, config_file: str = str(), directives: list[list[str]] = list(), line_no: int = 0, type: NagObjectType = NagObjectType.Nil): self.m_config_file: str = config_file self.m_directives: list[list[str]] = directives self.m_line_no: int = line_no self.m_type: NagObjectType = type def __str__(self): 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: 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 def get_directives(self) -> list[list[str]]: return self.m_directives