zoukankan      html  css  js  c++  java
  • python脚本为已创建的ios .h和.m文件添加费代码

    
    
    # -*- coding: utf-8 -*-
    import random
    import os
    
    def getArray():
        first = "abcdefghijklmnopqrstuvwxyz"
        second = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        third = "1234567890"
        number = "345"
        index = 0
        for i in range(500):
            final=(random.choice(first))
            index = random.randint(3, 5)
            for i in range(index):
                final+=(random.choice(first))
            final += (random.choice(second))
            for i in range(index):
                final+=(random.choice(third))
            array.append(final)
    classArray = ['NSString','UILabel','NSDictionary','NSData','UIScrollView','UIView','UITextView','UITableView','UIImageView']#.h文件里属性的类型从这个数组里随机选
    viewArray = ['UILabel','UIScrollView','UIView','UITextView','UITableView','UIImageView']#.m文件里创建的元素的类型从这个数组里随机选
    array = []
    getArray()#用于生成.h和.m文件中将要用的属性名
    array = list(set(array))
    #.h文件添加废代码
    def HFileAddMj(file_path,old_str1,old_str2,old_str3,old_str4,old_str5):
        file_data = ""
        Ropen=open(file_path,'r')
        for line in Ropen:
            nameStr = random.choice(array)
            className = random.choice(classArray)
            zhushi = '
    
    //###########################--' + random.choice(array) + '--############################/
    '
            if old_str1 in line:
                line += zhushi + '@property(nonatomic,strong)'+className+' * '+nameStr+';' + zhushi
                file_data += line
                array.remove(nameStr)#防止创建的属性名重复(创建一个从数组中删除一个)
            elif old_str2 in line:
                line += zhushi + '@property(nonatomic,strong)'+className +' * '+nameStr+';' + zhushi
                file_data += line
                array.remove(nameStr)
            elif old_str3 in line:
                line += zhushi + '@property(nonatomic,strong)'+className +' * '+nameStr+';' + zhushi
                file_data += line
                array.remove(nameStr)
            elif old_str4 in line:
                line += zhushi + '@property(nonatomic,strong)'+className +' * '+nameStr+';' + zhushi
                file_data += line
                array.remove(nameStr)
            elif old_str5 in line:
                line += zhushi + '@property(nonatomic,strong)'+className +' * '+nameStr+';' + zhushi
                file_data += line
                array.remove(nameStr)
            else:
                file_data += line
        Ropen.close()
        Wopen=open(file_path,'w')
        Wopen.write(file_data)
        Wopen.close()
        print(file_data)
    #.m文件添加废代码
    def MFileAddMj(file_path,old_str1,old_str2,old_str3,old_str4,old_str5,old_str6,old_str7):
        file_data = ""
        Ropen=open(file_path,'r')#读取文件
        for line in Ropen:
            nameStr = random.choice(array)
            className = random.choice(viewArray)
            zhushi = '
    
    //###########################--' + random.choice(array) + '--############################/
    '
            if old_str1 in line:#如果.h文件中的某一行里包含old_str,则往这一行添加一下语句
                line += zhushi +className +' * '+nameStr+' = '+'[['+className+' alloc]initWithFrame:CGRectMake('+str(random.randint(0,100))+','+str(random.randint(0,100))+','+str(random.randint(0,100))+','+str(random.randint(0,100))+')];
    '+' '+nameStr+'.layer.cornerRadius ='+str(random.randint(5,10))+';
    '+nameStr+'.userInteractionEnabled = YES;
    '+nameStr+'.layer.masksToBounds = YES;'+zhushi
                file_data += line
                array.remove(nameStr)#防止创建的元素名重复(创建一个从数组中删除一个)
            elif old_str2 in line:
                line += zhushi +className +' * '+nameStr+' = '+'[['+className+' alloc]initWithFrame:CGRectMake('+str(random.randint(0,100))+','+str(random.randint(0,100))+','+str(random.randint(0,100))+','+str(random.randint(0,100))+')];
    '+nameStr+'.layer.borderWidth = 1;'+nameStr+'.clipsToBounds = YES;
    '+' '+nameStr+'.layer.cornerRadius ='+str(random.randint(5,10))+';' + zhushi
                file_data += line
                array.remove(nameStr)
            elif old_str3 in line:
                line += zhushi +className +' * '+nameStr+' = '+'[['+className+' alloc]initWithFrame:CGRectMake('+str(random.randint(0,100))+','+str(random.randint(0,100))+','+str(random.randint(0,100))+','+str(random.randint(0,100))+')];
    '+nameStr+'.backgroundColor = [UIColor whiteColor];'+nameStr+'.layer.borderColor = [[UIColor greenColor] CGColor];'+' '+nameStr+'.layer.cornerRadius ='+str(random.randint(5,10))+';
    '+zhushi
                file_data += line
                array.remove(nameStr)
            elif old_str4 in line:
                line += zhushi +className +' * '+nameStr+' = '+'[['+className+' alloc]initWithFrame:CGRectMake('+str(random.randint(0,100))+','+str(random.randint(0,100))+','+str(random.randint(0,100))+','+str(random.randint(0,100))+')];
    '+nameStr+'.backgroundColor = [UIColor whiteColor];'+nameStr+'.layer.borderColor = [[UIColor greenColor] CGColor];'+' '+nameStr+'.layer.cornerRadius ='+str(random.randint(5,10))+';
    '+zhushi
                file_data += line
                array.remove(nameStr)
            elif old_str5 in line:
                line += zhushi +className +' * '+nameStr+' = '+'[['+className+' alloc]initWithFrame:CGRectMake('+str(random.randint(0,100))+','+str(random.randint(0,100))+','+str(random.randint(0,100))+','+str(random.randint(0,100))+')];
    '+nameStr+'.backgroundColor = [UIColor whiteColor];'+nameStr+'.layer.borderColor = [[UIColor greenColor] CGColor];'+' '+nameStr+'.layer.cornerRadius ='+str(random.randint(5,10))+';
    '+zhushi
                file_data += line
                array.remove(nameStr)
            elif old_str6 in line:
                line += zhushi +className +' * '+nameStr+' = '+'[['+className+' alloc]initWithFrame:CGRectMake('+str(random.randint(0,100))+','+str(random.randint(0,100))+','+str(random.randint(0,100))+','+str(random.randint(0,100))+')];
    '+nameStr+'.backgroundColor = [UIColor whiteColor];'+nameStr+'.layer.borderColor = [[UIColor greenColor] CGColor];'+' '+nameStr+'.layer.cornerRadius ='+str(random.randint(5,10))+';
    '+zhushi
                file_data += line
                array.remove(nameStr)
            elif old_str7 in line:
                line += zhushi +className +' * '+nameStr+' = '+'[['+className+' alloc]initWithFrame:CGRectMake('+str(random.randint(0,100))+','+str(random.randint(0,100))+','+str(random.randint(0,100))+','+str(random.randint(0,100))+')];
    '+nameStr+'.backgroundColor = [UIColor whiteColor];'+nameStr+'.layer.borderColor = [[UIColor greenColor] CGColor];'+' '+nameStr+'.layer.cornerRadius ='+str(random.randint(5,10))+';
    '+zhushi
                file_data += line
                array.remove(nameStr)
            else:
                file_data += line
        Ropen.close()
        Wopen=open(file_path,'w')
        Wopen.write(file_data)
        Wopen.close()
        #print(file_data)
    def file_name(file_dir):
        for root, dirs, files in os.walk(file_dir):
            #print(root) #当前目录路径
            #print(dirs) #当前路径下所有子目录
            print(files) #当前路径下所有非目录子文件
            fileNameArray = files
            #遍历文件夹下的.h和.m文件并添加废代码
            for file in files:
                if '.h' in file:#file_dir+'/'+file含义是file_dir文件夹下的file文件
                    HFileAddMj(file_dir+'/'+file, "w;", "v;","s;","d;","e;")#往凡是以  <"w;", "v;","s;","d;","e;"> 这些中的某一个结尾的oc语句后添加费代码
                    HFileAddMj(file_dir+'/'+file, "l;", "m;","n;","q;","y;")#往凡是以  <"l;", "m;","n;","q;","y;"> 这些中的某一个结尾的oc语句后添加费代码
                if '.m' in file:
                    MFileAddMj(file_dir+'/'+file, "d];", "e];","z];","s];","t];","o];","v];")#往凡是以 <"d];", "e];","z];","s];","t];","o];","v];">  这些中的某一个结尾的oc语句后添加费代码
                    MFileAddMj(file_dir+'/'+file, "n];", "w];","m];","c];","p];","q];","l];")#往凡是以 <"n];", "w];","m];","c];","p];","q];","l];">  这些中的某一个结尾的oc语句后添加费代码
    #要修改的文件所在的文件夹路径
    file_floadPath = 'G:/py_ios_test/x'#要修还得文件所在的文件夹路径
    file_name(file_floadPath)

    参考地址:https://blog.csdn.net/u013857988/article/details/79656048

  • 相关阅读:
    第二次作业循环语句
    c语言01次作业分支,顺序结构
    PAT 1027. Colors in Mars
    PAT 1026 Table Tennis
    PAT 1035 Password
    PAT 1038. Recover the Smallest Number
    PAT 1028 List Sorting (25)
    PAT 1041 Be Unique (20)
    PAT 1025 PAT Ranking
    1037. Magic Coupon
  • 原文地址:https://www.cnblogs.com/wodehao0808/p/12673404.html
Copyright © 2011-2022 走看看