Kaydet (Commit) 0ef878a5 authored tarafından Kurt Zenker's avatar Kurt Zenker

INTEGRATION: CWS layout_DEV300 (1.1.2); FILE ADDED

2008/02/13 08:56:23 jcn 1.1.2.1: Import src2xml and doc/layout from GIT.
üst 6c3eb0f5
#!/usr/bin/env python
import srclexer, srcparser, globals
class TestCase:
@staticmethod
def run (tokens, defines):
mcExpander = srcparser.MacroExpander(tokens, defines)
mcExpander.debug = True
mcExpander.expand()
tokens = mcExpander.getTokens()
print tokens
@staticmethod
def simpleNoArgs ():
tokens = ['FUNC_FOO', '(', 'left', ',', 'right', ')']
defines = {}
macro = globals.Macro('FUNC_FOO')
macro.tokens = ['Here', 'comes', 'X', 'and', 'Y']
defines['FUNC_FOO'] = macro
TestCase.run(tokens, defines)
@staticmethod
def simpleArgs ():
tokens = ['FUNC_FOO', '(', 'left', ',', 'right', ')']
defines = {}
macro = globals.Macro('FUNC_FOO')
macro.tokens = ['Here', 'comes', 'X', 'and', 'Y']
macro.vars['X'] = 0
macro.vars['Y'] = 1
defines['FUNC_FOO'] = macro
TestCase.run(tokens, defines)
@staticmethod
def multiTokenArgs ():
tokens = ['FUNC_FOO', '(', 'left1', 'left2', 'left3', ',', 'right', ')']
defines = {}
macro = globals.Macro('FUNC_FOO')
macro.tokens = ['Here', 'comes', 'X', 'and', 'Y']
macro.vars['X'] = 0
macro.vars['Y'] = 1
defines['FUNC_FOO'] = macro
TestCase.run(tokens, defines)
@staticmethod
def nestedTokenArgs ():
tokens = ['FUNC_BAA', '(', 'left', ',', 'right', ')']
defines = {}
macro = globals.Macro('FUNC_FOO')
macro.tokens = ['Here', 'comes', 'X', 'and', 'Y']
macro.vars['X'] = 0
macro.vars['Y'] = 1
defines['FUNC_FOO'] = macro
macro = globals.Macro('FUNC_BAA')
macro.tokens = ['FUNC_FOO']
defines['FUNC_BAA'] = macro
TestCase.run(tokens, defines)
def main ():
print "simple expansion with no arguments"
TestCase.simpleNoArgs()
print "simple argument expansion"
TestCase.simpleArgs()
print "multi-token argument expansion"
TestCase.multiTokenArgs()
print "nested argument expansion"
TestCase.nestedTokenArgs()
if __name__ == '__main__':
main()
import sys
from globals import *
import srclexer
class MacroParser(object):
def __init__ (self, buf):
self.buffer = buf
self.macro = None
self.debug = False
def parse (self):
"""
A macro with arguments must have its open paren immediately following
its name without any whitespace.
"""
if self.debug:
print "-"*68
print "parsing '%s'"%self.buffer
i = 0
bufSize = len(self.buffer)
name, buf = '', ''
while i < bufSize:
c = self.buffer[i]
if c in [' ', "\t"] and len(name) == 0:
# This is a simple macro with no arguments.
name = buf
vars = []
content = self.buffer[i:]
self.setMacro(name, vars, content)
return
elif c == '(' and len(name) == 0:
# This one has arguments.
name = buf
buf = self.buffer[i:]
vars, content = self.parseArgs(buf)
self.setMacro(name, vars, content)
return
else:
buf += c
i += 1
def parseArgs (self, buffer):
"""Parse arguments.
The buffer is expected to be formatted like '(a, b, c)' where the first
character is the open paren.
"""
scope = 0
buf = ''
vars = []
content = ''
bufSize = len(buffer)
i = 0
while i < bufSize:
c = buffer[i]
if c == '(':
scope += 1
elif c == ')':
scope -= 1
if len(buf) > 0:
vars.append(buf)
if scope == 0:
break
elif c == ',':
if len(buf) == 0:
raise globals.ParseError ('')
vars.append(buf)
buf = ''
elif c in " \t" and scope > 0:
pass
else:
buf += c
i += 1
if scope > 0:
raise globals.ParseError ('')
return vars, buffer[i+1:]
def setMacro (self, name, vars, content):
if self.debug:
print "-"*68
print "name: %s"%name
for var in vars:
print "var: %s"%var
if len(vars) == 0:
print "no vars"
print "content: '%s'"%content
if len(content) > 0:
self.macro = Macro(name)
for i in xrange(0, len(vars)):
self.macro.vars[vars[i]] = i
# tokinize it using lexer.
mclexer = srclexer.SrcLexer(content)
mclexer.expandHeaders = False
mclexer.inMacroDefine = True
mclexer.tokenize()
self.macro.tokens = mclexer.getTokens()
if self.debug:
print self.macro.tokens
if not self.isValidMacro(self.macro):
self.macro = None
if self.debug:
if self.macro != None:
print "macro registered!"
else:
print "macro not registered"
def isValidMacro (self, macro):
n = len(macro.tokens)
if n == 0:
return False
elif len(macro.name) > 4 and macro.name[1:4] == 'ID_':
# We don't want to expand macros like HID_, SID_, WID_, etc.
return False
return True
def getMacro (self):
return self.macro
#!/usr/bin/env python
import macroparser
def runParser (buf):
mparser = macroparser.MacroParser(buf)
mparser.debug = True
mparser.parse()
def main ():
buf = 'FOO (asdfsdaf)'
runParser(buf)
buf = 'FOO (x, y) (x) + (y)'
runParser(buf)
buf = 'FOO(x, y) (x) + (y)'
runParser(buf)
if __name__ == '__main__':
main()
#!/usr/bin/env python
import getopt
import os
import re
import sys
#
from srclexer import SrcLexer
from srcparser import SrcParser
from boxer import Boxer
# FIXME
from globals import *
def option_parser ():
import optparse
p = optparse.OptionParser ()
p.usage = '''src2xml.py [OPTION]... SRC-FILE...'''
examples = '''
Examples:
src2xml.py --output-dir=. --post-process --ignore-includes zoom.src
src2xml.py --dry-run -I svx/inc -I svx/source/dialog zoom.src
'''
def format_examples (self):
return examples
if 'epilog' in p.__dict__:
p.formatter.format_epilog = format_examples
p.epilog = examples
else:
p.formatter.format_description = format_examples
p.description = examples
p.description = '''OOo SRC To Layout XML Converter.
Convert OO.o's existing dialog resource files into XML layout files.
'''
p.add_option ('-l', '--debug-lexer', action='store_true',
dest='debug_lexer', default=False,
help='debug lexer')
p.add_option ('-p', '--debug-parser', action='store_true',
dest='debug_parser', default=False,
help='debug parser')
p.add_option ('-m', '--debug-macro', action='store_true',
dest='debug_macro', default=False,
help='debug macro')
p.add_option ('-n', '--dry-run', action='store_true',
dest='dry_run', default=False,
help='dry run')
p.add_option ('-k', '--keep-going', action='store_true',
dest='keep_going', default=False,
help='continue after error')
p.add_option ('-i', '--ignore-includes', action='store_true',
dest='ignore_includes', default=False,
help='ignore #include directives')
p.add_option ('-I', '--include-dir', action='append',
dest='include_path',
default=[],
metavar='DIR',
help='append DIR to include path')
def from_file (option, opt_str, value, parser):
lst = getattr (parser.values, option.dest)
lst += file (value).read ().split ('\n')
setattr (parser.values, option.dest, lst)
def from_path (option, opt_str, value, parser):
lst = getattr (parser.values, option.dest)
lst += value.split (':')
setattr (parser.values, option.dest, lst)
# Junk me?
p.add_option ('--includes-from-file', action='callback', callback=from_file,
dest='include_path',
default=[],
type='string',
metavar='FILE',
help='append directory list from FILE to include path')
p.add_option ('--include-path', action='callback', callback=from_path,
dest='include_path',
type='string',
default=[],
metavar='PATH',
help='append PATH to include path')
p.add_option ('--only-expand-macros', action='store_true',
dest='only_expand_macros', default=False,
help='FIXME: better to say what NOT to expand?')
p.add_option ('-o', '--output-dir', action='store',
dest='output_dir', default=None,
metavar='DIR',
help='Output to DIR')
p.add_option ('-s', '--post-process', action='store_true',
dest='post_process', default=False,
help='post process output for use in Layout')
p.add_option ('--stop-on-header', action='store_true',
dest='stopOnHeader', default=False,
help='FIXME: remove this?')
return p
def convert (file_name, options):
progress ("parsing %(file_name)s ..." % locals ())
fullpath = os.path.abspath(file_name)
if not os.path.isfile(fullpath):
error("no such file", exit=True)
##options.include_path.append (os.path.dirname (fullpath))
input = file (fullpath, 'r').read()
lexer = SrcLexer(input, fullpath)
lexer.expandHeaders = not options.ignore_includes
lexer.includeDirs = options.include_path
lexer.stopOnHeader = options.stopOnHeader
lexer.debugMacro = options.debug_macro
# lexer.debug = True
if options.debug_lexer:
lexer.debug = True
lexer.tokenize()
progress ("-"*68 + "\n")
progress ("** token dump\n")
lexer.dumpTokens()
progress ("** end of token dump\n")
return
# Tokenize it using lexer
lexer.tokenize()
parser = SrcParser(lexer.getTokens(), lexer.getDefines())
parser.only_expand_macros = options.only_expand_macros
if options.debug_parser:
parser.debug = True
root = parser.parse()
print root.dump()
return
# Parse the tokens.
root = parser.parse()
# Box it, and return the XML tree.
root = Boxer(root).layout()
output = root.dump()
if not options.dry_run:
progress ("\n")
return output
def dry_one_file (file_name, options):
try:
str = convert(file_name, options)
progress (" SUCCESS\n")
except Exception, e:
if options.keep_going:
progress (" FAILED\n")
else:
import traceback
print traceback.format_exc (None)
raise e
def post_process (s):
"""Make output directly usable by layout module."""
s = re.sub ('(</?)([a-z]+)-([a-z]+)-([a-z]+)', r'\1\2\3\4', s)
s = re.sub ('(</?)([a-z]+)-([a-z]+)', r'\1\2\3', s)
s = re.sub ('(<(radiobutton|(fixed(info|text)))[^>]*) text=', r'\1 label=', s)
s = re.sub (' (height|width|x|y)="[0-9]*"', '', s)
s = s.replace ('<modaldialog', '<modaldialog sizeable="true"')
s = s.replace (' rid=', ' id=')
s = s.replace (' border="true"', ' has_border="true"')
s = s.replace (' def-button="true"', ' default="true"')
return s
XML_HEADER = '<?xml version="1.0" encoding="UTF-8"?>\n'
def do_one_file (file_name, options):
str = XML_HEADER
str += convert(file_name, options)
str += '\n'
if options.post_process:
str = post_process (str)
h = sys.stdout
if options.output_dir:
base = os.path.basename (file_name)
root, ext = os.path.splitext (base)
out_name = options.output_dir + '/' + root + '.xml'
progress ("writing %(out_name)s ..." % locals ())
h = file (out_name, 'w')
h.write (str)
h.flush ()
progress ("\n")
def main ():
p = option_parser ()
(options, files) = option_parser ().parse_args ()
if not files:
p.error ("no input files")
for f in files:
if options.dry_run:
dry_one_file (f, options)
else:
do_one_file (f, options)
if __name__ == '__main__':
main ()
This diff is collapsed.
This diff is collapsed.
de
en-US
nl
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment