Skip to content

Commit

Permalink
start working on build structure
Browse files Browse the repository at this point in the history
  • Loading branch information
BaseMax committed Jan 8, 2025
1 parent 901a959 commit 9174d9b
Show file tree
Hide file tree
Showing 19 changed files with 176 additions and 0 deletions.
Binary file added build/__pycache__/attribute.cpython-312.pyc
Binary file not shown.
Binary file added build/__pycache__/element.cpython-312.pyc
Binary file not shown.
Binary file added build/__pycache__/lang.cpython-312.pyc
Binary file not shown.
Binary file added build/__pycache__/text.cpython-312.pyc
Binary file not shown.
Binary file added build/__pycache__/type.cpython-312.pyc
Binary file not shown.
45 changes: 45 additions & 0 deletions build/attribute.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from text import Text
from type import Type

class Attribute:
def __init__(self, generate_name):
self.generate_name = generate_name
self.text = Text()
self.id = "AST_LAYOUT_ATTRIBUTE_" + generate_name.upper()
self.type = Type.String
self.reserved_values = []

def set_generate_name(self, generate_name):
"""Set the generate name for the main element."""
self.generate_name = generate_name
return self

def set_type(self, type):
"""Set the type of the attribute."""
self.type = type
return self

def add_text(self, language_code, text):
"""Set text for the main element."""
self.text.add_translation(language_code, text)
return self

def add_reserve_value(self, value):
"""Add a reserved value for the attribute."""
self.reserved_values.append(value)
return self

def validate(self):
"""Check if all languages have a value for the text."""
self.text.validate()
return self

def to_dict(self):
"""Convert the attribute to a dictionary, including all information."""
return {
"id": self.id,
"generate_name": self.generate_name,
"type": self.type,
"text": self.text.to_dict(),
"reserved_values": self.reserved_values,
}
44 changes: 44 additions & 0 deletions build/element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from text import Text
from attribute import Attribute

class Element:
def __init__(self):
self.text = Text()
self.attributes = []
self.is_mother = False

def is_mother(self, is_mother):
"""Set the mother status for the main element."""
self.is_mother = is_mother

def set_generate_name(self, generate_name):
"""Set the generate name for the main element."""
self.generate_name = generate_name
self.id = "AST_LAYOUT_TYPE_" + generate_name.upper()

def set_text(self, language_code, text):
"""Set text for the main element."""
self.text.add_translation(language_code, text)

def add_attribute(self, attribute_generate_name):
"""Add an attribute with its own text translations."""
attribute = Attribute(attribute_generate_name)
self.attributes.append(attribute)

return attribute

def to_dict(self):
"""Convert the element to a dictionary, including all attributes."""
for attr in self.attributes:
attr.validate()

return {
"id": self.id,
"is_mother": self.is_mother,
"generate_name": self.generate_name,
"text": self.text.to_dict(),
"attributes": [
attr.to_dict() for attr in self.attributes
],
}

26 changes: 26 additions & 0 deletions build/lang.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Lang:
languages = {}

@classmethod
def add_lang(cls, name, full_name, international_name):
"""Add a new language to the list of supported languages."""
cls.languages[name] = {
"full_name": full_name,
"international_name": international_name,
}
setattr(cls, name, name)

@classmethod
def get_languages(cls):
"""Get the list of supported languages."""
return cls.languages

def __getattr__(self, name):
"""Get the object for a specific language."""
return self.languages.get(name.upper(), None)

# Add supported languages
Lang.add_lang("EN", "English", "English")
Lang.add_lang("FA", "Persian", "فارسی")
# Lang.add_lang("ES", "Spanish", "Español")
# Lang.add_lang("AR", "Arabic", "العربية")
File renamed without changes.
File renamed without changes.
30 changes: 30 additions & 0 deletions build/tags/a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import sys
import json
from pathlib import Path

parent_dir = Path(__file__).resolve().parent.parent
sys.path.append(str(parent_dir))

from element import Element
from lang import Lang
from type import Type

if __name__ == "__main__":
element = Element()
element.is_mother(True)
element.set_generate_name("a")
element.set_text(Lang.EN, "link")
element.set_text(Lang.FA, "لینک")

element.add_attribute() \
.set_generate_name("href") \
.set_type(Type.String) \
.add_text(Lang.EN, "url") \
.add_text(Lang.FA, "آدرس") \
.add_reserve_value(Value(

))

element_dict = element.to_dict()

print(json.dumps(element_dict, indent=4, ensure_ascii=False))
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file added build/tags/ul.py
Empty file.
25 changes: 25 additions & 0 deletions build/text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from lang import Lang

class Text:
def __init__(self):
self.translations = {}

def add_translation(self, language_code, text):
"""Add or update a translation for a specific language."""
if language_code not in self.translations:
self.translations[language_code] = []
self.translations[language_code].append(text)

def validate(self):
"""Check if all languages have a value for the text."""
langs = Lang().get_languages()

for lang_code in langs:
if lang_code not in self.translations:
raise ValueError(f"Missing translation for {lang_code}")

def to_dict(self):
"""Convert translations to dictionary format."""
self.validate()

return self.translations
6 changes: 6 additions & 0 deletions build/type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Type:
"""Type of an attribute."""
String = "AST_TYPE_STRING"
Number = "AST_TYPE_NUMBER"
Boolean = "AST_TYPE_BOOLEAN"
Any = "AST_TYPE_ANY"
Empty file added build/value.py
Empty file.

0 comments on commit 9174d9b

Please sign in to comment.