-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsunbrctl.py
More file actions
executable file
·206 lines (185 loc) · 7.3 KB
/
Copy pathsunbrctl.py
File metadata and controls
executable file
·206 lines (185 loc) · 7.3 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
#! /usr/bin/python
# Main file of the app 'sunbrctl'
# The application is for controlling display brightness
# depending on current time and sunrise/sunset.
# author: jogme
import argparse
from time import sleep
from multiprocessing import Process
from multiprocessing.managers import BaseManager
from yaml import load
try:
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader
from sys import stderr
from os import path
from sunbrctl_src.config import config
from sunbrctl_src import dbus_con
from sunbrctl_src.hw_brightness_control import HwBrightnessControl
from sunbrctl_src.debug import debug
def updater(hw):
while True:
sleep(config['updater']['sleep_time_s'])
hw.set_br()
def parse_arguments():
parser = argparse.ArgumentParser(
prog='BrightnessControl',
description='automatic monitor brightness control')
parser.add_argument('-v', action='store_true', default=False, help='Enable debug logs')
parser.add_argument('-e', '--external', action='store_true', default=False, help='Enable external monitor')
parser.add_argument('-c', '--change', type=int, help='Change brightness. If used with external option, \
change the external monitors brightness. The value should be given as a percentage \
(-100 - 100). Either decreases or increases depending in the value.')
parser.add_argument('-s', '--set', type=int, help='Set specific brightness. If used with external \
option, set the external monitors brightness. The value should be given as a \
percentage (0 - 100).')
parser.add_argument('--config', type=str, default=None, help='Path to config file to be used. \
Load only this config.')
args = parser.parse_args()
config['v'] = args.v
config['external'] = args.external
if args.change:
if args.change < -100 or args.change > 100:
print('The change value is out of bound.', file=stderr)
exit(-1)
try:
dbus_con.send_change(args.change, args.external)
except:
exit(-1)
return 0
elif args.set:
if args.set < 0 or args.set > 100:
print('The set value is out of bound.', file=stderr)
exit(-1)
try:
dbus_con.send_set(args.set, args.external)
except:
exit(-1)
return 0
return args
def _check_config():
c = config
#mandatory
if not 'position' in c:
debug('check_config: no \'position\' parameter given')
return -1
if not 'lat' in c['position'] or \
not 'lng' in c['position']:
debug('check_config: no \'lat\' or \'lng\' parameter given in \'position\'')
return -1
if not 'internal_monitor' in c and \
not 'external_monitor' in c and \
not 'hooks' in c:
debug('check_config: no internal or external monitor given or hooks.')
return -2
#optional
if not 'utc' in c['position']:
config['position']['utc'] = 0
debug('check_config: no \'utc\' parameter given, setting default to 0')
if not 'updater' in c or \
not 'sleep_time_s' in c['updater']:
# default to 5mins
debug('check_config: no \'sleep_time_s\' parameter given, setting default to 300s')
config['updater'] = {'sleep_time_s':300}
if 'hooks' in c:
h = c['hooks']
if not 'morning_on_startup' in h:
debug('check_config: no \'morning_on_startup\' parameter given in hooks, '
'setting default to false')
h['morning_on_startup'] = False
if not 'evening_time' in h:
if 'evening_scripts_static' in h or \
'evening_scripts_dynamic' in h:
debug('check_config: no \'evening_time\' parameter given in hooks, '
'setting default to astronomical')
h['evening_time'] = 'astronomical'
else:
h['evening_time'] = None
if not 'morning_time' in h:
if 'morning_scripts_static' in h or \
'morning_scripts_dynamic' in h:
debug('check_config: no \'morning_time\' parameter given in hooks, '
'setting default to astronomical')
h['morning_time'] = 'astronomical'
else:
h['morning_time'] = None
if not 'evening_scripts_static' in h and \
not 'evening_scripts_dynamic' in h and \
not 'morning_scripts_static' in h and \
not 'morning_scripts_dynamic' in h:
debug('check_config: no scripts given in hooks, turning hooks off.')
del c['hooks']
if 'internal_monitor' in c:
i = c['internal_monitor']
if not 'update_threshold' in i:
debug('check_config: no \'update_threshold\' parameter given in internal monitor, '
'setting default to 5')
i['update_threshold'] = 5
if not 'min_br' in i:
debug('check_config: no \'min_br\' parameter given in internal monitor, '
'setting default to 2')
i['min_br'] = 2
if not 'max_br' in i:
debug('check_config: no \'max_br\' parameter given in internal monitor, '
'setting default to 100')
i['max_br'] = 100
if 'external_monitor' in c:
e = c['external_monitor']
if not 'update_threshold' in e:
debug('check_config: no \'update_threshold\' parameter given in external monitor, '
'setting default to 5')
e['update_threshold'] = 5
if not 'min_br' in e:
debug('check_config: no \'min_br\' parameter given in external monitor, '
'setting default to 2')
e['min_br'] = 2
if not 'max_br' in e:
debug('check_config: no \'max_br\' parameter given in external monitor, '
'setting default to 100')
e['max_br'] = 100
return 0
def load_config(c_file=None):
if c_file:
config_files = [c_file]
else:
# last the most important
config_files = ['/etc/sunbrctl/config',
'~/.config/sunbrctl/config']
for x in config_files:
x = path.expanduser(x)
try:
with open(x, 'r') as f:
config.update(load(f, Loader))
except FileNotFoundError:
debug("Could not find config file at '{}'".format(x))
continue
return _check_config()
if __name__ == "__main__":
args = parse_arguments()
if args == 0:
exit(0)
r = load_config(args.config)
if r == -1:
print('The config is wrong. Check debug messages', file=stderr)
exit(-1)
elif r == -2:
print('Nothing to do. Exiting.')
exit(0)
# shared memory
BaseManager.register('HwBrightnessControl', HwBrightnessControl)
manager = BaseManager()
manager.start()
hw = manager.HwBrightnessControl()
# give time to boot
sleep(30)
p = Process(target=updater, args=[hw])
p.start()
# at app exit terminate the child processes
try:
# publish server and run the main loop
dbus_con.publish_dbus(hw)
finally:
hw.cleanup()
p.terminate()
manager.shutdown()