MCPcopy Create free account
hub / github.com/pallets/quart / find_app_by_string

Function find_app_by_string

src/quart/cli.py:112–180  ·  view source on GitHub ↗
(module: ModuleType, app_name: str)

Source from the content-addressed store, hash-verified

110
111
112def find_app_by_string(module: ModuleType, app_name: str) -> Quart:
113 from .app import Quart
114
115 try:
116 expr = ast.parse(app_name.strip(), mode="eval").body
117 except SyntaxError:
118 raise NoAppException(
119 f"Failed to parse {app_name!r} as an attribute name or function call."
120 ) from None
121
122 if isinstance(expr, ast.Name):
123 name = expr.id
124 args = []
125 kwargs = {}
126 elif isinstance(expr, ast.Call):
127 # Ensure the function name is an attribute name only.
128 if not isinstance(expr.func, ast.Name):
129 raise NoAppException(
130 f"Function reference must be a simple name: {app_name!r}."
131 )
132
133 name = expr.func.id
134
135 # Parse the positional and keyword arguments as literals.
136 try:
137 args = [ast.literal_eval(arg) for arg in expr.args]
138 kwargs = {kw.arg: ast.literal_eval(kw.value) for kw in expr.keywords}
139 except ValueError:
140 # literal_eval gives cryptic error messages, show a generic
141 # message with the full expression instead.
142 raise NoAppException(
143 f"Failed to parse arguments as literal values: {app_name!r}."
144 ) from None
145 else:
146 raise NoAppException(
147 f"Failed to parse {app_name!r} as an attribute name or function call."
148 )
149
150 try:
151 attr = getattr(module, name)
152 except AttributeError as e:
153 raise NoAppException(
154 f"Failed to find attribute {name!r} in {module.__name__!r}."
155 ) from e
156
157 # If the attribute is a function, call it with any args and kwargs
158 # to get the real application.
159 if inspect.isfunction(attr):
160 try:
161 app = attr(*args, **kwargs)
162 except TypeError as e:
163 if not _called_with_wrong_args(attr):
164 raise
165
166 raise NoAppException(
167 f"The factory {app_name!r} in module"
168 f" {module.__name__!r} could not be called with the"
169 " specified arguments."

Callers 1

locate_appFunction · 0.85

Calls 3

NoAppExceptionClass · 0.85
_called_with_wrong_argsFunction · 0.85
parseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…