# -*- coding: utf-8 -*-
"""
Created on Thu Jan  9 14:34:41 2025

@author: ym
"""
import os
def read_deletedBarcode_file(filePath):
    with open(filePath, 'r', encoding='utf-8') as f:
        lines = f.readlines()

        split_flag, all_list = False, []
        dict, barcode_list, similarity_list =  {}, [], []
        clean_lines = [line.strip().replace("'", '').replace('"', '') for line in lines]
        
        for i, line in enumerate(clean_lines):
            if line.endswith(','):  
                line = line[:-1]
                
            stripped_line = line.strip()
            if not stripped_line:
                if len(barcode_list): dict['barcode'] = barcode_list
                if len(similarity_list): dict['similarity'] = similarity_list
                if len(dict): all_list.append(dict)
                
                split_flag = False
                dict, barcode_list, similarity_list =  {}, [], []
                continue
            
            if line.find(':')<0: continue
            label = line.split(':')[0]
            value = line.split(':')[1]
            
            if label == 'SeqDir':
                dict['SeqDir'] = value
                dict['filetype'] = "deletedBarcode"
            if label == 'Deleted':
                dict['Deleted'] = value
            if label == 'List':
                split_flag = True
                continue
            if split_flag:
                barcode_list.append(label)
                similarity_list.append(value)
        
        if len(barcode_list): dict['barcode'] = barcode_list
        if len(similarity_list): dict['similarity'] = similarity_list
        if len(dict): all_list.append(dict)
    return all_list


def event_rename(path):
    for filename in  os.listdir(path):
        fpath = os.path.join(path, filename)
        if os.path.isfile(fpath): continue
        flist = filename.split("_")
        
        if len(flist)==3 and flist[-1].isdigit() and len(flist[-1])>=8:
            print(f"{path}: renamed!")
            return

    dpath = os.path.join(path, "deletedBarcode.txt")
    alist = read_deletedBarcode_file(dpath)
    
    input_events, getout_events = [], []
    for filename in  os.listdir(path):
        fpath = os.path.join(path, filename)
        if os.path.isfile(fpath): continue
        flist = filename.split("_")
        
        bcd = flist[-1]
        if len(flist)==2 and flist[0].find("2024")==0 and len(bcd)==0:
            bcds = [dt['Deleted'] for dt in  alist if dt['SeqDir'].strip()==filename]
            if len(bcds)==1:
                getout_events.append((filename, filename + bcds[0].strip()))
                
        if len(flist)==2 and flist[0].find("2024")==0 and len(bcd)>=10:            
            input_events.append((filename, filename + '_' + bcd))
    
    events = (input_events, getout_events)
    for evts in events:
        for name_old, name_new in evts:
            path_old = os.path.join(path, name_old)
            path_new = os.path.join(path, name_new)
            try:
                os.rename(path_old, path_new)
            except Exception as e:
                print(f"发生错误：{e}")




def main():
    
    path = r"\\192.168.1.28\share\测试视频数据以及日志\测试_202406\0910\images"
    event_rename(path)
    


if __name__ == "__main__": 
    main()
    
    
    
    
    
    
    
    
    
    