newsProxy / configuration.py /
f7c8dfe 3 years ago
1 contributor
79 lines | 2.286kb
#
# Copyright (C) Actility, SA. All Rights Reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version
# 2 only, as published by the Free Software Foundation.
#
# This program 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 version 2 for more details (a copy is
# included at /legal/license.txt).
#
# You should have received a copy of the GNU General Public License
# version 2 along with this work; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA
#
# Please contact Actility, SA.,  4, rue Ampere 22300 LANNION FRANCE
# or visit www.actility.com if you need additional
# information or have any questions.
#
import json
from userio import *
import os
import sys

server = None

def get_server():
    global server
    if server == None:
        with open("config.json") as config:
            json_data = json.load(config)
            server = json_data["server"]
    return server

default_config = {
  "server": {
    "port": 8081,
    "address": "0.0.0.0",
    "logfile": "output.log",
    "csv": "output.csv",
    "cachedir": "cache",
    "csvTemp": "output.temp",
    "csvMax": 10,
    "name": "MyProxy",
    "version": "1.0",
    "debug": 1,
    "pageStart": "pageBegin.skel",
    "pageEnd": "pageEnd.skel"
   }
}

def get_debug():
    return server['debug']

def get_logfile():
    return server['logfile']
    
def get_pageBegin():
    return server['pageStart']
    
def get_pageEnd():
    return server['pageEnd']

# Generate the default config. Will override existing config.
def generate_config():
    warn("Generating default config...")
    with open("config.json", "w") as config:
        json.dump(default_config, config, indent=2, sort_keys=True)
        ok("Generated default config!")

if not os.path.exists("config.json"):
    warn("No configuration file found!")
    generate_config()
    say("Please edit the configuration file. ThingPark Status Server will now shutdown.")
    sys.exit(1)