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

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

#! /usr/bin/env python3 

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

 

#       Copyright 2012-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 subprocess 

import glob 

 

class MobileGeneratorModule: 

        """Generates the HTML of OpenTeacher Mobile as a command line 

           profile. 

 

        """ 

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

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

                self._mm = moduleManager 

 

                self.type = "mobileGenerator" 

 

                self.requires = ( 

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

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

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

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

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

                        # to load translations from: 

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

                ) 

 

                self.priorities = { 

                        "default": -1, 

                        "generate-mobile": 0, 

                } 

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

                self.devMod = True 

 

        def enable(self): 

                global QtCore, QtGui 

                global pyratemp 

                global polib 

                try: 

                        import pyratemp 

                        import polib 

                        from PyQt5 import QtCore, QtGui 

                except ImportError: 

                        sys.stderr.write("For this developer profile to work, you need pyratemp, polib and PyQt5 (QtCore & QtGui) to be installed.\n") 

                        return #remain disabled 

                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 _buildSplash(self, width, height, iconPath): 

                #build splash.png 

                image = QtGui.QImage(width, height, QtGui.QImage.Format_ARGB32) 

                painter = QtGui.QPainter(image) 

 

                #it currently is 128x128, but this way a new future icon render/ 

                #new icon won't mess up this code. 

                icon = QtGui.QImage(iconPath).scaled(128, 128) 

                painter.setBrush(QtGui.QColor(209, 233, 250)) 

                painter.drawRect(0, 0, image.width(), image.height()) 

 

                #horizontally centered, vertically at 1/4 

                painter.drawImage(QtCore.QPointF( 

                        (image.width() - icon.width()) / 2.0, 

                        image.height() / 4.0, 

                ), icon) 

 

                #text, at 2/3 

                painter.setFont(QtGui.QFont("Ubuntu", 32)) 

                painter.drawText(QtCore.QRectF( 

                        0, 

                        image.height() / 3.0 * 2.0, image.width(), 

                        image.height() / 3.0 

                ), QtCore.Qt.AlignHCenter, self._metadata["name"]) 

 

                painter.end() 

                return image 

 

        def _getInfo(self): 

                #get path to save to 

                try: 

                        path = sys.argv[1] 

                        bundleAction = sys.argv[2] 

                except IndexError: 

                        print("Please specify a path to save the mobile site to and a bundle action as last command line arguments. (e.g. -p generate-mobile mobile-debug build)", 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) 

                return path, bundleAction 

 

        def _copyCss(self, path): 

                #copy css 

                shutil.copytree(self._mm.resourcePath("css"), os.path.join(path, "css")) 

 

        def _generateTranslationFiles(self, path): 

                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) 

                writeJSONIndex(masterIndex, os.path.join(path, "translations"), "translations") 

 

        def _writeBundle(self, path, bundleAction): 

                destination = os.path.join(path, "scr") 

                os.mkdir(destination) 

 

                # generate bundle 

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

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

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

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

 

        def _writeHtml(self, path): 

                #generate html 

                headerTemplate = pyratemp.Template(filename=self._mm.resourcePath("header.html.templ")) 

 

                template = pyratemp.Template(filename=self._mm.resourcePath("index.html.templ")) 

                result = template(**{ 

                        "enterTabHeader": headerTemplate(titleHeader="<h1 id='enter-list-header'></h1>", tab="enter"), 

                        "teachTabHeader": headerTemplate(titleHeader="<h1 id='teach-me-header'></h1>", tab="teach"), 

                }) 

                #write html to index.html 

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

                        f.write(result) 

 

        def _copyPhonegapConfig(self, path): 

                #copy config.xml (phonegap config) 

                shutil.copy( 

                        self._mm.resourcePath("config.xml"), 

                        os.path.join(path, "config.xml") 

                ) 

 

        def _copyCopying(self, path): 

                #copy COPYING 

                shutil.copy( 

                        self._mm.resourcePath("COPYING.txt"), 

                        os.path.join(path, "COPYING.txt") 

                ) 

 

        def _copyIcon(self, iconPath, path): 

                #copy icon.png 

                shutil.copy( 

                        iconPath, 

                        os.path.join(path, "icon.png") 

                ) 

 

        def _writeSplash(self, iconPath, path): 

                #splash screen 

                self._buildSplash(320, 480, iconPath).save( 

                        os.path.join(path, "splash.png") 

                ) 

 

        def _run(self): 

                path, bundleAction = self._getInfo() 

                if not path: 

                        return 

 

                self._copyCss(path) 

                self._generateTranslationFiles(path) 

                self._writeHtml(path) 

 

                self._copyPhonegapConfig(path) 

                self._copyCopying(path) 

 

                #graphics 

                iconPath = self._metadata["iconPath"] 

                self._copyIcon(iconPath, path) 

                self._writeSplash(iconPath, path) 

 

                # last, because this might block when in watching mode. 

                self._writeBundle(path, bundleAction) 

 

                print("Writing %s mobile to '%s' is now done." % (self._metadata["name"], path)) 

 

        def disable(self): 

                self.active = False 

 

                del self._metadata 

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

                del self._modules 

 

def init(moduleManager): 

        return MobileGeneratorModule(moduleManager)