Add has and id
This commit is contained in:
parent
47198413e2
commit
d2952ffab1
3 changed files with 24 additions and 2 deletions
|
@ -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
|
||||
|
||||
|
@ -77,3 +79,12 @@ class NagObject:
|
|||
|
||||
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,14 +11,22 @@ 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:
|
||||
|
|
Loading…
Add table
Reference in a new issue