from urllib import urlencode
from os import system
from falib.FADateTime import FADateTime
import hashlib
import base64

__author__ = 'mertsalik'


def url_concat(url, args):
    if not args:
        return url
    if url[-1] not in ('?', '&'):
        url += '&' if ('?' in url) else '?'
    return url + urlencode(args)


def is_alive(hostname):
    response = system("ping -c 1 -t 1 " + hostname + " > /dev/null")
    return response == 0


def __hash_path(path_string):
    return hashlib.md5(path_string).hexdigest()


def get_image_name(file_info, extension="JPG"):
    uid = uniq_identifier(file_info)
    return "{uid}.{ext}".format(uid=uid, ext=extension)


def uniq_identifier(file_info):
    if 'DecimalDate' not in file_info or 'DecimalTime' not in file_info:
        raise Exception("File info does not have required properties!")
    fad = FADateTime(int(file_info['DecimalDate']),
                     int(file_info['DecimalTime']))
    return fad.datetime.strftime(
        "ktmb_%Y%m%d_%H%M%S_{hash}".format(
            hash=__hash_path(file_info["Path"])))


def walk(top, topdown=True, onerror=None):
    pass
