Skip to content

Commit

Permalink
feat: shorten docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
CheeseCake87 committed Aug 5, 2024
1 parent 603dfa8 commit 729809a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 275 deletions.
2 changes: 1 addition & 1 deletion flask_imp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .imp import Imp as Imp
from .imp_blueprint import ImpBlueprint

__version__ = "5.0.1"
__version__ = "5.0.2"

__all__ = [
"Auth",
Expand Down
8 changes: 8 additions & 0 deletions flask_imp/imp.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ def import_app_resources(

def import_blueprint(self, blueprint: str) -> None:
"""
Import a blueprint from the given package.
:param blueprint: The blueprint (folder name) to import. Must be relative.
:return: None
"""
Expand All @@ -190,6 +192,8 @@ def import_blueprint(self, blueprint: str) -> None:

def import_blueprints(self, folder: str) -> None:
"""
Import all blueprints from the given folder.
:param folder: The folder to import from. Must be relative.
:return: None
"""
Expand All @@ -207,6 +211,8 @@ def import_blueprints(self, folder: str) -> None:

def import_models(self, file_or_folder: str) -> None:
"""
Import all models from the given file or folder.
:param file_or_folder: The file or folder to import from. Must be relative.
:return: None
"""
Expand All @@ -227,6 +233,8 @@ def import_models(self, file_or_folder: str) -> None:

def model(self, class_: str) -> DefaultMeta:
"""
Returns the model class for the given ORM class name.
:param class_: The class name of the model to return.
:return: The model class [DefaultMeta].
"""
Expand Down
278 changes: 4 additions & 274 deletions flask_imp/imp_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def __init__(
config: ImpBlueprintConfig
) -> None:
"""
Initializes the ImpBlueprint.
:param dunder_name: __name__
:param config: The blueprint's config.
"""
Expand Down Expand Up @@ -101,59 +103,8 @@ def import_resources(self, folder: str = "routes") -> None:
Will import all the resources (cli, routes, filters, context_processors...) from the given folder.
Given folder must be relative to the blueprint (in the same folder as the __init__.py file).
**Example use:**
--- Folder structure ---
.. code-block::
my_blueprint
├── user_routes
│ ├── user_dashboard.py
│ └── user_settings.py
├── car_routes
│ ├── car_dashboard.py
│ └── car_settings.py
├── __init__.py
└── config.toml
:raw-html:`<br />`
--- __init__.py ---
.. code-block::
from flask_imp import Blueprint
bp = Blueprint(__name__)
bp.import_resources("user_routes")
bp.import_resources("car_routes")
...
:raw-html:`<br />`
--- user_dashboard.py ---
.. code-block::
from flask import render_template
from .. import bp
@bp.route("/user-dashboard")
def user_dashboard():
return render_template(bp.tmpl("user_dashboard.html"))
:raw-html:`<br />`
The endpoint my_blueprint.user_dashboard will be available at /my_blueprint/user-dashboard
:raw-html:`<br />`
-----
:param folder: Folder to look for resources in. Defaults to "routes". Must be relative.
:return: None
"""

resource_path = self.location / folder
Expand All @@ -175,53 +126,6 @@ def import_nested_blueprint(self, blueprint: str) -> None:
Imports the specified Flask-Imp Blueprint or a standard Flask Blueprint as a nested blueprint,
under the current blueprint.
:raw-html:`<br />`
Has the same import rules as the `Imp.import_blueprint()` method.
:raw-html:`<br />`
**Must be setup in a Python package**
:raw-html:`<br />`
**Example:**
:raw-html:`<br />`
--- Folder structure ---
.. code-block::
app
├── my_blueprint
│ ├── ...
│ ├── my_nested_blueprint
│ │ ├── ...
│ │ ├── __init__.py
│ │ └── config.toml
│ ├── __init__.py
│ └── config.toml
└── ...
:raw-html:`<br />`
--- my_blueprint/__init__.py ---
.. code-block::
from flask_imp import Blueprint
bp = Blueprint(__name__)
bp.import_nested_blueprint("my_nested_blueprint")
...
:raw-html:`<br />`
-----
:param blueprint: The blueprint (folder name) to import. Must be relative.
:return: None
"""
Expand All @@ -243,65 +147,8 @@ def import_nested_blueprints(self, folder: str) -> None:
"""
Imports all blueprints in the given folder.
.. Note::
Folder has no requirement to be a Python package.
:raw-html:`<br />`
See `Imp.import_nested_blueprint()` for more information.
:raw-html:`<br />`
**Example:**
:raw-html:`<br />`
--- Folder structure ---
.. code-block::
app
├── my_blueprint
│ ├── ...
│ ├── nested_blueprints
│ │ ├── my_nested_blueprint_1
│ │ │ ├── ...
│ │ │ ├── __init__.py
│ │ │ └── config.toml
│ │ ├── my_nested_blueprint_2
│ │ │ ├── ...
│ │ │ ├── __init__.py
│ │ │ └── config.toml
│ │ └── my_nested_blueprint_3
│ │ ├── ...
│ │ ├── __init__.py
│ │ └── config.toml
│ ├── __init__.py
│ └── config.toml
└── ...
:raw-html:`<br />`
--- my_blueprint/__init__.py ---
.. code-block::
from flask_imp import Blueprint
bp = Blueprint(__name__)
bp.import_nested_blueprints("nested_blueprints")
...
:raw-html:`<br />`
All blueprints in the nested_blueprints folder will be imported and nested under my_blueprint.
:raw-html:`<br />`
-----
:param folder: Folder to look for nested blueprints in.
Must be relative.
:return: None
"""

folder_path = Path(self.location / folder)
Expand All @@ -317,84 +164,6 @@ def import_models(self, file_or_folder: str) -> None:
"""
Same actions as `Imp.import_models()`, but scoped to the current blueprint's package.
:raw-html:`<br />`
**Each model found will be added to the model registry.**
:raw-html:`<br />`
See: `Imp.model()` for more information.
:raw-html:`<br />`
**Example usage from files:**
:raw-html:`<br />`
.. code-block::
# in my_blueprint/__init__.py
bp.import_models("users.py")
bp.import_models("cars.py")
:raw-html:`<br />`
-- Folder structure --
.. code-block::
my_blueprint
├── ...
├── users.py
├── cars.py
├── config.toml
└── __init__.py
:raw-html:`<br />`
**Example usage from folders:**
:raw-html:`<br />`
.. code-block::
# in my_blueprint/__init__.py
bp.import_models("models")
:raw-html:`<br />`
-- Folder structure --
.. code-block::
my_blueprint
├── ...
├── models
│ ├── users.py
│ └── cars.py
├── config.toml
└── __init__.py
:raw-html:`<br />`
**Example of model file:**
:raw-html:`<br />`
-- users.py --
.. code-block::
from app.extensions import db
class User(db.Model):
attribute = db.Column(db.String(255))
:raw-html:`<br />`
-----
:param file_or_folder: The file or folder to import from. Must be relative.
:return: None
"""
Expand All @@ -412,45 +181,6 @@ def tmpl(self, template: str) -> str:
This saves time in having to type out the blueprint name when rendering a
template file from the blueprint's template folder.
:raw-html:`<br />`
**Example usage:**
:raw-html:`<br />`
.. code-block::
@bp.route("/")
def index():
return render_template(bp.tmpl("index.html"))
:raw-html:`<br />`
-- Folder structure --
.. code-block::
my_blueprint
├── ...
├── templates
│ └── my_blueprint
│ └── index.html
├── config.toml
└── __init__.py
:raw-html:`<br />`
bp.tmpl("index.html") will return "my_blueprint/index.html"
:raw-html:`<br />`
This use case is a common workaround in Flask to allow for multiple templates with the same name,
but in different registered template folders.
:raw-html:`<br />`
-----
:param template: The template name to push the blueprint name to.
:return: str - The template name with the blueprint name pushed to it.
"""
Expand Down

0 comments on commit 729809a

Please sign in to comment.