forked from Screenly/Anthias
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.py
216 lines (172 loc) · 6.63 KB
/
settings.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import hashlib
import json
import logging
import os
import zmq
import ConfigParser
from os import path, getenv
from time import sleep
from UserDict import IterableUserDict
from lib.auth import WoTTAuth, BasicAuth, NoAuth
from lib.errors import ZmqCollectorTimeout
CONFIG_DIR = '.screenly/'
CONFIG_FILE = 'screenly.conf'
DEFAULTS = {
'main': {
'analytics_opt_out': False,
'assetdir': 'screenly_assets',
'database': CONFIG_DIR + 'screenly.db',
'date_format': 'mm/dd/yyyy',
'use_24_hour_clock': False,
'use_ssl': False,
'auth_backend': '',
'websocket_port': '9999'
},
'viewer': {
'audio_output': 'hdmi',
'debug_logging': False,
'default_duration': '10',
'default_streaming_duration': '300',
'player_name': '',
'resolution': '1920x1080',
'show_splash': True,
'shuffle_playlist': False,
'verify_ssl': True,
'usb_assets_key': '',
'default_assets': False
}
}
CONFIGURABLE_SETTINGS = DEFAULTS['viewer'].copy()
CONFIGURABLE_SETTINGS['use_24_hour_clock'] = DEFAULTS['main']['use_24_hour_clock']
CONFIGURABLE_SETTINGS['date_format'] = DEFAULTS['main']['date_format']
PORT = int(getenv('PORT', 8080))
LISTEN = getenv('LISTEN', '127.0.0.1')
# Initiate logging
logging.basicConfig(level=logging.INFO,
format='%(message)s',
datefmt='%a, %d %b %Y %H:%M:%S')
# Silence urllib info messages ('Starting new HTTP connection')
# that are triggered by the remote url availability check in view_web
requests_log = logging.getLogger("requests")
requests_log.setLevel(logging.WARNING)
logging.debug('Starting viewer.py')
class ScreenlySettings(IterableUserDict):
"""Screenly OSE's Settings."""
def __init__(self, *args, **kwargs):
IterableUserDict.__init__(self, *args, **kwargs)
self.home = getenv('HOME')
self.conf_file = self.get_configfile()
self.auth_backends_list = [NoAuth(), BasicAuth(self)]
if os.path.isdir('/opt/wott'):
self.auth_backends_list.append(WoTTAuth(self))
self.auth_backends = {}
for backend in self.auth_backends_list:
DEFAULTS.update(backend.config)
self.auth_backends[backend.name] = backend
if not path.isfile(self.conf_file):
logging.error('Config-file %s missing. Using defaults.', self.conf_file)
self.use_defaults()
self.save()
else:
self.load()
def _get(self, config, section, field, default):
try:
if isinstance(default, bool):
self[field] = config.getboolean(section, field)
elif isinstance(default, int):
self[field] = config.getint(section, field)
else:
self[field] = config.get(section, field)
if field == 'password' and self[field] != '' and len(self[field]) != 64: # likely not a hashed password.
self[field] = hashlib.sha256(self[field]).hexdigest() # hash the original password.
except ConfigParser.Error as e:
logging.debug("Could not parse setting '%s.%s': %s. Using default value: '%s'." % (section, field, unicode(e), default))
self[field] = default
if field in ['database', 'assetdir']:
self[field] = str(path.join(self.home, self[field]))
def _set(self, config, section, field, default):
if isinstance(default, bool):
config.set(section, field, self.get(field, default) and 'on' or 'off')
else:
config.set(section, field, unicode(self.get(field, default)))
def load(self):
"""Loads the latest settings from screenly.conf into memory."""
logging.debug('Reading config-file...')
config = ConfigParser.ConfigParser()
config.read(self.conf_file)
for section, defaults in DEFAULTS.items():
for field, default in defaults.items():
self._get(config, section, field, default)
def use_defaults(self):
for defaults in DEFAULTS.items():
for field, default in defaults[1].items():
self[field] = default
def save(self):
# Write new settings to disk.
config = ConfigParser.ConfigParser()
for section, defaults in DEFAULTS.items():
config.add_section(section)
for field, default in defaults.items():
self._set(config, section, field, default)
with open(self.conf_file, "w") as f:
config.write(f)
self.load()
def get_configdir(self):
return path.join(self.home, CONFIG_DIR)
def get_configfile(self):
return path.join(self.home, CONFIG_DIR, CONFIG_FILE)
@property
def auth(self):
backend_name = self['auth_backend']
if backend_name in self.auth_backends:
return self.auth_backends[self['auth_backend']]
settings = ScreenlySettings()
class ZmqPublisher:
INSTANCE = None
def __init__(self):
if self.INSTANCE is not None:
raise ValueError("An instance already exists!")
self.context = zmq.Context()
self.socket = self.context.socket(zmq.PUB)
self.socket.bind('tcp://0.0.0.0:10001')
sleep(1)
@classmethod
def get_instance(cls):
if cls.INSTANCE is None:
cls.INSTANCE = ZmqPublisher()
return cls.INSTANCE
def send_to_ws_server(self, msg):
self.socket.send("ws_server {}".format(msg))
def send_to_viewer(self, msg):
self.socket.send_string("viewer {}".format(msg))
class ZmqConsumer:
def __init__(self):
self.context = zmq.Context()
self.socket = self.context.socket(zmq.PUSH)
self.socket.setsockopt(zmq.LINGER, 0)
self.socket.connect('tcp://srly-ose-server:5558')
sleep(1)
def send(self, msg):
self.socket.send_json(msg, flags=zmq.NOBLOCK)
class ZmqCollector:
INSTANCE = None
def __init__(self):
if self.INSTANCE is not None:
raise ValueError("An instance already exists!")
self.context = zmq.Context()
self.socket = self.context.socket(zmq.PULL)
self.socket.bind('tcp://0.0.0.0:5558')
self.poller = zmq.Poller()
self.poller.register(self.socket, zmq.POLLIN)
sleep(1)
@classmethod
def get_instance(cls):
if cls.INSTANCE is None:
cls.INSTANCE = ZmqCollector()
return cls.INSTANCE
def recv_json(self, timeout):
if self.poller.poll(timeout):
return json.loads(self.socket.recv(zmq.NOBLOCK))
raise ZmqCollectorTimeout