summaryrefslogtreecommitdiff
path: root/nephilim/misc.py
diff options
context:
space:
mode:
Diffstat (limited to 'nephilim/misc.py')
-rw-r--r--nephilim/misc.py85
1 files changed, 85 insertions, 0 deletions
diff --git a/nephilim/misc.py b/nephilim/misc.py
new file mode 100644
index 0000000..b07360d
--- /dev/null
+++ b/nephilim/misc.py
@@ -0,0 +1,85 @@
+from PyQt4 import QtCore, QtGui
+from htmlentitydefs import name2codepoint as n2cp
+import re
+import urllib2, cookielib
+import socket
+import unicodedata
+
+import logging
+
+socket.setdefaulttimeout(8)
+
+appIcon = 'gfx/icon.png'
+APPNAME = 'nephilim'
+ORGNAME = 'nephilim'
+
+def doEvents():
+ """Make some time for necessary events."""
+ QtCore.QEventLoop().processEvents(QtCore.QEventLoop.AllEvents)
+
+def sec2min(secs):
+ """Converts seconds to min:sec."""
+ min=int(secs/60)
+ sec=secs%60
+ if sec<10:sec='0'+str(sec)
+ return str(min)+':'+str(sec)
+
+def numeric_compare(x, y):
+ if x>y:
+ return 1
+ elif x==y:
+ return 0
+ return -1
+def unique(seq):
+ """Retrieve list of unique elements."""
+ seen = []
+ return t(c for c in seq if not (c in seen or seen.append(c)))
+
+def toAscii(ustr):
+ if type(ustr)==str:
+ return ustr
+ return unicodedata.normalize('NFKD', ustr).encode('ascii', 'ignore')
+
+def substEntity(match):
+ ent = match.group(2)
+ if match.group(1) == "#":
+ return unichr(int(ent))
+ else:
+ cp = n2cp.get(ent)
+
+ if cp:
+ return unichr(cp)
+ else:
+ return match.group()
+
+def decodeHTMLEntities(string):
+ # replace entities with their UTF-counterpart
+ entity_re = re.compile("&(#?)(\d{1,5}|\w{1,8});")
+ return entity_re.subn(substEntity, string)[0]
+
+
+class Button(QtGui.QPushButton):
+ iconSize=32
+ """A simple Button class which calls $onClick when clicked."""
+ def __init__(self, caption, onClick=None, iconPath=None, iconOnly=False, parent=None):
+ QtGui.QPushButton.__init__(self, parent)
+
+ if onClick:
+ self.connect(self, QtCore.SIGNAL('clicked(bool)'), onClick)
+ if iconPath:
+ self.changeIcon(iconPath)
+
+ if not(iconPath and iconOnly):
+ QtGui.QPushButton.setText(self, caption)
+
+ self.setToolTip(caption)
+
+ def setText(self, caption):
+ self.setToolTip(caption)
+ if self.icon()==None:
+ self.setText(caption)
+
+ def changeIcon(self, iconPath):
+ icon=QtGui.QIcon()
+ icon.addFile(iconPath, QtCore.QSize(self.iconSize, self.iconSize))
+ self.setIcon(icon)