def __init__(self, parameters, public_testcases): """Initializer. parameters (object): format is specified in the subclasses. public_testcases (dict): associate to each testcase's codename a boolean indicating if the testcase is public. """ self.parameters = parameters self.public_testcases = public_testcases # Preload the maximum possible scores. try: self.max_score, self.max_public_score, self.ranking_headers = \ self.max_scores() self.public_score_header, self.private_score_header = \ self.score_column_headers() except Exception as e: raise ValueError( "Unable to instantiate score type (probably due to invalid " "values for the score type parameters): %s." % e) self.template = GLOBAL_ENVIRONMENT.from_string(self.TEMPLATE)
class ParameterTypeChoice(ParameterType): """Type for a parameter giving a choice among a finite number of items.""" TEMPLATE = GLOBAL_ENVIRONMENT.from_string(""" <select name="{{ prefix ~ parameter.short_name }}"> {% for choice_value, choice_description in parameter.values.items() %} <option value="{{ choice_value }}" {% if choice_value == previous_value %}selected{% endif %}> {{ choice_description }} </option> {% endfor %} </select> """) def __init__(self, name, short_name, description, values): """Initialization. values (dict): dictionary mapping each choice to a short description. """ super().__init__(name, short_name, description) self.values = values def validate(self, value): # Convert to string to avoid TypeErrors on unhashable types. if str(value) not in self.values: raise ValueError("Invalid choice %s for parameter %s" % (value, self.name)) def parse_string(self, value): if value not in self.values: raise ValueError("Value %s doesn't match any allowed choice." % value) return value
def __init__(self, file_cacher): super().__init__() self.file_cacher = file_cacher self.jinja2_env = GLOBAL_ENVIRONMENT.overlay(loader=PackageLoader( "cms.service", "templates/printing"), autoescape=False) self.jinja2_env.filters["escape_tex_normal"] = escape_tex_normal self.jinja2_env.filters["escape_tex_tt"] = escape_tex_tt
def __init__(self, file_cacher): super().__init__() self.file_cacher = file_cacher self.jinja2_env = GLOBAL_ENVIRONMENT.overlay( loader=PackageLoader("cms.service", "templates/printing"), autoescape=False) self.jinja2_env.filters["escape_tex_normal"] = escape_tex_normal self.jinja2_env.filters["escape_tex_tt"] = escape_tex_tt
class ParameterTypeInt(ParameterType): """Type for an integer parameter.""" TEMPLATE = GLOBAL_ENVIRONMENT.from_string(""" <input type="text" name="{{ prefix ~ parameter.short_name }}" value="{{ previous_value }}" /> """) def validate(self, value): if not isinstance(value, int): raise ValueError("Invalid value for int parameter %s" % self.name) def parse_string(self, value): return int(value)
class ParameterTypeCollection(ParameterType): """Type of a parameter containing a tuple of sub-parameters.""" TEMPLATE = GLOBAL_ENVIRONMENT.from_string(""" <table> {% for subp in parameter.subparameters %} {% set subp_prefix = "%s%s_%d_"|format(prefix, parameter.short_name, loop.index0) %} {% set subp_previous_value = (previous_value[loop.index0] if previous_value is not none else none) %} <tr> <td>{{ subp.name }}</td> <td>{{ subp.render(subp_prefix, subp_previous_value) }}</td> </tr> {% endfor %} </table> """) def __init__(self, name, short_name, description, subparameters): """Initialization. subparameters ([ParameterType]): list of types of each sub-parameter. """ super(ParameterTypeCollection, self).__init__(name, short_name, description) self.subparameters = subparameters def validate(self, value): if not isinstance(value, list): raise ValueError("Parameter %s should be a list" % self.name) if len(value) != len(self.subparameters): raise ValueError("Invalid value for parameter %s" % self.name) for subvalue, subparameter in zip(value, self.subparameters): subparameter.validate(subvalue) def parse_string(self, value): raise NotImplementedError( "parse_string is not implemented for composite parameter types.") def parse_handler(self, handler, prefix): parsed_values = [] for i in range(len(self.subparameters)): new_prefix = "%s%s_%d_" % (prefix, self.short_name, i) parsed_values.append(self.subparameters[i].parse_handler( handler, new_prefix)) return parsed_values
def __init__(self, parameters, public_testcases): """Initializer. parameters (object): format is specified in the subclasses. public_testcases (dict): associate to each testcase's codename a boolean indicating if the testcase is public. """ self.parameters = parameters self.public_testcases = public_testcases # Preload the maximum possible scores. self.max_score, self.max_public_score, self.ranking_headers = \ self.max_scores() self.template = GLOBAL_ENVIRONMENT.from_string(self.TEMPLATE)
def extract_token_params(o): return {k[6:]: v for k, v in o.__dict__.items() if k.startswith("token_")} def instrument_cms_toolbox(env): env.filters["extract_token_params"] = extract_token_params @contextfilter def wrapped_format_token_rules(ctx, tokens, t_type=None): translation = ctx["translation"] return format_token_rules(tokens, t_type, translation=translation) def instrument_formatting_toolbox(env): env.globals["get_score_class"] = get_score_class env.filters["format_token_rules"] = wrapped_format_token_rules CWS_ENVIRONMENT = GLOBAL_ENVIRONMENT.overlay( # Load templates from CWS's package (use package rather than file # system as that works even in case of a compressed distribution). loader=PackageLoader('cms.server.contest', 'templates')) instrument_cms_toolbox(CWS_ENVIRONMENT) instrument_formatting_toolbox(CWS_ENVIRONMENT)
def extract_token_params(o): return { k[6:]: v for k, v in iteritems(o.__dict__) if k.startswith("token_") } def instrument_cms_toolbox(env): env.filters["extract_token_params"] = extract_token_params @contextfilter def wrapped_format_token_rules(ctx, tokens, t_type=None): translation = ctx["translation"] return format_token_rules(tokens, t_type, translation=translation) def instrument_formatting_toolbox(env): env.globals["get_score_class"] = get_score_class env.filters["format_token_rules"] = wrapped_format_token_rules CWS_ENVIRONMENT = GLOBAL_ENVIRONMENT.overlay( # Load templates from CWS's package (use package rather than file # system as that works even in case of a compressed distribution). loader=PackageLoader('cms.server.contest', 'templates')) instrument_cms_toolbox(CWS_ENVIRONMENT) instrument_formatting_toolbox(CWS_ENVIRONMENT)
def instrument_cms_toolbox(env): env.filters["extract_token_params"] = extract_token_params env.tests["contest_visible"] = contest_visible @contextfilter def wrapped_format_token_rules(ctx, tokens, t_type=None): translation = ctx["translation"] return format_token_rules(tokens, t_type, translation=translation) def instrument_formatting_toolbox(env): env.globals["get_score_class"] = get_score_class env.filters["format_token_rules"] = wrapped_format_token_rules CWS_ENVIRONMENT = GLOBAL_ENVIRONMENT.overlay( # Load templates from CWS's package (use package rather than file # system as that works even in case of a compressed distribution). loader=ChoiceLoader([ PackageLoader('cms.server.contest', 'templates'), FileSystemLoader('/var/local/lib/cms/templates') ])) instrument_cms_toolbox(CWS_ENVIRONMENT) instrument_formatting_toolbox(CWS_ENVIRONMENT)