Coverage for skema/program_analysis/comment_extractor/server.py: 100%
32 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-30 17:15 +0000
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-30 17:15 +0000
1from pathlib import Path
2from io import BytesIO
3from zipfile import ZipFile
4from typing import List, Union, Optional
5from fastapi import FastAPI, APIRouter, File, UploadFile
7import skema.program_analysis.comment_extractor.comment_extractor as comment_service
8from skema.program_analysis.comment_extractor.model import (
9 SingleFileCommentRequest,
10 SingleFileCommentResponse,
11 MultiFileCommentRequest,
12 MultiFileCommentResponse,
13 SupportedLanguageResponse,
14)
17SUPPORTED_LANGUAGES = comment_service.get_supported_languages()
18SUPPORTED_FILE_EXTENSIONS = [
19 extension
20 for language in SUPPORTED_LANGUAGES.languages
21 for extension in language.extensions
22]
23EXTENSION_TO_LANGUAGE = {
24 extension: language.name
25 for language in SUPPORTED_LANGUAGES.languages
26 for extension in language.extensions
27}
29router = APIRouter()
31@router.get("/comments-get-supported-languages", summary="Endpoint for checking which languages and comment types are supported by comment extractor.")
32async def comments_get_supported_languages() -> SupportedLanguageResponse:
33 """Endpoint for checking which type of comments are supported for each language.
34 ### Python example
36 ```
37 import requests
39 response=requests.get("/comment_service/comments-get-supported-languages")
40 supported_languages = response.json()
41 """
42 return SUPPORTED_LANGUAGES
45@router.get("/comments-get-supported-file-extensions", summary="Endpoint for checking which files extensions are currently supported by comment extractor.", responses=
46 {
47 200: {
48 "content": {
49 "application/json":{
50 "example": [
51 ".py",
52 ".f",
53 ".f90"
54 ]
55 }
56 }
57 }
58 })
59async def comments_get_supported_file_extensions() -> List[str]:
60 """Endpoint for checking which file extensions are supported for comment extraction.
61 ### Python example
63 ```
64 import requests
66 response=requests.get("/comment_service/comments-get-supported-file_extensions")
67 supported_file_extensions = response.json()
68 """
69 return SUPPORTED_FILE_EXTENSIONS
72@router.post("/comments-extract", summary="Endpoint for extracting comments from a single file.")
73async def comments_extract(
74 request: SingleFileCommentRequest,
75) -> SingleFileCommentResponse:
76 """Endpoing for extracting comments from a single file.
78 ### Python example
79 ```
80 request = {
81 "source": "#Single line Python comment",
82 "language": "python"
83 }
84 response = requests.post(URL, json=request)
85 """
86 return comment_service.extract_comments_single(request)
89@router.post("/comments-extract-zip", summary="Endpoint for extracting comments from a .zip archive.")
90async def comments_extract_zip(
91 zip_file: UploadFile = File(),
92) -> MultiFileCommentResponse:
93 """
94 Endpoint for extracting comment from a zip archive of arbitrary depth and structure.
95 All source files with a supported file extension will be processed as a single GrometFNModuleCollection.
97 ### Python example
98 ```
99 files = {"zip_file": open("path/to/zip.zip")}
100 requests.post(URL, files=files)
101 """
102 request = {"files": {}}
103 with ZipFile(BytesIO(zip_file.file.read()), "r") as zip:
104 for file in zip.namelist():
105 file_obj = Path(file)
106 file_suffix = file_obj.suffix
108 if file_suffix in EXTENSION_TO_LANGUAGE:
109 request["files"][file] = {
110 "language": EXTENSION_TO_LANGUAGE[file_suffix],
111 "source": zip.open(file).read(),
112 }
114 return comment_service.extract_comments_multi(
115 MultiFileCommentRequest(**request)
116 )
118app = FastAPI()
119app.include_router(
120 router,
121 prefix="/comment_service",
122 tags=["comment_service"],
123)