-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved templated loading to seperate module
- Loading branch information
1 parent
6e1ad4a
commit 268cd94
Showing
7 changed files
with
79 additions
and
96 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
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,21 @@ | ||
from os.path import abspath | ||
from jinja2 import Template | ||
from parsing_types.argument import parse_args | ||
|
||
from utils import camel_case, THIS_DIR | ||
from utils import camel_case | ||
from parsing_types.complex_argument import ComplexArgument | ||
from templates import render | ||
|
||
with open(abspath(THIS_DIR + "/templates/enum.swift")) as file: | ||
enum_template = Template( | ||
file.read(), | ||
) | ||
|
||
class EnumArgument(ComplexArgument): | ||
def __init__(self, parent_name, desc, is_sub_arg=False): | ||
super().__init__(parent_name, desc, is_sub_arg) | ||
self.args = parse_args(desc.get("arguments", []), self.fullname(), make_options_arg=False, are_sub_args=True) | ||
self.args = parse_args( | ||
desc.get("arguments", []), | ||
self.fullname(), | ||
make_options_arg=False, | ||
are_sub_args=True, | ||
) | ||
|
||
def custom_type(self): | ||
return enum_template.render( | ||
enum_name=self.type, | ||
args=self.args, | ||
camel_case=camel_case | ||
) | ||
return render( | ||
"enum.swift", enum_name=self.type, args=self.args, camel_case=camel_case | ||
) |
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,26 +1,18 @@ | ||
from os.path import abspath | ||
from jinja2 import Template | ||
from parsing_types.complex_argument import ComplexArgument | ||
|
||
from utils import camel_case, THIS_DIR | ||
from utils import camel_case | ||
from parsing_types.argument import parse_args | ||
from templates import render | ||
|
||
with open(abspath(THIS_DIR + "/templates/struct.swift")) as file: | ||
struct_template = Template( | ||
file.read(), | ||
) | ||
|
||
with open(abspath(THIS_DIR + "/templates/options.swift")) as file: | ||
options_template = Template(file.read()) | ||
|
||
class StructArgument(ComplexArgument): | ||
def __init__(self, parent_name, desc, is_sub_arg=False): | ||
super().__init__(parent_name, desc, is_sub_arg) | ||
self.args = parse_args(desc.get("arguments", []), self.fullname(), are_sub_args=True) | ||
self.args = parse_args( | ||
desc.get("arguments", []), self.fullname(), are_sub_args=True | ||
) | ||
|
||
def custom_type(self): | ||
return struct_template.render( | ||
struct_name=self.type, | ||
args=self.args, | ||
camel_case=camel_case | ||
) | ||
return render( | ||
"struct.swift", struct_name=self.type, args=self.args, camel_case=camel_case | ||
) |
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,22 @@ | ||
import os | ||
from jinja2 import Environment, FileSystemLoader | ||
|
||
# Get the directory containing this script | ||
current_dir = os.path.dirname(os.path.abspath(__file__)) | ||
template_dir = os.path.join(current_dir, "templates") | ||
|
||
# Create a Jinja2 environment | ||
env = Environment(loader=FileSystemLoader(template_dir)) | ||
|
||
|
||
def render(template_name: str, *args, **kwargs) -> str: | ||
""" | ||
Render a Jinja2 template with the given arguments. | ||
:param template_name: The name of the template file. | ||
:param args: Positional arguments to pass to the template. | ||
:param kwargs: Keyword arguments to pass to the template. | ||
:return: The rendered template as a string. | ||
""" | ||
template = env.get_template(template_name) | ||
return template.render(*args, **kwargs) |