Coverage for gws-app/gws/base/application/core.py: 0%

145 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-17 01:37 +0200

1"""Core application object""" 

2 

3from typing import Optional 

4 

5import gws 

6import gws.base.action 

7import gws.base.application.middleware 

8import gws.base.auth 

9import gws.base.client 

10import gws.base.database 

11import gws.base.model 

12import gws.base.printer 

13import gws.base.project 

14import gws.base.search 

15import gws.base.storage 

16import gws.base.template 

17import gws.base.web 

18import gws.config 

19import gws.gis.cache 

20import gws.gis.mpx.config 

21import gws.lib.font 

22import gws.lib.importer 

23import gws.lib.metadata 

24import gws.lib.osx 

25import gws.server.manager 

26import gws.server.monitor 

27import gws.spec 

28 

29_DEFAULT_LOCALE = ['en_CA'] 

30 

31_DEFAULT_TEMPLATES = [ 

32 gws.Config( 

33 type='html', 

34 path=gws.u.dirname(__file__) + '/templates/project_description.cx.html', 

35 subject='project.description', 

36 access=gws.c.PUBLIC, 

37 uid='default_template.project_description', 

38 ), 

39 gws.Config( 

40 type='html', 

41 path=gws.u.dirname(__file__) + '/templates/layer_description.cx.html', 

42 subject='layer.description', 

43 access=gws.c.PUBLIC, 

44 uid='default_template.layer_description', 

45 ), 

46 gws.Config( 

47 type='html', 

48 path=gws.u.dirname(__file__) + '/templates/feature_description.cx.html', 

49 subject='feature.description', 

50 access=gws.c.PUBLIC, 

51 uid='default_template.feature_description', 

52 ), 

53 gws.Config( 

54 type='html', 

55 path=gws.u.dirname(__file__) + '/templates/feature_title.cx.html', 

56 subject='feature.title', 

57 access=gws.c.PUBLIC, 

58 uid='default_template.feature_title', 

59 ), 

60 gws.Config( 

61 type='html', 

62 path=gws.u.dirname(__file__) + '/templates/feature_label.cx.html', 

63 subject='feature.label', 

64 access=gws.c.PUBLIC, 

65 uid='default_template.feature_label', 

66 ), 

67] 

68 

69_DEFAULT_PRINTER = gws.Config( 

70 uid='gws.base.application.default_printer', 

71 access=gws.c.PUBLIC, 

72 template=gws.Config( 

73 type='html', 

74 path=gws.u.dirname(__file__) + '/templates/project_print.cx.html', 

75 mapSize=(200, 180, gws.Uom.mm), 

76 ), 

77 qualityLevels=[{'dpi': 72}], 

78) 

79 

80 

81class Config(gws.ConfigWithAccess): 

82 """Main application configuration""" 

83 

84 actions: Optional[list[gws.ext.config.action]] 

85 """System-wide server actions.""" 

86 auth: Optional[gws.base.auth.manager.Config] 

87 """Authorization methods and options.""" 

88 cache: Optional[gws.gis.cache.Config] 

89 """Global cache configuration.""" 

90 client: Optional[gws.base.client.Config] 

91 """Gws client configuration.""" 

92 database: Optional[gws.base.database.manager.Config] 

93 """Database configuration.""" 

94 developer: Optional[dict] 

95 """Developer options.""" 

96 finders: Optional[list[gws.ext.config.finder]] 

97 """Global search providers.""" 

98 fonts: Optional[gws.lib.font.Config] 

99 """Fonts configuration.""" 

100 helpers: Optional[list[gws.ext.config.helper]] 

101 """Helpers configurations.""" 

102 locales: Optional[list[str]] 

103 """Default locales for all projects.""" 

104 metadata: Optional[gws.Metadata] 

105 """Application metadata.""" 

106 models: Optional[list[gws.ext.config.model]] 

107 """Global data models.""" 

108 owsServices: Optional[list[gws.ext.config.owsService]] 

109 """OWS services configuration.""" 

110 plugins: Optional[list[dict]] 

111 """Configuration for plugins.""" 

112 projectDirs: Optional[list[gws.DirPath]] 

113 """Directories with additional projects.""" 

114 projectPaths: Optional[list[gws.FilePath]] 

115 """Additional project paths.""" 

116 printers: Optional[list[gws.ext.config.printer]] 

117 """Print configurations.""" 

118 projects: Optional[list[gws.ext.config.project]] 

119 """Project configurations.""" 

120 server: Optional[gws.server.Config] 

121 """Server engine options.""" 

122 storage: Optional[gws.base.storage.manager.Config] 

123 """Database configuration.""" 

124 templates: Optional[list[gws.ext.config.template]] 

125 """Default templates.""" 

126 web: Optional[gws.base.web.manager.Config] 

127 """Web server options.""" 

128 

129 

130class Object(gws.Application): 

131 """Main Application object""" 

132 

133 _helperMap: dict[str, gws.Node] 

134 

135 _developerOptions: dict 

136 

137 mpxUrl = '' 

138 mpxConfig = '' 

139 

140 def configure(self): 

141 self.serverMgr = self.create_child(gws.server.manager.Object, self.cfg('server')) 

142 # NB need defaults from the server 

143 self.config.server = self.serverMgr.config 

144 

145 p = self.cfg('server.log.level') 

146 if p: 

147 gws.log.set_level(p) 

148 

149 self.version = self.root.specs.version 

150 self.versionString = f'GWS version {self.version}' 

151 

152 if not gws.env.GWS_IN_TEST: 

153 gws.log.info('*' * 60) 

154 gws.log.info(self.versionString) 

155 gws.log.info('*' * 60) 

156 

157 self._developerOptions = self.cfg('developer') or {} 

158 if self._developerOptions: 

159 gws.log.warning('developer mode enabled') 

160 

161 self.monitor = self.create_child(gws.server.monitor.Object, self.serverMgr.cfg('monitor')) 

162 

163 self.localeUids = self.cfg('locales') or _DEFAULT_LOCALE 

164 self.metadata = gws.lib.metadata.from_config(self.cfg('metadata')) 

165 

166 self.middlewareMgr = self.create_child(gws.base.application.middleware.Object) 

167 

168 p = self.cfg('fonts') 

169 if p: 

170 gws.lib.font.configure(p) 

171 

172 # NB the order of initialization is important 

173 # - db 

174 # - helpers 

175 # - auth providers 

176 # - actions, client, web 

177 # - finally, projects 

178 

179 self.databaseMgr = self.create_child(gws.base.database.manager.Object, self.cfg('database')) 

180 

181 helpers = self.create_children(gws.ext.object.helper, self.cfg('helpers')) 

182 self._helperMap = {p.extType: p for p in helpers} 

183 

184 self.storageMgr = self.create_child(gws.base.storage.manager.Object, self.cfg('storage')) 

185 self.authMgr = self.create_child(gws.base.auth.manager.Object, self.cfg('auth')) 

186 

187 # @TODO default API 

188 self.actionMgr = self.create_child(gws.base.action.manager.Object) 

189 self.actions = self.create_children(gws.ext.object.action, self.cfg('actions')) 

190 

191 self.webMgr = self.create_child(gws.base.web.manager.Object, self.cfg('web')) 

192 

193 self.searchMgr = self.create_child(gws.base.search.manager.Object) 

194 self.finders = self.create_children(gws.ext.object.finder, self.cfg('finders')) 

195 

196 self.modelMgr = self.create_child(gws.base.model.manager.Object) 

197 self.models = self.create_children(gws.ext.object.model, self.cfg('models')) 

198 

199 self.templateMgr = self.create_child(gws.base.template.manager.Object) 

200 self.templates = self.create_children(gws.ext.object.template, self.cfg('templates')) 

201 for cfg in _DEFAULT_TEMPLATES: 

202 self.templates.append(self.root.create_shared(gws.ext.object.template, cfg)) 

203 

204 self.printerMgr = self.create_child(gws.base.printer.manager.Object) 

205 self.printers = self.create_children(gws.ext.object.printer, self.cfg('printers')) 

206 self.defaultPrinter = self.root.create_shared(gws.ext.object.printer, _DEFAULT_PRINTER) 

207 

208 self.owsServices = self.create_children(gws.ext.object.owsService, self.cfg('owsServices')) 

209 

210 self.client = self.create_child(gws.base.client.Object, self.cfg('client')) 

211 

212 self.projects = self.create_children(gws.ext.object.project, self.cfg('projects')) 

213 

214 def post_configure(self): 

215 if self.cfg('server.mapproxy.enabled'): 

216 self.mpxUrl = f"http://{self.cfg('server.mapproxy.host')}:{self.cfg('server.mapproxy.port')}" 

217 self.mpxConfig = gws.gis.mpx.config.create_and_save(self.root) 

218 

219 # NB these are populated in config.parser 

220 for p in self.config.get('configPaths', []): 

221 self.monitor.add_file(p) 

222 for p in self.config.get('projectPaths', []): 

223 self.monitor.add_file(p) 

224 for d in self.config.get('projectDirs', []): 

225 self.monitor.add_directory(d, gws.config.CONFIG_PATH_PATTERN) 

226 if self.developer_option('server.auto_reload'): 

227 self.monitor.add_directory(gws.c.APP_DIR, r'\.py$') 

228 

229 def project(self, uid): 

230 for p in self.projects: 

231 if p.uid == uid: 

232 return p 

233 

234 def helper(self, ext_type): 

235 if ext_type not in self._helperMap: 

236 p = self.create_child(gws.ext.object.helper, type=ext_type) 

237 gws.log.info(f'created helper {ext_type!r}') 

238 self._helperMap[ext_type] = p 

239 return self._helperMap.get(ext_type) 

240 

241 def developer_option(self, key): 

242 return self._developerOptions.get(key)