Menu

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

Download this file

102 lines (92 with data), 3.0 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
# -*- coding: utf-8 -*-
import os, tempfile, gettext
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import globalvars as g
#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())))
#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