~m-buck/+junk/gtk-desktop-info

« back to all changes in this revision

Viewing changes to plugin_common.py

  • Committer: Mark Buck (Kaivalagi)
  • Date: 2009-06-19 17:13:00 UTC
  • Revision ID: m_buck@hotmail.com-20090619171300-5cbhr90xwg62z27y
Added --backgroundblend and --backgroundcolour options for visual seperation of output from wallpaper if required, Fixed song length output in the rhythmbox plugin when songs are an hour long or more, Added copy option to right click, enabling the copying of html content to the clipboard for testing, Moved common functions into a plugin_common module

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: utf-8 -*-
 
3
###############################################################################
 
4
# plugin_common.py is a library of functions common to all plugins
 
5
#
 
6
#  Author: Kaivalagi
 
7
# Created: 14/06/2009
 
8
from htmlentitydefs import name2codepoint, codepoint2name
 
9
import os
 
10
import re
 
11
import textwrap
 
12
 
 
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", "")
 
16
 
 
17
def getTypedValue(value, expectedtype):
 
18
 
 
19
    try:
 
20
        if len(value.strip(" ")) == 0:
 
21
            return None
 
22
 
 
23
        elif value.lower() == "true":
 
24
            if expectedtype == "boolean":
 
25
                return True
 
26
            else:
 
27
                self.logger.error("Expected type was '%s', but the value '%s' was given"%(expectedtype, value))
 
28
 
 
29
        elif value.lower() == "false":
 
30
            if expectedtype == "boolean":
 
31
                return False
 
32
            else:
 
33
                self.logger.error("Expected type was '%s', but the value '%s' was given"%(expectedtype, value))
 
34
 
 
35
        elif isNumeric(value) == True:
 
36
            if expectedtype == "integer":
 
37
                return int(value)
 
38
            else:
 
39
                raise
 
40
 
 
41
        else:
 
42
            return value
 
43
 
 
44
    except (TypeError, ValueError):
 
45
        raise
 
46
 
 
47
def getFormattedDuration(seconds):
 
48
 
 
49
    # hours:minutes:seconds
 
50
    #hours = int(seconds/3600%60)
 
51
    #minutes = int(seconds/60%60)
 
52
    #seconds = int(seconds%60)
 
53
 
 
54
    #if hours > 0:
 
55
    #    output += str(hours).rjust(1,"0")+":"
 
56
    #output += str(minutes).rjust(2,"0")+":"+str(seconds).rjust(2,"0")
 
57
 
 
58
    # minutes:seconds
 
59
    minutes = int(seconds/60)
 
60
    seconds = int(seconds%60)
 
61
 
 
62
    output = str(minutes).rjust(2,"0")+":"+str(seconds).rjust(2,"0")
 
63
 
 
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")
 
65
 
 
66
    return output
 
67
 
 
68
def getHoursMinutesStringFromSeconds(seconds):
 
69
    time = int(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")
 
73
    return output
 
74
 
 
75
def removeLineByString(lines,string):
 
76
 
 
77
    # define list from multiple lines, to allow remove function
 
78
    lineslist = lines.split("\n")
 
79
    lineslistchanged = False
 
80
 
 
81
    for line in lineslist:
 
82
        if line.find(string) <> -1:
 
83
            lineslist.remove(line)
 
84
            lineslistchanged = True
 
85
            break
 
86
 
 
87
    if lineslistchanged == True:
 
88
        # rebuild lines string from updated list
 
89
        lines = "\n".join(lineslist)
 
90
 
 
91
    return lines
 
92
 
 
93
def getSpaces(spaces):
 
94
    string = ""
 
95
    for i in range(0, spaces+1):
 
96
        string = string + " "
 
97
    return string
 
98
 
 
99
def getWrappedText(text,width=40,indent=""):
 
100
    wrappedtext=""
 
101
    for line in textwrap.wrap(text,width=(width-len(indent)),expand_tabs=False,replace_whitespace=False):
 
102
        wrappedtext = wrappedtext + "\n" + indent + line
 
103
    return wrappedtext
 
104
 
 
105
 
 
106
def getHTMLText(text):
 
107
    try:
 
108
        text = u""+text
 
109
    except:
 
110
        pass
 
111
 
 
112
    try:
 
113
        htmlentities = []
 
114
        for char in text: #html:
 
115
            if ord(char) < 128:
 
116
                htmlentities.append(char)
 
117
            else:
 
118
                htmlentities.append('&%s;' % codepoint2name[ord(char)])
 
119
        html = "".join(htmlentities)
 
120
 
 
121
        html = html.replace("\n","<br>\n") # switch out new line for html breaks
 
122
        return html
 
123
    except:
 
124
        return text
 
125
 
 
126
def getCleanText(html):
 
127
    try:
 
128
        text = str(html)
 
129
        text = text.replace("\n","") # remove new lines from html
 
130
        text = text.replace("&apos;","'") # 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)
 
134
        return text
 
135
    except:
 
136
        return html
 
137
 
 
138
def isNumeric(string):
 
139
    try:
 
140
        dummy = float(string)
 
141
        return True
 
142
    except:
 
143
        return False