6
0

result.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import click
  2. import flask
  3. from flask.cli import AppGroup
  4. from pycs import app
  5. from pycs.database.Project import Project
  6. from pycs.interfaces.MediaStorage import MediaStorage
  7. result_cli = AppGroup("result", short_help="Result operations")
  8. @result_cli.command("export")
  9. @click.argument("project_id")
  10. @click.argument("indent", required=False)
  11. @click.argument("output", required=False)
  12. def export(project_id, output, indent):
  13. """ Export results for a specific project or for all projects """
  14. if project_id == "all":
  15. projects = Project.query.all()
  16. app.logger.info(f"Exporting results for all projects ({len(projects)})!")
  17. if output is None:
  18. output = "output.json"
  19. else:
  20. project = Project.query.get(project_id)
  21. if project is None:
  22. app.logger.error(f"Could not find project with ID {project_id}!")
  23. return
  24. app.logger.info(f"Exporting results for project {project}!")
  25. projects = [project]
  26. if output is None:
  27. output = f"output_project_{int(project_id):04d}.json"
  28. app.logger.info(f"Exporting to {output}")
  29. results = []
  30. for project in projects:
  31. storage = MediaStorage(project.id, None)
  32. project_files = [f.serialize() for f in storage.files().iter() if len(f.results()) != 0]
  33. results.append(dict(
  34. project_id=project.id,
  35. files=project_files,
  36. ))
  37. if indent is not None:
  38. indent = int(indent)
  39. with open(output, "w", encoding="utf-8") as out_f:
  40. flask.json.dump(results, out_f, app=app, indent=indent)