Menu

[r251]: / trunk / utils.py  Maximize  Restore  History

Download this file

135 lines (124 with data), 4.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# -*- 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