########################################################################
#
#  Copyright (C) 2009, CZ.NIC, z.s.p.o.
# 
#  This file is part of dnstester.
#
#  dnstester 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.
#
#  dnstester 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 this program.  If not, see <http://www.gnu.org/licenses/>.
#
########################################################################

import os
import threading
import thread
import webbrowser
import config

def readall(f):
    r = ''
    line = f.readline()
    while line != '':
        r += line
        line = f.readline()

    return r

def dbg_print_struct(s, indent = ''):
    
    if type(s) == list:
        print indent + '['
        for x in s: dbg_print_struct(x, indent + '  ')
        print indent + ']'
    elif type(s) == dict:
        print indent + '{'
        for k, v in s.iteritems():
            print indent + repr(k) + ':'
            dbg_print_struct(v, indent + '    ')
        print indent + '}'
    else:
        print indent + repr(s)

def which(file, paths):
    for p in paths:
        full = os.path.join(p, file)
        if os.path.isfile(full):
            return full
    return None


def openurl_async(url):
        webbrowser.open(url)
    

def openurl(url):
        thread = threading.Thread(target=openurl_async, args=(url,))
        thread.start()
    
def load_from_file(filename):
    try:
        f = open(filename, 'r')
        text = f.read()
        f.close()
        return text
    except:
        return None
 

def get_upload_url():
    return config.base_url + config.upload_path


def get_devices_url():
    return config.base_url + config.devices_path


def get_credentials_url():
    return config.base_url + config.credentials_path + '/' + config.version


def get_test_desc_base_url():
    return config.base_url + config.test_desc_base_path

