Skip to content

Instantly share code, notes, and snippets.

@mmerickel
Last active November 22, 2018 03:08
Show Gist options
  • Save mmerickel/d83ebca5a6ad1667d8f5 to your computer and use it in GitHub Desktop.
Save mmerickel/d83ebca5a6ad1667d8f5 to your computer and use it in GitHub Desktop.
decorate a bunch of items in a package and expose them as a public api
def _build_facade():
import sys
from .meta.api import scan
this = sys.modules[__name__]
registry = {}
scan(this, registry=registry)
globals().update(registry)
_build_facade()
del _build_facade
import venusian
def scan(pkg, registry=None):
if registry is None:
registry = {}
scanner = venusian.Scanner(registry=registry)
scanner.scan(pkg)
return registry
def expose(wrapped):
def callback(scanner, name, obj):
scanner.registry[name] = wrapped
venusian.attach(wrapped, callback)
return wrapped
from .api import expose
@expose
class User(Base):
name = Column(Text, nullable=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment