Module process_includes
[hide private]
[frames] | no frames]

Source Code for Module process_includes

  1  #!/usr/bin/env python 
  2  # 
  3  # This file belongs to the generateDS project. 
  4  # 
  5  # -*- mode: pymode; coding: latin1; -*- 
  6  """ 
  7  1. Synopsis: 
  8   
  9  Recusively process the include elements in an XML Schema file. 
 10  Produce a single file that contains all included content. 
 11  Input is read either from a file or from stdin. 
 12  Output is written either to a file or to stdout. 
 13   
 14  2. Usage: 
 15   
 16  python process_includes.py [options] [ infile [ outfile ] ] 
 17   
 18  3. Options: 
 19  -h, --help      Display this help message. 
 20  -f, --force     Force.  If outfile exists, overwrite without asking. 
 21   
 22  4. Examples: 
 23   
 24  python process_includes.py infile.xsd 
 25  python process_includes.py infile.xsd outfile.xsd 
 26  python process_includes.py infile.xsd > outfile.xsd 
 27  cat infile.py | python process_includes.py > outfile.xsd 
 28   
 29  """ 
 30   
 31  # 
 32  # Imports 
 33   
 34  import sys 
 35  import os 
 36  import getopt 
 37  import re 
 38  import types 
 39   
 40  WhichElementTree = '' 
 41  try: 
 42      from xml.etree import ElementTree as etree 
 43      WhichElementTree = 'xml.etree' 
 44  except ImportError, e: 
 45      try: 
 46          from lxml import etree 
 47          WhichElementTree = 'lxml' 
 48      except ImportError, e: 
 49          try: 
 50              from elementtree import ElementTree as etree 
 51              WhichElementTree = 'elementtree' 
 52          except ImportError, e: 
 53              print '***' 
 54              print '*** Error: Must install >=Python-2.5 or ElementTree or lxml.' 
 55              print '***' 
 56              raise 
 57  #print WhichElementTree, etree 
 58   
 59   
 60  # 
 61  # Globals and constants 
 62   
 63  FORCE = False 
 64  NAMESPACE_PAT = re.compile(r'\{.*\}') 
 65   
 66   
 67  # 
 68  # Classes 
 69   
 70   
 71   
 72  # 
 73  # Functions 
 74   
 75   
76 -def process_include(inpath, outpath):
77 # if inpath: 78 # doc = etree.parse(inpath) 79 # root = doc.getroot() 80 # process_include_tree(root) 81 # else: 82 # s1 = sys.stdin.read() 83 # root = etree.fromstring(s1) 84 # process_include_tree(root) 85 # doc = etree.ElementTree(root) 86 # if outpath: 87 # outfile = make_file(outpath) 88 # if outfile: 89 # doc.write(outfile) 90 # outfile.close() 91 # else: 92 # doc.write(sys.stdout) 93 94 if inpath: 95 doc = etree.parse(inpath) 96 root = doc.getroot() 97 process_include_tree(root) 98 else: 99 s1 = sys.stdin.read() 100 root = etree.fromstring(s1) 101 process_include_tree(root) 102 doc = etree.ElementTree(root) 103 if outpath: 104 outfile = make_file(outpath) 105 if outfile: 106 doc.write("tmp_" + outfile.name) 107 fTemp = open("tmp_" + outfile.name) 108 strContent = fTemp.read() 109 strContent = str.replace(strContent, "ns0", "xs", -1) 110 outfile.write(strContent) 111 outfile.close() 112 else: 113 doc.write(sys.stdout)
114 115
116 -def process_include_tree(root):
117 idx = 0 118 children = root.getchildren() 119 while idx < len(children): 120 child = children[idx] 121 tag = child.tag 122 123 if type(tag) == types.StringType: 124 tag = NAMESPACE_PAT.sub("", tag) 125 else: 126 tag = None 127 if tag == 'include' and 'schemaLocation' in child.attrib: 128 root.remove(child) 129 path = child.attrib['schemaLocation'] 130 if os.path.exists(path): 131 doc = etree.parse(path) 132 node = doc.getroot() 133 process_include_tree(node) 134 children1 = node.getchildren() 135 for child1 in children1: 136 root.insert(idx, child1) 137 idx += 1 138 else: 139 process_include_tree(child) 140 idx += 1 141 children = root.getchildren()
142 143
144 -def make_file(outFileName):
145 global FORCE 146 outFile = None 147 # if (not FORCE) and os.path.exists(outFileName): 148 # reply = raw_input('File %s exists. Overwrite? (y/n): ' % outFileName) 149 # if reply == 'y': 150 # outFile = open(outFileName, 'w') 151 # else: 152 # outFile = open(outFileName, 'w') 153 154 outFile = open(outFileName, 'w') 155 156 return outFile
157 158 159 USAGE_TEXT = __doc__ 160
161 -def usage():
162 print USAGE_TEXT 163 sys.exit(-1)
164 165
166 -def main():
167 global FORCE 168 args = sys.argv[1:] 169 try: 170 opts, args = getopt.getopt(args, 'hf', ['help', 'force', ]) 171 except Exception: 172 usage() 173 name = 'nobody' 174 for opt, val in opts: 175 if opt in ('-h', '--help'): 176 usage() 177 elif opt in ('-f', '--force'): 178 FORCE = True 179 if len(args) == 2: 180 inpath = args[0] 181 outpath = args[1] 182 elif len(args) == 1: 183 inpath = args[0] 184 outpath = None 185 elif len(args) == 0: 186 inpath = None 187 outpath = None 188 else: 189 usage() 190 process_include(inpath, outpath)
191 192 193 if __name__ == '__main__': 194 #import pdb; pdb.set_trace() 195 main() 196