[go: up one dir, main page]

Menu

[b8934f]: / bin / calise.py  Maximize  Restore  History

Download this file

248 lines (224 with data), 8.0 kB

  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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
#
# Calise - CameraLightSensorProject
# calculates ambient brightness and suggests (or sets)
# screen's correct backlight level using a camera.
#
# Copyright (C) 2011-2012 Nicolo' Barbon
#
# Calise is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# Calise is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Calise. If not, see <http://www.gnu.org/licenses/>.
#
import gettext
import locale
import os
import sys
import logging
import signal
import time
from subprocess import Popen, PIPE # NOTE: probably temporary
from calise.infos import __LowerName__
from calise import optionsd as options
from calise.shared import *
try:
locale.setlocale(locale.LC_ALL, '')
gettext.bindtextdomain( __LowerName__)
gettext.textdomain(__LowerName__)
gettext.install(__LowerName__, unicode=False)
except locale.Error, err:
sys.stderr.write(err)
# manage clean exit/pause
def clear(sig=None, func=None):
if sig == signal.SIGTERM or sig == signal.SIGINT:
if trd is not None:
trd.func.sig = 'quit'
for x in range(20):
if not trd.isAlive(): break
time.sleep(0.1)
if x == 19:
os.kill(os.getpid(), signal.SIGKILL)
sys.exit(sig)
elif sig == signal.SIGTSTP:
if trd is not None:
trd.func.sig = 'pause'
elif sig == signal.SIGCONT:
if trd is not None:
trd.func.sig = 'resume'
def printBriefInfos():
print((
_("Using \"%s\" profile") + "\n" +
_("Camera") + ": %s\n" +
_("Sysfs backlight path") + ": %s\n" +
_("Backlight steps") + ": %s\n" +
_("Delta and offset") + ": %.3f, %.1f\n" +
_("Time gap") + ": %.2f (%.2f/s)\n" +
_("Number of values to average") + ": %d (%d sec)") % (
options.settings['profile'],
options.settings['cam'],
options.settings['path']\
[:-len('/' + os.path.basename(options.settings['path']))],
' -> '.join([
str(options.settings['bkofs']),
str(options.settings['bkofs'] + options.settings['steps'] - 1)]),
options.settings['delta'], options.settings['offset'],
options.settings['gap'], 1.0 / options.settings['gap'],
options.settings['avg'],
options.settings['avg'] * options.settings['gap'],)
)
def cliInterface():
''' Interactive cli interface
Interactive interface commands:
q) exit
p) pause/resume
a) start/stop collecting data
e) export collected data
'''
from calise.getkey import _Getch
getch = _Getch()
while trd.isAlive():
try:
ch = getch()
except IOError as err:
if err.errno == 4:
ch = '0'
if ch in 'qQ':
trd.func.sig = 'quit'
for x in range(20):
if not trd.isAlive():
break
time.sleep(0.1)
if x == 19:
os.kill(os.getpid(), signal.SIGKILL)
break
if ch in 'eE':
trd.func.sig = 'export'
if ch in ' pP':
if trd.func.sig is not 'pause':
trd.func.sig = 'pause'
else:
trd.func.sig = 'resume'
if ch in 'aA':
current = options.settings['record']
options.settings['record'] = not current
trd.func.arguments['record'] = not current
print ''
def isServiceAlive():
''' temporary fix until proper implementation
Checks for a running service instance and if so, stops it (at the end of
execution, service will be started again, take a look at the end of this
page)
NOTE: This implementation is extremely experimental and temporary, real
implementation needs return codes from dbus objects (can be done
with tuple)
'''
service_wasAlive = False
pcs = Popen(['calised', '--check'], stdout=PIPE, stderr=PIPE)
dmp = pcs.communicate()
# service alive and running
if ''.join(dmp[0].split('\n')) == "service is alive and running":
return True
# service alive and paused
elif ''.join(dmp[0].split('\n')) == "service is alive but paused":
return False
# service is not alive
elif ''.join(dmp[0].split('\n')) == "service is not alive":
return False
# warning: unable to check service execution
elif ''.join(dmp[0].split('\n')).startswith("warning: "):
return None
def pauseService(action='pause'):
pcs = Popen(['calised', '--%s' % action], stdout=PIPE, stderr=PIPE)
dmp = pcs.communicate()
if ''.join(dmp[0].split('\n')).startswith("warning: "):
logger.warning("Unable to %s a running calised execution" % action)
return False
else:
return True
def main():
parseArguments(sys.argv[1:])
setNiceness(10)
if (
options.settings.keys().count('configure') and
options.settings['configure'] is True
):
from calise.calibration.calibration import CliCalibration
if options.settings.keys().count('path'):
confpath = options.settings['path']
else:
confpath = None
CliCalibration(confpath)
return 0
keepOnly('profile')
logLevel, logFile = tempLoggerInit(sys.argv[0])
tempdir = os.path.dirname(logFile)
#setSetting('logfile', logFile) # NOTE: service-only
lg = options.wlogger(logLevel, logFile)
global logger
logger = logging.getLogger('.'.join([__LowerName__, 'root']))
logger.info("Searching valid profiles within search paths")
loadProfile()
parseArguments(sys.argv[1:])
finalLoggerInit(lg, logLevel, logFile)
checkCalibrationNeed()
options.settings = getSettings()
sa = isServiceAlive()
if sa is True:
sa = pauseService('pause')
# remove temporary directories
# NOTE: interactive-only
try:
os.remove(logFile)
except OSError as err:
if err.errno != 2:
raise
os.rmdir(tempdir)
defName = None
# If necessary settings cannot be found (bad or non existing profiles
# and no cli integration), log critical error and exit.
if checkMissingSettings(options.settings.keys()):
logger.critical(
"Missing needed settings. "
"Either provide them manually or run with \"--configure\".")
return 11
# checked till there...OK
# NOTE: interactive-only from now on
if os.getenv('DISPLAY') is not None and options.settings['gui'] is True:
try:
import calise.QtGui
rc = calise.QtGui.gui(options.settings)
if sa is True:
sa = pauseService('resume')
return rc
except ImportError:
logger.warning(
'Not able to load Pyqt4 module, using cli-interface')
options.settings['gui'] = False
printBriefInfos()
from calise.ExecThreads import ExecThread
global trd
trd = ExecThread(options.settings)
trd.start()
cliInterface()
if sa is True:
sa = pauseService('resume')
return 0
if __name__ == '__main__':
trd = None
logger = None
signal.signal(signal.SIGCONT, clear)
signal.signal(signal.SIGTERM, clear)
signal.signal(signal.SIGINT, clear)
signal.signal(signal.SIGTSTP, clear)
r = main()
sys.exit(r)