Author: pfg Date: Sat Aug 4 00:01:47 2012 New Revision: 1369251 URL: http://svn.apache.org/viewvc?rev=1369251&view=rev Log: i115780 - Update pyAltFCFGMerge to run under Python 3.
This is only a partial commit to avoid conflicts with Python2. Also reindent while here. Author: Simon A. Wilper Reviewed by: hanya Modified: incubator/ooo/trunk/main/filter/source/config/tools/merge/pyAltFCFGMerge Modified: incubator/ooo/trunk/main/filter/source/config/tools/merge/pyAltFCFGMerge URL: http://svn.apache.org/viewvc/incubator/ooo/trunk/main/filter/source/config/tools/merge/pyAltFCFGMerge?rev=1369251&r1=1369250&r2=1369251&view=diff ============================================================================== --- incubator/ooo/trunk/main/filter/source/config/tools/merge/pyAltFCFGMerge (original) +++ incubator/ooo/trunk/main/filter/source/config/tools/merge/pyAltFCFGMerge Sat Aug 4 00:01:47 2012 @@ -1,4 +1,4 @@ -#!/bin/env python +#!/usr/bin/env python #_____________________________________________ # Caolan McNamara caol...@redhat.com # converted from original java written by Andreas Schluens so we can continue @@ -9,7 +9,7 @@ # there is a java which is available for use by all #_____________________________________________ -import sys, string, os.path +import sys, string, os.path CFGFILE = os.environ["SOLARVER"] + "/" + os.environ["INPATH"] + "/inc/l10ntools/FCFGMerge.cfg" @@ -44,14 +44,14 @@ PROP_ITEMS = "items" #---begin java.util.Properties copy---# """ -An incomplete clean room implementation of +An incomplete clean room implementation of java.util.Properties written in Python. Copyright (C) 2002,2004 - Ollie Rutherfurd <oli...@rutherfurd.net> Based on: - http://java.sun.com/j2se/1.3/docs/api/java/util/Properties.html + http://java.sun.com/j2se/1.3/docs/api/java/util/Properties.html Missing: @@ -89,198 +89,199 @@ __all__ = ['Properties'] def dec2hex(n): - h = hex(n)[2:].upper() - return '\\u' + '0' * (4 - len(h)) + h + h = hex(n)[2:].upper() + return '\\u' + '0' * (4 - len(h)) + h def escapestr(s): - buff = [] - # QUESTION: escape leading or trailing spaces? - for c in s: - if c == '\\': - buff.append('\\\\') - elif c == '\t': - buff.append('\\t') - elif c == '\n': - buff.append('\\n') - elif c == '\r': - buff.append('\\r') - elif c == ' ': - buff.append('\\ ') - elif c == "'": - buff.append("\\'") - elif c == '"': - buff.append('\\"') - elif c == '#': - buff.append('\\#') - elif c == '!': - buff.append('\\!') - elif c == '=': - buff.append('\\=') - elif 32 <= ord(c) <= 126: - buff.append(c) - else: - buff.append(dec2hex(c)) + buff = [] + # QUESTION: escape leading or trailing spaces? + for c in s: + if c == '\\': + buff.append('\\\\') + elif c == '\t': + buff.append('\\t') + elif c == '\n': + buff.append('\\n') + elif c == '\r': + buff.append('\\r') + elif c == ' ': + buff.append('\\ ') + elif c == "'": + buff.append("\\'") + elif c == '"': + buff.append('\\"') + elif c == '#': + buff.append('\\#') + elif c == '!': + buff.append('\\!') + elif c == '=': + buff.append('\\=') + elif 32 <= ord(c) <= 126: + buff.append(c) + else: + buff.append(dec2hex(c)) - return ''.join(buff) + return ''.join(buff) # TODO: add support for \uXXXX? def unescapestr(line): - buff = [] - escape = 0 - for i in range(len(line)): - c = line[i] - if c == '\\': - if escape: - escape = 0 - buff.append('\\') - continue - else: - # this is to deal with '\' - # acting as a line continuation - # character - if i == len(line) - 1: - buff.append('\\') - break - else: - escape = 1 - continue - elif c == 'n': - if escape: - escape = 0 - buff.append('\n') - continue - elif c == 'r': - if escape: - escape = 0 - buff.append('\r') - continue - elif c == 't': - if escape: - escape = 0 - buff.append('\t') - continue - - buff.append(c) - - # make sure escape doesn't stay one - # all expected escape sequences either break - # or continue, so this should be safe - if escape: - escape = 0 + buff = [] + escape = 0 + for i in range(len(line)): + c = line[i] + if c == '\\': + if escape: + escape = 0 + buff.append('\\') + continue + else: + # this is to deal with '\' + # acting as a line continuation + # character + if i == len(line) - 1: + buff.append('\\') + break + else: + escape = 1 + continue + elif c == 'n': + if escape: + escape = 0 + buff.append('\n') + continue + elif c == 'r': + if escape: + escape = 0 + buff.append('\r') + continue + elif c == 't': + if escape: + escape = 0 + buff.append('\t') + continue - return ''.join(buff) + buff.append(c) + + # make sure escape doesn't stay one + # all expected escape sequences either break + # or continue, so this should be safe + if escape: + escape = 0 + + return ''.join(buff) class Properties(dict): - def __init__(self, defaults={}): - dict.__init__(self) - for n,v in defaults.items(): - self[n] = v - - def __getittem__(self,key): - try: - return dict.__getittem__(self,key) - except KeyError: - return None - - def read(self,filename): - """ - Reads properties from a file (java Property class - reads from an input stream -- see load()). - """ - f = None - try: - f = open(filename) - self.load(f) - finally: - if f: - f.close() - - def load(self, buff): - """ - Reads properties from a stream (StringIO, file, etc...) - """ - props = readprops(buff) - for n,v in props.iteritems(): - self[n] = v + def __init__(self, defaults={}): + dict.__init__(self) + for n,v in defaults.items(): + self[n] = v + + def __getittem__(self,key): + try: + return dict.__getittem__(self,key) + except KeyError: + return None + + def read(self,filename): + """ + Reads properties from a file (java Property class + reads from an input stream -- see load()). + """ + f = None + try: + f = open(filename) + self.load(f) + finally: + if f: + f.close() + + def load(self, buff): + """ + Reads properties from a stream (StringIO, file, etc...) + """ + props = readprops(buff) + #for n,v in props.iteritems(): + for n in props.keys(): + self[n] = props[n] def readprops(buff): - name,value = None,'' - props = {} - continued = 0 - - while 1: - line = buff.readline() - if not line: - break - line = line.strip() - - # empty line - if not line: - continue - - # comment - if line[0] in ('#','!'): - continue - - # find name - i,escaped = 0,0 - while i < len(line): - c = line[i] - - if c == '\\': - if escaped: - escaped = 0 - else: - escaped = 1 - i += 1 - continue - - elif c in (' ', '\t', ':', '=') and not escaped: - name = unescapestr(line[:i]) - break - - # make sure escaped doesn't stay on - if escaped: - escaped = 0 - - i += 1 - - # no dlimiter was found, name is entire line, there is no value - if name == None: - name = unescapestr(line.lstrip()) - - # skip delimiter - while line[i:i+1] in ('\t', ' ', ':', '='): - i += 1 - - value = unescapestr(line[i:].strip()) - while value[-1:] == '\\': - value = value[:-1] # remove \ - line = buff.readline() - if not line: - break - value += unescapestr(line.strip()) + name,value = None,'' + props = {} + continued = 0 + + while 1: + line = buff.readline() + if not line: + break + line = line.strip() + + # empty line + if not line: + continue + + # comment + if line[0] in ('#','!'): + continue + + # find name + i,escaped = 0,0 + while i < len(line): + c = line[i] + + if c == '\\': + if escaped: + escaped = 0 + else: + escaped = 1 + i += 1 + continue + + elif c in (' ', '\t', ':', '=') and not escaped: + name = unescapestr(line[:i]) + break + + # make sure escaped doesn't stay on + if escaped: + escaped = 0 + + i += 1 + + # no dlimiter was found, name is entire line, there is no value + if name == None: + name = unescapestr(line.lstrip()) + + # skip delimiter + while line[i:i+1] in ('\t', ' ', ':', '='): + i += 1 + + value = unescapestr(line[i:].strip()) + while value[-1:] == '\\': + value = value[:-1] # remove \ + line = buff.readline() + if not line: + break + value += unescapestr(line.strip()) - #print 'value:',value ## - props[name] = value + #print 'value:',value ## + props[name] = value - return props + return props #---end java.util.Properties copy---# # Its a simple command line tool, which can merge different XML fragments # together. Such fragments must exist as files on disk, will be moved into # one file together on disk. -# +# # @author Andreas Schluens -# +# def run(sCmdLine): printCopyright() @@ -299,21 +300,21 @@ def run(sCmdLine): #prints out a copyright message on stdout. def printCopyright(): - print "FCFGMerge" - print "Copyright: 2003 by Red Hat, Inc., based on FCFGMerge.java` by Sun" - print "All Rights Reserved." + print("FCFGMerge") + print("Copyright: 2003 by Red Hat, Inc., based on FCFGMerge.java` by Sun") + print("All Rights Reserved.") #prints out a help message on stdout. def printHelp(): - print "____________________________________________________________" - print "usage: FCFGMerge cfg=<file name>" - print "parameters:" - print "\tcfg=<file name>" - print "\t\tmust point to a system file, which contains" - print "\t\tall neccessary configuration data for the merge process." - print "\tFurther cou can specify every parameter allowed in the" - print "\tconfig file as command line parameter too, to overwrite" - print "\tthe value from the file." + print("____________________________________________________________") + print("usage: FCFGMerge cfg=<file name>") + print("parameters:") + print("\tcfg=<file name>") + print("\t\tmust point to a system file, which contains") + print("\t\tall neccessary configuration data for the merge process.") + print("\tFurther cou can specify every parameter allowed in the") + print("\tconfig file as command line parameter too, to overwrite") + print("\tthe value from the file.") def StringTokenizer(mstring, separators, isSepIncluded=0): #Return a list of tokens given a base string and a string of @@ -337,11 +338,11 @@ def StringTokenizer(mstring, separators, # and merge it together with might existing config # files. That provides the possibility to overwrite # config values via command line parameter. -# +# # @author Andreas Schluens class ConfigHelper: def __init__(self, sPropFile, lCommandLineArgs): - self.m_bEmpty = 1 + self.m_bEmpty = 1 # first load prop file, so its values can be overwritten # by command line args later # Do it only, if a valid file name was given. @@ -354,9 +355,10 @@ class ConfigHelper: count = 0 if lCommandLineArgs != None: count = len(lCommandLineArgs) + print("Count is {c}".format(c=count)) self.m_bEmpty = (count < 1) - print lCommandLineArgs, "and len is", count + #print( lCommandLineArgs, "and len is", count ) for arg in range(count): # is it a named-value argument? # Note: We ignores double "=" signs! => search from left to right @@ -370,40 +372,39 @@ class ConfigHelper: # is it a boolean argument? # Note: Because "--" and "-" will be interpreted as the same # we search from right to left! - pos = string.rfind(lCommandLineArgs[arg], '-') + pos = lCommandLineArgs[arg].rfind('-') if pos == -1: pos = lCommandLineArgs[arg].rfind('/') if pos != -1: sArg = lCommandLineArgs[arg][pos+1:] self.props[sArg] = 1 continue - - raise Exception("Invalid command line detected. The argument \""+\ - lCommandLineArgs[arg]+"\" use an unsupported format.") + + raise Exception("Invalid command line detected. The argument \""+lCommandLineArgs[arg]+"\" use an unsupported format.") # for item in self.props: # print item, '->', self.props[item] def isHelp(self): return ( - (self.props.has_key("help")) or - (self.props.has_key("?") ) or - (self.props.has_key("h") ) + ("help" in self.props) or + ("?" in self.props ) or + ("h" in self.props ) ) def getValue(self, sProp): - if not self.props.has_key(sProp): + if not sProp in self.props: raise Exception("The requested config value \""+sProp+"\" "\ "does not exists!"); return self.props[sProp]; def getValueWithDefault(self, sProp, default): - if not self.props.has_key(sProp): - return default; + if not sProp in self.props: + return default; return self.props[sProp]; def getStringList(self, sProp, sDelimiter, bTrim, bDecode): - if not self.props.has_key(sProp): + if not sProp in self.props: raise Exception("The requested config value \""+sProp+"\" does "\ "not exists!"); sValue = self.props[sProp] @@ -412,7 +413,8 @@ class ConfigHelper: lTokens = StringTokenizer(sValue, sDelimiter) for sToken in lTokens: if bTrim: - sToken = string.strip(sToken) + sToken = sToken.strip() + # remove "" if ((bDecode) and (sToken.find("\"") == 0) and \ (sToken.rfind("\"") == len(sToken)-1)): @@ -449,9 +451,9 @@ def generateFooter(): return "</oor:component-data>\n" # can merge different xml fragments together. -# +# # @author Caolan McNamara converted from the original java by Andreas Schluens -# +# class Merger: def __init__(self, aCfg): self.m_aCfg = aCfg @@ -472,6 +474,7 @@ class Merger: aFcfg = ConfigHelper(self.m_aCfg.getValue(PROP_FCFG), None) self.m_lFilters = aFcfg.getStringList(PROP_ITEMS, sDelimiter, bTrim, bDecode) except: + print( "Filters are empty" ) self.m_lFilters = [] try: @@ -489,8 +492,8 @@ class Merger: def merge(self): sPackage = self.m_aCfg.getValue(PROP_PKG) - print "create package \""+sPackage+"\" ..." - print "generate package header ... " + print("create package \""+sPackage+"\" ...") + print("generate package header ... ") sBuffer = generateHeader(\ self.m_aCfg.getValue(PROP_XMLVERSION ),\ @@ -511,58 +514,60 @@ class Merger: try: if i == 0: #types - print "generate set for types ... " + print("generate set for types ... ") sSetName = self.m_aCfg.getValue(PROP_SETNAME_TYPES) sSubDir = self.m_aCfg.getValue(PROP_SUBDIR_TYPES ) lFragments = self.m_lTypes elif i == 1: # filters - print "generate set for filter ... " + print("generate set for filter ... ") sSetName = self.m_aCfg.getValue(PROP_SETNAME_FILTERS) sSubDir = self.m_aCfg.getValue(PROP_SUBDIR_FILTERS ) lFragments = self.m_lFilters elif i == 2: # loaders - print "generate set for frame loader ... " + print("generate set for frame loader ... ") sSetName = self.m_aCfg.getValue(PROP_SETNAME_LOADERS) sSubDir = self.m_aCfg.getValue(PROP_SUBDIR_LOADERS ) lFragments = self.m_lLoaders elif i == 3: # handlers - print "generate set for content handler ... " + print("generate set for content handler ... ") sSetName = self.m_aCfg.getValue(PROP_SETNAME_HANDLERS) sSubDir = self.m_aCfg.getValue(PROP_SUBDIR_HANDLERS ) lFragments = self.m_lHandlers except: continue + print("Length of Fragments: {f} Set Name {setname} Subdir {subdir}". + format(f=len(lFragments),setname=sSetName,subdir=sSubDir)) + #sys.stdin.readline() nItemCount = nItemCount + len(lFragments) sBuffer = sBuffer + self.getFragments(\ os.path.join(self.m_aFragmentsDir, sSubDir), \ sSetName, lFragments, 1) - print "generate package footer ... " + print("generate package footer ... ") sBuffer = sBuffer + generateFooter() # Attention! - # If the package seem to be empty, it make no sense to generate a - # corresponding xml file. We should suppress writing of this file on - # disk completly ... + # If the package seems to be empty, it make no sense to generate a + # corresponding xml file. We should suppress writing of this file on + # disk completely ... if nItemCount < 1: - print "Package is empty and will not result into a xml file on "\ - "disk!? Please check configuration file." + print("Package is empty and will not result into an xml file on disk!? Please check configuration file.") return - print "package contains "+str(nItemCount)+" items" + print("package contains "+str(nItemCount)+" items") - aPackage = open(sPackage, 'w') - print "write temp package \""+sPackage + aPackage = open(sPackage, mode="w") + print("write temp package {pkg}".format(pkg=sPackage)) aPackage.write(sBuffer) def getFragments(self, aDir, sSetName, lFragments, nPrettyTabs): - sBuffer = '' + sBuffer = '' sExtXcu = self.m_aCfg.getValue(PROP_EXTENSION_XCU); if len(lFragments) < 1: return sBuffer - + for tabs in range(nPrettyTabs): sBuffer = sBuffer + "\t" sBuffer = sBuffer + "<node oor:name=\""+sSetName+"\">\n" @@ -576,9 +581,9 @@ class Merger: # handle simple files only and check for existence! raise Exception("fragment \""+sFragPath+"\" does not exists.") - print "merge fragment \""+sFragPath+"\" ..." + print("merge fragment \""+sFragPath+"\" ...") sBuffer = sBuffer + aFragmentFile.read() - + sBuffer = sBuffer + "\n" nPrettyTabs = nPrettyTabs - 1 @@ -588,4 +593,3 @@ class Merger: return sBuffer run(sys.argv) -