2016-10-19 00:12:14 +02:00
|
|
|
from contextlib import contextmanager
|
|
|
|
from datetime import datetime
|
|
|
|
import locale
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import shlex
|
|
|
|
import threading
|
|
|
|
from subprocess import Popen, PIPE
|
|
|
|
|
|
|
|
|
|
|
|
LOCALE_LOCK = threading.Lock()
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def setlocale(name):
|
|
|
|
with LOCALE_LOCK:
|
|
|
|
saved = locale.setlocale(locale.LC_ALL)
|
|
|
|
try:
|
|
|
|
yield locale.setlocale(locale.LC_ALL, name)
|
|
|
|
finally:
|
|
|
|
locale.setlocale(locale.LC_ALL, saved)
|
|
|
|
|
|
|
|
|
|
|
|
class RtcDevice():
|
|
|
|
name = ""
|
|
|
|
devStatus = {
|
|
|
|
"active": False,
|
|
|
|
"date": 0
|
|
|
|
}
|
|
|
|
modes = []
|
|
|
|
utc = False
|
|
|
|
|
|
|
|
def __init__(self, name = ""):
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
self.modes()
|
|
|
|
self.status()
|
|
|
|
|
|
|
|
def modes(self):
|
|
|
|
cmd = "rtcwake --list-modes -d " + self.name
|
|
|
|
process = Popen(shlex.split(cmd), stdout=PIPE)
|
|
|
|
(output, err) = process.communicate()
|
|
|
|
exit_code = process.wait()
|
|
|
|
|
2016-10-22 22:13:50 +02:00
|
|
|
output = output.decode()
|
|
|
|
|
|
|
|
self.modes = output.split()
|
2016-10-19 00:12:14 +02:00
|
|
|
|
|
|
|
def status(self):
|
|
|
|
cmd = "rtcwake -m show -d " + self.name
|
|
|
|
process = Popen(shlex.split(cmd), stdout=PIPE)
|
|
|
|
(output, err) = process.communicate()
|
|
|
|
exit_code = process.wait()
|
|
|
|
|
|
|
|
self.formatStatus(str(output))
|
|
|
|
|
|
|
|
def formatStatus(self, message):
|
|
|
|
re1='.*?' # Non-greedy match on filler
|
|
|
|
re2='(?:[a-z][a-z]+)' # Uninteresting: word
|
|
|
|
re3='.*?' # Non-greedy match on filler
|
|
|
|
re4='((?:[a-z][a-z]+))' # Word 1
|
|
|
|
re5='.*?' # Non-greedy match on filler
|
|
|
|
re6='((?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Sept|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?))' # Month 1
|
|
|
|
re7='.*?' # Non-greedy match on filler
|
|
|
|
re8='((?:(?:[0-2]?\\d{1})|(?:[3][01]{1})))(?![\\d])' # Day 1
|
|
|
|
re9='.*?' # Non-greedy match on filler
|
|
|
|
re10='((?:(?:[0-1][0-9])|(?:[2][0-3])|(?:[0-9])):(?:[0-5][0-9])(?::[0-5][0-9])?(?:\\s?(?:am|AM|pm|PM))?)' # HourMinuteSec 1
|
|
|
|
re11='.*?' # Non-greedy match on filler
|
|
|
|
re12='((?:(?:[1]{1}\\d{1}\\d{1}\\d{1})|(?:[2]{1}\\d{3})))(?![\\d])' # Year 1
|
|
|
|
|
|
|
|
rg = re.compile(re1+re2+re3+re4,re.IGNORECASE|re.DOTALL)
|
|
|
|
m = rg.search(message)
|
|
|
|
if m:
|
|
|
|
active = m.group(1)
|
|
|
|
if active == "on":
|
|
|
|
self.devStatus["active"] = True
|
|
|
|
else:
|
|
|
|
self.devStatus["active"] = False
|
|
|
|
|
|
|
|
if self.devStatus["active"]:
|
|
|
|
rgx = re.compile(re1+re2+re3+re4+re5+re6+re7+re8+re9+re10+re11+re12,re.IGNORECASE|re.DOTALL)
|
|
|
|
mx = rgx.search(message)
|
|
|
|
if mx:
|
|
|
|
month = mx.group(2)
|
|
|
|
day = mx.group(3)
|
|
|
|
if len(day) == 1:
|
|
|
|
day = "0" + day
|
|
|
|
time = mx.group(4)
|
|
|
|
year = mx.group(5)
|
|
|
|
with setlocale('C'):
|
|
|
|
self.devStatus["date"] = datetime.strptime(year + " " + month + " " + day + " " + time, "%Y %b %d %H:%M:%S")
|
|
|
|
|
|
|
|
def getDevices():
|
|
|
|
mainDir = "/sys/class/rtc"
|
|
|
|
if os.path.isdir(mainDir):
|
|
|
|
devices = [ name for name in os.listdir(mainDir) if os.path.isdir(os.path.join(mainDir, name)) ]
|
|
|
|
|
|
|
|
if len(devices) > 0:
|
|
|
|
return devices
|
|
|
|
|
|
|
|
return False
|