'''
Created on Apr 17, 2014

@author: mandy
'''
import re, random, os, errno
from PyQt4.QtGui import QFont
'''General'''
def setFontForUI(widget, size=20):
        import platform
        operSys = platform.system()
        if operSys == 'Linux':
            font = widget.font()
            font.setPointSize(size)
            widget.setFont(font)
        elif operSys == 'Darwin':
            font = QFont('Courier', size)
            widget.setFont(font)
        
def generateRandomCoordinates(width, height):
    x = random.randint(10, int(width))
    y = random.randint(10, int(height))
    return x, y

def compareTwoLists(list1, list2):
    if len(list2) < len(list1):
        return False
    if list1 == [] and list2 != []:
        return True
    for i in xrange(len(list1)):
        if list1[i] != list2[i]:
                return False
    return True
 
def addItemToList(item, listInstance):
    if item not in listInstance:
        listInstance.append(item)
        
def deleteItemToList(item, listInstance):
    if item in listInstance:
        listInstance.remove(item)

# def indexOfListContainItem(item, listOfList):
#     for l in listOfList:
#         if item in l:
#             return List
        
def indexofSetInListcontainItem(item, listOfList):
    for l in listOfList:
        if item in l:
            return listOfList.index(l)
        
def removeDictElementInValue(item, dict1):
    indexIndexToRemove = set()
    indexToRemove = set()
    for key, level in dict1.iteritems():
        for k, v in level.iteritems():
            if item in v:
                v.remove(item)
                if v == []:
                    indexIndexToRemove.add(k)
        for i in indexIndexToRemove:
            del level[i]
        if level == {}:
            indexToRemove.add(key)
    for i in indexToRemove:
        del dict1[i]
                    
'''GUI'''
def removeAllItemsInTableView(view):
    while view.rowCount() > 0:
        view.removeRow(0)

def removeAllItemsInWidget(layout):
    while not layout.isEmpty():
        item = layout.takeAt(0)
        del item
    
def setWidgetVisibility(widget, state):
    widget.setVisible(state)
    
def addItemToDict(dictionary, index, item):
    if index == len(dictionary):
        dictionary[index] = [item]
    elif item not in dictionary[index]:
        dictionary[index].append(item)
        
def addItemToTextKeyDict(dictionary, text, item):
    if text not in dictionary.keys():
        dictionary[text] = [item]
    elif item not in dictionary[text]:
        dictionary[text].append(item)
        
def setWidgetLocation(widget, widgetGeometry, pos):
    widget.setGeometry(pos.x(), pos.y(), widgetGeometry.width(), widgetGeometry.height())
    
def setWidgetWidth(widget, widgetGeometry, width):
    widget.setGeometry(widgetGeometry.x(), widgetGeometry.y(), width, widgetGeometry.height())
    
def setWidgetHeight(widget, widgetGeometry, height):
    widget.setGeometry(widgetGeometry.x(), widgetGeometry.y(), widgetGeometry.width(), height)

'''Common'''
def nameOnlyContainLetterNumbers(name):
    if re.match("^[A-Za-z0-9/]*$", name):
        return True
    return False

def checkNameDuplication(name, checklist):
    for t in checklist:
        if t == name:
            return True
    return False

def make_sure_path_exists(path):
    try:
        os.makedirs(path)
    except OSError as exception:
        if exception.errno != errno.EEXIST:
            raise
        
'''Permissions'''
def convertOctToRWXString(perm):
    perms = ''
    if bool(perm&0b100):
        perms+='r'
    else:
        perms+='-'
    if bool(perm&0b010):
        perms+='w'
    else:
        perms+='-'
    if bool(perm&0b001):
        perms+='x'
    else:
        perms+='-'
    return perms