Надеюсь, это кому-нибудь пригодится (вы можете использовать буфер обмена win32 для чтения данных)
import sys, string
import win32clipboard
import re
data = '''
enum FeedTypeT
{
AA,
BB,
DDD,
F
};
'''
def get_from_clippord():
# get clipboard data
win32clipboard.OpenClipboard()
data = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()
return data
def enum_type(inenum):
inenum = inenum.replace('\r\n', '\n')
inenum = re.sub( r'\s', r'', inenum )
inenum = re.sub( r'^(.*)\{.*$', r'\1', inenum )
return inenum
def cleanup_enum(inenum):
inenum = inenum.replace('\r\n', '\n')
# inenum = inenum.replace('enum', '')
inenum = re.sub( r'\s', r'', inenum )
# inenum = re.sub( r'^.*\{(.+)[|,]\}.*$', r'\1', inenum )
inenum = re.sub( r'^.*\{(.+)\}.*$', r'\1', inenum )
inenum = inenum.split(',')
return inenum
def get_element(inlist):
for element in inlist:
[one, two] = element.split('=')
print('{0:20} ==> {1:>20}'.format(one, two)) # right align
# one = element.split('=')
# print('{0:20} ==> {1:10}'.format(one[0], one[1]))
def print_switch(typename):
retstr = 'const std::string toString( ' + typename + ' type )'
retstr += '\n{\n switch( type )\n {'
return retstr
def print_case_line(instr, w):
retstr = ' case ' + '{:{fw}}'.format(instr + ':', fw = w) + ' return "' + instr + '";'
return retstr
def print_switch_end(w):
retstr = ' default: ' + ' '*(w-4) + ' return "undef";\n }\n}\n'
return retstr
def main():
#data = get_from_clippord()
ll = cleanup_enum(data)
print ( ll )
print ("="*80 + "\n\n")
print ( print_switch( enum_type(data)) )
w = 25
# pick right with for formating, based on the lenght of elements of enum
for line in ll:
if w < len(line):
w = len(line) + 2
for line in ll:
print ( print_case_line(line, w) )
print ( print_switch_end(w) )
if __name__ == '__main__':
main()
Выход:
['AA', 'BB', 'DDD', 'F']
================================================================================
const std::string toString( enumFeedTypeT type )
{
switch( type )
{
case AA: return "AA";
case BB: return "BB";
case DDD: return "DDD";
case F: return "F";
default: return "undef";
}
}