Crossfire Server, Branch 1.12  R12190
arch2xml.py
Go to the documentation of this file.
00001 #arch2xml - Todd Mitchell - work in progress - use if you like
00002 #usage : python arch2xml.py <directory> <xml file name>
00003 #FYI - the Walk function is very useful for other python programs as well
00004 
00005 import fnmatch, os, string
00006 
00007 #Flexable Directory Walker from the Python CookBook
00008 
00009 def Walk( root, recurse=0, pattern='*', return_folders=0 ):
00010 
00011 
00012         # initialize
00013         result = []
00014 
00015         # must have at least root folder
00016         try:
00017                 names = os.listdir(root)
00018         except os.error:
00019                 return result
00020 
00021         # expand pattern
00022         pattern = pattern or '*'
00023         pat_list = string.splitfields( pattern , ';' )
00024 
00025         # check each file
00026         for name in names:
00027                 fullname = os.path.normpath(os.path.join(root, name))
00028 
00029                 # grab if it matches our pattern and entry type
00030                 for pat in pat_list:
00031                         if fnmatch.fnmatch(name, pat):
00032                                 if os.path.isfile(fullname) or (return_folders and os.path.isdir(fullname)):
00033                                         result.append(fullname)
00034                                 continue
00035 
00036                 # recursively scan other folders, appending results
00037                 if recurse:
00038                         if os.path.isdir(fullname) and not os.path.islink(fullname):
00039                                 result = result + Walk( fullname, recurse, pattern, return_folders )
00040 
00041         return result
00042 
00043 def arch2xml(root,filename,xsl_file='cfarches.xsl'):
00044     files = Walk(root, 1, '*.arc', 1)
00045     print 'searching for arch files in %s' %root
00046     xml = open(filename,'w')
00047     xml.write('<?xml version="1.0"?>\n<?xml-stylesheet type="text/xsl" href="%s"?>\n<ARCHES>'%xsl_file)
00048     for file in files:
00049             arc = open(file,'r')
00050             contents = arc.read().split('\n')
00051             xml.write('<arch>\n')
00052             mess = 0
00053             for line in contents:
00054                     xp = line.split()
00055                     if mess == 1 and len(xp)>1:
00056                             str = string.join(xp[0:])
00057                             xml.write('%s\n' %str)
00058                     elif len(xp) == 1:
00059                             tag = string.lower(xp[0])
00060                             if tag == 'end':
00061                                     tag = '     <END />\n'
00062                             elif tag == 'more':
00063                                     tag = '     <MORE />\n'
00064                             elif tag =='msg':
00065                                     tag = '     <message>\n'
00066                                     mess = 1
00067                             elif tag =='endmsg':
00068                                     tag = '     </message>\n'
00069                                     mess = 0
00070                             elif tag == 'anim':
00071                                     tag = '     <anim>\n'
00072                             elif tag =='mina':
00073                                     tag = '\n     </anim>\n'
00074                             else:
00075                                     tag = '[%s]'%(tag)
00076                             xml.write('%s' %(tag))
00077                     elif len(xp)>1:
00078                             tag = string.lower(xp[0])
00079                             if (tag[0] == "#"):
00080                                 str = string.join(xp)[1:]
00081                                 xml.write('     <comment>%s</comment>\n' %(str))
00082                             else:
00083                                 str = string.join(xp[1:])
00084                                 xml.write('     <%s>%s</%s>\n' %(tag,str,tag))
00085             xml.write('\n</arch>\n')
00086             arc.close()
00087     xml.write('\n</ARCHES>')
00088     xml.close()
00089     print "DONE"
00090 
00091 if __name__ == '__main__':
00092     import sys
00093     if len(sys.argv) < 3:
00094         sys.stderr.write ('Converts arc files in a directory and all sub directories to an xml file\nUsage: arch2xml.py <directory> <XML-filename>\n')
00095         sys.exit()
00096     else:
00097         arch2xml(sys.argv[1],sys.argv[2])