# Path: marathoner/commands/tag_one.py
# class Command(BaseCommand):
#     syntax = 'tag <tag>'
#     help = 'print scores of selected tag'
# 
#     cmd_re = re.compile(r'^\s*tags?\s+(\w+)\s*$', re.IGNORECASE)
#     def is_match(self, command):
#         match = self.cmd_re.match(command)
#         return match and match.group(1) not in ('create', 'delete')
# 
#     def handle(self, command):
#         tag_name = self.cmd_re.match(command).group(1)
#         tag = self.project.tags.get(tag_name)
#         if tag is None:
#             print_('Tag "%s" doesn\'t exist.' % tag_name)
#             return
# 
#         # prepare the data for table
#         header = [['Seed', 'Score', 'Best', 'Relative', 'Run time']]
#         table = []
#         zero_seeds = []
#         score_sum = 0.0
# 
#         for seed in sorted(tag.scores.seeds):
#             best_score = self.project.scores[seed]
#             current_score = tag.scores[seed]
#             relative = Score.relative_score(
#                 self.project.maximize, current_score, best_score)
#             score_sum += relative
#             if not current_score.score:
#                 zero_seeds.append(seed)
#             table.append([
#                 seed,
#                 '%.2f' % current_score.score,
#                 '%.2f' % best_score.score,
#                 '%.3f' % relative,
#                 '%.2f' % current_score.run_time])
# 
#         print_table(header, table, header)
#         print_()
#         num_tests = len(tag.scores)
#         print_('Relative score of "%s" tag on %s tests: %.5f' % (tag_name, num_tests, score_sum))
#         if num_tests:
#             print_('Average relative score: %.5f' % (score_sum / num_tests))
#         if zero_seeds:
#             print_('You have scored zero points on %s seeds. Here are some of the seeds: %s' %
#                    (len(zero_seeds), zero_seeds[:10]))
# 
# Path: marathoner/commands/base.py
# class BaseCommand(object):
#     syntax = ''
#     help = ''
# 
#     def __init__(self, project, executor):
#         self.project = project
#         self.executor = executor
#         self.contest = project.contest
# 
#     def is_match(self, command):
#         '''Given is the text that user typed into command line, return True
#         if this command should execute.
#         '''
#         raise NotImplementedError()
# 
#     def handle(self, command):
#         '''Given is the text that user typed into command line, execute the
#         command.
#         '''
# 
# class CommandSyntaxError(Exception):
#     pass


import re
from six import print_
from .tag_one import Command as TagOneCommand
from marathoner.commands.base import BaseCommand, CommandSyntaxError




class Command(BaseCommand):
    syntax = 'cached <tag> <seed> [<vis params>]'
    help = 'run a cached solution of the selected tag'

    cmd_re = re.compile(r'^\s*cached\s+(\w+)(?:\s+(\d+)(?:\s+([^\d\s].*))?)?\s*$', re.IGNORECASE)
    match_re = re.compile(r'^\s*cached\b', re.IGNORECASE)
    def is_match(self, command):
        return self.match_re.match(command)

    def handle(self, command):
        match = self.cmd_re.match(command)
        if not match:
            raise CommandSyntaxError
        tag_name = match.group(1)
        tag = self.project.tags.get(tag_name)
        if tag is None:
            print_('Tag "%s" doesn\'t exist.' % tag_name)
            return

        if match.group(2) is None:
            command = 'tag %s' % tag_name
            TagOneCommand(self.project, self.executor).handle(command)
        else:
            seed = int(match.group(2))

# Path: marathoner/commands/tag_one.py
# class Command(BaseCommand):
#     syntax = 'tag <tag>'
#     help = 'print scores of selected tag'
# 
#     cmd_re = re.compile(r'^\s*tags?\s+(\w+)\s*$', re.IGNORECASE)
#     def is_match(self, command):
#         match = self.cmd_re.match(command)
#         return match and match.group(1) not in ('create', 'delete')
# 
#     def handle(self, command):
#         tag_name = self.cmd_re.match(command).group(1)
#         tag = self.project.tags.get(tag_name)
#         if tag is None:
#             print_('Tag "%s" doesn\'t exist.' % tag_name)
#             return
# 
#         # prepare the data for table
#         header = [['Seed', 'Score', 'Best', 'Relative', 'Run time']]
#         table = []
#         zero_seeds = []
#         score_sum = 0.0
# 
#         for seed in sorted(tag.scores.seeds):
#             best_score = self.project.scores[seed]
#             current_score = tag.scores[seed]
#             relative = Score.relative_score(
#                 self.project.maximize, current_score, best_score)
#             score_sum += relative
#             if not current_score.score:
#                 zero_seeds.append(seed)
#             table.append([
#                 seed,
#                 '%.2f' % current_score.score,
#                 '%.2f' % best_score.score,
#                 '%.3f' % relative,
#                 '%.2f' % current_score.run_time])
# 
#         print_table(header, table, header)
#         print_()
#         num_tests = len(tag.scores)
#         print_('Relative score of "%s" tag on %s tests: %.5f' % (tag_name, num_tests, score_sum))
#         if num_tests:
#             print_('Average relative score: %.5f' % (score_sum / num_tests))
#         if zero_seeds:
#             print_('You have scored zero points on %s seeds. Here are some of the seeds: %s' %
#                    (len(zero_seeds), zero_seeds[:10]))
# 
# Path: marathoner/commands/base.py
# class BaseCommand(object):
#     syntax = ''
#     help = ''
# 
#     def __init__(self, project, executor):
#         self.project = project
#         self.executor = executor
#         self.contest = project.contest
# 
#     def is_match(self, command):
#         '''Given is the text that user typed into command line, return True
#         if this command should execute.
#         '''
#         raise NotImplementedError()
# 
#     def handle(self, command):
#         '''Given is the text that user typed into command line, execute the
#         command.
#         '''
# 
# class CommandSyntaxError(Exception):
#     pass


# Path: marathoner/commands/cached.py
import re
from six import print_
from .tag_one import Command as TagOneCommand
from marathoner.commands.base import BaseCommand, CommandSyntaxError





class Command(BaseCommand):
    syntax = 'cached <tag> <seed> [<vis params>]'
    help = 'run a cached solution of the selected tag'

    cmd_re = re.compile(r'^\s*cached\s+(\w+)(?:\s+(\d+)(?:\s+([^\d\s].*))?)?\s*$', re.IGNORECASE)
    match_re = re.compile(r'^\s*cached\b', re.IGNORECASE)
    def is_match(self, command):
        return self.match_re.match(command)

    def handle(self, command):
        match = self.cmd_re.match(command)
        if not match:
            raise CommandSyntaxError
        tag_name = match.group(1)
        tag = self.project.tags.get(tag_name)
        if tag is None:
            print_('Tag "%s" doesn\'t exist.' % tag_name)
            return

        if match.group(2) is None:
            command = 'tag %s' % tag_name
            TagOneCommand(self.project, self.executor).handle(command)
        else:
            seed = int(match.group(2))

            vis_params = match.group(3) or ''