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

#! /usr/bin/env python3 

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

 

#       Copyright 2011-2013, 2017, Marten de Vries 

#       Copyright 2011, Milan Boers 

# 

#       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 os 

 

class Everything: 

        def __contains__(self, item): 

                return True 

 

class LoaderModule: 

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

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

                self._mm = moduleManager 

 

                self.type = "loader" 

 

                self.uses = ( 

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

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

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

                ) 

 

        @property 

        def _supportedFileTypes(self): 

                types = set() 

                for lesson in self._mm.mods("active", type="lesson"): 

                        try: 

                                types.add(lesson.dataType) 

                        except AttributeError: 

                                pass 

                return types 

 

        @property 

        def usableExtensions(self): 

                """Returns a alphabetically sorted list of tuples. The tuples 

                   have this form: (file_format_name, ext). For example: 

                   ("OpenTeaching words file", "otwd"). The list includes all 

                   extensions that can be loaded with the modules currently in 

                   use by OpenTeacher 

 

                """ 

                exts = set() 

 

                #Collect exts the loader modules support, if there is a gui 

                #module for the type(s) they can provide 

                for module in self._modules.sort("active", type="load"): 

                        for ext, fileTypes in module.loads.items(): 

                                for fileType in fileTypes: 

                                        if fileType in self._supportedFileTypes: 

                                                exts.add((ext, module.name)) 

                return sorted(exts) 

 

        @property 

        def openSupport(self): 

                """Tells if there's a chance that calling load() will succeed 

                   (and not fail with a NotImplementedError) 

 

                """ 

                return len(self.usableExtensions) != 0 

 

        @property 

        def _addToRecentlyOpened(self): 

                try: 

                        recentlyOpenedModule = self._modules.default("active", type="recentlyOpened") 

                except IndexError: 

                        return 

                return recentlyOpenedModule.add 

 

        def load(self, path): 

                """"Opens the file at ``path`` in the GUI.""" 

 

                type, lessonData = self.loadToLesson(path, self._supportedFileTypes) 

 

                if self._addToRecentlyOpened: 

                        # Add to recently opened 

                        self._addToRecentlyOpened(**{ 

                                "label": lessonData["list"].get("title", "") or os.path.basename(path), 

                                "args": {}, 

                                "kwargs": {"path": path}, 

                                "method": "load", 

                                "moduleArgsSelectors": ["active"], 

                                "moduleKwargsSelectors": {"type": "loader"}, 

                        }) 

 

                lesson = self.loadFromLesson(type, lessonData) 

                lesson.path = path 

 

        def loadToLesson(self, path, dataTypes=None): 

                """Loads the file in ``path`` and returns a tuple containing 

                   ``(dataType, lessonData)`` or raises NotImplementedError if 

                   that is impossible. When a list is passed to ``dataTypes``, 

                   it only loads the file if it can do so returning lessonData 

                   of one of the dataTypes in the list. By default, it returns 

                   lessonData of any type. 

 

                """ 

117                if dataTypes is None: 

                        dataTypes = Everything() 

121                for loadModule in self._modules.sort("active", type="load"): 

                        fileType = loadModule.getFileTypeOf(path) 

                        if fileType is not None and fileType in dataTypes: 

                                return fileType, loadModule.load(path) 

                raise NotImplementedError() 

 

        def loadFromLesson(self, dataType, lessonData): 

                """Tries to show ``lessonData`` of type ``type`` in a GUI 

                   lesson. 

 

                """ 

                loaders = [ 

                        loader 

                        for loader in self._modules.sort("active", type="lesson") 

                        if loader.dataType == dataType 

                ] 

                if not loaders: 

                        raise NotImplementedError() 

                loader = loaders[0] 

                lesson = loader.createLesson() 

                lesson.resources = lessonData["resources"] 

                lesson.list = lessonData["list"] 

                if "changed" in lessonData: 

                        lesson.changed = lessonData["changed"] 

                return lesson 

 

        def enable(self): 

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

 

                self.active = True 

 

        def disable(self): 

                self.active = False 

 

                del self._modules 

 

def init(moduleManager): 

        return LoaderModule(moduleManager)