This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/teal/utils.py

34 lines
883 B
Python
Raw Permalink Normal View History

2023-03-21 11:08:13 +00:00
import inspect
from typing import Dict, Iterator, Tuple
from sqlalchemy.dialects import postgresql
from ereuse_devicehub.teal import resource
def compiled(Model, query) -> Tuple[str, Dict[str, str]]:
"""
Generates a SQL statement.
:return A tuple with 1. the SQL statement and 2. the params for it.
"""
c = Model.query.filter(*query).statement.compile(dialect=postgresql.dialect())
return str(c), c.params
def import_resource(module) -> Iterator['resource.Resource']:
"""
Gets the resource classes from the passed-in module.
This method yields subclasses of :class:`teal.resource.Resource`
found in the given module.
"""
for obj in vars(module).values():
if (
inspect.isclass(obj)
and issubclass(obj, resource.Resource)
and obj != resource.Resource
):
yield obj