Coverage for skema/program_analysis/comment_extractor/model.py: 100%
34 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 typing import List, Dict, Union, Optional
2from pydantic import BaseModel, Field
5class SingleLineComment(BaseModel):
6 content: str = Field(
7 ...,
8 description="The content of the single line comment.",
9 examples=["#Single line Fortran comment"],
10 )
11 line_number: int = Field(
12 ..., description="The line number where the comment appears.", examples=[10]
13 )
16class MultiLineComment(BaseModel):
17 content: List[str] = Field(
18 ...,
19 description="The content of the multi line comment.",
20 examples=[["/*", "Multi-line", "C", "comment", "*/"]],
21 )
22 start_line_number: int = Field(
23 ...,
24 description="The line number where the multi line comment starts.",
25 examples=[15],
26 )
27 end_line_number: int = Field(
28 ...,
29 description="The line number where the multi line comment ends.",
30 examples=[20],
31 )
34class Docstring(BaseModel):
35 content: List[str] = Field(
36 ...,
37 description="The content of the docstring.",
38 examples=[[
39 '"""',
40 "This is a Python docstring.",
41 "It provides information about a function.",
42 '"""',
43 ]],
44 )
45 function_name: str = Field(
46 ...,
47 description="The name of the function that the docstring belongs to.",
48 examples=["foo"],
49 )
50 start_line_number: int = Field(
51 ..., description="The line number where the docstring starts.", examples=[25]
52 )
53 end_line_number: int = Field(
54 ..., description="The line number where the docstring ends.", examples=[30]
55 )
58class SingleFileCommentRequest(BaseModel):
59 source: str = Field(
60 ...,
61 description="The source code of the file.",
62 examples=["def foo():\n # Single line Python comment\n pass"],
63 )
64 language: str = Field(
65 ...,
66 description="The programming language of the source code.",
67 examples=["python"],
68 )
71class SingleFileCommentResponse(BaseModel):
72 single: List[SingleLineComment] = Field(
73 ...,
74 description="List of single line comments in the file.",
75 examples=[[
76 {"content": "# Comment 1", "line_number": 10},
77 {"content": "# Comment 2", "line_number": 15},
78 ]],
79 )
80 multi: List[MultiLineComment] = Field(
81 ...,
82 description="List of multi line comments in the file.",
83 examples=[[
84 {
85 "content": ["/*", "Multi-line", "C comment", "*/"],
86 "start_line_number": 5,
87 "end_line_number": 8,
88 }
89 ]],
90 )
91 docstring: List[Docstring] = Field(
92 ...,
93 description="List of docstrings in the file.",
94 examples=[[
95 {
96 "content": ['"""', "This is a sample", "docstring.", '"""'],
97 "function_name": "my_function",
98 "start_line_number": 20,
99 "end_line_number": 25,
100 }
101 ]],
102 )
105class MultiFileCommentRequest(BaseModel):
106 files: Dict[str, SingleFileCommentRequest] = Field(
107 ...,
108 description="Dictionary of file names and SingleFileCommentRequest objects.",
109 examples=[{
110 "file1.py": {
111 "source": "def func():\n # Comment\n pass",
112 "language": "Python",
113 },
114 "file2.c": {"source": "/*\nMulti-line\ncomment\n*/", "language": "C"},
115 }],
116 )
119class MultiFileCommentResponse(BaseModel):
120 files: Dict[str, SingleFileCommentResponse] = Field(
121 ...,
122 description="Dictionary of file names and SingleFileCommentResponse objects.",
123 examples=[{
124 "file1.py": {
125 "single": [{"content": "# Comment 1", "line_number": 5}],
126 "multi": [],
127 "docstring": [],
128 },
129 "file2.c": {
130 "single": [],
131 "multi": [
132 {
133 "content": ["/*", "Multi-line", "C comment", "*/"],
134 "start_line_number": 10,
135 "end_line_number": 13,
136 }
137 ],
138 "docstring": [],
139 },
140 }],
141 )
144class SupportedLanguage(BaseModel):
145 name: str = Field(
146 ...,
147 description="The name of the supported programming language.",
148 examples=["python"],
149 )
150 extensions: List[str] = Field(
151 ...,
152 description="List of file extensions supported for corresponding language",
153 )
154 single: bool = Field(
155 ...,
156 description="Indicates whether single line comments are supported for this language.",
157 )
158 multi: bool = Field(
159 ...,
160 description="Indicates whether multi line comments are supported for this language.",
161 )
162 docstring: bool = Field(
163 ..., description="Indicates whether docstrings are supported for this language."
164 )
167class SupportedLanguageResponse(BaseModel):
168 languages: List[SupportedLanguage] = Field(
169 ...,
170 description="List of SupportedLanguage objects representing the supported languages.",
171 examples=[[
172 {"name": "python", "single": True, "multi": False, "docstring": True},
173 {"name": "c", "single": True, "multi": True, "docstring": True},
174 ]],
175 )
178CodeComments = Union[SingleFileCommentResponse, MultiFileCommentResponse]