| Conditions | 1 |
| Total Lines | 89 |
| Code Lines | 57 |
| 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 | """ |
||
| 147 | def startup(self): |
||
| 148 | |||
| 149 | # XXX HACK |
||
| 150 | # circumventing dependency injection like this is bad and |
||
| 151 | # needs to be refactored once it's all runnign |
||
| 152 | workspace = self.workspace |
||
| 153 | |||
| 154 | app = FastAPI( |
||
| 155 | title="OCR-D METS Server", |
||
| 156 | description="Providing simultaneous write-access to mets.xml for OCR-D", |
||
| 157 | ) |
||
| 158 | |||
| 159 | @app.exception_handler(ValidationError) |
||
| 160 | async def exception_handler_invalid400(request: Request, exc: ValidationError): |
||
| 161 | return JSONResponse(status_code=400, content=exc.errors()) |
||
| 162 | |||
| 163 | @app.exception_handler(FileExistsError) |
||
| 164 | async def exception_handler_invalid400(request: Request, exc: FileExistsError): |
||
| 165 | return JSONResponse(status_code=400, content=str(exc)) |
||
| 166 | |||
| 167 | @app.exception_handler(re.error) |
||
| 168 | async def exception_handler_invalid400(request: Request, exc: re.error): |
||
| 169 | return JSONResponse(status_code=400, content=f'invalid regex: {exc}') |
||
| 170 | |||
| 171 | @app.get("/", response_model=OcrdFileListModel) |
||
| 172 | async def find_files( |
||
| 173 | file_grp : Union[str, None] = None, |
||
| 174 | file_id : Union[str, None] = None, |
||
| 175 | page_id : Union[str, None] = None, |
||
| 176 | mimetype : Union[str, None] = None, |
||
| 177 | ): |
||
| 178 | """ |
||
| 179 | Find files in the mets |
||
| 180 | """ |
||
| 181 | found = workspace.mets.find_all_files(fileGrp=file_grp, ID=file_id, pageId=page_id, mimetype=mimetype) |
||
| 182 | return OcrdFileListModel( |
||
| 183 | files=[OcrdFileModel(file_grp=of.fileGrp, file_id=of.ID, mimetype=of.mimetype, page_id=of.pageId, url=of.url) for of in found] |
||
| 184 | ) |
||
| 185 | |||
| 186 | @app.put('/') |
||
| 187 | def save(): |
||
| 188 | return workspace.save_mets() |
||
| 189 | |||
| 190 | @app.post('/', response_model=OcrdFileModel) |
||
| 191 | async def add_file( |
||
| 192 | data : bytes = File(), |
||
| 193 | file_grp : str = Form(), |
||
| 194 | file_id : str = Form(), |
||
| 195 | page_id : Union[str, None] = Form(), |
||
| 196 | mimetype : str = Form(), |
||
| 197 | url : str = Form(), |
||
| 198 | ): |
||
| 199 | """ |
||
| 200 | Add a file |
||
| 201 | """ |
||
| 202 | # Validate |
||
| 203 | file_resource = OcrdFileModel(file_grp=file_grp, file_id=file_id, page_id=page_id, mimetype=mimetype, url=url) |
||
| 204 | # Add to workspace |
||
| 205 | kwargs = file_resource.dict() |
||
| 206 | kwargs['page_id'] = page_id |
||
| 207 | kwargs['content'] = data |
||
| 208 | kwargs['local_filename'] = kwargs.pop('url') |
||
| 209 | workspace.add_file(**kwargs) |
||
| 210 | workspace.save_mets() |
||
| 211 | return file_resource |
||
| 212 | |||
| 213 | @app.get('/file_groups', response_model=OcrdFileGroupListModel) |
||
| 214 | async def file_groups(): |
||
| 215 | return {'file_groups': workspace.mets.file_groups} |
||
| 216 | |||
| 217 | @app.post('/agent', response_model=OcrdAgentModel) |
||
| 218 | async def add_agent(agent : OcrdAgentModel): |
||
| 219 | kwargs = agent.dict() |
||
| 220 | workspace.mets.add_agent(**kwargs) |
||
| 221 | workspace.save_mets() |
||
| 222 | return agent |
||
| 223 | |||
| 224 | @app.delete('/') |
||
| 225 | async def stop(): |
||
| 226 | """ |
||
| 227 | Stop the server |
||
| 228 | """ |
||
| 229 | getLogger('ocrd_models.ocrd_mets').info('Shutting down') |
||
| 230 | workspace.save_mets() |
||
| 231 | # XXX HACK os._exit to not trigger SystemExit caught by uvicorn with sys.exit |
||
| 232 | _exit(0) |
||
| 233 | |||
| 234 | |||
| 235 | uvicorn.run(app, host=self.host, port=self.port, uds=self.socket) |
||
| 236 | |||
| 238 |