-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created initial validator for JSON files, and associated validator pr…
…ogram
- Loading branch information
1 parent
e417180
commit ae3675d
Showing
6 changed files
with
89 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|