Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

#! /usr/bin/env python3 

# -*- coding: utf-8 -*- 

 

#       Copyright 2013-2014, 2017, Marten de Vries 

# 

#       This file is part of OpenTeacher. 

# 

#       OpenTeacher is free software: you can redistribute it and/or modify 

#       it under the terms of the GNU General Public License as published by 

#       the Free Software Foundation, either version 3 of the License, or 

#       (at your option) any later version. 

# 

#       OpenTeacher is distributed in the hope that it will be useful, 

#       but WITHOUT ANY WARRANTY; without even the implied warranty of 

#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

#       GNU General Public License for more details. 

# 

#       You should have received a copy of the GNU General Public License 

#       along with OpenTeacher.  If not, see <http://www.gnu.org/licenses/>. 

 

import shutil 

import sys 

import os 

import datetime 

import posixpath 

import glob 

import subprocess 

 

class WebGeneratorModule: 

        def __init__(self, moduleManager, *args, **kwargs): 

                super().__init__(*args, **kwargs) 

                self._mm = moduleManager 

 

                self.type = "webGenerator" 

                self.requires = ( 

                        self._mm.mods(type="execute"), 

                        self._mm.mods(type="translationIndexBuilder"), 

                        self._mm.mods(type="translationIndexesMerger"), 

                        self._mm.mods(type="translationIndexJSONWriter"), 

                        self._mm.mods(type="metadata"), 

                        # to load translations from: 

                        self._mm.mods(type="jsInputTypingLogic"), 

                ) 

                self.priorities = { 

                        "default": -1, 

                        "generate-web": 0, 

                } 

                #all files directly in the scr directory are web-specific and 

                #thus ui related and thus contain translations. (In theory ;)) 

                self.filesWithTranslations = glob.glob(os.path.abspath('node_modules/ot-web/src/*.js')) 

                self.devMod = True 

 

        def enable(self): 

                global pyratemp 

                global QtGui 

                try: 

                        import pyratemp 

                        from PyQt5 import QtGui 

                except ImportError: 

                        return 

                self._modules = set(self._mm.mods(type="modules")).pop() 

                self._modules.default(type="execute").startRunning.handle(self._run) 

 

                self._metadata = self._modules.default("active", type="metadata").metadata 

 

                self.active = True 

 

        def _run(self): 

                #get path to save to 

                try: 

                        path = sys.argv[1] 

                        couchdbHost = sys.argv[2] 

                        servicesHost = sys.argv[3] 

                        bundleAction = sys.argv[4] 

                except IndexError: 

                        print("Please specify a path to save the site to, the couchdb hostname, the services server hostname and the bundle action as last command line arguments. (e.g. -p generate-web web-debug http://localhost:5984 http://localhost:5000 watch)", file=sys.stderr) 

                        return 

                #ask if overwrite 

                if os.path.isdir(path): 

                        confirm = input("There is already a directory at '%s'. Do you want to remove it and continue (y/n). " % path) 

                        if confirm != "y": 

                                return 

                        shutil.rmtree(path) 

 

                #copy all static stuff 

                shutil.copytree(self._mm.resourcePath("static"), path) 

                os.mkdir(os.path.join(path, 'css')) 

                os.mkdir(os.path.join(path, 'scr')) 

 

                #create the config file 

                template = pyratemp.Template(filename=self._mm.resourcePath("config.templ.js")) 

                with open(os.path.join(path, "scr", "config.js"), "w", encoding='UTF-8') as f: 

                        f.write(template( 

                                couchdbHost=couchdbHost, 

                                servicesHost=servicesHost, 

                                appName=self._metadata 

                        )) 

 

                #create the style file 

                template = pyratemp.Template(filename=self._mm.resourcePath("style.templ.css")) 

                hue = self._metadata["mainColorHue"] 

                data = { 

                        "headerBgColor": QtGui.QColor.fromHsv(hue, 41, 250).name(), 

                        "footerBgColor": QtGui.QColor.fromHsv(hue, 30, 228).name(), 

                } 

                with open(os.path.join(path, "css/style.css"), "w", encoding='UTF-8') as f: 

                        f.write(template(**data)) 

 

                #write the translation index 

                buildIndex = self._modules.default("active", type="translationIndexBuilder").buildTranslationIndex 

                mergeIndexes = self._modules.default("active", type="translationIndexesMerger").mergeIndexes 

                writeJSONIndex = self._modules.default("active", type="translationIndexJSONWriter").writeJSONIndex 

 

                index = buildIndex(self._mm.resourcePath("translations")) 

                masterIndex = mergeIndexes(index, self._modules.default('active', type='jsInputTypingLogic').translationIndex) 

                url = "scr/translations" 

                writeJSONIndex(masterIndex, os.path.join(path, url), url) 

 

                #copy the logo 

                shutil.copy(self._metadata["iconPath"], os.path.join(path, "img/logo")) 

 

                #create the bundle 

                os.chdir(os.path.join('node_modules', 'ot-web')) 

                bundlePath = os.path.join('..', '..', path, 'scr', 'bundle.js') 

                subprocess.check_call(['npm', 'run', bundleAction, '--silent', '--', '-o', bundlePath]) 

                os.chdir(os.path.join('..', '..')) 

 

                print("Writing OpenTeacher web to '%s' is now done." % path) 

 

        def disable(self): 

                self.active = False 

 

                self._modules.default(type="execute").startRunning.unhandle(self._run) 

                del self._modules 

                del self._metadata 

 

def init(moduleManager): 

        return WebGeneratorModule(moduleManager)