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'
9 def __init__(self
,dist
):
10 Command
.__init
__(self
,dist
)
11 self
.pp
=PrettyPrinter(80,'adesklets/commands.py')
13 def initialize_options(self
):
15 def finalize_options(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+')
25 fd
=file('../enums', 'r')
28 fd
=file('../prototypes','r')
34 print 'adesklets commands module'
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.'
44 print 'from error_handler import ADESKLETSError'
45 print 'import commands_handler'
46 print 'import strings'
51 for name
,value
in map(lambda x
: x
.strip().split('\t'), lines
):
52 print '%-20s = %2d' % (name
,int(value
))
58 cmd
,help,proto
=line
.strip().split('\t')
61 match
=re_brackets
.match(proto
)
63 newproto
=match
.expand('\\1')
68 for param
in newproto
.split(','):
69 if re_asterix
.search(param
):
70 param
=re_asterix
.sub('',param
)
73 if re_boolean
.search(param
):
77 args
.append((param
.split()[-1],optional
or stringify
))
79 # Ok, now we collected all needed information,
80 # so let's define the function
85 output
=re_spaces
.sub('=None ',output
)
86 output
=re_spaces
.sub(', ',output
.strip())
88 print 'def', cmd
+ '(' + output
+ '):'
97 print '\tcomm=commands_handler._Static_commands_handler()'
99 print '\tprint >> comm,',"'"+cmd
+"',",
101 for arg
, stringify
in args
:
103 output
+='strings.String('+arg
+') '
106 output
=re_spaces
.sub(', ',output
.strip())
109 print '\tprint >> comm,',"'"+cmd
+"'"
110 print '\treturn comm.out()\n'