-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,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, | ||
} |
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,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 | ||
], | ||
} | ||
|
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,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.
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,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.
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,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 |
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 @@ | ||
class Type: | ||
"""Type of an attribute.""" | ||
String = "AST_TYPE_STRING" | ||
Number = "AST_TYPE_NUMBER" | ||
Boolean = "AST_TYPE_BOOLEAN" | ||
Any = "AST_TYPE_ANY" |
Empty file.