From 98b681fa56d3295cc1af39709a44775369c1e8af Mon Sep 17 00:00:00 2001 From: Lexi Date: Thu, 11 Aug 2011 09:17:53 +0100 Subject: [PATCH] Created ostools module to unify OSX integration and made everything use it. --- irc.py | 4 +-- logviewer.py | 15 ++++------ ostools.py | 18 ++++++++++++ pesterchum.py | 78 ++++++++++++++++++--------------------------------- pyquirks.py | 6 ++-- 5 files changed, 55 insertions(+), 66 deletions(-) create mode 100644 ostools.py diff --git a/irc.py b/irc.py index 16400c5..f73b9de 100644 --- a/irc.py +++ b/irc.py @@ -11,8 +11,8 @@ from dataobjs import Mood, PesterProfile from generic import PesterList from version import _pcVersion -import sys, os -if sys.platform == "darwin" and os.path.abspath('.').find('.app') != -1: +import ostools +if ostools.isOSXBundle(): logging.basicConfig(level=logging.WARNING) else: logging.basicConfig(level=logging.INFO) diff --git a/logviewer.py b/logviewer.py index f11df3e..91896d7 100644 --- a/logviewer.py +++ b/logviewer.py @@ -1,6 +1,7 @@ import os, sys import codecs import re +import ostools from time import strftime, strptime from PyQt4 import QtGui, QtCore from generic import RightClickList, RightClickTree @@ -41,10 +42,7 @@ class PesterLogUserSelect(QtGui.QDialog): self.theme = theme self.parent = parent self.handle = parent.profile().handle - if sys.platform != "darwin": - self.logpath = "logs" - else: - self.logpath = _datadir+"logs" + self.logpath = _datadir+"logs" self.setStyleSheet(self.theme["main/defaultwindow/style"]) self.setWindowTitle("Pesterlogs") @@ -125,8 +123,8 @@ class PesterLogUserSelect(QtGui.QDialog): @QtCore.pyqtSlot() def openDir(self): - if sys.platform == "darwin": - _datadir = os.path.join(str(QtGui.QDesktopServices.storageLocation(QtGui.QDesktopServices.DataLocation)),"Pesterchum/") + if ostools.isOSX(): + _datadir = ostools.getDataDir() QtGui.QDesktopServices.openUrl(QtCore.QUrl("file:///" + os.path.join(_datadir, "logs"), QtCore.QUrl.TolerantMode)) else: QtGui.QDesktopServices.openUrl(QtCore.QUrl("file:///" + os.path.join(os.getcwd(), "logs"), QtCore.QUrl.TolerantMode)) @@ -143,10 +141,7 @@ class PesterLogViewer(QtGui.QDialog): self.handle = parent.profile().handle self.chum = chum self.convos = {} - if sys.platform != "darwin": - self.logpath = "logs" - else: - self.logpath = _datadir+"logs" + self.logpath = _datadir+"logs" self.setStyleSheet(self.theme["main/defaultwindow/style"]) self.setWindowTitle("Pesterlogs with " + self.chum) diff --git a/ostools.py b/ostools.py new file mode 100644 index 0000000..68d980e --- /dev/null +++ b/ostools.py @@ -0,0 +1,18 @@ +from os import path +from sys import platform +from PyQt4.QtGui import QDesktopServices + +def isOSX(): + return platform == "darwin" + +def isWin32(): + return platform == "win32" + +def isOSXBundle(): + return isOSX() and path.abspath('.').find(".app") + +def getDataDir(): + if isOSX(): + return path.join(str(QDesktopServices.storageLocation(QDesktopServices.DataLocation)), "Pesterchum/") + else: + return '' diff --git a/pesterchum.py b/pesterchum.py index c73d4ea..ae9a646 100644 --- a/pesterchum.py +++ b/pesterchum.py @@ -12,6 +12,7 @@ import codecs import re import socket import platform +import ostools from time import strftime, time missing = [] @@ -46,25 +47,19 @@ if not ((major > 4) or (major == 4 and minor >= 6)): # OSX's data directory and it doesn't hurt to have everything set up before # plowing on. :o) # ~Lex -_datadir = os.path.join(str(QtGui.QDesktopServices.storageLocation(QtGui.QDesktopServices.DataLocation)),"Pesterchum/") -if sys.platform == "darwin": - if not os.path.exists(_datadir): - os.mkdir(_datadir) - if not os.path.exists(_datadir+"profiles"): - os.mkdir(_datadir+"profiles") - if not os.path.exists(_datadir+"pesterchum.js"): - f = open(_datadir+"pesterchum.js", 'w') - f.write("{}") - f.close() -else: - if not os.path.exists("logs"): - os.mkdir("logs") - if not os.path.exists("profiles"): - os.mkdir("profiles") - if not os.path.exists("pesterchum.js"): - f = open("pesterchum.js", 'w') - f.write("{}") - f.close() +_datadir = ostools.getDataDir() +# See, what I've done here is that _datadir is '' if we're not on OSX, so the +# concatination is the same as if it wasn't there. +if not os.path.exists(_datadir): + os.mkdir(_datadir) +if not os.path.exists(_datadir+"profiles"): + os.mkdir(_datadir+"profiles") +if not os.path.exists(_datadir+"pesterchum.js"): + f = open(_datadir+"pesterchum.js", 'w') + f.write("{}") + f.close() +if not os.path.exists(_datadir+"logs"): + os.mkdir(_datadir+"logs") from menus import PesterChooseQuirks, PesterChooseTheme, \ PesterChooseProfile, PesterOptions, PesterUserlist, PesterMemoList, \ @@ -124,10 +119,7 @@ class PesterLog(object): self.parent = parent self.handle = handle self.convos = {} - if sys.platform != "darwin": - self.logpath = "logs" - else: - self.logpath = _datadir+"logs" + self.logpath = _datadir+"logs" def log(self, handle, msg): if self.parent.config.time12Format(): @@ -181,10 +173,7 @@ class PesterLog(object): class PesterProfileDB(dict): def __init__(self): - if sys.platform != "darwin": - self.logpath = "logs" - else: - self.logpath = _datadir+"logs" + self.logpath = _datadir+"logs" if not os.path.exists(self.logpath): os.makedirs(self.logpath) @@ -248,12 +237,9 @@ class PesterProfileDB(dict): class pesterTheme(dict): def __init__(self, name, default=False): - if sys.platform != "darwin": + self.path = _datadir+"themes/%s" % (name) + if not os.path.exists(self.path): self.path = "themes/%s" % (name) - else: - self.path = _datadir+"themes/%s" % (name) - if not os.path.exists(self.path): - self.path = "themes/%s" % (name) self.name = name fp = open(self.path+"/style.js") @@ -327,10 +313,7 @@ class userConfig(object): # Use for bit flag blink self.PBLINK = 1 self.MBLINK = 2 - if sys.platform != "darwin": - self.filename = "pesterchum.js" - else: - self.filename = _datadir+"pesterchum.js" + self.filename = _datadir+"pesterchum.js" fp = open(self.filename) self.config = json.load(fp) fp.close() @@ -339,10 +322,7 @@ class userConfig(object): else: self.userprofile = None - if sys.platform != "darwin": - self.logpath = "logs" - else: - self.logpath = _datadir+"logs" + self.logpath = _datadir+"logs" if not os.path.exists(self.logpath): os.makedirs(self.logpath) @@ -537,11 +517,13 @@ class userConfig(object): fp.close() def availableThemes(self): themes = [] - for dirname, dirnames, filenames in os.walk('themes'): + # Load user themes. + for dirname, dirnames, filenames in os.walk(_datadir+'themes'): for d in dirnames: themes.append(d) - if sys.platform == "darwin": - for dirname, dirnames, filenames in os.walk(_datadir+'themes'): + # For OSX, also load embedded themes. + if ostools.isOSX(): + for dirname, dirnames, filenames in os.walk('themes'): for d in dirnames: if d not in themes: themes.append(d) @@ -549,10 +531,7 @@ class userConfig(object): return themes def availableProfiles(self): profs = [] - if sys.platform == "darwin": - profileloc = _datadir+'profiles' - else: - profileloc = 'profiles' + profileloc = _datadir+'profiles' for dirname, dirnames, filenames in os.walk(profileloc): for filename in filenames: l = len(filename) @@ -562,10 +541,7 @@ class userConfig(object): return [userProfile(p) for p in profs] class userProfile(object): def __init__(self, user): - if sys.platform != "darwin": - self.profiledir = "profiles" - else: - self.profiledir = _datadir+"profiles" + self.profiledir = _datadir+"profiles" if type(user) is PesterProfile: self.chat = user diff --git a/pyquirks.py b/pyquirks.py index f13df83..57d3d38 100644 --- a/pyquirks.py +++ b/pyquirks.py @@ -1,9 +1,9 @@ -import os, sys, imp, re +import os, sys, imp, re, ostools from PyQt4 import QtGui, QtCore class PythonQuirks(object): def __init__(self): - self._datadir = os.path.join(str(QtGui.QDesktopServices.storageLocation(QtGui.QDesktopServices.DataLocation)),"Pesterchum/") + self._datadir = ostools.getDataDir() self.home = os.getcwd() self.quirks = {} self.last = {} @@ -18,7 +18,7 @@ class PythonQuirks(object): for fn in os.listdir(os.path.join(self.home, 'quirks')): if fn.endswith('.py') and not fn.startswith('_'): filenames.append(os.path.join(self.home, 'quirks', fn)) - if sys.platform == "darwin": + if ostools.isOSX(): if not os.path.exists(os.path.join(self._datadir, 'quirks')): os.mkdir(os.path.join(self._datadir, 'quirks')) for fn in os.listdir(os.path.join(self._datadir, 'quirks')):