summaryrefslogtreecommitdiff
path: root/yaksh/language_registry.py
blob: 512e2f5cd129d4aaa334c278024209cb75bde939 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from settings import code_evaluators
import importlib
import json

registry = None

# def set_registry():
#     global registry
#     registry = _LanguageRegistry()
    
def get_registry(): #@@@get_evaluator_registry
    global registry
    if registry is None:
        registry = _LanguageRegistry()
    return registry

def unpack_json(json_data):
    data = json.loads(json_data)
    return data

def create_evaluator_instance(language, test_case_type, json_data, in_dir):
    """Create instance of relevant EvaluateCode class based on language"""
    # set_registry()
    registry = get_registry()
    cls = registry.get_class(language, test_case_type) #@@@get_evaluator_for_language
    instance = cls(in_dir)
    # instance = cls.from_json(language, json_data, in_dir)
    return instance

class _LanguageRegistry(object):
    def __init__(self):
        self._register = {}
        for language, module in code_evaluators.iteritems():
            self._register[language] = None

    # Public Protocol ##########
    def get_class(self, language, test_case_type):
        """ Get the code evaluator class for the given language """
        if not self._register.get(language):
            self._register[language] = code_evaluators.get(language)

        test_case_register = self._register[language]
        cls = test_case_register.get(test_case_type)
        module_name, class_name = cls.rsplit(".", 1)
        # load the module, will raise ImportError if module cannot be loaded
        get_module = importlib.import_module(module_name)
        # get the class, will raise AttributeError if class cannot be found
        get_class = getattr(get_module, class_name)
        return get_class

    def register(self, language, class_name):
        """ Register a new code evaluator class for language"""
        self._register[language] = class_name