您的当前位置:首页正文

python 提取iOS项目控制器页面的属性和方法

来源:要发发知识网

场景:代码混淆
完成了需求,代码性能有待优化



import os                           
import sys
import re   
urls=''
if len(sys.argv) == 1:
    print 'zz'
else:
    path = sys.argv[1]+''
for dirpath, dirname, filenames in os.walk(path):   
    
    for filename in filenames:
      if filename.count('VC.h') > 0:
        outName = '--' + filename.replace('.h','') +'\n'
        filePath = os.path.join(dirpath, filename)
        file_object = open(filePath, 'rU')
        
        try: 
            proOrMethodString = ''
            for lineIdx,line in enumerate(file_object):
                if line.startswith('#import') or line.startswith('@interface') or line.startswith('@protocol') or \
                line.startswith('@required') or line.startswith('@end') or line.startswith('@protocol') or \
                line.startswith('/*') or line.startswith('//') or \
                line.count('@param')>0 or line.count('@return')>0:
                    continue
                elif line.count('@property') > 0 and line.count('^') > 0 and not line.startswith('//'):
                    arr = line.split('^');
                    if len(arr) > 1:
                        # @property (nonatomic) BOOL isFinish;
                        arr = line.split('^');
                        property = arr[-1].replace(';','')
                        property = property.replace('\n','')
                        property = property.replace(')(void)','')
                        property = property.replace('(^','')
                        proOrMethodString = proOrMethodString + property +'\n'
                elif line.count('@property') > 0 and line.count('<') > 0 and not line.startswith('//'):
                    arr = line.split('*');
                    if len(arr) < 2:
                        # @property (nonatomic) BOOL isFinish;
                        arr = line.split(' ');
                        property = arr[-1].replace(';','')
                        property = property.replace('\n','')
                        proOrMethodString = proOrMethodString + property +'\n'
                elif line.count('@property') > 0 and not line.startswith('//'):
                    # propertyReg = '(.*?)@property(.*?)*(.*?);(.*?)'
                    # properties = re.findall(propertyReg,line,re.S)
                    arr = line.split('*');
                    if len(arr) < 2:
                        # @property (nonatomic) BOOL isFinish;
                        arr = line.split(' ');
                        property = arr[-1].replace(';','')
                        property = property.replace('\n','')
                        proOrMethodString =  proOrMethodString + property +'\n'
                    else:
                        # @property (nonatomic) NSURL *sourceURL;
                        property =  arr[1].split(';')[0]
                        proOrMethodString = proOrMethodString + property +'\n'
                    # urls.append(properties[2])   
                elif line.startswith('+') or line.startswith('-'):
                    if not line.startswith('//'):
                        method = line.replace('+','')
                        method = method.replace('-','')
                        method = method.replace('void','')
                        method = method.replace('instancetype','')
                        method = method.replace('(','')

                        method = method.replace(';','')
                        temArr = method.split(':')
                        if len(temArr) > 1:
                            method = temArr[0]
                        temArr2 = method.split(')')
                        if len(temArr2) > 1:
                            method = temArr2[1]
                        method = "".join(method.split())
                        if proOrMethodString.count(method) < 1:
                            proOrMethodString = proOrMethodString + method +'\n'
            if len(proOrMethodString) > 0:
                proOrMethodString =  outName + proOrMethodString
            urls=urls+proOrMethodString
        
        finally:
            file_object.close()
print urls