MCPcopy
hub / github.com/benoitc/gunicorn / parse_dirty_app_spec

Function parse_dirty_app_spec

gunicorn/dirty/app.py:140–212  ·  view source on GitHub ↗

Parse a dirty app specification. Supports two formats: - ``"module:Class"`` - standard format, all workers load the app - ``"module:Class:N"`` - worker-limited format, only N workers load the app Args: spec: The app specification string Returns: tuple: (im

(spec)

Source from the content-addressed store, hash-verified

138
139
140def parse_dirty_app_spec(spec):
141 """
142 Parse a dirty app specification.
143
144 Supports two formats:
145 - ``"module:Class"`` - standard format, all workers load the app
146 - ``"module:Class:N"`` - worker-limited format, only N workers load the app
147
148 Args:
149 spec: The app specification string
150
151 Returns:
152 tuple: (import_path, worker_count)
153 - import_path: The "module:Class" part for importing
154 - worker_count: Integer limit or None for all workers
155
156 Raises:
157 DirtyAppError: If the spec format is invalid or worker_count is < 1
158
159 Examples::
160
161 >>> parse_dirty_app_spec("myapp:App")
162 ("myapp:App", None)
163
164 >>> parse_dirty_app_spec("myapp:App:2")
165 ("myapp:App", 2)
166
167 >>> parse_dirty_app_spec("myapp.sub:App:1")
168 ("myapp.sub:App", 1)
169 """
170 if ':' not in spec:
171 raise DirtyAppError(
172 f"Invalid import path format: {spec}. "
173 f"Expected 'module.path:ClassName' or 'module.path:ClassName:N'",
174 app_path=spec
175 )
176
177 parts = spec.split(':')
178
179 # Standard format: "module:Class" or "module.sub:Class"
180 if len(parts) == 2:
181 return (spec, None)
182
183 # Worker-limited format: "module:Class:N"
184 if len(parts) == 3:
185 module_path, class_name, count_str = parts
186 import_path = f"{module_path}:{class_name}"
187
188 # Validate the worker count
189 try:
190 worker_count = int(count_str)
191 except ValueError:
192 raise DirtyAppError(
193 f"Invalid worker count in spec: {spec}. "
194 f"Expected integer, got '{count_str}'",
195 app_path=spec
196 )
197

Calls 1

DirtyAppErrorClass · 0.85