Showing 3 changed files with 173 additions and 0 deletions
+16
config.json
... ...
@@ -0,0 +1,16 @@
1
+{
2
+  "server": {
3
+    "address": "127.0.0.1",
4
+    "csv": "output.csv",
5
+    "csvMax": 1000,
6
+    "csvTemp": "output.temp",
7
+    "cachedir": "cache",
8
+    "debug": 0,
9
+    "logfile": "output.log",
10
+    "name": "newsProxy",
11
+    "port": 8081,
12
+    "version": "1.0",
13
+    "pageStart": "pageBegin.skel",
14
+    "pageEnd": "pageEnd.skel"
15
+  }
16
+}
+79
configuration.py
... ...
@@ -0,0 +1,79 @@
1
+#
2
+# Copyright (C) Actility, SA. All Rights Reserved.
3
+# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
4
+#
5
+# This program is free software; you can redistribute it and/or
6
+# modify it under the terms of the GNU General Public License version
7
+# 2 only, as published by the Free Software Foundation.
8
+#
9
+# This program is distributed in the hope that it will be useful, but
10
+# WITHOUT ANY WARRANTY; without even the implied warranty of
11
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+# General Public License version 2 for more details (a copy is
13
+# included at /legal/license.txt).
14
+#
15
+# You should have received a copy of the GNU General Public License
16
+# version 2 along with this work; if not, write to the Free Software
17
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18
+# 02110-1301 USA
19
+#
20
+# Please contact Actility, SA.,  4, rue Ampere 22300 LANNION FRANCE
21
+# or visit www.actility.com if you need additional
22
+# information or have any questions.
23
+#
24
+import json
25
+from userio import *
26
+import os
27
+import sys
28
+
29
+server = None
30
+
31
+def get_server():
32
+    global server
33
+    if server == None:
34
+        with open("config.json") as config:
35
+            json_data = json.load(config)
36
+            server = json_data["server"]
37
+    return server
38
+
39
+default_config = {
40
+  "server": {
41
+    "port": 8081,
42
+    "address": "0.0.0.0",
43
+    "logfile": "output.log",
44
+    "csv": "output.csv",
45
+    "cachedir": "cache",
46
+    "csvTemp": "output.temp",
47
+    "csvMax": 10,
48
+    "name": "MyProxy",
49
+    "version": "1.0",
50
+    "debug": 1,
51
+    "pageStart": "pageBegin.skel",
52
+    "pageEnd": "pageEnd.skel"
53
+   }
54
+}
55
+
56
+def get_debug():
57
+    return server['debug']
58
+
59
+def get_logfile():
60
+    return server['logfile']
61
+    
62
+def get_pageBegin():
63
+    return server['pageStart']
64
+    
65
+def get_pageEnd():
66
+    return server['pageEnd']
67
+
68
+# Generate the default config. Will override existing config.
69
+def generate_config():
70
+    warn("Generating default config...")
71
+    with open("config.json", "w") as config:
72
+        json.dump(default_config, config, indent=2, sort_keys=True)
73
+        ok("Generated default config!")
74
+
75
+if not os.path.exists("config.json"):
76
+    warn("No configuration file found!")
77
+    generate_config()
78
+    say("Please edit the configuration file. ThingPark Status Server will now shutdown.")
79
+    sys.exit(1)
+78
userio.py
... ...
@@ -0,0 +1,78 @@
1
+#
2
+# Copyright (C) Actility, SA. All Rights Reserved.
3
+# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
4
+#
5
+# This program is free software; you can redistribute it and/or
6
+# modify it under the terms of the GNU General Public License version
7
+# 2 only, as published by the Free Software Foundation.
8
+#
9
+# This program is distributed in the hope that it will be useful, but
10
+# WITHOUT ANY WARRANTY; without even the implied warranty of
11
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+# General Public License version 2 for more details (a copy is
13
+# included at /legal/license.txt).
14
+#
15
+# You should have received a copy of the GNU General Public License
16
+# version 2 along with this work; if not, write to the Free Software
17
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18
+# 02110-1301 USA
19
+#
20
+# Please contact Actility, SA.,  4, rue Ampere 22300 LANNION FRANCE
21
+# or visit www.actility.com if you need additional
22
+# information or have any questions.
23
+#
24
+from colorama import Fore, Back, Style
25
+from time import gmtime, strftime
26
+import configuration
27
+
28
+name = "newsProxy"
29
+
30
+def say(message):
31
+    date_string = strftime("%Y-%m-%d %H:%M:%S %z", gmtime())
32
+    prefix = Fore.CYAN + name + " " + Fore.RESET + date_string + " "
33
+    with open(configuration.get_logfile(), "a+") as f:
34
+        f.write(prefix + Style.DIM + message + Style.RESET_ALL+"\n")
35
+        f.close()
36
+        
37
+    print(prefix + Style.DIM + message + Style.RESET_ALL)
38
+
39
+
40
+def ok(message, detail=""):
41
+    date_string = strftime("%Y-%m-%d %H:%M:%S %z", gmtime())
42
+    level = Fore.GREEN + "[OK] " + Fore.RESET
43
+    prefix = Fore.CYAN + name + " " + Fore.RESET + date_string + " "
44
+    with open(configuration.get_logfile(), "a+") as f:
45
+        f.write(prefix + level + Style.BRIGHT + message + Style.RESET_ALL + " " + detail + Style.RESET_ALL+"\n")
46
+        f.close()
47
+    print(prefix + level + Style.BRIGHT + message + Style.RESET_ALL + " " + detail + Style.RESET_ALL)
48
+
49
+
50
+def warn(message, detail=""):
51
+    date_string = strftime("%Y-%m-%d %H:%M:%S %z", gmtime())
52
+    prefix = Fore.CYAN + name + " " + Fore.RESET + date_string + " "
53
+    level = Fore.YELLOW + "[WARN] " + Fore.RESET
54
+    with open(configuration.get_logfile(), "a+") as f:
55
+        f.write(prefix  + level +  Style.BRIGHT + message + Style.RESET_ALL + " " + detail + Style.RESET_ALL+"\n")
56
+        f.close()
57
+    print(prefix  + level +  Style.BRIGHT + message + Style.RESET_ALL + " " + detail + Style.RESET_ALL)
58
+
59
+
60
+def error(message, detail=""):
61
+    date_string = strftime("%Y-%m-%d %H:%M:%S %z", gmtime())
62
+    prefix = Fore.CYAN + name + " " + Fore.RESET + date_string + " "
63
+    level = Fore.RED + "[ERR] " + Fore.RESET
64
+    with open(configuration.get_logfile(), "a+") as f:
65
+        f.write(prefix + level +  Style.BRIGHT + message + Style.RESET_ALL + " " + detail + Style.RESET_ALL+"\n")
66
+        f.close()
67
+    print(prefix + level +  Style.BRIGHT + message + Style.RESET_ALL + " " + detail + Style.RESET_ALL)
68
+
69
+
70
+def debug(message, detail=""):
71
+    if configuration.get_debug() != 0:
72
+        date_string = strftime("%Y-%m-%d %H:%M:%S %z", gmtime())
73
+        prefix = Fore.CYAN + name + " " + Fore.RESET + date_string + " "
74
+        level = Fore.MAGENTA + "[DEBUG] " + Fore.RESET
75
+        with open(configuration.get_logfile(), "a+") as f:
76
+            f.write(prefix + level +  Style.DIM + message + Style.RESET_ALL + " " + detail + Style.RESET_ALL+"\n")
77
+            f.close()
78
+        print(prefix + level +  Style.DIM + message + Style.RESET_ALL + " " + detail + Style.RESET_ALL)