def validate(data, trace=False):
    items = {}

    for (li, tvdb_id, sep, alias_list, line) in parse_data(data):
        # Check line ends with a ','
        if not line.strip().endswith(','):
            if trace:
                print "line does not end with ','"
                print "=================="
                print_error(li, line)
            return False

        # Check tvdb_id is a valid integer
        if not validate_tvdb_id(tvdb_id, trace=trace):
            if trace:
                print_error(li, line)
            return False

        # Check for duplicates
        if tvdb_id in items:
            if trace:
                print 'item already exists'
                print 'items[' + str(tvdb_id) + '] = ' + str(alias_list)
                print '=================='
                print_error(li, line)
            return False

        items[tvdb_id] = alias_list

    if trace:
        print 'valid'
    return True
def get_parsed_data(features):
    parsed_features = []
    # parsing line by line
    for feature in features:
        parsed_data = parse_data(str(feature))
        
        # If list is not empty
        if parsed_data != None:
            parsed_features.append(parsed_data)
            
    final_features = remove_inconsistencies(parsed_features)      
      
    return final_features
def get_features(folderpath):
    features = []
    fileData = read_file(folderpath)
    # For each line
    for dataVar in fileData:
        parsed_data = parse_data(dataVar)
        # If list is not empty
        if parsed_data != None:
            features.append(parsed_data)
    
    # Imputing missing data        
    final_features = remove_inconsistencies(features)  
          
    return final_features
def merge(localPath, remotePath):
    items = {}
    key_order = []

    changes = {}

    for path in [localPath, remotePath]:
        for (li, tvdb_id, sep, alias_list, line) in parse_data(get_text(path)):
            if not validate_tvdb_id(tvdb_id):
                continue

            if not items.has_key(tvdb_id):
                items[tvdb_id] = []
                key_order.append(tvdb_id)

            for alias in alias_list:
                alias = alias.strip().replace("'", "\\'")

                if not find_match(alias, items[tvdb_id]):
                    items[tvdb_id].append(alias)

                    # track remote changes
                    if path == remotePath:
                        if not changes.has_key(tvdb_id):
                            changes[tvdb_id] = []
                        changes[tvdb_id].append(alias)

    print "----------------------------------------------------------"
    print "New Shows"
    print "----------------------------------------------------------"
    for ck, added in changes.items():
        if items[ck] == added:
            print str(ck) + '\tnew\t\t' + str(added)

    print "----------------------------------------------------------"
    print "New Aliases"
    print "----------------------------------------------------------"
    for ck, added in changes.items():
        if items[ck] != added:
            print str(ck) + '\tadd\t\t' + str(added)
            print '=============\t', items[ck]
            print

    return dict_to_data(items, key_order)
def remove_duplicates(path):
    items = {}
    key_order = []

    changes = {}

    for (li, tvdb_id, sep, alias_list, line) in parse_data(get_text(path)):
        if not validate_tvdb_id(tvdb_id):
            continue

        if not items.has_key(tvdb_id):
            items[tvdb_id] = []
            key_order.append(tvdb_id)

        for alias in alias_list:
            alias = alias.strip().replace("'", "\\'")

            if not find_match(alias, items[tvdb_id]):
                items[tvdb_id].append(alias)

    return dict_to_data(items, key_order)