forked from jmdaweb/NVDARemoteServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.py
More file actions
125 lines (119 loc) · 3.98 KB
/
options.py
File metadata and controls
125 lines (119 loc) · 3.98 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
# -*- coding: utf-8 -*-
import platform
import sys
import codecs
import os
# global variables
configfile = None
interface = ""
interface6 = ""
port = 6837
port6 = 6837
logfile = None
pidfile = ""
loglevel = 2
keyfile = None
certfile = None
motd = ""
motd_force_display = False
includeTracebacks = False
allowedMessageLength = 0
timeout = 5.0
ping_time = 300
def setup():
global configfile, pidfile, logfile, certfile, motd, motd_force_display
# set default arguments
system = platform.system()
if system=='Linux':
pidfile = "/run/NVDARemoteServer/NVDARemoteServer.pid"
elif system == 'Darwin' or system.startswith('MSYS') or system.startswith('CYGWIN'):
pidfile = "/var/run/NVDARemoteServer.pid"
if system == 'Linux' or system == 'Darwin' or system.startswith('MSYS') or system.startswith('CYGWIN'):
logfile = "/var/log/NVDARemoteServer/NVDARemoteServer.log"
configfile = "/etc/NVDARemoteServer.conf"
certfile = "/usr/share/NVDARemoteServer/server.pem"
else:
if hasattr(sys, 'frozen'):
logfile = os.path.join(os.path.abspath(os.path.dirname(sys.executable)), "NVDARemoteServer.log")
configfile = os.path.join(os.path.abspath(os.path.dirname(sys.executable)), "NVDARemoteServer.conf")
certfile = os.path.join(sys.prefix, 'server.pem')
else:
logfile = os.path.join(os.path.abspath(os.path.dirname(__file__)), "NVDARemoteServer.log")
configfile = os.path.join(os.path.abspath(os.path.dirname(__file__)), "NVDARemoteServer.conf")
certfile = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'server.pem')
arguments = parseArguments()
if "configfile" in list(arguments.keys()):
configfile = arguments['configfile']
config = {}
try:
config = readConfig()
except:
print("Can't open the configuration file, using default or commandline values")
for k, v in config.items():
setattr(sys.modules[__name__], k, v)
# the command line arguments are parsed after the configfile. They take priority over the options in the file
for k, v in arguments.items():
setattr(sys.modules[__name__], k, v)
if loglevel > 3:
motd = "Warning! This server is running with the maximum allowed log level. All your activity is being recorded inside a log file. " + motd
motd_force_display = True
validate()
def parseArguments():
options = {}
for arg in sys.argv:
if arg.startswith("--"):
option = arg.split("=")
try:
option[0] = option[0].replace("--", "")
if option[0] in ["port", "port6", "loglevel", "allowedMessageLength", "ping_time"]:
option[1] = int(option[1])
if option[0] == "timeout":
option[1] = float(option[1])
if option[1] < 5.0:
option[1] = 5.0
if option[0] in ['motd_force_display', 'includeTracebacks']:
option[1] = bool(int(option[1]))
if option[0] == 'motd':
if len(option) > 2:
option[1] = '='.join(option[1:])
options[option[0]] = option[1]
except:
pass
return options
def readConfig():
options = {}
f = codecs.open(configfile, "r", "utf-8")
content = f.read()
f.close()
lines = content.split("\n")
for line in lines:
if line.startswith("#") or line == "":
continue
option = line.split("=")
try:
if option[0] in ["port", "port6", "loglevel", "allowedMessageLength", "ping_time"]:
option[1] = int(option[1])
if option[0] == "timeout":
option[1] = float(option[1])
if option[1] < 5.0:
option[1] = 5.0
if option[0] in ['motd_force_display', 'includeTracebacks']:
option[1] = bool(int(option[1]))
if option[0] == 'motd':
if len(option) > 2:
option[1] = '='.join(option[1:])
options[option[0]] = option[1]
except:
pass
return options
def validate():
# This function checks some settings. If a wrong value is found, it is replaced by its default.
global port, port6, timeout, ping_time
if port < 1 or port > 65535:
port = 6837
if port6 < 1 or port > 65535:
port6 = 6837
if ping_time < 30:
ping_time = 300
if timeout < 1.0:
timeout = 5.0