[36] | 1 | # Configuration and related stuff
|
---|
[113] | 2 | # -*- encoding: utf-8 -*-
|
---|
[36] | 3 |
|
---|
| 4 | #-------------------------------------------------------------------------------
|
---|
| 5 |
|
---|
[132] | 6 | import const
|
---|
| 7 |
|
---|
[40] | 8 | import os
|
---|
[43] | 9 | import sys
|
---|
[40] | 10 | import ConfigParser
|
---|
| 11 |
|
---|
| 12 | #-------------------------------------------------------------------------------
|
---|
| 13 |
|
---|
| 14 | configPath = os.path.join(os.path.expanduser("~"),
|
---|
| 15 | "mlx.config" if os.name=="nt" else ".mlxrc")
|
---|
| 16 |
|
---|
| 17 | #-------------------------------------------------------------------------------
|
---|
| 18 |
|
---|
[124] | 19 | if os.name=="nt":
|
---|
| 20 | _languageMap = { "en_GB" : "eng",
|
---|
| 21 | "hu_HU" : "hun" }
|
---|
| 22 |
|
---|
| 23 | #-------------------------------------------------------------------------------
|
---|
| 24 |
|
---|
[36] | 25 | class Config(object):
|
---|
| 26 | """Our configuration."""
|
---|
[40] | 27 | DEFAULT_UPDATE_URL = "http://mlx.varadiistvan.hu/update"
|
---|
[132] | 28 |
|
---|
| 29 | _messageTypesSection = "messageTypes"
|
---|
[40] | 30 |
|
---|
[36] | 31 | def __init__(self):
|
---|
| 32 | """Construct the configuration with default values."""
|
---|
[40] | 33 |
|
---|
[42] | 34 | self._pilotID = ""
|
---|
| 35 | self._password = ""
|
---|
[45] | 36 | self._rememberPassword = False
|
---|
[42] | 37 |
|
---|
[132] | 38 | self._language = ""
|
---|
[147] | 39 | self._hideMinimizedWindow = True
|
---|
[136] | 40 | self._onlineGateSystem = True
|
---|
[139] | 41 | self._onlineACARS = True
|
---|
[132] | 42 | self._flareTimeFromFS = False
|
---|
[148] | 43 | self._syncFSTime = False
|
---|
[149] | 44 |
|
---|
| 45 | self._pirepDirectory = None
|
---|
[132] | 46 |
|
---|
[40] | 47 | self._autoUpdate = True
|
---|
| 48 | self._updateURL = Config.DEFAULT_UPDATE_URL
|
---|
[132] | 49 |
|
---|
| 50 | self._messageTypeLevels = {}
|
---|
[131] | 51 |
|
---|
[123] | 52 | self._modified = False
|
---|
[107] | 53 |
|
---|
[40] | 54 | @property
|
---|
[42] | 55 | def pilotID(self):
|
---|
| 56 | """Get the pilot ID."""
|
---|
| 57 | return self._pilotID
|
---|
| 58 |
|
---|
| 59 | @pilotID.setter
|
---|
| 60 | def pilotID(self, pilotID):
|
---|
| 61 | """Set the pilot ID."""
|
---|
| 62 | if pilotID!=self._pilotID:
|
---|
| 63 | self._pilotID = pilotID
|
---|
| 64 | self._modified = True
|
---|
| 65 |
|
---|
| 66 | @property
|
---|
| 67 | def password(self):
|
---|
| 68 | """Get the password."""
|
---|
| 69 | return self._password
|
---|
| 70 |
|
---|
| 71 | @password.setter
|
---|
| 72 | def password(self, password):
|
---|
| 73 | """Set the password."""
|
---|
| 74 | if password!=self._password:
|
---|
| 75 | self._password = password
|
---|
| 76 | self._modified = True
|
---|
| 77 |
|
---|
| 78 | @property
|
---|
[45] | 79 | def rememberPassword(self):
|
---|
| 80 | """Get if we should remember the password."""
|
---|
| 81 | return self._rememberPassword
|
---|
| 82 |
|
---|
| 83 | @rememberPassword.setter
|
---|
| 84 | def rememberPassword(self, rememberPassword):
|
---|
| 85 | """Set if we should remember the password."""
|
---|
| 86 | if rememberPassword!=self._rememberPassword:
|
---|
| 87 | self._rememberPassword = rememberPassword
|
---|
| 88 | self._modified = True
|
---|
| 89 |
|
---|
| 90 | @property
|
---|
[123] | 91 | def language(self):
|
---|
| 92 | """Get the language to use."""
|
---|
| 93 | return self._language
|
---|
| 94 |
|
---|
| 95 | @language.setter
|
---|
| 96 | def language(self, language):
|
---|
| 97 | """Set the language to use."""
|
---|
| 98 | if language!=self._language:
|
---|
| 99 | self._language = language
|
---|
| 100 | self._modified = True
|
---|
| 101 |
|
---|
| 102 | @property
|
---|
[147] | 103 | def hideMinimizedWindow(self):
|
---|
| 104 | """Get whether a minimized window should be hidden."""
|
---|
| 105 | return self._hideMinimizedWindow
|
---|
| 106 |
|
---|
| 107 | @hideMinimizedWindow.setter
|
---|
| 108 | def hideMinimizedWindow(self, hideMinimizedWindow):
|
---|
| 109 | """Set whether a minimized window should be hidden."""
|
---|
| 110 | if hideMinimizedWindow!=self._hideMinimizedWindow:
|
---|
| 111 | self._hideMinimizedWindow = hideMinimizedWindow
|
---|
| 112 | self._modified = True
|
---|
| 113 |
|
---|
| 114 | @property
|
---|
[136] | 115 | def onlineGateSystem(self):
|
---|
| 116 | """Get whether the online gate system should be used."""
|
---|
| 117 | return self._onlineGateSystem
|
---|
| 118 |
|
---|
| 119 | @onlineGateSystem.setter
|
---|
| 120 | def onlineGateSystem(self, onlineGateSystem):
|
---|
| 121 | """Set whether the online gate system should be used."""
|
---|
| 122 | if onlineGateSystem!=self._onlineGateSystem:
|
---|
| 123 | self._onlineGateSystem = onlineGateSystem
|
---|
| 124 | self._modified = True
|
---|
| 125 |
|
---|
| 126 | @property
|
---|
[139] | 127 | def onlineACARS(self):
|
---|
| 128 | """Get whether the online ACARS system should be used."""
|
---|
| 129 | return self._onlineACARS
|
---|
| 130 |
|
---|
| 131 | @onlineACARS.setter
|
---|
| 132 | def onlineACARS(self, onlineACARS):
|
---|
| 133 | """Set whether the online ACARS system should be used."""
|
---|
| 134 | if onlineACARS!=self._onlineACARS:
|
---|
| 135 | self._onlineACARS = onlineACARS
|
---|
| 136 | self._modified = True
|
---|
| 137 |
|
---|
| 138 | @property
|
---|
[131] | 139 | def flareTimeFromFS(self):
|
---|
| 140 | """Get whether the flare time should be calculated from the time values
|
---|
| 141 | returned by the simulator."""
|
---|
| 142 | return self._flareTimeFromFS
|
---|
| 143 |
|
---|
| 144 | @flareTimeFromFS.setter
|
---|
| 145 | def flareTimeFromFS(self, flareTimeFromFS):
|
---|
| 146 | """Set whether the flare time should be calculated from the time values
|
---|
| 147 | returned by the simulator."""
|
---|
| 148 | if flareTimeFromFS!=self._flareTimeFromFS:
|
---|
| 149 | self._flareTimeFromFS = flareTimeFromFS
|
---|
| 150 | self._modified = True
|
---|
| 151 |
|
---|
[148] | 152 | @property
|
---|
| 153 | def syncFSTime(self):
|
---|
| 154 | """Get whether the simulator's time should be synchronized with the
|
---|
| 155 | machine's clock."""
|
---|
| 156 | return self._syncFSTime
|
---|
| 157 |
|
---|
| 158 | @syncFSTime.setter
|
---|
| 159 | def syncFSTime(self, syncFSTime):
|
---|
| 160 | """Set whether the simulator's time should be synchronized with the
|
---|
| 161 | machine's clock."""
|
---|
| 162 | if syncFSTime!=self._syncFSTime:
|
---|
| 163 | self._syncFSTime = syncFSTime
|
---|
| 164 | self._modified = True
|
---|
| 165 |
|
---|
[149] | 166 | @property
|
---|
| 167 | def pirepDirectory(self):
|
---|
| 168 | """Get the directory offered by default when saving a PIREP."""
|
---|
| 169 | return self._pirepDirectory
|
---|
| 170 |
|
---|
| 171 | @pirepDirectory.setter
|
---|
| 172 | def pirepDirectory(self, pirepDirectory):
|
---|
| 173 | """Get the directory offered by default when saving a PIREP."""
|
---|
| 174 | if pirepDirectory!=self._pirepDirectory and \
|
---|
| 175 | (pirepDirectory!="" or self._pirepDirectory is not None):
|
---|
| 176 | self._pirepDirectory = None if pirepDirectory=="" \
|
---|
| 177 | else pirepDirectory
|
---|
| 178 | self._modified = True
|
---|
| 179 |
|
---|
[132] | 180 | def getMessageTypeLevel(self, messageType):
|
---|
| 181 | """Get the level for the given message type."""
|
---|
| 182 | return self._messageTypeLevels[messageType] \
|
---|
| 183 | if messageType in self._messageTypeLevels \
|
---|
| 184 | else const.MESSAGELEVEL_NONE
|
---|
| 185 |
|
---|
[134] | 186 | def isMessageTypeFS(self, messageType):
|
---|
| 187 | """Determine if the given message type is displayed in the
|
---|
| 188 | simulator."""
|
---|
| 189 | level = self.getMessageTypeLevel(messageType)
|
---|
| 190 | return level==const.MESSAGELEVEL_FS or \
|
---|
| 191 | level==const.MESSAGELEVEL_BOTH
|
---|
| 192 |
|
---|
[132] | 193 | def setMessageTypeLevel(self, messageType, level):
|
---|
| 194 | """Set the level of the given message type."""
|
---|
| 195 | if messageType not in self._messageTypeLevels or \
|
---|
| 196 | self._messageTypeLevels[messageType]!=level:
|
---|
| 197 | self._messageTypeLevels[messageType] = level
|
---|
| 198 | self._modified = True
|
---|
| 199 |
|
---|
[131] | 200 | @property
|
---|
[40] | 201 | def autoUpdate(self):
|
---|
| 202 | """Get if an automatic update is needed."""
|
---|
| 203 | return self._autoUpdate
|
---|
| 204 |
|
---|
| 205 | @autoUpdate.setter
|
---|
| 206 | def autoUpdate(self, autoUpdate):
|
---|
| 207 | """Set if an automatic update is needed."""
|
---|
| 208 | if autoUpdate!=self._autoUpdate:
|
---|
| 209 | self._autoUpdate = autoUpdate
|
---|
| 210 | self._modified = True
|
---|
| 211 |
|
---|
| 212 | @property
|
---|
| 213 | def updateURL(self):
|
---|
| 214 | """Get the update URL."""
|
---|
| 215 | return self._updateURL
|
---|
| 216 |
|
---|
| 217 | @updateURL.setter
|
---|
| 218 | def updateURL(self, updateURL):
|
---|
| 219 | """Set the update URL."""
|
---|
| 220 | if updateURL!=self._updateURL:
|
---|
| 221 | self._updateURL = updateURL
|
---|
| 222 | self._modified = True
|
---|
| 223 |
|
---|
| 224 | def load(self):
|
---|
| 225 | """Load the configuration from its default location."""
|
---|
| 226 | config = ConfigParser.RawConfigParser()
|
---|
| 227 | config.read(configPath)
|
---|
| 228 |
|
---|
[42] | 229 | self._pilotID = self._get(config, "login", "id", "")
|
---|
| 230 | self._password = self._get(config, "login", "password", "")
|
---|
[45] | 231 | self._rememberPassword = self._getBoolean(config, "login",
|
---|
| 232 | "rememberPassword", False)
|
---|
[42] | 233 |
|
---|
[132] | 234 | self._language = self._get(config, "general", "language", "")
|
---|
[147] | 235 | self._hideMinimizedWindow = self._getBoolean(config, "general",
|
---|
| 236 | "hideMinimizedWindow",
|
---|
| 237 | True)
|
---|
[136] | 238 | self._onlineGateSystem = self._getBoolean(config, "general",
|
---|
| 239 | "onlineGateSystem",
|
---|
| 240 | True)
|
---|
[139] | 241 | self._onlineACARS = self._getBoolean(config, "general",
|
---|
| 242 | "onlineACARS", True)
|
---|
[132] | 243 | self._flareTimeFromFS = self._getBoolean(config, "general",
|
---|
| 244 | "flareTimeFromFS",
|
---|
| 245 | False)
|
---|
[148] | 246 | self._syncFSTime = self._getBoolean(config, "general",
|
---|
| 247 | "syncFSTime",
|
---|
| 248 | False)
|
---|
[149] | 249 | self._pirepDirectory = self._get(config, "general",
|
---|
| 250 | "pirepDirectory", None)
|
---|
[132] | 251 |
|
---|
| 252 | self._messageTypeLevels = {}
|
---|
| 253 | for messageType in const.messageTypes:
|
---|
| 254 | self._messageTypeLevels[messageType] = \
|
---|
| 255 | self._getMessageTypeLevel(config, messageType)
|
---|
| 256 |
|
---|
[40] | 257 | self._autoUpdate = self._getBoolean(config, "update", "auto", True)
|
---|
| 258 | self._updateURL = self._get(config, "update", "url",
|
---|
| 259 | Config.DEFAULT_UPDATE_URL)
|
---|
[107] | 260 |
|
---|
[40] | 261 | self._modified = False
|
---|
| 262 |
|
---|
| 263 | def save(self):
|
---|
| 264 | """Save the configuration file if it has been modified."""
|
---|
| 265 | if not self._modified:
|
---|
| 266 | return
|
---|
| 267 |
|
---|
| 268 | config = ConfigParser.RawConfigParser()
|
---|
| 269 |
|
---|
[42] | 270 | config.add_section("login")
|
---|
| 271 | config.set("login", "id", self._pilotID)
|
---|
| 272 | config.set("login", "password", self._password)
|
---|
[45] | 273 | config.set("login", "rememberPassword",
|
---|
| 274 | "yes" if self._rememberPassword else "no")
|
---|
[42] | 275 |
|
---|
[132] | 276 | config.add_section("general")
|
---|
| 277 | if self._language:
|
---|
| 278 | config.set("general", "language", self._language)
|
---|
[147] | 279 | config.set("general", "hideMinimizedWindow",
|
---|
| 280 | "yes" if self._hideMinimizedWindow else "no")
|
---|
[136] | 281 | config.set("general", "onlineGateSystem",
|
---|
| 282 | "yes" if self._onlineGateSystem else "no")
|
---|
[139] | 283 | config.set("general", "onlineACARS",
|
---|
| 284 | "yes" if self._onlineACARS else "no")
|
---|
[132] | 285 | config.set("general", "flareTimeFromFS",
|
---|
| 286 | "yes" if self._flareTimeFromFS else "no")
|
---|
[148] | 287 | config.set("general", "syncFSTime",
|
---|
| 288 | "yes" if self._syncFSTime else "no")
|
---|
[132] | 289 |
|
---|
[149] | 290 | if self._pirepDirectory is not None:
|
---|
| 291 | config.set("general", "pirepDirectory", self._pirepDirectory)
|
---|
| 292 |
|
---|
[132] | 293 | config.add_section(Config._messageTypesSection)
|
---|
| 294 | for messageType in const.messageTypes:
|
---|
| 295 | if messageType in self._messageTypeLevels:
|
---|
| 296 | option = self._getMessageTypeLevelOptionName(messageType)
|
---|
| 297 | level = self._messageTypeLevels[messageType]
|
---|
| 298 | config.set(Config._messageTypesSection, option,
|
---|
| 299 | const.messageLevel2string(level))
|
---|
| 300 |
|
---|
[40] | 301 | config.add_section("update")
|
---|
[45] | 302 | config.set("update", "auto",
|
---|
| 303 | "yes" if self._autoUpdate else "no")
|
---|
[40] | 304 | config.set("update", "url", self._updateURL)
|
---|
| 305 |
|
---|
| 306 | try:
|
---|
[43] | 307 | fd = os.open(configPath, os.O_CREAT|os.O_TRUNC|os.O_WRONLY,
|
---|
| 308 | 0600)
|
---|
| 309 | with os.fdopen(fd, "wt") as f:
|
---|
[40] | 310 | config.write(f)
|
---|
| 311 | self._modified = False
|
---|
| 312 | except Exception, e:
|
---|
[43] | 313 | print >> sys.stderr, "Failed to update config: " + str(e)
|
---|
[40] | 314 |
|
---|
| 315 | def _getBoolean(self, config, section, option, default):
|
---|
| 316 | """Get the given option as a boolean, if found in the given config,
|
---|
| 317 | otherwise the default."""
|
---|
| 318 | return config.getboolean(section, option) \
|
---|
| 319 | if config.has_option(section, option) \
|
---|
| 320 | else default
|
---|
| 321 |
|
---|
| 322 | def _get(self, config, section, option, default):
|
---|
| 323 | """Get the given option as a string, if found in the given config,
|
---|
| 324 | otherwise the default."""
|
---|
| 325 | return config.get(section, option) \
|
---|
| 326 | if config.has_option(section, option) \
|
---|
| 327 | else default
|
---|
[36] | 328 |
|
---|
[132] | 329 | def _getMessageTypeLevel(self, config, messageType):
|
---|
| 330 | """Get the message type level for the given message type."""
|
---|
| 331 | option = self._getMessageTypeLevelOptionName(messageType)
|
---|
| 332 | if config.has_option(Config._messageTypesSection, option):
|
---|
| 333 | value = config.get(Config._messageTypesSection, option)
|
---|
| 334 | return const.string2messageLevel(value)
|
---|
[133] | 335 | elif messageType in [const.MESSAGETYPE_LOGGER_ERROR,
|
---|
| 336 | const.MESSAGETYPE_FAULT,
|
---|
| 337 | const.MESSAGETYPE_NOGO,
|
---|
| 338 | const.MESSAGETYPE_GATE_SYSTEM,
|
---|
| 339 | const.MESSAGETYPE_HELP]:
|
---|
| 340 | return const.MESSAGELEVEL_BOTH
|
---|
[132] | 341 | else:
|
---|
[133] | 342 | return const.MESSAGELEVEL_FS
|
---|
[132] | 343 |
|
---|
| 344 | def _getMessageTypeLevelOptionName(self, messageType):
|
---|
| 345 | """Get the option name for the given message type level."""
|
---|
| 346 | return const.messageType2string(messageType)
|
---|
[155] | 347 |
|
---|
| 348 | def setupLocale(self):
|
---|
| 349 | """Setup the locale based on the language set.
|
---|
| 350 |
|
---|
| 351 | Return True if a specific language was set, False otherwise."""
|
---|
| 352 | import locale
|
---|
| 353 | if self._language:
|
---|
| 354 | print "Setting up locale for", self._language
|
---|
| 355 | os.environ["LANGUAGE"] = self._language
|
---|
| 356 | langAndEncoding = self._language + "." + locale.getpreferredencoding()
|
---|
| 357 | print langAndEncoding
|
---|
| 358 | os.environ["LANG"] = langAndEncoding
|
---|
| 359 | os.environ["LC_MESSAGES"] = langAndEncoding
|
---|
| 360 | os.environ["LC_COLLATE"] = langAndEncoding
|
---|
| 361 | os.environ["LC_CTYPE"] = langAndEncoding
|
---|
| 362 | return True
|
---|
| 363 | else:
|
---|
| 364 | return False
|
---|
| 365 |
|
---|
[107] | 366 | def getLanguage(self):
|
---|
| 367 | """Get the language to be used."""
|
---|
[113] | 368 | import locale
|
---|
[107] | 369 | if self._language:
|
---|
[124] | 370 | if os.name=="nt":
|
---|
| 371 | if self._language in _languageMap:
|
---|
| 372 | locale.setlocale(locale.LC_ALL, _languageMap[self._language])
|
---|
| 373 | else:
|
---|
| 374 | locale.setlocale(locale.LC_ALL, "")
|
---|
| 375 | else:
|
---|
| 376 | locale.setlocale(locale.LC_ALL, (self._language,
|
---|
| 377 | locale.getpreferredencoding()))
|
---|
[107] | 378 | return self._language
|
---|
| 379 | else:
|
---|
[113] | 380 | locale.setlocale(locale.LC_ALL, "")
|
---|
[107] | 381 | return locale.getdefaultlocale()[0]
|
---|
| 382 |
|
---|
[36] | 383 | #-------------------------------------------------------------------------------
|
---|