# -*- coding: utf-8 -*-
import os, tempfile, gettext, urllib, time, platform
import ConfigParser, StringIO
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import globalvars as g
from BeautifulSoup import BeautifulSoup
#Locale support
try:
LOCALE_DIR = "locale"
t = gettext.translation("main", LOCALE_DIR)
_ = t.ugettext
except:
_ = lambda s: s
#Skins
def get_skins():
skins = {}
try:
for item in os.listdir("skins" + os.path.sep):
if os.path.isdir("skins" + os.path.sep + item) and not item[:1] == ".":
skins[item] = "skins" + os.path.sep + item + os.path.sep + "style.qss"
for item in os.listdir(g.skins_path):
if os.path.isdir(g.skins_path + item) and not item[:1] == ".":
skins[item] = g.skins_path + item + os.path.sep + "style.qss"
except:
pass
return skins
#Plugins
def get_plugins():
pdirs = []
try:
for item in os.listdir("plugins" + os.path.sep):
if os.path.isdir("plugins" + os.path.sep + item) and not item[:1] == ".":
pdirs.append("plugins." + item)
for item in os.listdir(g.plugins_path):
if os.path.isdir(g.plugins_path + item) and not item[:1] == ".":
pdirs.append(item)
except:
pass
return pdirs
#Screens
def get_screens():
qscreen = QDesktopWidget()
screens = {}
for screen in range(0, qscreen.screenCount()):
screens[str(screen+1)] = screen
return screens
#Check for read and write perms
def canRW(path):
try:
f = tempfile.mkstemp("", "", path)
os.remove(f[1])
f = tempfile.mkdtemp("", "", path)
os.rmdir(f)
return True
except:
return False
#Warnings
def warnNoPrefs():
QMessageBox.question(None, _("Warning"), _("You don't have permissions to read/write in the directory %s. Storing plugins preferences won't work!" % g.user_path))
def warnNoUpdatable():
QMessageBox.question(None, _("Warning"), _("You don't have permissions to read/write in the directory %s. Jake-PyQT4 won't be able to update itself!" % os.getcwd()))
def warnNoPrefsNoUpdatable():
QMessageBox.question(None, _("Warning"), _("You don't have permissions to read/write in the directories %s and %s. Storing plugins preferences and self-updating won't work!" % (g.user_path, os.getcwd())))
#Check for updates
def checkUpdates():
try:
url = "https://python-jake.svn.sourceforge.net/svnroot/python-jake/releases/"
res = urllib2.urlopen(url, timeout = 5).read()
soup = BeautifulSoup(res)
items = soup.findAll("li")
updates = []
del items[0]
for item in items:
if int(item.text) > g.build:
updates.append(item.text)
updates.sort()
latest = updates[len(updates)-1]
url = "https://python-jake.svn.sourceforge.net/svnroot/python-jake/releases/" + str(latest)
res = urllib2.urlopen(url, timeout = 5).read()
confparser = ConfigParser.ConfigParser()
memfile = StringIO.StringIO(res)
confparser.readfp(memfile)
if platform.system() == 'Windows':
link = confparser.get("Windows", "link")
elif platform.system() == 'Linux':
link = confparser.get("Linux", "link")
elif platform.system() == 'Darwin':
link = confparser.get("Mac", "link")
else:
pass
return link
except:
return None
#Generate an HTML table from a list of words
def commands2table(widget, capabilities):
fm = QFontMetrics(widget.font())
maxlenstr = max(capabilities, key=len)
maxlen = fm.width(maxlenstr) + 40
maxncols = widget.width() / maxlen
i = 0
output = "<table><tr>"
for capability in capabilities:
output += "<td width='" + str(maxlen) + "'>" + capability + "</td>"
i += 1
if i == maxncols:
i = 0
output += "</tr><tr>"
output += "</tr></table>"
return output
#Guess autocomplete command(s)
def findAutocompleteCommand(capabilities):
prefix = capabilities[0]
for s in capabilities:
if len(s) < len(prefix):
prefix = prefix[:len(s)]
if not prefix:
break
for i in range(len(prefix)):
if prefix[i] != s[i]:
prefix = prefix[:i]
break
return prefix