python写一段自动生成Objective-C模型对象的脚本

来源:互联网 发布:借款网站源码 编辑:程序博客网 时间:2024/06/02 11:19

新学python,读取文件操作文件部分,一直是我比较头疼的部分

其他平台的编译调试比较累。

想来每次写iphone程序mvc分离时候,最头疼的就是复制粘贴model和object部分了。

于是亲自动手练习一下=_=代码写的不是很好,希望指正。


1.0版本


支持ARC与非ARC

支持NSString,NSInteger,NSDate


下面的工作:

1:安装到本地和打包上传

2:增加自定义对象。

3:模型部分(支持单件模式,数组列表,增删改查函数)


都不是很麻烦,以后想起来在加=_=





以下是代码


__author__ = 'watsy'import sys"""title comment"""def fileTitleComment():    return """  /**    *author:watsy    */\n""""""help"""def showHelp():    print """help\n    example:    1:createObject.py obj:CWDemoObject 1 s:sName s:sTitle i:nID d:dBirtyday    2:createObject.py obj:CWDemoObject 0 s:sName s:sTitle i:nID d:dBirtyday    3:createObject.py obj:CWDemoObject s:sName s:sTitle i:nID d:dBirtyday    4:createObject.py CWDemoObject s:sName s:sTitle i:nID d:dBirtyday    obj:name (name means file name and object name.)    1: open arc flag ^-^    s:NSString    i:NSInteger    d:NSDate""""""@property (nonatomic, strong) NSString*     sCode;"""def writeObj(obj_string, fileHandle):    try:        (obj, name) = obj_string.strip().split(':')        if obj == 's':            fileHandle.write('@property (nonatomic, strong) NSString*\t\t\t%s;\n' %(name))        elif obj == 'i':            fileHandle.write('@property (nonatomic, assign) NSInteger\t\t\t%s;\n' %(name))        elif obj == 'd':            fileHandle.write('@property (nonatomic, strong) NSDate*\t\t\t%s;\n' %(name))    except ValueError as err:        print ('error %s' % (err))"""@synthesize _sCode;"""def writeSynthesize(obj_string, fileHandle):    try:        (obj, name) = obj_string.strip().split(':')        fileHandle.write('@synthesize _%s;\n' %(name))    except ValueError as err:        print ('error %s' % (err))"""    [object release];"""def writeObjectRelease(obj_string, fileHandle):    try:        (obj, name) = obj_string.strip().split(':')        fileHandle.write('\n\t[_%s release];' % (name))    except ValueError as err:        print ('error %s' % (err))def createObject(*args, **kwargs):    # print args    try:        args  = args[0]        if len(args) == 2 and args[1] == '--help':            showHelp()        else:            filename = args[1]            #get file name            if 'obj:' in filename:                filename = filename.split(":")[1]            arc = args[2]            bArc = False            if arc == '1':                bArc = True                objs = args[3:]            elif arc =='0':                objs = args[3:]            else:                objs = args[2:]            #create file.h            with open("%s.h" % (filename), 'w') as fn:                #write title comment                fn.write(fileTitleComment())                #write import file                fn.write('import <Foundation/Foundation.h>\n\n')                #write object                fn.write("@interface %s : NSObject\n\n" % (filename))                for eachObj in objs:                    writeObj(eachObj, fn)                #write end                fn.write("\n@end")            #create file.m            with open("%s.m" % (filename), 'w') as fn:                #write import file                fn.write('#import "%s.h"\n\n' % (filename))                fn.write('@implementation %s\n\n' % (filename))                #@synthesize                for eachObj in objs:                    writeSynthesize(eachObj, fn)                #init                fn.write('\n- (id) init {\n\tif (self = [super init]) {\n\t\t//add code\n\t}\n\treturn self;\n}')                #arc model                if bArc == False:                    fn.write('\n\n- (void) dealloc {\n');                    for eachObj in objs:                        writeObjectRelease(eachObj, fn)                    fn.write('\n\n\t[super dealloc]\n}\n\n');                fn.write('\n@end')    except:        print "input error : %s" % ( args)        print showHelp()if __name__ == '__main__':    createObject(sys.argv)


原创粉丝点击