|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 |
|
3 | 3 | import time |
4 | | - |
5 | 4 | import curses |
6 | | -import json |
7 | 5 | import signal |
8 | 6 | import threading |
9 | 7 | import traceback |
|
15 | 13 | from task_maker.printer import StdoutPrinter, Printer, CursesPrinter |
16 | 14 | from task_maker.source_file import SourceFile |
17 | 15 | from task_maker.task_maker_frontend import Result, ResultStatus, Resources |
| 16 | +from task_maker.uis.ui_printer import UIPrinter |
18 | 17 | from typing import Dict, List, Optional |
19 | 18 |
|
20 | 19 |
|
@@ -430,286 +429,6 @@ def _print_running_tasks(self, printer: CursesPrinter): |
430 | 429 | task.strip(), duration)) |
431 | 430 |
|
432 | 431 |
|
433 | | -class UIPrinter: |
434 | | - """ |
435 | | - This class will manage the printing to the console, whether if it's text |
436 | | - based or json |
437 | | - """ |
438 | | - |
439 | | - def __init__(self, printer: Printer, json: bool): |
440 | | - self.printer = printer |
441 | | - self.json = json |
442 | | - |
443 | | - def compilation_non_solution(self, |
444 | | - name: str, |
445 | | - state: str, |
446 | | - data: str = None, |
447 | | - cached: bool = False): |
448 | | - if self.json: |
449 | | - self._json("compilation-non-solution", state, {"name": name}, data, |
450 | | - cached) |
451 | | - else: |
452 | | - log = ("Compilation of non-solution %s " % name).ljust(50) |
453 | | - log += state |
454 | | - self._print(log, state, data=data, cached=cached) |
455 | | - |
456 | | - def compilation_solution(self, |
457 | | - name: str, |
458 | | - state: str, |
459 | | - data: str = None, |
460 | | - cached: bool = False): |
461 | | - if self.json: |
462 | | - self._json("compilation-solution", state, {"name": name}, data, |
463 | | - cached) |
464 | | - else: |
465 | | - log = ("Compilation of solution %s " % name).ljust(50) |
466 | | - log += state |
467 | | - self._print(log, state, data=data, cached=cached) |
468 | | - |
469 | | - def generation(self, |
470 | | - testcase: int, |
471 | | - subtask: int, |
472 | | - state: str, |
473 | | - data: str = None, |
474 | | - cached: bool = False): |
475 | | - if self.json: |
476 | | - self._json("generation", state, { |
477 | | - "testcase": testcase, |
478 | | - "subtask": subtask |
479 | | - }, data, cached) |
480 | | - else: |
481 | | - log = ("Generation of input %d of subtask %d " % |
482 | | - (testcase, subtask)).ljust(50) |
483 | | - log += state |
484 | | - self._print(log, state, data=data, cached=cached) |
485 | | - |
486 | | - def terry_generation(self, |
487 | | - solution: str, |
488 | | - seed: int, |
489 | | - state: str, |
490 | | - data: str = None, |
491 | | - cached: bool = False): |
492 | | - if self.json: |
493 | | - self._json("generation", state, { |
494 | | - "solution": solution, |
495 | | - "seed": seed |
496 | | - }, data, cached) |
497 | | - else: |
498 | | - log = ("Generation of input for %s with seed %d " % |
499 | | - (solution, seed)).ljust(50) |
500 | | - log += state |
501 | | - self._print(log, state, data=data, cached=cached) |
502 | | - |
503 | | - def validation(self, |
504 | | - testcase: int, |
505 | | - subtask: int, |
506 | | - state: str, |
507 | | - data: str = None, |
508 | | - cached: bool = False): |
509 | | - if self.json: |
510 | | - self._json("validation", state, { |
511 | | - "testcase": testcase, |
512 | | - "subtask": subtask |
513 | | - }, data, cached) |
514 | | - else: |
515 | | - log = ("Validation of input %d of subtask %d " % |
516 | | - (testcase, subtask)).ljust(50) |
517 | | - log += state |
518 | | - self._print(log, state, data=data, cached=cached) |
519 | | - |
520 | | - def terry_validation(self, |
521 | | - solution: str, |
522 | | - state: str, |
523 | | - data: str = None, |
524 | | - cached: bool = False): |
525 | | - if self.json: |
526 | | - self._json("validation", state, {"solution": solution}, data, |
527 | | - cached) |
528 | | - else: |
529 | | - log = ("Validation of input for %s " % solution).ljust(50) |
530 | | - log += state |
531 | | - self._print(log, state, data=data, cached=cached) |
532 | | - |
533 | | - def solving(self, |
534 | | - testcase: int, |
535 | | - subtask: int, |
536 | | - state: str, |
537 | | - data: str = None, |
538 | | - cached: bool = False): |
539 | | - if self.json: |
540 | | - self._json("solving", state, { |
541 | | - "testcase": testcase, |
542 | | - "subtask": subtask |
543 | | - }, data, cached) |
544 | | - else: |
545 | | - log = ("Generation of output %d of subtask %d " % |
546 | | - (testcase, subtask)).ljust(50) |
547 | | - log += state |
548 | | - self._print(log, state, data=data, cached=cached) |
549 | | - |
550 | | - def evaluate(self, |
551 | | - solution: str, |
552 | | - num: int, |
553 | | - num_processes: int, |
554 | | - testcase: int, |
555 | | - subtask: int, |
556 | | - state: str, |
557 | | - data: str = None, |
558 | | - cached: bool = False): |
559 | | - if self.json: |
560 | | - self._json( |
561 | | - "evaluate", state, { |
562 | | - "solution": solution, |
563 | | - "num": num, |
564 | | - "num_processes": num_processes, |
565 | | - "testcase": testcase, |
566 | | - "subtask": subtask |
567 | | - }, data, cached) |
568 | | - else: |
569 | | - if num_processes == 1: |
570 | | - log = "Evaluate %s on case %d of subtask %d " % ( |
571 | | - solution, testcase, subtask) |
572 | | - else: |
573 | | - log = "Evaluate %s (%d/%d) on case %d of subtask %d " % ( |
574 | | - solution, num + 1, num_processes, testcase, subtask) |
575 | | - log = log.ljust(50) + state |
576 | | - self._print(log, state, data=data, cached=cached) |
577 | | - |
578 | | - def terry_evaluate(self, |
579 | | - solution: str, |
580 | | - state: str, |
581 | | - data: str = None, |
582 | | - cached: bool = False): |
583 | | - if self.json: |
584 | | - self._json("evaluate", state, {"solution": solution}, data, cached) |
585 | | - else: |
586 | | - log = ("Evaluate solution %s " % solution).ljust(50) |
587 | | - log += state |
588 | | - self._print(log, state, data=data, cached=cached) |
589 | | - |
590 | | - def checking(self, |
591 | | - solution: str, |
592 | | - testcase: int, |
593 | | - subtask: int, |
594 | | - state: str, |
595 | | - data: str = None, |
596 | | - cached: bool = False): |
597 | | - if self.json: |
598 | | - self._json("checking", state, { |
599 | | - "solution": solution, |
600 | | - "testcase": testcase, |
601 | | - "subtask": subtask |
602 | | - }, data, cached) |
603 | | - else: |
604 | | - log = ("Checking solution %s on case %d of subtask %d " % |
605 | | - (solution, testcase, subtask)).ljust(50) |
606 | | - log += state |
607 | | - self._print(log, state, data=data, cached=cached) |
608 | | - |
609 | | - def terry_checking(self, |
610 | | - solution: str, |
611 | | - state: str, |
612 | | - data: str = None, |
613 | | - cached: bool = False): |
614 | | - if self.json: |
615 | | - self._json("checking", state, {"solution": solution}, data, cached) |
616 | | - else: |
617 | | - log = ("Checking solution %s " % solution).ljust(50) |
618 | | - log += state |
619 | | - self._print(log, state, data=data, cached=cached) |
620 | | - |
621 | | - def testcase_outcome(self, solution: str, testcase: int, subtask: int, |
622 | | - info: "TestcaseSolutionInfo"): |
623 | | - if self.json: |
624 | | - self._json( |
625 | | - "testcase-outcome", "success", { |
626 | | - "solution": solution, |
627 | | - "testcase": testcase, |
628 | | - "subtask": subtask, |
629 | | - "status": str(info.status).split(".")[-1], |
630 | | - "score": info.score, |
631 | | - "message": info.message |
632 | | - }) |
633 | | - else: |
634 | | - log = "Outcome of solution %s: score=%f message=%s" % ( |
635 | | - solution, info.score, info.message) |
636 | | - self._print(log, "SUCCESS") |
637 | | - |
638 | | - def terry_solution_outcome(self, solution: str, info: "SolutionInfo"): |
639 | | - if self.json: |
640 | | - self._json( |
641 | | - "solution-outcome", "success", { |
642 | | - "solution": |
643 | | - solution, |
644 | | - "status": |
645 | | - str(info.status).split(".")[-1], |
646 | | - "score": |
647 | | - info.score, |
648 | | - "message": |
649 | | - info.message, |
650 | | - "testcases": |
651 | | - [str(s).split(".")[-1] for s in info.testcases_status] |
652 | | - }) |
653 | | - else: |
654 | | - log = "Outcome of solution %s: score=%f message=%s" % ( |
655 | | - solution, info.score, info.message) |
656 | | - self._print(log, "SUCCESS") |
657 | | - |
658 | | - def warning(self, message: str): |
659 | | - if self.json: |
660 | | - self._json("warning", "warning", {"message": message}) |
661 | | - else: |
662 | | - self._print("WARNING", "WARNING", data=message) |
663 | | - |
664 | | - def error(self, message: str): |
665 | | - if self.json: |
666 | | - self._json("error", "error", {"message": message}) |
667 | | - else: |
668 | | - self._print("ERROR", "ERROR", data=message) |
669 | | - |
670 | | - def _print(self, |
671 | | - prefix: str, |
672 | | - state: str, |
673 | | - data: str = None, |
674 | | - cached: bool = False): |
675 | | - if cached: |
676 | | - prefix += " [cached]" |
677 | | - if state == "WAITING": |
678 | | - self.printer.text(prefix + "\n") |
679 | | - elif state == "SKIPPED": |
680 | | - self.printer.yellow(prefix + "\n") |
681 | | - elif state == "START": |
682 | | - self.printer.text(prefix + "\n") |
683 | | - elif state == "SUCCESS": |
684 | | - self.printer.green(prefix + "\n") |
685 | | - elif state == "WARNING": |
686 | | - self.printer.yellow(prefix + " " + str(data) + "\n") |
687 | | - elif state == "FAIL" or state == "ERROR": |
688 | | - self.printer.red(prefix + " " + str(data) + "\n") |
689 | | - elif state == "STDERR": |
690 | | - if data: |
691 | | - self.printer.text(prefix + "\n" + str(data) + "\n") |
692 | | - else: |
693 | | - raise ValueError("Unknown state " + state) |
694 | | - |
695 | | - def _json(self, |
696 | | - action: str, |
697 | | - state: str, |
698 | | - extra: dict, |
699 | | - data: str = None, |
700 | | - cached: bool = False): |
701 | | - data = { |
702 | | - "action": action, |
703 | | - "state": state, |
704 | | - "data": data, |
705 | | - "cached": cached |
706 | | - } |
707 | | - for k, v in extra.items(): |
708 | | - data[k] = v |
709 | | - res = json.dumps(data) |
710 | | - print(res, flush=True) |
711 | | - |
712 | | - |
713 | 432 | def result_to_str(result: Result) -> str: |
714 | 433 | status = result.status |
715 | 434 | if status == ResultStatus.SUCCESS: |
|
0 commit comments