-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
161 lines (137 loc) · 5.27 KB
/
common.py
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# -*- coding: utf-8 -*-
#########################################################
# SCRIPT : common.py #
# Common functions for PVR-Manager #
# I. Helwegen 2015 #
#########################################################
####################### IMPORTS #########################
import os, subprocess
import xbmc, xbmcaddon, xbmcgui
import xbmcvfs
import time, datetime
#########################################################
####################### GLOBALS #########################
# shutdown methods
CMD_NONE = 0
CMD_SHUTDOWN = 1
CMD_SUSPEND = 2
CMD_HIBERNATE = 3
CMD_SENDMAIL = 4
__addon__ = xbmcaddon.Addon()
__addonname__ = __addon__.getAddonInfo('id')
__path__ = __addon__.getAddonInfo('path')
__datapath__ = xbmcvfs.translatePath(os.path.join('special://temp/', __addonname__))
__logfile__ = os.path.join(__datapath__, __addonname__ + '.log')
__shutdownaction__ = __addon__.getSetting('shutdown_action')
IconStop = xbmcvfs.translatePath(os.path.join(__path__, 'resources', 'media', 'stop.png'))
IconError = xbmcvfs.translatePath(os.path.join(__path__, 'resources', 'media', 'error.png'))
IconSchedule = xbmcvfs.translatePath(os.path.join(__path__, 'resources', 'media', 'schedule.png'))
# parameters
PID = 'pid'
CMD = 'cmd'
MSG = 'message'
MSGCNT = 'mescount'
OSD = xbmcgui.Dialog()
__parameterwindow__ = 10000
#########################################################
#########################################################
# Functions : Local #
#########################################################
def num(s):
try:
return int(s)
except ValueError:
return 0
def setParam(param, value):
xbmcgui.Window(__parameterwindow__).setProperty(__addonname__ + '_' + param, value)
def getParam(param):
return xbmcgui.Window(__parameterwindow__).getProperty(__addonname__ + '_' + param)
def clearParam(param):
xbmcgui.Window(__parameterwindow__).clearProperty(__addonname__ + '_' + param)
def incParam(param):
val = num(getParam(param))
val += 1
setParam(param,str(val))
#########################################################
#########################################################
# Functions : Global #
#########################################################
def getProcessPID(process):
_syscmd = subprocess.Popen(['pidof','-x', process], stdout=subprocess.PIPE)
try:
PID = int(_syscmd.stdout.read().strip())
except:
PID = 0
return PID if PID > 0 else False
# check for PID, if no PID available, user or system has
# powered on the system at first time (_isPID = false)
def isPID(setPIDparam=True):
_isPID = False
pidofXBMC = str(getProcessPID('kodi.bin'))
# KODI
if not pidofXBMC: pidofXBMC = str(getProcessPID('xbmc.bin'))
if num(getParam(PID)) == 0:
if setPIDparam:
setParam(PID,pidofXBMC)
else:
pidofVar = getParam(PID)
if pidofVar == pidofXBMC:
_isPID = True
elif setPIDparam:
setParam(PID,pidofXBMC)
return _isPID
def delPID():
clearParam(PID)
def getCommand():
return num(getParam(CMD))
def setCommand(methd):
if methd == CMD_NONE:
clearParam(CMD)
else:
setParam(CMD,str(methd))
def notifyOSD(header, message, icon=xbmcgui.NOTIFICATION_INFO):
OSD.notification(header.encode('utf-8'), message.encode('utf-8'), icon)
def dialogOK(header, message):
OSD.ok(header.encode('utf-8'), message.encode('utf-8'))
# Change this one later to include ok button
def dialogProgress(title, message, duration):
__bar = 0
__percent = 0
pb = xbmcgui.DialogProgress()
pb.create(title, message % (duration))
pb.update(__percent)
# actualize progressbar
while __bar < duration and not pb.iscanceled():
__bar += 1
__percent = int((__bar * 100)/duration)
pb.update(__percent, message % (duration - __bar))
xbmc.sleep(1000)
pb.close()
return pb.iscanceled()
def writeLog(message, level=xbmc.LOGINFO, forcePrint=False):
if getParam(MSG) == message and not forcePrint:
incParam(MSGCNT)
return
else:
try:
if not os.path.exists(__datapath__): os.makedirs(__datapath__)
if not os.path.isfile(__logfile__):
__f = open(__logfile__, 'w')
else:
__f = open(__logfile__, 'a')
if num(getParam(MSGCNT)) > 0:
__f.write('%s: >>> Last message repeated %s time(s)\n' % (
datetime.datetime.now().strftime('%d.%m.%Y %H:%M:%S'), num(getParam(MSGCNT))))
setParam(MSG, message)
clearParam(MSGCNT)
__f.write('%s: %s\n' % (datetime.datetime.now().strftime('%d.%m.%Y %H:%M:%S'), message.encode('utf-8')))
__f.close()
except Exception as e:
xbmc.log('%s: %s' % (__addonname__, e), xbmc.LOGERROR)
xbmc.log('%s: %s' % (__addonname__, message), level)
def getShutdownAction():
methd = CMD_SHUTDOWN # default shutdown
if __shutdownaction__ == "Suspend": methd = CMD_SUSPEND
if __shutdownaction__ == "Hibernate": methd = CMD_HIBERNATE
return methd
#########################################################