| Conditions | 2 |
| Total Lines | 91 |
| Code Lines | 61 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | """ |
||
| 184 | def startup(self): |
||
| 185 | |||
| 186 | workspace = self.workspace |
||
| 187 | |||
| 188 | app = FastAPI( |
||
| 189 | title="OCR-D METS Server", |
||
| 190 | description="Providing simultaneous write-access to mets.xml for OCR-D", |
||
| 191 | ) |
||
| 192 | |||
| 193 | @app.exception_handler(ValidationError) |
||
| 194 | async def exception_handler_validation_error(request: Request, exc: ValidationError): |
||
| 195 | return JSONResponse(status_code=400, content=exc.errors()) |
||
| 196 | |||
| 197 | @app.exception_handler(FileExistsError) |
||
| 198 | async def exception_handler_file_exists(request: Request, exc: FileExistsError): |
||
| 199 | return JSONResponse(status_code=400, content=str(exc)) |
||
| 200 | |||
| 201 | @app.exception_handler(re.error) |
||
| 202 | async def exception_handler_invalid_regex(request: Request, exc: re.error): |
||
| 203 | return JSONResponse(status_code=400, content=f'invalid regex: {exc}') |
||
| 204 | |||
| 205 | @app.get("/file", response_model=OcrdFileListModel) |
||
| 206 | async def find_files( |
||
| 207 | file_grp : Union[str, None] = None, |
||
| 208 | file_id : Union[str, None] = None, |
||
| 209 | page_id : Union[str, None] = None, |
||
| 210 | mimetype : Union[str, None] = None, |
||
| 211 | ): |
||
| 212 | """ |
||
| 213 | Find files in the mets |
||
| 214 | """ |
||
| 215 | found = workspace.mets.find_all_files(fileGrp=file_grp, ID=file_id, pageId=page_id, mimetype=mimetype) |
||
| 216 | return OcrdFileListModel.create(found) |
||
| 217 | |||
| 218 | @app.put('/') |
||
| 219 | def save(): |
||
| 220 | return workspace.save_mets() |
||
| 221 | |||
| 222 | @app.post('/file', response_model=OcrdFileModel) |
||
| 223 | async def add_file( |
||
| 224 | file_grp : str = Form(), |
||
| 225 | file_id : str = Form(), |
||
| 226 | page_id : Union[str, None] = Form(), |
||
| 227 | mimetype : str = Form(), |
||
| 228 | url : str = Form(), |
||
| 229 | local_filename : str = Form(), |
||
| 230 | ): |
||
| 231 | """ |
||
| 232 | Add a file |
||
| 233 | """ |
||
| 234 | # Validate |
||
| 235 | file_resource = OcrdFileModel.create(file_grp=file_grp, file_id=file_id, page_id=page_id, mimetype=mimetype, url=url, local_filename=local_filename) |
||
| 236 | # Add to workspace |
||
| 237 | kwargs = file_resource.dict() |
||
| 238 | workspace.add_file(**kwargs) |
||
| 239 | return file_resource |
||
| 240 | |||
| 241 | @app.get('/file_groups', response_model=OcrdFileGroupListModel) |
||
| 242 | async def file_groups(): |
||
| 243 | return {'file_groups': workspace.mets.file_groups} |
||
| 244 | |||
| 245 | @app.post('/agent', response_model=OcrdAgentModel) |
||
| 246 | async def add_agent(agent : OcrdAgentModel): |
||
| 247 | kwargs = agent.dict() |
||
| 248 | workspace.mets.add_agent(**kwargs) |
||
| 249 | return agent |
||
| 250 | |||
| 251 | @app.get('/agent', response_model=OcrdAgentListModel) |
||
| 252 | async def agents(): |
||
| 253 | return OcrdAgentListModel.create(workspace.mets.agents) |
||
| 254 | |||
| 255 | @app.get('/unique_identifier', response_model=str) |
||
| 256 | async def unique_identifier(): |
||
| 257 | return Response(content=workspace.mets.unique_identifier, media_type='text/plain') |
||
| 258 | |||
| 259 | @app.delete('/') |
||
| 260 | async def stop(): |
||
| 261 | """ |
||
| 262 | Stop the server |
||
| 263 | """ |
||
| 264 | getLogger('ocrd_models.ocrd_mets').info('Shutting down') |
||
| 265 | workspace.save_mets() |
||
| 266 | # os._exit because uvicorn catches SystemExit raised by sys.exit |
||
| 267 | _exit(0) |
||
| 268 | |||
| 269 | if self.url.startswith('http'): |
||
| 270 | parsed = urlparse(self.url) |
||
| 271 | uvicorn_kwargs = {'host': parsed.hostname, 'port': parsed.port} |
||
| 272 | else: |
||
| 273 | uvicorn_kwargs = {'uds': self.url} |
||
| 274 | uvicorn.run(app, **uvicorn_kwargs) |
||
| 275 |