| 49 |
|
#
|
| 50 |
|
# from pwiki.wxHelper import IconCache
|
| 51 |
|
# from pwiki.MiscEvent import KeyFunctionSink
|
| 52 |
|
# from pwiki.PersonalWikiFrame import PersonalWikiFrame
|
| 53 |
|
# from pwiki.StringOps import mbcsDec, createRandomString
|
| 54 |
|
# from pwiki.CmdLineAction import CmdLineAction
|
| 55 |
|
# from pwiki.Serialization import SerializeStream
|
| 56 |
|
# from pwiki import Ipc
|
| 57 |
|
# from pwiki.Configuration import createGlobalConfiguration
|
| 58 |
|
# from pwiki.Localization import getCollatorByString, CASEMODE_UPPER_INSIDE, \
|
| 59 |
|
# CASEMODE_UPPER_FIRST
|
| 60 |
|
#
|
| 61 |
|
#
|
| 62 |
|
#
|
| 63 |
|
# def findDirs():
|
| 64 |
|
# """
|
| 65 |
|
# Returns tuple (wikiAppDir, globalConfigDir)
|
| 66 |
|
# """
|
| 67 |
|
# wikiAppDir = None
|
| 68 |
|
#
|
| 69 |
|
# isWindows = (wxGetOsVersion()[0] == wxWIN95) or \
|
| 70 |
|
# (wxGetOsVersion()[0] == wxWINDOWS_NT)
|
| 71 |
|
#
|
| 72 |
|
# try:
|
| 73 |
|
# wikiAppDir = os.path.dirname(os.path.abspath(sys.argv[0]))
|
| 74 |
|
# if not wikiAppDir:
|
| 75 |
|
# wikiAppDir = r"C:\Program Files\WikidPad"
|
| 76 |
|
#
|
| 77 |
|
# globalConfigDir = None
|
| 78 |
|
#
|
| 79 |
|
# # This allows to keep the program with config on an USB stick
|
| 80 |
|
# if os.path.exists(os.path.join(wikiAppDir, "WikidPad.config")):
|
| 81 |
|
# globalConfigDir = wikiAppDir
|
| 82 |
|
# else:
|
| 83 |
|
# globalConfigDir = os.environ.get("HOME")
|
| 84 |
|
# if not (globalConfigDir and os.path.exists(globalConfigDir)):
|
| 85 |
|
# # globalConfigDir = os.environ.get("USERPROFILE")
|
| 86 |
|
# # if not (globalConfigDir and os.path.exists(globalConfigDir)):
|
| 87 |
|
# # Instead of checking USERNAME, the user config dir. is
|
| 88 |
|
# # now used
|
| 89 |
|
# globalConfigDir = wxStandardPaths.Get().GetUserConfigDir()
|
| 90 |
|
# if os.path.exists(globalConfigDir) and isWindows:
|
| 91 |
|
# try:
|
| 92 |
|
# realGlobalConfigDir = os.path.join(globalConfigDir,
|
| 93 |
|
# "WikidPad")
|
| 94 |
|
# if not os.path.exists(realGlobalConfigDir):
|
| 95 |
|
# # If it doesn't exist, create the directory
|
| 96 |
|
# os.mkdir(realGlobalConfigDir)
|
| 97 |
|
#
|
| 98 |
|
# globalConfigDir = realGlobalConfigDir
|
| 99 |
|
# except:
|
| 100 |
|
# traceback.print_exc()
|
| 101 |
|
#
|
| 102 |
|
# # user = os.environ.get("USERNAME")
|
| 103 |
|
# # if user:
|
| 104 |
|
# # globalConfigDir = r"c:\Documents And Settings\%s" % user
|
| 105 |
|
#
|
| 106 |
|
# # if globalConfigDir and os.path.exists(globalConfigDir) and isWindows:
|
| 107 |
|
# # try:
|
| 108 |
|
# # realGlobalConfigDir = os.path.join(globalConfigDir, "WikidPad")
|
| 109 |
|
# # if not os.path.exists(realGlobalConfigDir):
|
| 110 |
|
# # # If it doesn't exist, create the directory
|
| 111 |
|
# # os.mkdir(realGlobalConfigDir)
|
| 112 |
|
# # # ... and try to move already created config files to it
|
| 113 |
|
# # oldCfgFile = os.path.join(globalConfigDir,
|
| 114 |
|
# # "WikidPad.config")
|
| 115 |
|
# # oldGlobalsDir = os.path.join(globalConfigDir,
|
| 116 |
|
# # ".WikidPadGlobals")
|
| 117 |
|
# # newCfgFile = os.path.join(realGlobalConfigDir,
|
| 118 |
|
# # "WikidPad.config")
|
| 119 |
|
# # newGlobalsDir = os.path.join(realGlobalConfigDir,
|
| 120 |
|
# # ".WikidPadGlobals")
|
| 121 |
|
# # if os.path.exists(oldCfgFile):
|
| 122 |
|
# # os.rename(oldCfgFile, newCfgFile)
|
| 123 |
|
# # if os.path.exists(oldGlobalsDir):
|
| 124 |
|
# # os.rename(oldGlobalsDir, newGlobalsDir)
|
| 125 |
|
# #
|
| 126 |
|
# # globalConfigDir = realGlobalConfigDir
|
| 127 |
|
# # except:
|
| 128 |
|
# # traceback.print_exc()
|
| 129 |
|
#
|
| 130 |
|
# finally:
|
| 131 |
|
# pass
|
| 132 |
|
#
|
| 133 |
|
# if not globalConfigDir:
|
| 134 |
|
# globalConfigDir = wikiAppDir
|
| 135 |
|
#
|
| 136 |
|
# # mbcs decoding
|
| 137 |
|
# if wikiAppDir is not None:
|
| 138 |
|
# wikiAppDir = mbcsDec(wikiAppDir, "replace")[0]
|
| 139 |
|
#
|
| 140 |
|
# if globalConfigDir is not None:
|
| 141 |
|
# globalConfigDir = mbcsDec(globalConfigDir, "replace")[0]
|
| 142 |
|
#
|
| 143 |
|
# ExceptionLogger._exceptionDestDir = globalConfigDir
|
| 144 |
|
#
|
| 145 |
|
# return (wikiAppDir, globalConfigDir)
|
| 146 |
|
#
|
| 147 |
|
#
|
| 148 |
|
#
|
| 149 |
|
#
|
| |
42 |
|
| 189 |
|
#
|
| 190 |
|
#
|
| 191 |
|
# class App(wxApp):
|
| 192 |
|
# def __init__(self, *args, **kwargs):
|
| 193 |
|
# global app
|
| 194 |
|
# app = self
|
| 195 |
|
#
|
| 196 |
|
# wxApp.__init__(self, *args, **kwargs)
|
| 197 |
|
# self.SetAppName("WikidPad")
|
| 198 |
|
# # Do not initialize member variables here!
|
| 199 |
|
#
|
| 200 |
|
#
|
| 201 |
|
# def OnInit(self):
|
| 202 |
|
# # TODO Load global config here!!!
|
| 203 |
|
# ## _prof.start()
|
| 204 |
|
#
|
| 205 |
|
# self.SetAppName("WikidPad")
|
| 206 |
|
# self.removeAppLockOnExit = False
|
| 207 |
|
# appdir = os.path.dirname(os.path.abspath(sys.argv[0]))
|
| 208 |
|
#
|
| 209 |
|
# wikiAppDir, globalConfigDir = findDirs()
|
| 210 |
|
#
|
| 211 |
|
# if not globalConfigDir or not os.path.exists(globalConfigDir):
|
| 212 |
|
# raise Exception(u"Error initializing environment, couldn't locate "
|
| 213 |
|
# u"global config directory")
|
| 214 |
|
#
|
| 215 |
|
# self.wikiAppDir = wikiAppDir
|
| 216 |
|
# self.globalConfigDir = globalConfigDir
|
| 217 |
|
#
|
| 218 |
|
# self.globalConfigSubDir = os.path.join(self.globalConfigDir,
|
| 219 |
|
# ".WikidPadGlobals")
|
| 220 |
|
# if not os.path.exists(self.globalConfigSubDir):
|
| 221 |
|
# os.mkdir(self.globalConfigSubDir)
|
| 222 |
|
#
|
| 223 |
|
# # load or create global configuration
|
| 224 |
|
# globalConfigLoc = os.path.join(self.globalConfigDir, "WikidPad.config")
|
| 225 |
|
# self.globalConfig = createGlobalConfiguration()
|
| 226 |
|
# if os.path.exists(globalConfigLoc):
|
| 227 |
|
# try:
|
| 228 |
|
# self.globalConfig.loadConfig(globalConfigLoc)
|
| 229 |
|
# except Configuration.Error:
|
| 230 |
|
# self.createDefaultGlobalConfig(globalConfigLoc)
|
| 231 |
|
# else:
|
| 232 |
|
# self.createDefaultGlobalConfig(globalConfigLoc)
|
| 233 |
|
#
|
| 234 |
|
# self.globalConfig.getMiscEvent().addListener(KeyFunctionSink((
|
| 235 |
|
# ("configuration changed", self.onGlobalConfigurationChanged),
|
| 236 |
|
# )), False)
|
| 237 |
|
#
|
| 238 |
|
#
|
| 239 |
|
# self.lowResources = self.globalConfig.getboolean("main", "lowresources")
|
| 240 |
|
#
|
| 241 |
|
# # Build icon cache
|
| 242 |
|
# iconDir = os.path.join(self.wikiAppDir, "icons")
|
| 243 |
|
# self.iconCache = IconCache(iconDir, self.lowResources)
|
| 244 |
|
#
|
| 245 |
|
# # self.collator = getCollatorByString("c")
|
| 246 |
|
# # self.collator = getCollatorByString("Default")
|
| 247 |
|
# # self.collator = getCollatorByString("", True)
|
| 248 |
|
#
|
| 249 |
|
# if self.globalConfig.getboolean("main", "single_process"):
|
| 250 |
|
# # Single process mode means to create a server, detect an already
|
| 251 |
|
# # running server and, if there, just send the commandline to
|
| 252 |
|
# # the running server and quit then.
|
| 253 |
|
#
|
| 254 |
|
# # We create a "password" so that no other user can send commands to this
|
| 255 |
|
# # WikidPad instance.
|
| 256 |
|
# appCookie = createRandomString(30)
|
| 257 |
|
#
|
| 258 |
|
# port = Ipc.createCommandServer(appCookie)
|
| 259 |
|
#
|
| 260 |
|
# # True if this is the single existing instance which should write
|
| 261 |
|
# # a new "AppLock.lock" file which either didn't exist or was invalid
|
| 262 |
|
#
|
| 263 |
|
# singleInstance = True
|
| 264 |
|
#
|
| 265 |
|
# # TODO maybe more secure method to ensure atomic exist. check and
|
| 266 |
|
# # writing of file
|
| 267 |
|
# if os.path.exists(os.path.join(self.globalConfigSubDir, "AppLock.lock")):
|
| 268 |
|
# singleInstance = False
|
| 269 |
|
# # There seems to be(!) another instance already
|
| 270 |
|
# # TODO Try to send commandline
|
| 271 |
|
# f = open(os.path.join(self.globalConfigSubDir, "AppLock.lock"), "ra")
|
| 272 |
|
# appLockContent = f.read()
|
| 273 |
|
# f.close()
|
| 274 |
|
#
|
| 275 |
|
# lines = appLockContent.split("\n")
|
| 276 |
|
# if len(lines) != 3:
|
| 277 |
|
# return True # TODO Error handling!!!
|
| 278 |
|
#
|
| 279 |
|
# appCookie = lines[0]
|
| 280 |
|
# remotePort = int(lines[1])
|
| 281 |
|
#
|
| 282 |
|
# if port != remotePort:
|
| 283 |
|
# # Everything ok so far
|
| 284 |
|
# sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
| 285 |
|
# sock.settimeout(10.0)
|
| 286 |
|
# try:
|
| 287 |
|
# try:
|
| 288 |
|
# sock.connect(("127.0.0.1", remotePort))
|
| 289 |
|
# greet = self._readSocketLine(sock)
|
| 290 |
|
# if greet == "WikidPad_command_server 1.0":
|
| 291 |
|
# sock.send("cmdline\n" + appCookie + "\n")
|
| 292 |
|
#
|
| 293 |
|
# ack = self._readSocketLine(sock)
|
| 294 |
|
# if ack[0] == "+":
|
| 295 |
|
# # app cookie ok
|
| 296 |
|
# sst = SerializeStream(stringBuf="", readMode=False)
|
| 297 |
|
# sst.serArrString(sys.argv[1:])
|
| 298 |
|
# sock.send(sst.getBytes())
|
| 299 |
|
#
|
| 300 |
|
# return True
|
| 301 |
|
#
|
| 302 |
|
# # Reaching this point means something went wrong
|
| 303 |
|
# singleInstance = True # TODO More fine grained reaction
|
| 304 |
|
# except socket.timeout, e:
|
| 305 |
|
# singleInstance = True
|
| 306 |
|
# except socket.error, e:
|
| 307 |
|
# if e.args[0] == 10061:
|
| 308 |
|
# # Connection refused (port not bound to a server)
|
| 309 |
|
# singleInstance = True
|
| 310 |
|
# else:
|
| 311 |
|
# raise
|
| 312 |
|
# finally:
|
| 313 |
|
# sock.close()
|
| 314 |
|
#
|
| 315 |
|
# else:
|
| 316 |
|
# # Sure indicator that AppLock file is invalid if newly
|
| 317 |
|
# # created server opened a port which is claimed to be used
|
| 318 |
|
# # already by previously started instance.
|
| 319 |
|
# singleInstance = True
|
| 320 |
|
#
|
| 321 |
|
# if not singleInstance:
|
| 322 |
|
# return False
|
| 323 |
|
#
|
| 324 |
|
# if port != -1:
|
| 325 |
|
# # Server is connected, start it
|
| 326 |
|
# Ipc.startCommandServer()
|
| 327 |
|
#
|
| 328 |
|
# appLockContent = appCookie + "\n" + str(port) + "\n"
|
| 329 |
|
#
|
| 330 |
|
# f = open(os.path.join(self.globalConfigSubDir, "AppLock.lock"), "wa")
|
| 331 |
|
# f.write(appLockContent)
|
| 332 |
|
# f.close()
|
| 333 |
|
#
|
| 334 |
|
# self.removeAppLockOnExit = True
|
| 335 |
|
#
|
| 336 |
|
# self.collator = None
|
| 337 |
|
#
|
| 338 |
|
# # Further configuration settings
|
| 339 |
|
# self._rereadGlobalConfig()
|
| 340 |
|
#
|
| 341 |
|
# # Load wxPython XML resources
|
| 342 |
|
# rf = open(os.path.join(appdir, "WikidPad.xrc"), "r")
|
| 343 |
|
# rd = rf.read()
|
| 344 |
|
# rf.close()
|
| 345 |
|
#
|
| 346 |
|
# res = xrc.wxXmlResource.Get()
|
| 347 |
|
# res.SetFlags(0)
|
| 348 |
|
# res.LoadFromString(rd)
|
| 349 |
|
#
|
| 350 |
|
# self.startPersonalWikiFrame(CmdLineAction(sys.argv[1:]))
|
| 351 |
|
#
|
| 352 |
|
# return True
|
| 353 |
|
#
|
| 354 |
|
#
|
| 355 |
|
# def _readSocketLine(self, sock):
|
| 356 |
|
# result = []
|
| 357 |
|
# read = 0
|
| 358 |
|
# while read < 300:
|
| 359 |
|
# c = sock.recv(1)
|
| 360 |
|
# if c == "\n" or c == "":
|
| 361 |
|
# return "".join(result)
|
| 362 |
|
# result.append(c)
|
| 363 |
|
# read += 1
|
| 364 |
|
#
|
| 365 |
|
# return ""
|
| 366 |
|
#
|
| 367 |
|
#
|
| 368 |
|
# def onGlobalConfigurationChanged(self, miscevt):
|
| 369 |
|
# self._rereadGlobalConfig()
|
| 370 |
|
#
|
| 371 |
|
#
|
| 372 |
|
# def _rereadGlobalConfig(self):
|
| 373 |
|
# """
|
| 374 |
|
# Make settings from global config which are changeable during session
|
| 375 |
|
# """
|
| 376 |
|
# collationOrder = self.globalConfig.get("main", "collation_order")
|
| 377 |
|
# collationUppercaseFirst = self.globalConfig.getboolean("main",
|
| 378 |
|
# "collation_uppercaseFirst")
|
| 379 |
|
#
|
| 380 |
|
# if collationUppercaseFirst:
|
| 381 |
|
# collationCaseMode = CASEMODE_UPPER_FIRST
|
| 382 |
|
# else:
|
| 383 |
|
# collationCaseMode = CASEMODE_UPPER_INSIDE
|
| 384 |
|
#
|
| 385 |
|
# self.collator = getCollatorByString(collationOrder, collationCaseMode)
|
| 386 |
|
#
|
| 387 |
|
#
|
| 388 |
|
# def OnExit(self):
|
| 389 |
|
# if self.removeAppLockOnExit:
|
| 390 |
|
# try:
|
| 391 |
|
# os.remove(os.path.join(self.globalConfigSubDir, "AppLock.lock"))
|
| 392 |
|
# except: # OSError, ex:
|
| 393 |
|
# traceback.print_exc()
|
| 394 |
|
# # TODO Error message!
|
| 395 |
|
#
|
| 396 |
|
# try:
|
| 397 |
|
# Ipc.stopCommandServer()
|
| 398 |
|
# except:
|
| 399 |
|
# traceback.print_exc()
|
| 400 |
|
#
|
| 401 |
|
# if ExceptionLogger._exceptionOccurred and hasattr(sys, 'frozen'):
|
| 402 |
|
# wxMessageBox("An error occurred during this session\nSee file %s" %
|
| 403 |
|
# os.path.join(ExceptionLogger._exceptionDestDir, "WikidPad_Error.log"),
|
| 404 |
|
# "Error", style = wxOK)
|
| 405 |
|
#
|
| 406 |
|
#
|
| 407 |
|
# def startPersonalWikiFrame(self, clAction):
|
| 408 |
|
# wikiFrame = PersonalWikiFrame(None, -1, "WikidPad", self.wikiAppDir,
|
| 409 |
|
# self.globalConfigDir, self.globalConfigSubDir, clAction)
|
| 410 |
|
#
|
| 411 |
|
# self.SetTopWindow(wikiFrame)
|
| 412 |
|
# ## _prof.stop()
|
| 413 |
|
#
|
| 414 |
|
# # set the icon of the app
|
| 415 |
|
# try:
|
| 416 |
|
# wikiFrame.SetIcon(wxIcon(os.path.join(self.wikiAppDir, 'icons',
|
| 417 |
|
# 'pwiki.ico'), wxBITMAP_TYPE_ICO))
|
| 418 |
|
# except:
|
| 419 |
|
# pass
|
| 420 |
|
#
|
| 421 |
|
#
|
| 422 |
|
# def createDefaultGlobalConfig(self, globalConfigLoc):
|
| 423 |
|
# self.globalConfig.createEmptyConfig(globalConfigLoc)
|
| 424 |
|
# self.globalConfig.fillWithDefaults()
|
| 425 |
|
#
|
| 426 |
|
# wikidPadHelp = os.path.join(self.wikiAppDir, 'WikidPadHelp',
|
| 427 |
|
# 'WikidPadHelp.wiki')
|
| 428 |
|
#
|
| 429 |
|
# self.globalConfig.set("main", "wiki_history", wikidPadHelp)
|
| 430 |
|
# self.globalConfig.set("main", "last_wiki", wikidPadHelp)
|
| 431 |
|
#
|
| 432 |
|
# self.globalConfig.set("main", "last_active_dir", os.getcwd())
|
| 433 |
|
#
|
| 434 |
|
#
|
| 435 |
|
# def getGlobalConfigSubDir(self):
|
| 436 |
|
# return self.globalConfigSubDir
|
| 437 |
|
#
|
| 438 |
|
# def getGlobalConfig(self):
|
| 439 |
|
# return self.globalConfig
|
| 440 |
|
#
|
| 441 |
|
# def getLowResources(self):
|
| 442 |
|
# """
|
| 443 |
|
# Return state of the low resources global setting
|
| 444 |
|
# """
|
| 445 |
|
# return self.lowResources
|
| 446 |
|
#
|
| 447 |
|
# def getIconCache(self):
|
| 448 |
|
# """
|
| 449 |
|
# Return the icon cache object
|
| 450 |
|
# """
|
| 451 |
|
# return self.iconCache
|
| 452 |
|
#
|
| 453 |
|
# def getCollator(self):
|
| 454 |
|
# return self.collator
|
| 455 |
|
#
|
| 456 |
|
#
|
| 457 |
|
#
|
| 458 |
|
class ErrorFrame(wxFrame):
|
| |
82 |
|
| |
83 |
|
| |
84 |
class ErrorFrame(wx.Frame):
|