|
4 | 4 | and return either Pydantic models, primitives, or raw dicts depending on the operation.""" |
5 | 5 |
|
6 | 6 | import logging |
| 7 | +import os |
7 | 8 | from typing import Any, Dict, List, Literal, Optional, Union, overload |
8 | 9 |
|
| 10 | +import httpx |
| 11 | + |
9 | 12 | from comcheck_api.api import COMCheckApiService |
10 | 13 | from comcheck_api.constants.building_area_constants import DEFAULT_BUILDING_AREA |
11 | 14 | from comcheck_api.exceptions import ( |
@@ -271,11 +274,93 @@ def fill_empty_description(assemblies: list[dict]) -> list[dict]: |
271 | 274 | "skylight": fill_empty_description(envelope_data["skylight"]), |
272 | 275 | "window": fill_empty_description(envelope_data["window"]), |
273 | 276 | "door": fill_empty_description(envelope_data["door"]), |
274 | | - "floor": fill_empty_description(envelope_data["floor"]) |
| 277 | + "floor": fill_empty_description(envelope_data["floor"]), |
275 | 278 | } |
276 | | - assemblie_uvalue = self._service.assemblies_uvalue(reformatted_envelope_data, energy_code) |
| 279 | + assemblie_uvalue = self._service.assemblies_uvalue( |
| 280 | + reformatted_envelope_data, energy_code |
| 281 | + ) |
277 | 282 | return assemblie_uvalue["data"] |
278 | 283 |
|
| 284 | + def check_compliance(self, project: ComBuilding) -> Any: |
| 285 | + """Check code compliance for a project. |
| 286 | +
|
| 287 | + Args: |
| 288 | + project: The project data to evaluate. |
| 289 | +
|
| 290 | + Returns: |
| 291 | + The compliance results payload returned by the API. |
| 292 | + """ |
| 293 | + project_data = project.model_dump(mode="json", exclude_unset=True) |
| 294 | + response = self._service.check_compliance(project_data) |
| 295 | + return response.get("data") |
| 296 | + |
| 297 | + def check_requirements(self, project: ComBuilding) -> Any: |
| 298 | + """Check the applicable requirements for a project. |
| 299 | +
|
| 300 | + Args: |
| 301 | + project: The project data to evaluate. |
| 302 | +
|
| 303 | + Returns: |
| 304 | + The requirements payload returned by the API. |
| 305 | + """ |
| 306 | + project_data = project.model_dump(mode="json", exclude_unset=True) |
| 307 | + response = self._service.check_requirements(project_data) |
| 308 | + return response.get("data") |
| 309 | + |
| 310 | + def generate_report( |
| 311 | + self, |
| 312 | + project: ComBuilding, |
| 313 | + envelope: bool = True, |
| 314 | + extlighting: bool = True, |
| 315 | + intlighting: bool = True, |
| 316 | + mechanical: bool = True, |
| 317 | + download: bool = False, |
| 318 | + download_dir: Optional[str] = None, |
| 319 | + ) -> Optional[Dict[str, Any]]: |
| 320 | + """Generate a PDF report for a project. |
| 321 | +
|
| 322 | + The report PDF is stored in S3; the API returns a presigned URL to |
| 323 | + download it. The URL is short-lived (expires within a few minutes). |
| 324 | +
|
| 325 | + This method does not open the report in a browser — as a library it |
| 326 | + returns the report metadata and lets the caller decide what to do |
| 327 | + (e.g. ``webbrowser.open(report["url"])``, stream it onward, or save it |
| 328 | + via *download*). |
| 329 | +
|
| 330 | + Args: |
| 331 | + project: The project data to generate a report for. |
| 332 | + envelope: Whether to include the envelope section in the report. |
| 333 | + extlighting: Whether to include the exterior lighting section. |
| 334 | + intlighting: Whether to include the interior lighting section. |
| 335 | + mechanical: Whether to include the mechanical section. |
| 336 | + download: When ``True``, fetch the PDF from the presigned URL and |
| 337 | + save it using the server-provided ``fileName``. |
| 338 | + download_dir: Directory to save the PDF into when *download* is |
| 339 | + ``True``. Defaults to the user's ``~/Downloads`` folder. |
| 340 | +
|
| 341 | + Returns: |
| 342 | + The report metadata dict with ``url`` (the presigned S3 URL), |
| 343 | + ``expires``, and ``fileName``. |
| 344 | + """ |
| 345 | + report_data = { |
| 346 | + "building": project.model_dump(mode="json", exclude_unset=True), |
| 347 | + "envelope": envelope, |
| 348 | + "extlighting": extlighting, |
| 349 | + "intlighting": intlighting, |
| 350 | + "mechanical": mechanical, |
| 351 | + } |
| 352 | + response = self._service.generate_report(report_data) |
| 353 | + if response and download: |
| 354 | + if download_dir is None: |
| 355 | + download_dir = os.path.join(os.path.expanduser("~"), "Downloads") |
| 356 | + os.makedirs(download_dir, exist_ok=True) |
| 357 | + pdf_response = httpx.get(response["url"], timeout=60.0) |
| 358 | + pdf_response.raise_for_status() |
| 359 | + output_path = os.path.join(download_dir, response["fileName"]) |
| 360 | + with open(output_path, "wb") as f: |
| 361 | + f.write(pdf_response.content) |
| 362 | + return response |
| 363 | + |
279 | 364 | def start_run_simulation( |
280 | 365 | self, project: ComBuilding, project_id: Optional[int] = None |
281 | 366 | ) -> str: |
@@ -312,7 +397,7 @@ def start_run_simulation( |
312 | 397 | for wall in ag_Walls: |
313 | 398 | for uvalue in updated_assembly_uvalues.get("agWall", []): |
314 | 399 | if wall.assemblyType == uvalue["description"]: |
315 | | - wall.effectiveUFactor = float(uvalue["propUValue"]) |
| 400 | + wall.effectiveUFactor = float(uvalue["effectiveUFactor"]) |
316 | 401 | wall.propUValue = float(uvalue["propUValue"]) |
317 | 402 | for roof in roofs: |
318 | 403 | for uvalue in updated_assembly_uvalues.get("roof", []): |
|
0 commit comments