Skip to content

Commit

Permalink
Created initial validator for JSON files, and associated validator pr…
Browse files Browse the repository at this point in the history
…ogram
  • Loading branch information
idarthjedi committed Feb 23, 2023
1 parent e417180 commit ae3675d
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 33 deletions.
13 changes: 10 additions & 3 deletions dayz_admin_tools/utilities/files/fManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@
class FileManager(ABC):

@abstractmethod
def __init__(self, pathname: str):
self._pathname = pathname
def __init__(self):
"""
Creates a copy of the File Manager
"""
pass

@staticmethod
@abstractmethod
def validate(self):
def validate_files(filepath: str) -> tuple[bool, list]:
"""
:return: True if all files validated, otherwise false
"""
pass
37 changes: 27 additions & 10 deletions dayz_admin_tools/utilities/files/json.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
from . fManager import FileManager
from dayz_admin_tools.utilities.files.fManager import FileManager
import json
import os
"""JSON File Manager
Creates an Instance of a JSON File for various Management functions
File Manager for JSON objects
"""


class JSONManager(FileManager):
@staticmethod
def validate_files(filepath: str) -> tuple[bool, list]:
"""
def __init__(self, pathname: str):
super().__init__(pathname)
pass
:param filepath: location of the directories & subdirectories to search for JSON files
:return: Tuple (SUCCESS_STATUS, FAILED_FILES)
"""

def validate(self):
print(f"Validating {self._pathname} JSON file!")
pass
validated = True
output = []

for root, dirs, files in os.walk(filepath):
for file in files:
if file.endswith(".json") and not file.startswith("."):
filename = os.path.join(root, file)
with open(filename) as validate_file:
try:
json.load(validate_file)
except json.JSONDecodeError as error:
output.append(filename)
validated = False

if __name__ == "__main__":
file1 = JSONManager("file.json")
return validated, output

def __init__(self):
super().__init__()


if __name__ == "__main__":
pass
16 changes: 8 additions & 8 deletions dayz_admin_tools/utilities/files/xml.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
from . fManager import FileManager
from dayz_admin_tools.utilities.files.fManager import FileManager

"""XML File Manager
Creates an Instance of an XML File for various Management functions
Creates an Instance of a XML File for various Management functions
"""


class XMLManager(FileManager):

def __init__(self, pathname: str):
super().__init__(pathname)
@staticmethod
def validate_files(filepath: str) -> tuple[bool, list]:
pass

def validate(self):
print(f"Validating {self._pathname} XML file!")
pass
def __init__(self):
super().__init__()


if __name__ == "__main__":
file1 = XMLManager("file.xml")
pass
12 changes: 0 additions & 12 deletions main.py

This file was deleted.

6 changes: 6 additions & 0 deletions tests/test_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from unittest import TestCase


class TestJSONManager(TestCase):
def test_validate(self):
self.fail()
38 changes: 38 additions & 0 deletions validator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import sys

from dayz_admin_tools.utilities.files.json import JSONManager
import argparse


def validate_json(root_path: str):
resp, files = JSONManager.validate_files(root_path)
if resp:
print(f"All JSON Files validated in path {root_path}\n")
else:
print(f"\nThe following files failed JSON validation:")
[print(err) for err in files]


parser = argparse.ArgumentParser(prog="validator.py",
description="Quick file validator for JSON and XML files",
)
parser.add_argument("-t", "--type", choices=['JSON', 'XML'], default='XML',
help="Specify whether you want to validate JSON or XML(Default)",
action="store")
parser.add_argument("-d", "--dir",
help="Specify a directory to search for files to validate."\
"Multiple directories can be added using spaces between them",
action="append")

if len(sys.argv) < 2:
parser.print_help()

args = parser.parse_args()

match args.type:
case "JSON":
for x in args.dir:
validate_json(x)
case "XML" | _:
pass

0 comments on commit ae3675d

Please sign in to comment.