Source code for timApp.modules.cs.modifiers

"""
Adding new modifier to csPlugin:

1. Create a new class inheriting from Modifier.
2. Give the class a ttype name in the variable 'ttype'.
3. Overload runner_name and modify_query methods if needed.
"""

from traceback import print_exc


[docs]class Modifier: ttype = "_modifier" def __init__(self, query): self.query = query
[docs] def runner_name(self): return None
[docs] def modify_query(self): pass
[docs] @staticmethod def js_files(): return []
[docs] @staticmethod def css_files(): return []
[docs] @classmethod def all_subclasses(cls): subclasses = cls.__subclasses__() return subclasses + [i for sc in subclasses for i in sc.all_subclasses()]
[docs] @classmethod def get_client_ttype(cls, ttype): """Returns the ttype of this class that should be given to client""" if isinstance(cls.ttype, list): return cls.ttype[0] return cls.ttype
[docs]class ModifierError(Modifier): ttype = "_error" def __init__(self, query, error_str): try: super().__init__(query) except Exception as e: print("Error:", str(e)) print_exc() self.valid = False self.own_error = str(e) else: self.valid = True self.own_error = None self.query = query self.error = error_str
[docs] def modify_query(self): error = self.error if "error" in self.query.query: error = self.query.query["error"][0] + "\n" + error self.query.query["error"] = [error] if self.own_error is not None: own_error = self.own_error if "own_error" in self.query.query: own_error = self.query.query["own_error"][0] + "\n" + own_error self.query.query["own_error"] = [own_error]
[docs] def runner_name(self): return "cs-error"
[docs] def is_valid(self): return self.valid
[docs]class Tiny(Modifier): ttype = "tiny"
[docs] def runner_name(self): return "cs-text-runner"
[docs]class Input(Modifier): ttype = "input"
[docs]class Args(Modifier): ttype = "args"
[docs]class Doc(Modifier): ttype = "doc"
[docs]class CSConsole(Modifier): ttype = "csconsole"
[docs] def runner_name(self): return "cs-console"
[docs]class Parsons(Modifier): ttype = "parsons"
[docs]class TruthTable(Modifier): ttype = "truthtable"
[docs]class Console(Modifier): ttype = "console"
[docs]class Vis(Modifier): ttype = "vis"