Compare commits

...

2 Commits

@ -2,4 +2,5 @@
import sys
from nagparse import NagParse
parser = NagParse(sys.argv[1:])
print(parser)
for object in parser.m_objects:
print(object.get_id())

@ -1,3 +1,5 @@
import hashlib
import json
import shlex
from enum import Enum
@ -72,8 +74,38 @@ class NagObject:
wrap(dir[1], ws_base).strip().strip('\\\n').strip())
return string + '}\n'
def from_object(self, object: dict) -> None:
self.m_type.value = object['type']
self.m_config_file = object['config_file']
self.m_directives = object['directives']
self.m_line_no = object['line_number']
def to_object(self) -> dict:
object = {
'type': self.m_type.value,
'config_file': self.m_config_file,
'directives': self.m_directives,
'line_number': self.m_line_no
}
return object
def from_json(self, string: str) -> None:
self.from_object(json.loads(string))
def to_json(self) -> str:
return json.dumps(self.to_object)
def get_type(self) -> NagObjectType:
return self.m_type
def get_directives(self) -> list[list[str]]:
return self.m_directives
def get_config_hash(self) -> int:
hash = hashlib.sha256()
hash.update(self.m_config_file.encode('utf-8'))
digest = hash.hexdigest()
return int(digest,16) * 1000000
def get_id(self) -> int:
return self.get_config_hash() + self.m_line_no

@ -1,5 +1,7 @@
#!/usr/bin/env python3
import re
import json
import hashlib
from enum import Enum, auto
from nag_object import NagObject, NagObjectType
@ -9,19 +11,29 @@ class ParserState(Enum):
DIRECTIVE = auto()
MULTILINE = auto()
def hash_string(string:str) -> int:
hash = hashlib.sha256()
hash.update(string.encode('utf-8'))
digest = hash.hexdigest()
return int(digest,16) * 1000000
class NagParse:
def __init__(self, config_files: list[str]):
self.m_config_files: list[str] = config_files
self.m_config_table: dict[int,str] = dict()
self.m_objects: list[NagObject] = list()
for config_file in self.m_config_files:
for config_file in self.m_config_files:
self.m_config_table[hash_string(config_file)] = config_file
for object in self.parse_config(config_file):
self.m_objects.append(object)
def __str__(self):
string = str()
for object in self.m_objects:
string += str(object) + "\n"
return string
def to_json(self):
return json.dumps([x.to_object() for x in self.m_objects])
def parse_config(self,config_file: str) -> list[NagObject]:
state = ParserState.DEFINE

Loading…
Cancel
Save