Skip to content

Commit

Permalink
Merge pull request #372 from BDadmehr0/python
Browse files Browse the repository at this point in the history
Refactor & Modular /config python files
  • Loading branch information
BaseMax authored Nov 30, 2024
2 parents c415c07 + 6d99977 commit be893b2
Show file tree
Hide file tree
Showing 13 changed files with 307 additions and 261 deletions.
31 changes: 11 additions & 20 deletions config/language.validate.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
import yaml
from typing import Dict, List

FILE = "language.yaml"

file = open(FILE, "r", encoding="utf-8")
docs = yaml.safe_load_all(file)
from module.utils import error, load_yaml
from module.validation import validate_item_structure

FILE = "language.yaml"
LANGUAGES = ["en", "fa"]

def error(msg: str) -> None:
print("Error: " + msg)
exit(1)

docs: List[Dict] = load_yaml(FILE)

if __name__ == "__main__":
for doc in docs:
for item in doc["items"]:
if "id" not in item:
print(item)
error("id is required and missed in an item")

if "name" not in item:
print(item)
error("name is required and missed in an item")

if "local_name" not in item:
for item in doc.get("items", []):
try:
validate_item_structure(item, LANGUAGES)
except ValueError as e:
print(item)
error("local_name is required and missed in an item")
error(str(e))

print(FILE + ": Validation is successful")
44 changes: 23 additions & 21 deletions config/layout/attribute/style/global_value.validate.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import yaml
import os
import sys

FILE = "global_value.yaml"
sys.path.append(
os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../module"))
)

from utils import error, load_yaml
from validation import validate_item_structure

FILE = os.path.join(os.path.dirname(__file__), "global_value.yaml")
LANGUAGES = ["en", "fa"]

file = open(FILE, "r", encoding="utf-8")
docs = yaml.safe_load_all(file)

if __name__ == "__main__":
try:
docs = load_yaml(FILE)

def error(msg: str) -> None:
print("Error: " + msg)
exit(1)
for doc in docs:
for item in doc.get("items", []):
try:
validate_item_structure(item, LANGUAGES)
except ValueError as e:
print(item)
error(str(e))

print(FILE + ": Validation is successful")

if __name__ == "__main__":
for doc in docs:
for item in doc["items"]:
if "id" not in item:
print(item)
error("id is required and missed in an item")

if "text" in item:
for lang in LANGUAGES:
if lang not in item["text"]:
print(item)
error("text is required for " + lang + " language")

print(FILE + ": Validation is successful")
except Exception as e:
error(f"An unexpected error occurred: {e}")
45 changes: 21 additions & 24 deletions config/layout/attribute/style/state.validate.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
import yaml
import os
import sys

FILE = "global_value.yaml"
LANGUAGES = ["en", "fa"]

file = open(FILE, "r", encoding="utf-8")
docs = yaml.safe_load_all(file)
sys.path.append(
os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../module"))
)

from utils import error, load_yaml
from validation import validate_item_structure

def error(msg: str) -> None:
print("Error: " + msg)
exit(1)

FILE = os.path.join(os.path.dirname(__file__), "state.yaml")
LANGUAGES = ["en", "fa"]

if __name__ == "__main__":
for doc in docs:
for item in doc["items"]:
if "id" not in item:
print(item)
error("id is required and missed in an item")
try:
docs = load_yaml(FILE)

if "generate_name" not in item:
print(item)
error("generate_name is required")
for doc in docs:
for item in doc.get("items", []):
try:
validate_item_structure(item, LANGUAGES)
except ValueError as e:
print(item)
error(str(e))

if "text" in item:
for lang in LANGUAGES:
if lang not in item["text"]:
print(item)
error("text is required for " + lang + " language")
print(FILE + ": Validation is successful")

print(FILE + ": Validation is successful")
except Exception as e:
error(f"An unexpected error occurred: {e}")
60 changes: 21 additions & 39 deletions config/layout/attribute/style/type.validate.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,29 @@
import yaml
import os
import sys

FILE = "type.yaml"
LANGUAGES = ["en", "fa"]

file = open(FILE, "r", encoding="utf-8")
docs = yaml.safe_load_all(file)
sys.path.append(
os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../module"))
)

from utils import error, load_yaml
from validation import validate_item_structure

def error(msg: str) -> None:
print("Error: " + msg)
exit(1)

FILE = "type.yaml"
LANGUAGES = ["en", "fa"]

if __name__ == "__main__":
for doc in docs:
for item in doc["items"]:
if "id" not in item:
print(item)
error("id is required and missed in an item")

if "type" not in item:
print(item)
error("type is required")

if "generate_name" in item:
if "reserved_values" not in item:
print(item)
error("reserved_values is required")
if "text" not in item:
print(item)
error("text is required")
else:
if "reserved_values" in item:
print(item)
error("Why reserved_values exists for a non-generate_name item?")
if "text" in item:
try:
docs = load_yaml(FILE)

for doc in docs:
for item in doc.get("items", []):
try:
validate_item_structure(item, LANGUAGES)
except ValueError as e:
print(item)
error("Why text exists for a non-generate_name item?")
error(str(e))

if "text" in item:
for lang in LANGUAGES:
if lang not in item["text"]:
print(item)
error("text is required for " + lang + " language")
print(FILE + ": Validation is successful")

print(FILE + ": Validation is successful")
except Exception as e:
error(f"An unexpected error occurred: {e}")
48 changes: 21 additions & 27 deletions config/layout/attribute/style/value.validate.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
import yaml
import os
import sys

sys.path.append(
os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../module"))
)

from utils import error, load_yaml
from validation import validate_item_structure

FILE = "value.yaml"
LANGUAGES = ["en", "fa"]

file = open(FILE, "r", encoding="utf-8")
docs = yaml.safe_load_all(file)
if __name__ == "__main__":
try:
docs = load_yaml(FILE)

for doc in docs:
for key in doc.get("items", {}):
items = doc["items"][key]

def error(msg: str) -> None:
print("Error: " + msg)
exit(1)
if items is not None:
for item in items:
validate_item_structure(item, LANGUAGES)

print(FILE + ": Validation is successful")

if __name__ == "__main__":
for doc in docs:
for key in doc["items"]:
items = doc["items"][key]

if items is not None:
for item in items:
if "generate_name" not in item:
print(item)
error("generate_name is required")

if "text" not in item:
print(item)
error("text is required")

for lang in LANGUAGES:
if lang not in item["text"]:
print(item)
error("text is required for " + lang + " language")

print(FILE + ": Validation is successful")
except Exception as e:
error(f"An unexpected error occurred: {e}")
45 changes: 25 additions & 20 deletions config/layout/attribute/type.validate.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
import yaml
import os
import sys

sys.path.append(
os.path.abspath(os.path.join(os.path.dirname(__file__), "../..", "module"))
)

from utils import error, load_yaml
from validation import validate_item_structure

FILE = "type.yaml"
LANGUAGES = ["en", "fa"]

file = open(FILE, "r", encoding="utf-8")
docs = yaml.safe_load_all(file)

def main() -> None: # Add the return type annotation here
try:
docs = load_yaml(FILE)

for doc in docs:
for item in doc.get("items", []):
try:
validate_item_structure(item, LANGUAGES)
except ValueError as e:
print(item)
error(str(e))

print(FILE + ": Validation is successful")

def error(msg: str) -> None:
print("Error: " + msg)
exit(1)
except Exception as e:
error(f"An unexpected error occurred: {e}")


if __name__ == "__main__":
for doc in docs:
for item in doc["items"]:
if "id" not in item:
print(item)
error("id is required and missed in an item")

if "generate_name" not in item:
if "text" in item:
for lang in LANGUAGES:
if lang not in item["text"]:
print(item)
error("text is required for " + lang + " language")

print(FILE + ": Validation is successful")
main() # Call the main function
46 changes: 21 additions & 25 deletions config/layout/type.validate.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
import yaml

FILE = "type.yaml"
LANGUAGES = ["en", "fa"]

file = open(FILE, "r", encoding="utf-8")
docs = yaml.safe_load_all(file)
import os
import sys

sys.path.append(
os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "module"))
)

def error(msg: str) -> None:
print("Error: " + msg)
exit(1)
from utils import error, load_yaml
from validation import validate_item_structure

FILE = "type.yaml"
LANGUAGES = ["en", "fa"]

if __name__ == "__main__":
for doc in docs:
for item in doc["items"]:
if "id" not in item:
print(item)
error("id is required and missed in an item")

if "is_mother" in item:
if item["is_mother"] != True and item["is_mother"] != False:
try:
docs = load_yaml(FILE)

for doc in docs:
for item in doc.get("items", []):
try:
validate_item_structure(item, LANGUAGES)
except ValueError as e:
print(item)
error("is_mother is invalid in an item")
error(str(e))

if "text" in item:
for lang in LANGUAGES:
if lang not in item["text"]:
print(item)
error("text is required for " + lang + " language")
print(FILE + ": Validation is successful")

print(FILE + ": Validation is successful")
except Exception as e:
error(f"An unexpected error occurred: {e}")
Binary file added config/module/__pycache__/utils.cpython-312.pyc
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit be893b2

Please sign in to comment.