2
# -*- coding: utf-8 -*-
3
###############################################################################
4
# plugin_common.py is a library of functions common to all plugins
8
from htmlentitydefs import name2codepoint, codepoint2name
13
app_name = "gtk-desktop-info"
14
app_path = os.path.dirname(os.path.abspath(__file__))
15
module_name = __file__.replace(os.path.dirname (__file__) + "/", "").replace(".pyc","").replace(".py", "")
17
def getTypedValue(value, expectedtype):
20
if len(value.strip(" ")) == 0:
23
elif value.lower() == "true":
24
if expectedtype == "boolean":
27
self.logger.error("Expected type was '%s', but the value '%s' was given"%(expectedtype, value))
29
elif value.lower() == "false":
30
if expectedtype == "boolean":
33
self.logger.error("Expected type was '%s', but the value '%s' was given"%(expectedtype, value))
35
elif isNumeric(value) == True:
36
if expectedtype == "integer":
44
except (TypeError, ValueError):
47
def getFormattedDuration(seconds):
49
# hours:minutes:seconds
50
#hours = int(seconds/3600%60)
51
#minutes = int(seconds/60%60)
52
#seconds = int(seconds%60)
55
# output += str(hours).rjust(1,"0")+":"
56
#output += str(minutes).rjust(2,"0")+":"+str(seconds).rjust(2,"0")
59
minutes = int(seconds/60)
60
seconds = int(seconds%60)
62
output = str(minutes).rjust(2,"0")+":"+str(seconds).rjust(2,"0")
64
#output = str(int(seconds/3600%60)).rjust(1,"0")+":"+str(int(seconds/60%60)).rjust(1,"0")+":"+str(int(seconds%60)).rjust(2,"0")
68
def getHoursMinutesStringFromSeconds(seconds):
70
hours, time = divmod(time, 60*60)
71
minutes, seconds = divmod(time, 60)
72
output = str(hours).rjust(2,"0") + ":" + str(minutes).rjust(2,"0")
75
def removeLineByString(lines,string):
77
# define list from multiple lines, to allow remove function
78
lineslist = lines.split("\n")
79
lineslistchanged = False
81
for line in lineslist:
82
if line.find(string) <> -1:
83
lineslist.remove(line)
84
lineslistchanged = True
87
if lineslistchanged == True:
88
# rebuild lines string from updated list
89
lines = "\n".join(lineslist)
93
def getSpaces(spaces):
95
for i in range(0, spaces+1):
99
def getWrappedText(text,width=40,indent=""):
101
for line in textwrap.wrap(text,width=(width-len(indent)),expand_tabs=False,replace_whitespace=False):
102
wrappedtext = wrappedtext + "\n" + indent + line
106
def getHTMLText(text):
114
for char in text: #html:
116
htmlentities.append(char)
118
htmlentities.append('&%s;' % codepoint2name[ord(char)])
119
html = "".join(htmlentities)
121
html = html.replace("\n","<br>\n") # switch out new line for html breaks
126
def getCleanText(html):
129
text = text.replace("\n","") # remove new lines from html
130
text = text.replace("'","'") # workaround for shitty xml codes not compliant with html
131
text = text.replace("<br>","\n") # switch out html breaks for new line
132
text = re.sub('<(.|\n)+?>','',text) # remove any html tags
133
text = re.sub('&(%s);' % '|'.join(name2codepoint), lambda m: chr(name2codepoint[m.group(1)]), text)
138
def isNumeric(string):
140
dummy = float(string)