Skip to main content

Discover packages and classes in a python project.

Project description

Roman Discovery

The package scans the project to execute some actions with found modules and objects. It's specifically helpful for frameworks that define resources on the fly with decorators and expect you to import all necessary modules.

For example, it can be helpful for Flask to load all your blueprints, initialize extensions, and import SQLAlchemy models.

Install

pip install roman-discovery

Usage with Flask

Using within the opinionated Flask structure was the initial purpose of the package. Use roman_discovery.discover() with roman_discovery.flask.get_flask_rules().

The function expects the following project structure.

myproject

  app.py
  config.py
  services.py

  foo/
    controllers.py
    models.py
    cli.py

  bar/
    controllers/
      api.py
      admin.py
    models/
      users.py
      projects.py
    cli/
      user_commands.py
      project_commands.py

With this structure, it will do the following.

  • Scan controllers.py and controllers/ to find blueprints and attach the blueprints to the flask application.
  • Import all files in models.py and models/ to help flask-migrate find all the SQLAlchemy models to create migrations.
  • Scan cli.py and cli/ to find flask.cli.AppGroup instances and attach them to Flask's CLI.
  • Scan top-level services.py, find all the instances that have init_app() methods, and call obj.init_app(app=flask_app) for each of them.

An example of your top-level app.py

# file: myproject/app.py
from flask import Flask
from roman_discovery import discover
from roman_discovery.flask import get_flask_rules


def app() -> Flask:
    flask_app = Flask(__name__, instance_relative_config=True)
    flask_app.config.from_object("myproject.config")
    flask_rules = get_flask_rules("myproject", flask_app)
    discover("myproject", flask_rules)
    return flask_app

An example of your top-level services.py

# file: myproject/services.py

from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_mail import Mail

db = SQLAlchemy()
migrate = Migrate(db=db)
mail = Mail()

Usage with anything else

You can create your own discovery rules with the discover() function, ModuleRule and ObjectRule. Optionally, you can take advantage of custom matchers, defined in roman_discovery.matchers.

For example, that's how you print all modules and all callable objects within the roman_discovery itself.

from roman_discovery import discover, ModuleRule, ObjectRule

module_printer = ModuleRule(
    name="module printer",
    module_matches=lambda module_name: True,
    module_action=lambda module_name: print(f"Found module {module_name}"),
)

object_printer = ObjectRule(
    name="object printer",
    module_matches=lambda module_name: True,
    object_matches=callable,
    object_action=lambda obj: print(f"Found callable object {obj!r}"),
)

discover("roman_discovery", rules=[module_printer, object_printer])

Why the "roman" prefix?

I use it as my own "pseudo-namespace." If I ever abandon the project, at least the package doesn't occupy a common name.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

roman_discovery-0.2.0.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

roman_discovery-0.2.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file roman_discovery-0.2.0.tar.gz.

File metadata

  • Download URL: roman_discovery-0.2.0.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for roman_discovery-0.2.0.tar.gz
Algorithm Hash digest
SHA256 bc13c9c0d0513a1746daae39fdbef8351dccfca94b10f352f69dfee5825e501c
MD5 47e6594a913960ce4f71e0c0a5279e68
BLAKE2b-256 24493aa590252af31f0ea19388e07c47adc50f3cd61c9c2a0c45f65a67d624c5

See more details on using hashes here.

File details

Details for the file roman_discovery-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: roman_discovery-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

File hashes

Hashes for roman_discovery-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d3520d0de724de87b1c05c9fef07468578b653c44a56d2fa5a81ff6490935e36
MD5 664f2078795b6e922401e9c61a71ef73
BLAKE2b-256 d7f960f01b5731728e398ec60e7fb4c4c7087fcdb6e983e58c33d15eff4113be

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page