def matchup_from_config(self, matchup_number, matchup_config, matchup_defaults): """Make a Matchup from a Matchup_config. This does the following checks and fixups before calling make_matchup(): Checks that the player_1 and player_2 parameters exist, and that the player codes are present in self.players. Validates all the matchup_config arguments, and merges them with the defaults. If player_1 and player_2 are the same, takes the following actions: - sets player_2 to <player_1>#2 - if it doesn't already exist, creates <player_1>#2 as a clone of player_1 and adds it to self.players """ matchup_id = str(matchup_number) try: arguments = matchup_config.resolve_arguments() if 'id' in arguments: try: matchup_id = interpret_identifier(arguments['id']) except ValueError, e: raise ValueError("'id': %s" % e) try: player_1 = arguments['player_1'] player_2 = arguments['player_2'] except KeyError: raise ControlFileError("not enough arguments") if player_1 not in self.players: raise ControlFileError("unknown player %s" % player_1) if player_2 not in self.players: raise ControlFileError("unknown player %s" % player_2) # If both players are the same, make a clone. if player_1 == player_2: player_2 += "#2" if player_2 not in self.players: self.players[player_2] = \ self.players[player_1].copy(player_2) interpreted = load_settings(tournaments.matchup_settings, arguments, apply_defaults=False, allow_missing=True) matchup_name = arguments.get('name') if matchup_name is not None: try: matchup_name = interpret_as_utf8(matchup_name) except ValueError, e: raise ValueError("'name': %s" % e)
def competitor_spec_from_config(self, i, competitor_config): """Make a Competitor_spec from a Competitor_config. i -- ordinal number of the competitor. Raises ControlFileError if there is an error in the configuration. Returns a Competitor_spec with all attributes set. """ arguments = competitor_config.resolve_arguments() cspec = Competitor_spec() if 'player' not in arguments: raise ValueError("player not specified") cspec.player = arguments['player'] if cspec.player not in self.players: raise ControlFileError("unknown player") def let(n): return chr(ord('A') + n) if i < 26: cspec.short_code = let(i) elif i < 26 * 27: n, m = divmod(i, 26) cspec.short_code = let(n - 1) + let(m) else: raise ValueError("too many competitors") return cspec
def _load_control_file(self): """Main implementation for __init__.""" control_s = self._read_control_file() try: self.competition_type = self._parse_competition_type(control_s) except ValueError, e: raise ControlFileError("can't find competition_type")
def initialise_from_control_file(self, config): Competition.initialise_from_control_file(self, config) try: matchup_defaults = load_settings(tournaments.matchup_settings, config, allow_missing=True) except ValueError, e: raise ControlFileError(str(e))
def _initialise_from_control_file(self, config): """Interpret the parts of the control file which belong to Ringmaster. Sets attributes from ringmaster_settings. """ try: to_set = load_settings(self.ringmaster_settings, config) except ValueError, e: raise ControlFileError(str(e))
def initialise_from_control_file(self, config): Competition.initialise_from_control_file(self, config) matchup_settings = [ setting for setting in competitions.game_settings if setting.name not in ('handicap', 'handicap_style') ] + [ Setting('rounds', allow_none(interpret_int), default=None), ] try: matchup_parameters = load_settings(matchup_settings, config) except ValueError, e: raise ControlFileError(str(e))
def _load_control_file(self): """Main implementation for __init__.""" control_s = self._read_control_file() try: self.competition_type = self._parse_competition_type(control_s) except ValueError, e: raise ControlFileError("can't find competition_type") try: competition_class = self._get_competition_class( self.competition_type) except ValueError: raise ControlFileError("unknown competition type: %s" % self.competition_type) self.competition = competition_class(self.competition_code) self.competition.set_base_directory(self.base_directory) try: control_u = control_s.decode("utf-8") except UnicodeDecodeError: raise ControlFileError("file is not encoded in utf-8") try: config = interpret_python(control_u, self.competition.control_file_globals(), display_filename=self.control_pathname) except KeyboardInterrupt: raise except ControlFileError, e:
class Allplayall(tournaments.Tournament): """A Tournament with matchups for all pairs of competitors. The game ids are like AvB_2, where A and B are the competitor short_codes and 2 is the game number between those two competitors. This tournament type doesn't permit ghost matchups. """ def control_file_globals(self): result = Competition.control_file_globals(self) result.update({ 'Competitor': Competitor_config, }) return result special_settings = [ Setting( 'competitors', interpret_sequence_of_quiet_configs(Competitor_config, allow_simple_values=True)), ] def competitor_spec_from_config(self, i, competitor_config): """Make a Competitor_spec from a Competitor_config. i -- ordinal number of the competitor. Raises ControlFileError if there is an error in the configuration. Returns a Competitor_spec with all attributes set. """ arguments = competitor_config.resolve_arguments() cspec = Competitor_spec() if 'player' not in arguments: raise ValueError("player not specified") cspec.player = arguments['player'] if cspec.player not in self.players: raise ControlFileError("unknown player") def let(n): return chr(ord('A') + n) if i < 26: cspec.short_code = let(i) elif i < 26 * 27: n, m = divmod(i, 26) cspec.short_code = let(n - 1) + let(m) else: raise ValueError("too many competitors") return cspec @staticmethod def _get_matchup_id(c1, c2): return "%sv%s" % (c1.short_code, c2.short_code) def initialise_from_control_file(self, config): Competition.initialise_from_control_file(self, config) matchup_settings = [ setting for setting in competitions.game_settings if setting.name not in ('handicap', 'handicap_style') ] + [ Setting('rounds', allow_none(interpret_int), default=None), ] try: matchup_parameters = load_settings(matchup_settings, config) except ValueError, e: raise ControlFileError(str(e)) matchup_parameters['alternating'] = True matchup_parameters['number_of_games'] = matchup_parameters.pop( 'rounds') try: specials = load_settings(self.special_settings, config) except ValueError, e: raise ControlFileError(str(e))
] try: matchup_parameters = load_settings(matchup_settings, config) except ValueError, e: raise ControlFileError(str(e)) matchup_parameters['alternating'] = True matchup_parameters['number_of_games'] = matchup_parameters.pop( 'rounds') try: specials = load_settings(self.special_settings, config) except ValueError, e: raise ControlFileError(str(e)) if not specials['competitors']: raise ControlFileError("competitors: empty list") # list of Competitor_specs self.competitors = [] seen_competitors = set() for i, competitor_spec in enumerate(specials['competitors']): try: cspec = self.competitor_spec_from_config(i, competitor_spec) except StandardError, e: code = competitor_spec.get_key() if code is None: code = i raise ControlFileError("competitor %s: %s" % (code, e)) if cspec.player in seen_competitors: raise ControlFileError("duplicate competitor: %s" % cspec.player) seen_competitors.add(cspec.player)
interpreted = load_settings(tournaments.matchup_settings, arguments, apply_defaults=False, allow_missing=True) matchup_name = arguments.get('name') if matchup_name is not None: try: matchup_name = interpret_as_utf8(matchup_name) except ValueError, e: raise ValueError("'name': %s" % e) parameters = matchup_defaults.copy() parameters.update(interpreted) return self.make_matchup(matchup_id, player_1, player_2, parameters, matchup_name) except StandardError, e: raise ControlFileError("matchup %s: %s" % (matchup_id, e)) def initialise_from_control_file(self, config): Competition.initialise_from_control_file(self, config) try: matchup_defaults = load_settings(tournaments.matchup_settings, config, allow_missing=True) except ValueError, e: raise ControlFileError(str(e)) # Check default handicap settings when possible, for friendlier error # reporting (would be caught in the matchup anyway). if 'board_size' in matchup_defaults: try: