Created ostools module to unify OSX integration and made everything use it.

This commit is contained in:
Lexi 2011-08-11 09:17:53 +01:00
parent da7cc697e5
commit 98b681fa56
5 changed files with 55 additions and 66 deletions

4
irc.py
View file

@ -11,8 +11,8 @@ from dataobjs import Mood, PesterProfile
from generic import PesterList from generic import PesterList
from version import _pcVersion from version import _pcVersion
import sys, os import ostools
if sys.platform == "darwin" and os.path.abspath('.').find('.app') != -1: if ostools.isOSXBundle():
logging.basicConfig(level=logging.WARNING) logging.basicConfig(level=logging.WARNING)
else: else:
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)

View file

@ -1,6 +1,7 @@
import os, sys import os, sys
import codecs import codecs
import re import re
import ostools
from time import strftime, strptime from time import strftime, strptime
from PyQt4 import QtGui, QtCore from PyQt4 import QtGui, QtCore
from generic import RightClickList, RightClickTree from generic import RightClickList, RightClickTree
@ -41,9 +42,6 @@ class PesterLogUserSelect(QtGui.QDialog):
self.theme = theme self.theme = theme
self.parent = parent self.parent = parent
self.handle = parent.profile().handle 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.setStyleSheet(self.theme["main/defaultwindow/style"])
@ -125,8 +123,8 @@ class PesterLogUserSelect(QtGui.QDialog):
@QtCore.pyqtSlot() @QtCore.pyqtSlot()
def openDir(self): def openDir(self):
if sys.platform == "darwin": if ostools.isOSX():
_datadir = os.path.join(str(QtGui.QDesktopServices.storageLocation(QtGui.QDesktopServices.DataLocation)),"Pesterchum/") _datadir = ostools.getDataDir()
QtGui.QDesktopServices.openUrl(QtCore.QUrl("file:///" + os.path.join(_datadir, "logs"), QtCore.QUrl.TolerantMode)) QtGui.QDesktopServices.openUrl(QtCore.QUrl("file:///" + os.path.join(_datadir, "logs"), QtCore.QUrl.TolerantMode))
else: else:
QtGui.QDesktopServices.openUrl(QtCore.QUrl("file:///" + os.path.join(os.getcwd(), "logs"), QtCore.QUrl.TolerantMode)) QtGui.QDesktopServices.openUrl(QtCore.QUrl("file:///" + os.path.join(os.getcwd(), "logs"), QtCore.QUrl.TolerantMode))
@ -143,9 +141,6 @@ class PesterLogViewer(QtGui.QDialog):
self.handle = parent.profile().handle self.handle = parent.profile().handle
self.chum = chum self.chum = chum
self.convos = {} 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.setStyleSheet(self.theme["main/defaultwindow/style"])

18
ostools.py Normal file
View file

@ -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 ''

View file

@ -12,6 +12,7 @@ import codecs
import re import re
import socket import socket
import platform import platform
import ostools
from time import strftime, time from time import strftime, time
missing = [] 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 # OSX's data directory and it doesn't hurt to have everything set up before
# plowing on. :o) # plowing on. :o)
# ~Lex # ~Lex
_datadir = os.path.join(str(QtGui.QDesktopServices.storageLocation(QtGui.QDesktopServices.DataLocation)),"Pesterchum/") _datadir = ostools.getDataDir()
if sys.platform == "darwin": # See, what I've done here is that _datadir is '' if we're not on OSX, so the
if not os.path.exists(_datadir): # concatination is the same as if it wasn't there.
if not os.path.exists(_datadir):
os.mkdir(_datadir) os.mkdir(_datadir)
if not os.path.exists(_datadir+"profiles"): if not os.path.exists(_datadir+"profiles"):
os.mkdir(_datadir+"profiles") os.mkdir(_datadir+"profiles")
if not os.path.exists(_datadir+"pesterchum.js"): if not os.path.exists(_datadir+"pesterchum.js"):
f = open(_datadir+"pesterchum.js", 'w') f = open(_datadir+"pesterchum.js", 'w')
f.write("{}") f.write("{}")
f.close() f.close()
else: if not os.path.exists(_datadir+"logs"):
if not os.path.exists("logs"): os.mkdir(_datadir+"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()
from menus import PesterChooseQuirks, PesterChooseTheme, \ from menus import PesterChooseQuirks, PesterChooseTheme, \
PesterChooseProfile, PesterOptions, PesterUserlist, PesterMemoList, \ PesterChooseProfile, PesterOptions, PesterUserlist, PesterMemoList, \
@ -124,9 +119,6 @@ class PesterLog(object):
self.parent = parent self.parent = parent
self.handle = handle self.handle = handle
self.convos = {} self.convos = {}
if sys.platform != "darwin":
self.logpath = "logs"
else:
self.logpath = _datadir+"logs" self.logpath = _datadir+"logs"
def log(self, handle, msg): def log(self, handle, msg):
@ -181,9 +173,6 @@ class PesterLog(object):
class PesterProfileDB(dict): class PesterProfileDB(dict):
def __init__(self): 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): if not os.path.exists(self.logpath):
@ -248,9 +237,6 @@ class PesterProfileDB(dict):
class pesterTheme(dict): class pesterTheme(dict):
def __init__(self, name, default=False): def __init__(self, name, default=False):
if sys.platform != "darwin":
self.path = "themes/%s" % (name)
else:
self.path = _datadir+"themes/%s" % (name) self.path = _datadir+"themes/%s" % (name)
if not os.path.exists(self.path): if not os.path.exists(self.path):
self.path = "themes/%s" % (name) self.path = "themes/%s" % (name)
@ -327,9 +313,6 @@ class userConfig(object):
# Use for bit flag blink # Use for bit flag blink
self.PBLINK = 1 self.PBLINK = 1
self.MBLINK = 2 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) fp = open(self.filename)
self.config = json.load(fp) self.config = json.load(fp)
@ -339,9 +322,6 @@ class userConfig(object):
else: else:
self.userprofile = None 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): if not os.path.exists(self.logpath):
@ -537,11 +517,13 @@ class userConfig(object):
fp.close() fp.close()
def availableThemes(self): def availableThemes(self):
themes = [] 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: for d in dirnames:
themes.append(d) themes.append(d)
if sys.platform == "darwin": # For OSX, also load embedded themes.
for dirname, dirnames, filenames in os.walk(_datadir+'themes'): if ostools.isOSX():
for dirname, dirnames, filenames in os.walk('themes'):
for d in dirnames: for d in dirnames:
if d not in themes: if d not in themes:
themes.append(d) themes.append(d)
@ -549,10 +531,7 @@ class userConfig(object):
return themes return themes
def availableProfiles(self): def availableProfiles(self):
profs = [] profs = []
if sys.platform == "darwin":
profileloc = _datadir+'profiles' profileloc = _datadir+'profiles'
else:
profileloc = 'profiles'
for dirname, dirnames, filenames in os.walk(profileloc): for dirname, dirnames, filenames in os.walk(profileloc):
for filename in filenames: for filename in filenames:
l = len(filename) l = len(filename)
@ -562,9 +541,6 @@ class userConfig(object):
return [userProfile(p) for p in profs] return [userProfile(p) for p in profs]
class userProfile(object): class userProfile(object):
def __init__(self, user): 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: if type(user) is PesterProfile:

View file

@ -1,9 +1,9 @@
import os, sys, imp, re import os, sys, imp, re, ostools
from PyQt4 import QtGui, QtCore from PyQt4 import QtGui, QtCore
class PythonQuirks(object): class PythonQuirks(object):
def __init__(self): 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.home = os.getcwd()
self.quirks = {} self.quirks = {}
self.last = {} self.last = {}
@ -18,7 +18,7 @@ class PythonQuirks(object):
for fn in os.listdir(os.path.join(self.home, 'quirks')): for fn in os.listdir(os.path.join(self.home, 'quirks')):
if fn.endswith('.py') and not fn.startswith('_'): if fn.endswith('.py') and not fn.startswith('_'):
filenames.append(os.path.join(self.home, 'quirks', fn)) 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')): if not os.path.exists(os.path.join(self._datadir, 'quirks')):
os.mkdir(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')): for fn in os.listdir(os.path.join(self._datadir, 'quirks')):