-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement 'make:module' command in Rosemary
- Loading branch information
1 parent
bc6abbd
commit a922740
Showing
17 changed files
with
168 additions
and
5 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from flask import Blueprint | ||
|
||
zenodo_bp = Blueprint('zenodo', __name__, template_folder='templates') |
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 flask_wtf import FlaskForm | ||
from wtforms import SubmitField | ||
|
||
|
||
class ZenodoForm(FlaskForm): | ||
submit = SubmitField('Save zenodo') |
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,5 @@ | ||
from app import db | ||
|
||
|
||
class Zenodo(db.Model): | ||
id = db.Column(db.Integer, primary_key=True) |
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,7 @@ | ||
from app.blueprints.zenodo.models import Zenodo | ||
from app.repositories.BaseRepository import BaseRepository | ||
|
||
|
||
class ZenodoRepository(BaseRepository): | ||
def __init__(self): | ||
super().__init__(Zenodo) |
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,7 @@ | ||
from flask import render_template | ||
from app.blueprints.zenodo import zenodo_bp | ||
|
||
|
||
@zenodo_bp.route('/zenodo', methods=['GET']) | ||
def index(): | ||
return render_template('zenodo/index.html') |
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,7 @@ | ||
from app.blueprints.zenodo.repositories import ZenodoRepository | ||
from app.services.BaseService import BaseService | ||
|
||
|
||
class Zenodo(BaseService): | ||
def __init__(self): | ||
super().__init__(ZenodoRepository()) |
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,7 @@ | ||
{% extends "base_template.html" %} | ||
|
||
{% block title %}View zenodo{% endblock %} | ||
|
||
{% block content %} | ||
|
||
{% endblock %} |
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import click | ||
from jinja2 import Environment, FileSystemLoader, select_autoescape | ||
import os | ||
|
||
|
||
def pascalcase(s): | ||
"""Converts string to PascalCase.""" | ||
return ''.join(word.capitalize() for word in s.split('_')) | ||
|
||
|
||
def setup_jinja_env(): | ||
"""Configures and returns a Jinja environment.""" | ||
env = Environment( | ||
loader=FileSystemLoader(searchpath="./rosemary/templates"), | ||
autoescape=select_autoescape(['html', 'xml', 'j2']) | ||
) | ||
env.filters['pascalcase'] = pascalcase | ||
return env | ||
|
||
|
||
def render_and_write_file(env, template_name, filename, context): | ||
"""Renders a template and writes it to a specified file.""" | ||
template = env.get_template(template_name) | ||
content = template.render(context) + "\n" | ||
with open(filename, 'w') as f: | ||
f.write(content) | ||
|
||
|
||
@click.command('make:module', help="Creates a new module with a given name.") | ||
@click.argument('name') | ||
def make_module(name): | ||
blueprint_path = f'app/blueprints/{name}' | ||
|
||
if os.path.exists(blueprint_path): | ||
click.echo(f"The module '{name}' already exists.") | ||
return | ||
|
||
env = setup_jinja_env() | ||
|
||
files_and_templates = { | ||
'__init__.py': 'blueprint_init.py.j2', | ||
'routes.py': 'blueprint_routes.py.j2', | ||
'models.py': 'blueprint_models.py.j2', | ||
'repositories.py': 'blueprint_repositories.py.j2', | ||
'services.py': 'blueprint_services.py.j2', | ||
'forms.py': 'blueprint_forms.py.j2', | ||
os.path.join('templates', name, 'index.html'): 'blueprint_templates_index.html.j2' | ||
} | ||
|
||
# Create necessary directories | ||
os.makedirs(os.path.join(blueprint_path, 'templates', name), exist_ok=True) | ||
|
||
# Render and write files | ||
for filename, template_name in files_and_templates.items(): | ||
render_and_write_file(env, template_name, os.path.join(blueprint_path, filename), {'blueprint_name': name}) | ||
|
||
click.echo(f"Module '{name}' created successfully.") |
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 flask_wtf import FlaskForm | ||
from wtforms import SubmitField | ||
|
||
|
||
class {{ blueprint_name | pascalcase }}Form(FlaskForm): | ||
submit = SubmitField('Save {{ blueprint_name }}') |
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,3 @@ | ||
from flask import Blueprint | ||
|
||
{{ blueprint_name }}_bp = Blueprint('{{ blueprint_name }}', __name__, template_folder='templates') |
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,5 @@ | ||
from app import db | ||
|
||
|
||
class {{ blueprint_name | pascalcase }}(db.Model): | ||
id = db.Column(db.Integer, primary_key=True) |
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,7 @@ | ||
from app.blueprints.{{ blueprint_name }}.models import {{ blueprint_name | pascalcase }} | ||
from app.repositories.BaseRepository import BaseRepository | ||
|
||
|
||
class {{ blueprint_name | pascalcase }}Repository(BaseRepository): | ||
def __init__(self): | ||
super().__init__({{ blueprint_name | pascalcase }}) |
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,7 @@ | ||
from flask import render_template | ||
from app.blueprints.{{ blueprint_name }} import {{ blueprint_name }}_bp | ||
|
||
|
||
@{{ blueprint_name }}_bp.route('/{{ blueprint_name }}', methods=['GET']) | ||
def index(): | ||
return render_template('{{ blueprint_name }}/index.html') |
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,7 @@ | ||
from app.blueprints.{{ blueprint_name }}.repositories import {{ blueprint_name | pascalcase }}Repository | ||
from app.services.BaseService import BaseService | ||
|
||
|
||
class {{ blueprint_name | pascalcase }}(BaseService): | ||
def __init__(self): | ||
super().__init__({{ blueprint_name | pascalcase }}Repository()) |
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,7 @@ | ||
{% raw %}{%{% endraw %} extends "base_template.html" {% raw %}%}{% endraw %} | ||
|
||
{% raw %}{%{% endraw %} block title {% raw %}%}{% endraw %}View {{ blueprint_name }}{% raw %}{%{% endraw %} endblock {% raw %}%}{% endraw %} | ||
|
||
{% raw %}{%{% endraw %} block content {% raw %}%}{% endraw %} | ||
|
||
{% raw %}{%{% endraw %} endblock {% raw %}%}{% endraw %} |