Advertisement
SharkyEXE

Untitled

Aug 3rd, 2018
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.86 KB | None | 0 0
  1. from Plugins.Plugin import PluginDescriptor
  2. from Screens.Screen import Screen
  3. from Screens.MessageBox import MessageBox
  4. from Components.Button import Button
  5. from Components.ActionMap import ActionMap
  6. from Components.ConfigList import ConfigListScreen
  7. from Components.config import config, ConfigSubsection, ConfigDateTime, ConfigClock, ConfigText, getConfigListEntry
  8. import enigma
  9. from enigma import eTimer, iServiceInformation
  10. from datetime import datetime, time as dt_time
  11. from os import system
  12. import gettext
  13. import time, httplib
  14.  
  15. config.plugins.SetClock = ConfigSubsection()
  16. config.plugins.SetClock.HostName = ConfigText(default='ya.ru', fixed_size=False)
  17. ver = '1.1'
  18.  
  19. def _(txt):
  20. t = gettext.gettext(txt)
  21. return t
  22.  
  23.  
  24. def Plugins(**kwargs):
  25. return [PluginDescriptor(name='SetClock', description='SetClock plug-in v.' + ver + ' (c)2011 by SatCat', where=PluginDescriptor.WHERE_PLUGINMENU, icon='plugin.png', fnc=main), PluginDescriptor(name='SetClock', description='SetClock plug-in v.' + ver + '(c)2011 by SatCat', where=[PluginDescriptor.WHERE_SESSIONSTART, PluginDescriptor.WHERE_AUTOSTART], fnc=autostart)]
  26.  
  27.  
  28. def main(session, servicereference = None, **kwargs):
  29. try:
  30. session.open(SetClockMain)
  31. except:
  32. print '[SetClock] Pluginexecution failed'
  33.  
  34.  
  35. def autostart(reason, **kwargs):
  36. global session
  37. if reason == 0 and kwargs.has_key('session'):
  38. session = kwargs['session']
  39. session.open(AutoCorrTime)
  40.  
  41.  
  42. def get_time_str(res_time):
  43. replace_months = {
  44. 'Jan': '1',
  45. 'Feb': '2',
  46. 'Mar': '3',
  47. 'Apr': '4',
  48. 'May': '5',
  49. 'June': '6',
  50. 'Jun': '6',
  51. 'July': '7',
  52. 'Jul': '7',
  53. 'Aug': '8',
  54. 'Sept': '9',
  55. 'Sep': '9',
  56. 'Oct': '10',
  57. 'Nov': '11',
  58. 'Dec': '12',
  59. }
  60. gmts = [
  61. 'GMT',
  62. ]
  63. _res_time = res_time
  64. try:
  65. time_tuple = time.strptime(res_time, '%a, %d %b %Y %H:%M:%S %Z')
  66. except ValueError:
  67. for m1, m2 in replace_months.items():
  68. res_time = res_time.replace(m1, m2)
  69. fro gmt in gmts:
  70. res_time = res_time.replace(gmt, '')
  71. res_time = res_time.split(', ')[1].strip()
  72. try:
  73. time_tuple = time.strptime(res_time, '%d %m %Y %H:%M:%S')
  74. except ValueError:
  75. open('/usr/lib/enigma2/python/Plugins/Extensions/SetClock/log.txt', 'a').write(_res_time)
  76. raise
  77. t = time.localtime(time.mktime(time_tuple) - time.timezone)
  78. return time.strftime('%Y%m%d%H%M.%S', t)
  79.  
  80. class AutoCorrTime(Screen):
  81. skin = '''
  82. <screen position="100,100" size="280,300" title="SetClock" >
  83. </screen>'''
  84.  
  85. def __init__(self, session):
  86. Screen.__init__(self, session)
  87. self.session = session
  88. if time.localtime().tm_year == 2000:
  89. self.limit = 0
  90. self.cursorTimer = eTimer()
  91. self.cursorTimer.callback.append(self.timer)
  92. self.cursorTimer.start(75, False)
  93.  
  94. def timer(self):
  95. if time.localtime().tm_year > 2000:
  96. refstr = dtt = 'n/a'
  97. try:
  98. service = self.session.nav.getCurrentService()
  99. if service:
  100. info = service.info()
  101. if info:
  102. refstr = info.getInfoString(iServiceInformation.sServiceref)
  103. except:
  104. refstr = 'ref error'
  105.  
  106. if refstr == '1:0:1:1:65:64:3840000:0:0:0:':
  107. tc = time.time() - 189391980
  108. dtt = datetime.fromtimestamp(tc).strftime('%Y%m%d%H%M')
  109. enigma.eDVBLocalTimeHandler.getInstance().setUseDVBTime(False)
  110. try:
  111. system('/bin/date -s %s' % dtt)
  112. print '[SetClock] Set clock!'
  113. except:
  114. pass
  115.  
  116. self.cursorTimer.stop()
  117. print '[SetClock] Stop timer.'
  118. self.limit = self.limit + 1
  119. if self.limit > 1200:
  120. self.cursorTimer.stop()
  121. print '[SetClock] Stop timer (limit).'
  122.  
  123.  
  124. class SetClockMain(ConfigListScreen, Screen):
  125. skin = '''
  126. <screen position="center,250" size="420,190" title="SetClock Menu" >
  127. <widget name="config" position="10,10" size="400,130" scrollbarMode="showOnDemand" />
  128. <widget name="key_green" position="0,150" size="140,40" valign="center" halign="center" zPosition="4" foregroundColor="white" font="Regular;18" transparent="1"/>
  129. <ePixmap name="green" position="0,150" size="140,40" zPosition="2" pixmap="skin_default/buttons/green.png" transparent="1" alphatest="on" />
  130. <widget name="key_blue" position="140,150" size="140,40" valign="center" halign="center" zPosition="4" foregroundColor="white" font="Regular;18" transparent="1"/>
  131. <ePixmap name="blue" position="140,150" size="140,40" zPosition="2" pixmap="skin_default/buttons/blue.png" transparent="1" alphatest="on" />
  132. <widget name="key_red" position="280,150" size="140,40" valign="center" halign="center" zPosition="4" foregroundColor="white" font="Regular;18" transparent="1"/>
  133. <ePixmap name="red" position="280,150" size="140,40" zPosition="2" pixmap="skin_default/buttons/red.png" transparent="1" alphatest="on" />
  134. </screen>'''
  135.  
  136. def __init__(self, session, args = 0):
  137. self.skin = SetClockMain.skin
  138. self.session = session
  139. Screen.__init__(self, session)
  140. self.list = []
  141. self.date_en = ConfigDateTime(default=time.time(), formatstring='%d.%m.%y')
  142. self.entryDate = getConfigListEntry(_('Date'), self.date_en)
  143. self.time_en = ConfigClock(default=time.time())
  144. self.entryTime = getConfigListEntry(_('Time'), self.time_en)
  145. self.list.append(self.entryDate)
  146. self.list.append(self.entryTime)
  147. self.list.append(getConfigListEntry('HTTP host:', config.plugins.SetClock.HostName))
  148. self['key_green'] = Button(_('SET!'))
  149. self['key_blue'] = Button(_('HTTP SET'))
  150. self['key_red'] = Button(_('Cancel'))
  151. ConfigListScreen.__init__(self, self.list, session=session)
  152. self['setupActions'] = ActionMap(['SetupActions', 'ColorActions'], {'red': self.cancel,
  153. 'green': self.set,
  154. 'blue': self.inet_set,
  155. 'save': self.set,
  156. 'cancel': self.cancel,
  157. 'ok': self.pressed_ok}, -2)
  158. self['config'].list = self.list
  159. self['config'].l.setList(self.list)
  160.  
  161. def pressed_ok(self):
  162. config.plugins.SetClock.HostName.save()
  163. self.set()
  164.  
  165. def set(self, dtt = 0):
  166. d = time.localtime(self.date_en.value)
  167. dtt = '%d%02d%02d%02d%02d' % (d.tm_year,
  168. d.tm_mon,
  169. d.tm_mday,
  170. self.time_en.value[0],
  171. self.time_en.value[1])
  172. enigma.eDVBLocalTimeHandler.getInstance().setUseDVBTime(False)
  173. try:
  174. system('/bin/date -s %s' % dtt)
  175. except:
  176. pass
  177.  
  178. self.close()
  179. print '[SetClock] SET! ** %s' % time.strftime('%X %x (%Y) %Z')
  180.  
  181. def inet_set(self):
  182. config.plugins.SetClock.HostName.save()
  183. conn = httplib.HTTPConnection(config.plugins.SetClock.HostName.value)
  184. try:
  185. conn.request('HEAD', '/favicon.ico')
  186. t_rtt = time.clock()
  187. res_time = conn.getresponse().getheader('date')
  188. except:
  189. self.session.open(MessageBox, 'Error: No server responce: ' + config.plugins.SetClock.HostName.value, MessageBox.TYPE_ERROR, timeout=3)
  190. return
  191.  
  192. enigma.eDVBLocalTimeHandler.getInstance().setUseDVBTime(False)
  193.  
  194. try:
  195. system('/bin/date -s %s' % get_time_str(res_time))
  196. except:
  197. pass
  198.  
  199. self.close()
  200. print '[SetClock] inet SET! ** %s' % time.strftime('%X %x (%Y) %Z')
  201.  
  202. def cancel(self):
  203. self.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement