| Conditions | 2 | 
| Total Lines | 115 | 
| Code Lines | 79 | 
| 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 | """  | 
            ||
| 210 | def startup(self):  | 
            ||
| 211 |         self.log.info("Starting up METS server") | 
            ||
| 212 | |||
| 213 | workspace = self.workspace  | 
            ||
| 214 | |||
| 215 | app = FastAPI(  | 
            ||
| 216 | title="OCR-D METS Server",  | 
            ||
| 217 | description="Providing simultaneous write-access to mets.xml for OCR-D",  | 
            ||
| 218 | )  | 
            ||
| 219 | |||
| 220 | @app.exception_handler(ValidationError)  | 
            ||
| 221 | async def exception_handler_validation_error(request: Request, exc: ValidationError):  | 
            ||
| 222 | return JSONResponse(status_code=400, content=exc.errors())  | 
            ||
| 223 | |||
| 224 | @app.exception_handler(FileExistsError)  | 
            ||
| 225 | async def exception_handler_file_exists(request: Request, exc: FileExistsError):  | 
            ||
| 226 | return JSONResponse(status_code=400, content=str(exc))  | 
            ||
| 227 | |||
| 228 | @app.exception_handler(re.error)  | 
            ||
| 229 | async def exception_handler_invalid_regex(request: Request, exc: re.error):  | 
            ||
| 230 |             return JSONResponse(status_code=400, content=f'invalid regex: {exc}') | 
            ||
| 231 | |||
| 232 |         @app.get("/file", response_model=OcrdFileListModel) | 
            ||
| 233 | async def find_files(  | 
            ||
| 234 | file_grp : Optional[str] = None,  | 
            ||
| 235 | file_id : Optional[str] = None,  | 
            ||
| 236 | page_id : Optional[str] = None,  | 
            ||
| 237 | mimetype : Optional[str] = None,  | 
            ||
| 238 | local_filename : Optional[str] = None,  | 
            ||
| 239 | url : Optional[str] = None,  | 
            ||
| 240 | ):  | 
            ||
| 241 | """  | 
            ||
| 242 | Find files in the mets  | 
            ||
| 243 | """  | 
            ||
| 244 | found = workspace.mets.find_all_files(fileGrp=file_grp, ID=file_id, pageId=page_id, mimetype=mimetype, local_filename=local_filename, url=url)  | 
            ||
| 245 | return OcrdFileListModel.create(found)  | 
            ||
| 246 | |||
| 247 |         @app.put('/') | 
            ||
| 248 | def save():  | 
            ||
| 249 | return workspace.save_mets()  | 
            ||
| 250 | |||
| 251 |         @app.post('/file', response_model=OcrdFileModel) | 
            ||
| 252 | async def add_file(  | 
            ||
| 253 | file_grp : str = Form(),  | 
            ||
| 254 | file_id : str = Form(),  | 
            ||
| 255 | page_id : Optional[str] = Form(),  | 
            ||
| 256 | mimetype : str = Form(),  | 
            ||
| 257 | url : Optional[str] = Form(None),  | 
            ||
| 258 | local_filename : Optional[str] = Form(None),  | 
            ||
| 259 | ):  | 
            ||
| 260 | """  | 
            ||
| 261 | Add a file  | 
            ||
| 262 | """  | 
            ||
| 263 | # Validate  | 
            ||
| 264 | file_resource = OcrdFileModel.create(file_grp=file_grp, file_id=file_id, page_id=page_id, mimetype=mimetype, url=url, local_filename=local_filename)  | 
            ||
| 265 | # Add to workspace  | 
            ||
| 266 | kwargs = file_resource.dict()  | 
            ||
| 267 | workspace.add_file(**kwargs)  | 
            ||
| 268 | return file_resource  | 
            ||
| 269 | |||
| 270 |         @app.get('/file_groups', response_model=OcrdFileGroupListModel) | 
            ||
| 271 | async def file_groups():  | 
            ||
| 272 |             return {'file_groups': workspace.mets.file_groups} | 
            ||
| 273 | |||
| 274 |         @app.post('/agent', response_model=OcrdAgentModel) | 
            ||
| 275 | async def add_agent(agent : OcrdAgentModel):  | 
            ||
| 276 | kwargs = agent.dict()  | 
            ||
| 277 |             kwargs['_type'] = kwargs.pop('type') | 
            ||
| 278 | workspace.mets.add_agent(**kwargs)  | 
            ||
| 279 | return agent  | 
            ||
| 280 | |||
| 281 |         @app.get('/agent', response_model=OcrdAgentListModel) | 
            ||
| 282 | async def agents():  | 
            ||
| 283 | return OcrdAgentListModel.create(workspace.mets.agents)  | 
            ||
| 284 | |||
| 285 |         @app.get('/unique_identifier', response_model=str) | 
            ||
| 286 | async def unique_identifier():  | 
            ||
| 287 | return Response(content=workspace.mets.unique_identifier, media_type='text/plain')  | 
            ||
| 288 | |||
| 289 |         @app.get('/workspace_path', response_model=str) | 
            ||
| 290 | async def workspace_path():  | 
            ||
| 291 | return Response(content=workspace.directory, media_type="text/plain")  | 
            ||
| 292 | |||
| 293 |         @app.post('/reload') | 
            ||
| 294 | async def workspace_reload_mets():  | 
            ||
| 295 | workspace.reload_mets()  | 
            ||
| 296 |             return Response(content=f'Reloaded from {workspace.directory}', media_type="text/plain") | 
            ||
| 297 | |||
| 298 |         @app.delete('/') | 
            ||
| 299 | async def stop():  | 
            ||
| 300 | """  | 
            ||
| 301 | Stop the server  | 
            ||
| 302 | """  | 
            ||
| 303 |             getLogger('ocrd.models.ocrd_mets').info(f'Shutting down METS Server {self.url}') | 
            ||
| 304 | workspace.save_mets()  | 
            ||
| 305 | self.shutdown()  | 
            ||
| 306 | |||
| 307 | # ------------- #  | 
            ||
| 308 | |||
| 309 | if self.is_uds:  | 
            ||
| 310 | # Create socket and change to world-readable and -writable to avoid  | 
            ||
| 311 | # permsission errors  | 
            ||
| 312 |             self.log.debug(f"chmod 0o677 {self.url}") | 
            ||
| 313 | server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)  | 
            ||
| 314 | server.bind(self.url) # creates the socket file  | 
            ||
| 315 | atexit.register(self.shutdown)  | 
            ||
| 316 | server.close()  | 
            ||
| 317 | chmod(self.url, 0o666)  | 
            ||
| 318 |             uvicorn_kwargs = {'uds': self.url} | 
            ||
| 319 | else:  | 
            ||
| 320 | parsed = urlparse(self.url)  | 
            ||
| 321 |             uvicorn_kwargs = {'host': parsed.hostname, 'port': parsed.port} | 
            ||
| 322 | |||
| 323 |         self.log.debug("Starting uvicorn") | 
            ||
| 324 | uvicorn.run(app, **uvicorn_kwargs)  | 
            ||
| 325 |