Crossfire Server, Trunk
arch2xml.py
Go to the documentation of this file.
1 #arch2xml - Todd Mitchell - work in progress - use if you like
2 #usage : python arch2xml.py <directory> <xml file name>
3 #FYI - the Walk function is very useful for other python programs as well
4 
5 import fnmatch, os, string
6 
7 #Flexable Directory Walker from the Python CookBook
8 
9 def Walk( root, recurse=0, pattern='*', return_folders=0 ):
10 
11 
12  # initialize
13  result = []
14 
15  # must have at least root folder
16  try:
17  names = os.listdir(root)
18  except os.error:
19  return result
20 
21  # expand pattern
22  pattern = pattern or '*'
23  pat_list = string.splitfields( pattern , ';' )
24 
25  # check each file
26  for name in names:
27  fullname = os.path.normpath(os.path.join(root, name))
28 
29  # grab if it matches our pattern and entry type
30  for pat in pat_list:
31  if fnmatch.fnmatch(name, pat):
32  if os.path.isfile(fullname) or (return_folders and os.path.isdir(fullname)):
33  result.append(fullname)
34  continue
35 
36  # recursively scan other folders, appending results
37  if recurse:
38  if os.path.isdir(fullname) and not os.path.islink(fullname):
39  result = result + Walk( fullname, recurse, pattern, return_folders )
40 
41  return result
42 
43 
44 #
45 # Function escape(string)
46 #
47 # Creates HTML/XML escaped characters for display in a browser to work correctly
48 #
49 # @param esc_me
50 # The string to escape characters in.
51 #
52 # @return
53 # The equivalent string with escaped versions of important characters.
54 #
55 # @todo
56 # Add more escaped characters as the need arises
57 #
58 def escape(esc_me):
59  # Converting ampersand must be first, since escaping other characters generates ampersands.
60  esc_me = esc_me.replace('&', '&amp;', 4)
61  esc_me = esc_me.replace('<', '&lt;', 3)
62  esc_me = esc_me.replace('>', '&gt;', 3)
63  return esc_me
64 
65 
66 def arch2xml(root,filename,xsl_file='cfarches.xsl'):
67  files = Walk(root, 1, '*.arc', 1)
68  print 'searching for arch files in %s' %root
69  xml = open(filename,'w')
70  xml.write('<?xml version="1.0"?>\n<?xml-stylesheet type="text/xsl" href="%s"?>\n<ARCHES>'%xsl_file)
71  for file in files:
72  arc = open(file,'r')
73  contents = arc.read().split('\n')
74  xml.write('<arch>\n')
75  mess = 0
76  # Need_new_arch is set to 1 when an arch end is found in the middle of a file,
77  # otherwise it is set to 0.
78  need_new_arch = 0
79  for line in contents:
80  xp = line.split()
81  if mess == 1 and len(xp)>1:
82  str = escape(string.join(xp[0:]))
83  xml.write('%s\n' %str)
84  elif len(xp) == 1:
85  tag = string.lower(xp[0])
86  # if an empty comment line, ignore it
87  if tag == '#':
88  continue
89  if tag == 'end':
90  tag = ' <END />'
91  # We reached the end of the arch
92  need_new_arch = 1
93  elif tag == 'more':
94  tag = ' <MORE />'
95  # A quick hack to ensure the arch tags
96  # don't split up separate parts of the
97  # same archetype.
98  need_new_arch = 0
99  elif tag =='msg':
100  tag = ' <message>'
101  mess = 1
102  elif tag =='endmsg':
103  tag = ' </message>'
104  mess = 0
105  elif tag == 'anim':
106  tag = ' <anim>'
107  elif tag =='mina':
108  tag = ' </anim>'
109  else:
110  tag = '[%s]'%(tag)
111  xml.write('%s\n' %(tag))
112  elif len(xp)>1:
113 
114  tag = string.lower(xp[0])
115  if (tag[0] == "#"):
116  str = string.join(xp)[1:]
117  xml.write(' <comment>%s</comment>\n' %(escape(str)))
118  else:
119  # A quick and dirty hack to make multiple independent arches
120  # in a single .arc file each get their own arch tag.
121  #
122  # TODO: Make a couple .arc files that consist entirely
123  # of comments not receive their own arch tag.
124  #
125  # -- SilverNexus 2015-05-21
126  #
127  if need_new_arch == 1:
128  xml.write('\n</arch>\n<arch>\n')
129  need_new_arch = 0
130 
131  str = string.join(xp[1:])
132  xml.write(' <%s>%s</%s>\n' %(tag,str,tag))
133  xml.write('\n</arch>\n')
134  arc.close()
135  xml.write('\n</ARCHES>')
136  xml.close()
137  print "DONE"
138 
139 if __name__ == '__main__':
140  import sys
141  if len(sys.argv) < 3:
142  sys.stderr.write ('Converts arc files in a directory and all sub directories to an xml file\nUsage: arch2xml.py <directory> <XML-filename>\n')
143  sys.exit()
144  else:
145  arch2xml(sys.argv[1],sys.argv[2])
CFBank.open
def open()
Definition: CFBank.py:69
arch2xml
Definition: arch2xml.py:1
arch2xml.escape
def escape(esc_me)
Definition: arch2xml.py:58
arch2xml.arch2xml
def arch2xml(root, filename, xsl_file='cfarches.xsl')
Definition: arch2xml.py:66
arch2xml.Walk
def Walk(root, recurse=0, pattern=' *', return_folders=0)
Definition: arch2xml.py:9
split
static std::vector< std::string > split(const std::string &field, const std::string &by)
Definition: mapper.cpp:2606