8 from srclexer
import SrcLexer
9 from srcparser
import SrcParser
10 from boxer
import Boxer
16 p
= optparse
.OptionParser ()
18 p
.usage
= '''src2xml.py [OPTION]... SRC-FILE...'''
22 src2xml.py --output-dir=. --post-process --ignore-includes zoom.src
23 src2xml.py --dry-run -I svx/inc -I svx/source/dialog zoom.src
26 def format_examples (self
):
29 if 'epilog' in p
.__dict
__:
30 p
.formatter
.format_epilog
= format_examples
33 p
.formatter
.format_description
= format_examples
34 p
.description
= examples
36 p
.description
= '''OOo SRC To Layout XML Converter.
38 Convert OO.o's existing dialog resource files into XML layout files.
41 p
.add_option ('-l', '--debug-lexer', action
='store_true',
42 dest
='debug_lexer', default
=False,
45 p
.add_option ('-p', '--debug-parser', action
='store_true',
46 dest
='debug_parser', default
=False,
49 p
.add_option ('-m', '--debug-macro', action
='store_true',
50 dest
='debug_macro', default
=False,
53 p
.add_option ('-n', '--dry-run', action
='store_true',
54 dest
='dry_run', default
=False,
57 p
.add_option ('-k', '--keep-going', action
='store_true',
58 dest
='keep_going', default
=False,
59 help='continue after error')
61 p
.add_option ('-i', '--ignore-includes', action
='store_true',
62 dest
='ignore_includes', default
=False,
63 help='ignore #include directives')
65 p
.add_option ('-I', '--include-dir', action
='append',
69 help='append DIR to include path')
71 def from_file (option
, opt_str
, value
, parser
):
72 lst
= getattr (parser
.values
, option
.dest
)
73 lst
+= file (value
).read ().split ('\n')
74 setattr (parser
.values
, option
.dest
, lst
)
76 def from_path (option
, opt_str
, value
, parser
):
77 lst
= getattr (parser
.values
, option
.dest
)
78 lst
+= value
.split (':')
79 setattr (parser
.values
, option
.dest
, lst
)
82 p
.add_option ('--includes-from-file', action
='callback', callback
=from_file
,
87 help='append directory list from FILE to include path')
89 p
.add_option ('--include-path', action
='callback', callback
=from_path
,
94 help='append PATH to include path')
96 p
.add_option ('--only-expand-macros', action
='store_true',
97 dest
='only_expand_macros', default
=False,
98 help='FIXME: better to say what NOT to expand?')
100 p
.add_option ('-o', '--output-dir', action
='store',
101 dest
='output_dir', default
=None,
103 help='Output to DIR')
105 p
.add_option ('-s', '--post-process', action
='store_true',
106 dest
='post_process', default
=False,
107 help='post process output for use in Layout')
109 p
.add_option ('--stop-on-header', action
='store_true',
110 dest
='stopOnHeader', default
=False,
111 help='FIXME: remove this?')
116 def convert (file_name
, options
):
117 progress ("parsing %(file_name)s ..." % locals ())
118 fullpath
= os
.path
.abspath(file_name
)
119 if not os
.path
.isfile(fullpath
):
120 error("no such file", exit
=True)
122 ##options.include_path.append (os.path.dirname (fullpath))
124 input = file (fullpath
, 'r').read()
125 lexer
= SrcLexer(input, fullpath
)
126 lexer
.expandHeaders
= not options
.ignore_includes
127 lexer
.includeDirs
= options
.include_path
128 lexer
.stopOnHeader
= options
.stopOnHeader
129 lexer
.debugMacro
= options
.debug_macro
130 if options
.debug_lexer
:
133 progress ("-"*68 + "\n")
134 progress ("** token dump\n")
136 progress ("** end of token dump\n")
139 # Tokenize it using lexer
142 parser
= SrcParser(lexer
.getTokens(), lexer
.getDefines())
143 parser
.only_expand_macros
= options
.only_expand_macros
144 if options
.debug_parser
:
146 root
= parser
.parse()
151 root
= parser
.parse()
153 # Box it, and return the XML tree.
154 root
= Boxer(root
).layout()
156 if not options
.dry_run
:
160 def dry_one_file (file_name
, options
):
162 str = convert(file_name
, options
)
163 progress (" SUCCESS\n")
165 if options
.keep_going
:
166 progress (" FAILED\n")
169 print traceback
.format_exc (None)
172 def post_process (s
):
173 """Make output directly usable by layout module."""
174 s
= re
.sub ('(</?)([a-z]+)-([a-z]+)-([a-z]+)', r
'\1\2\3\4', s
)
175 s
= re
.sub ('(</?)([a-z]+)-([a-z]+)', r
'\1\2\3', s
)
176 s
= re
.sub ('(<(checkbox|(cancel|help|ignore|ok|push|more|no|radio|reset|retry|yes)button|(fixed(info|text)))[^>]*) text=', r
'\1 label=', s
)
177 s
= re
.sub (' (height|width|x|y)="[0-9]*"', '', s
)
178 s
= re
.sub (' (label|text|title)="', r
' _\1="', s
)
179 s
= re
.sub ('&([^m][^p]*[^;]*)', r
'&\1', s
)
180 s
= re
.sub (' hide="(TRUE|true|1)"', ' show="false"', s
)
182 s
= s
.replace ('<modaldialog', '<modaldialog sizeable="true"')
183 s
= s
.replace (' rid=', ' id=')
184 s
= s
.replace (' border="true"', ' has_border="true"')
185 s
= s
.replace (' def-button="true"', ' defbutton="true"')
186 s
= s
.replace (' drop-down="', ' dropdown="')
187 s
= s
.replace (' tab-stop="', ' tabstop="')
190 XML_HEADER
= '''<?xml version="1.0" encoding="UTF-8"?>
191 <!-- This is a template. i18n translation is not performed in-place;
192 i18n translated XML files are generated from this template by
193 transex3/layout/tralay. !-->
196 def do_one_file (file_name
, options
):
198 str += convert(file_name
, options
)
201 if options
.post_process
:
202 str = post_process (str)
204 if options
.output_dir
:
205 base
= os
.path
.basename (file_name
)
206 root
, ext
= os
.path
.splitext (base
)
207 out_name
= options
.output_dir
+ '/' + root
+ '.xml'
208 progress ("writing %(out_name)s ..." % locals ())
209 h
= file (out_name
, 'w')
216 (options
, files
) = option_parser ().parse_args ()
218 p
.error ("no input files")
222 dry_one_file (f
, options
)
224 do_one_file (f
, options
)
226 if __name__
== '__main__':