~openteachermaintainers/openteacher/3.x

« back to all changes in this revision

Viewing changes to modules/org/openteacher/logic/noteCalculators/javaScript/_ects/ects.py

  • Committer: Marten de Vries
  • Date: 2017-06-28 18:05:48 UTC
  • Revision ID: git-v1:b4c406307aa345c58b9904b76580f15c5bff2a4e
Move JS into npm modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python3
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
#       Copyright 2011, Cas Widdershoven
 
5
#       Copyright 2009-2013, Marten de Vries
 
6
#
 
7
#       This file is part of OpenTeacher.
 
8
#
 
9
#       OpenTeacher is free software: you can redistribute it and/or modify
 
10
#       it under the terms of the GNU General Public License as published by
 
11
#       the Free Software Foundation, either version 3 of the License, or
 
12
#       (at your option) any later version.
 
13
#
 
14
#       OpenTeacher is distributed in the hope that it will be useful,
 
15
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
#       GNU General Public License for more details.
 
18
#
 
19
#       You should have received a copy of the GNU General Public License
 
20
#       along with OpenTeacher.  If not, see <http://www.gnu.org/licenses/>.
 
21
 
 
22
class ECTSNoteCalculatorModule:
 
23
        def __init__(self, moduleManager, *args, **kwargs):
 
24
                super().__init__(*args, **kwargs)
 
25
                self._mm = moduleManager
 
26
 
 
27
                self.type = "noteCalculator"
 
28
                self.requires = (
 
29
                        self._mm.mods("javaScriptImplementation", type="percentsCalculator"),
 
30
                        self._mm.mods("javaScriptImplementation", type="bisectfunc"),
 
31
                        self._mm.mods(type="javaScriptEvaluator"),
 
32
                )
 
33
                self.uses = (
 
34
                        self._mm.mods(type="translator"),
 
35
                )
 
36
                self.filesWithTranslations = ("ects.py",)
 
37
                self.javaScriptImplementation = True
 
38
 
 
39
                self.priorities = {
 
40
                        "default": 935,
 
41
                }
 
42
 
 
43
        calculateNote = property(lambda self: self._js.global_.calculateNote)
 
44
        calculateAverageNote = property(lambda self: self._js.global_.calculateAverageNote)
 
45
 
 
46
        def enable(self):
 
47
                self._modules = next(iter(self._mm.mods(type="modules")))
 
48
                self.code = self._modules.default(
 
49
                        "active",
 
50
                        "javaScriptImplementation",
 
51
                        type="percentsCalculator"
 
52
                ).code
 
53
                self.code += self._modules.default(
 
54
                        "active",
 
55
                        "javaScriptImplementation",
 
56
                        type="bisectfunc"
 
57
                ).code
 
58
                with open(self._mm.resourcePath("ects.js"), encoding='UTF-8') as f:
 
59
                        self.code += f.read()
 
60
 
 
61
                self._js = self._modules.default("active", type="javaScriptEvaluator").createEvaluator()
 
62
                self._js.eval(self.code)
 
63
 
 
64
                #Connect to the languageChanged event so retranslating is done.
 
65
                try:
 
66
                        translator = self._modules.default("active", type="translator")
 
67
                except IndexError:
 
68
                        pass
 
69
                else:
 
70
                        translator.languageChanged.handle(self._retranslate)
 
71
                self._retranslate()
 
72
 
 
73
                self.active = True
 
74
 
 
75
        def _retranslate(self):
 
76
                #Load translations
 
77
                try:
 
78
                        translator = self._modules.default("active", type="translator")
 
79
                except IndexError:
 
80
                        _, ngettext = str, lambda a, b, n: a if n == 1 else b
 
81
                else:
 
82
                        _, ngettext = translator.gettextFunctions(
 
83
                                self._mm.resourcePath("translations")
 
84
                        )
 
85
                self.name = _("ECTS")
 
86
 
 
87
        def disable(self):
 
88
                self.active = False
 
89
                del self.name
 
90
                del self._modules
 
91
                del self._js
 
92
                del self.code
 
93
 
 
94
def init(moduleManager):
 
95
        return ECTSNoteCalculatorModule(moduleManager)