# This file is part of WeWi.
#
# WeWi is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# WeWi 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 for more details.
#
# You should have received a copy of the GNU General Public License
# along with WeWi.  If not, see <http://www.gnu.org/licenses/>.
#
# File: config.py
#
# Read and parse configuration options
#

import json
import sys
from os.path import dirname, join

config_data = None

try:
    # Read the config file
    with open(join(dirname(__file__), 'weather.conf')) as config:
        config_data = json.load(config)
except (OSError, ValueError):
    print("Error reading config file", file=sys.stderr)


def get_real_url(latitude, longitude):
    """ Build url """
    global config_data

    try:
        api_key = config_data['api_key']
        real_url = config_data['url'].format(api_key, latitude, longitude)
    except KeyError:
        print("api_key wrong or missing in config file", file=sys.stderr)
        real_url = None

    return real_url
