site: end of line message, adesklets is not dead.
[adesklets.git] / scripting / python / protoize.py
blobbe2b9cffc0848a2b3cebd68d52ae3bc1efdad6a8
1 import re
2 from distutils.cmd import Command
3 from time import strftime
4 from pretty_printer import PrettyPrinter
6 class Protoize(Command):
7 description = 'generate adesklets.commands module'
8 user_options = []
9 def __init__(self,dist):
10 Command.__init__(self,dist)
11 self.pp=PrettyPrinter(80,'adesklets/commands.py')
13 def initialize_options(self):
14 pass
15 def finalize_options(self):
16 pass
17 def run(self):
18 # Usefull regular expressions
19 re_asterix=re.compile('\*')
20 re_brackets=re.compile('^\[(.*)\]$')
21 re_boolean=re.compile('\s?bool\s+')
22 re_spaces=re.compile('\s+')
24 # Open and read files
25 fd=file('../enums', 'r')
26 lines=fd.readlines()
27 fd.close()
28 fd=file('../prototypes','r')
29 lines2=fd.readlines()
30 fd.close()
32 # Ouput the prelude
33 print '"""'
34 print 'adesklets commands module'
35 print
36 print '===', strftime('%Y-%m-%d %H:%M:%S'), '==='
37 print 'Automatically generated by `setup.py protoize\' from'
38 print 'files `../prototypes\' and `../enums\'.'
39 print str(len(lines)) + ' constants and ' + str(len(lines2)) + ' functions successfully created.'
40 print
41 print '"""'
42 print '# Imports'
43 print '#'
44 print 'from error_handler import ADESKLETSError'
45 print 'import commands_handler'
46 print 'import strings'
47 print
48 print '# Constants'
49 print '#'
51 for name,value in map(lambda x: x.strip().split('\t'), lines):
52 print '%-20s = %2d' % (name,int(value))
54 print
55 print '# Functions'
56 print '#'
57 for line in lines2:
58 cmd,help,proto=line.strip().split('\t')
59 args=[]
60 if proto!='void':
61 match=re_brackets.match(proto)
62 if match:
63 newproto=match.expand('\\1')
64 optional=True
65 else:
66 newproto=proto
67 optional=False
68 for param in newproto.split(','):
69 if re_asterix.search(param):
70 param=re_asterix.sub('',param)
71 stringify=True
72 else:
73 if re_boolean.search(param):
74 stringify=True
75 else:
76 stringify=False
77 args.append((param.split()[-1],optional or stringify))
79 # Ok, now we collected all needed information,
80 # so let's define the function
81 output=''
82 for arg in args:
83 output+=arg[0] + ' '
84 if optional:
85 output=re_spaces.sub('=None ',output)
86 output=re_spaces.sub(', ',output.strip())
87 self.pp.state=True
88 print 'def', cmd + '(' + output + '):'
89 print '\t"""'
90 print '\t' + help
91 self.pp.state=False
92 print
93 self.pp.state=True
94 print '\t'+proto
95 print '\t"""'
96 self.pp.state=False
97 print '\tcomm=commands_handler._Static_commands_handler()'
98 if len(args)>0:
99 print '\tprint >> comm,',"'"+cmd+"',",
100 output=''
101 for arg, stringify in args:
102 if stringify:
103 output+='strings.String('+arg+') '
104 else:
105 output+=arg + ' '
106 output=re_spaces.sub(', ',output.strip())
107 print output
108 else:
109 print '\tprint >> comm,',"'"+cmd+"'"
110 print '\treturn comm.out()\n'
112 # Close file
113 fd.close()