#!/usr/bin/env python2.7 # -*- coding: utf-8 -*- ''' 功能:自动生成opcode ''' import os from os.path import isfile, join # proto文件目录 PROTO_PATH = "../protol" # java文件生成目录 OPCODE_PATH = "./out" opcode2Comment = {'-1000': '测试'}; opCodeFile = open(OPCODE_PATH + '/OpCode.java','w') opCodeFile.write('package xpq.msg;\r\n') opCodeFile.write('import xpq.platform.io.OpCodeComment;\r\n') opCodeFile.write('public class OpCode {\r\n') onlyfiles = [ f for f in os.listdir(PROTO_PATH) if isfile(join(PROTO_PATH,f)) ] for fileName in onlyfiles: opCodeFile.write('// automatic generated begin fileName:' + fileName + '\r') file = open(PROTO_PATH + '/' + fileName) lines = file.readlines() index = 0 for line in lines: if line.startswith('//') and line.find(':', 0,len(line)) != -1: comment = line.split(':')[0] opcode = line.split(':')[1].strip() opcode2Comment[opcode] = comment lineNext = lines[index + 1] variable = lineNext.split(' ')[1].split('{')[0] tureLine = ' public static final short ' + variable + ' = ' + opcode + ';' + comment + '\r' opCodeFile.write(tureLine) index=index+1 file.close() opCodeFile.write('// automatic generated end fileName:' + fileName + '\r\n') opCodeFile.write(' static{\r') for k, v in opcode2Comment.items(): opCodeFile.write(' OpCodeComment.putComment(' + k + ', \"' + v + '\");\r') opCodeFile.write(' }') opCodeFile.write('\r\n}') opCodeFile.close() os.system('genProtocol.bat') # while True: # comment = '' # opcode = '' # variable = '' # line = file.readline() # if not line: # break # if line.startswith('//'): # comment = line.split(':')[0] # opcode = line.split(':')[1] # if line.startswith('message'): # variable = line.split(' ')[1] # print 'public static final short ' + variable + ' = ' + opcode + ';' + '//' + comment