Permalink
Browse files

My colleagues have insisted that code generation in setup.py is a Bad…

… Idea.
  • Loading branch information...
1 parent 5ef32ed commit 67f052662bea751cb0b8116b6114b43bb183985a Steven Buss committed Apr 6, 2012
Showing with 70 additions and 48 deletions.
  1. +1 −48 setup.py
  2. +69 −0 tigershark/tools/generate_all_parsers.py
View
@@ -1,55 +1,8 @@
-import os
from distutils.core import setup
-from distutils.command.build_py import build_py
-import fnmatch
-import logging
-import zipfile
from tigershark import __version__
-class GenerateParsers(build_py):
- """ After building tigershark, use it to build the parsers.
-
- This happens before everything in the build directory is copied to
- site-packages, so this effectively installs global default parsers.
- """
- def run(self):
- build_py.run(self)
- from tigershark.tools import convertPyX12
-
- def convert(filename):
- data_ele_file = zipf.open("pyx12-1.5.0/map/dataele.xml")
- codes_file = zipf.open("pyx12-1.5.0/map/codes.xml")
- fname = "M%s.py" % filename.rsplit('/', 1)[1].rsplit('.', 1)[0].\
- replace(".", "_")
- dest_fname = os.path.join(self.build_lib, "tigershark", "parsers",
- fname)
- parser_name = "parsed_%s" % \
- filename.rsplit('/', 1)[1].split('.', 1)[0]
- convertPyX12.writeFile(dest_fname, parser_name,
- convertPyX12.convertFile(zipf.open(filename), data_ele_file,
- codes_file))
- # ZipFile file-like objects don't support seek, so we have to
- # close and re-open them every time :\
- data_ele_file.close()
- codes_file.close()
-
- zipf = zipfile.ZipFile("Downloads/pyx12-1.5.0.zip")
-
- filenames = [f for f in zipf.namelist() if "map" in f and "xml" in f]
- for filename in filenames:
- if fnmatch.fnmatch(filename.rsplit('/', 1)[1],
- "[0-9][0-9][0-9]*.4010.X*.xml"):
- print(filename)
- try:
- convert(filename)
- except Exception as e:
- raise e
- print(e)
- logging.error(e)
-
-
setup(
name='TigerShark',
version=__version__,
@@ -82,7 +35,7 @@ def convert(filename):
],
scripts=[
'tigershark/tools/convertPyX12.py',
+ 'tigershark/tools/generate_all_parsers.py',
],
package_data={'tigershark': ['tests/*.txt', 'tests/*.xml', 'tests/*.csv']},
- cmdclass={"build_py": GenerateParsers},
)
@@ -0,0 +1,69 @@
+#!/usr/bin/python
+"""
+Generate all parsers from PyX12-1.5.0.zip
+
+Example Usage:
+ python generate_all_parsers.py ../../Downloads/pyx12-1.5.0.zip
+"""
+import argparse
+import fnmatch
+import logging
+import os
+import sys
+import zipfile
+
+from tigershark.tools import convertPyX12
+
+
+def convert_from_zip(zip_file_path, dest_path, structure):
+ """ Convert all of the xml files in a zip. """
+ zipf = zipfile.ZipFile(zip_file_path)
+
+ filenames = [f for f in zipf.namelist() if "map" in f and "xml" in f]
+ for filename in filenames:
+ if fnmatch.fnmatch(filename.rsplit('/', 1)[1],
+ "[0-9][0-9][0-9]*.4010.X*.xml"):
+ try:
+ data_ele_file = zipf.open("pyx12-1.5.0/map/dataele.xml")
+ codes_file = zipf.open("pyx12-1.5.0/map/codes.xml")
+ spec_file = zipf.open(filename)
+ dest_fname = os.path.join(dest_path, "M%s.py") % \
+ filename.rsplit('/', 1)[1].rsplit('.', 1)[0].\
+ replace(".", "_")
+ parser_name = "parsed_%s" % \
+ filename.rsplit('/', 1)[1].split('.', 1)[0]
+
+ logging.info("Converting file: {filename} to {dest}".format(
+ filename=filename, dest=dest_fname))
+ convertPyX12.writeFile(dest_fname, parser_name,
+ convertPyX12.convertFile(spec_file, data_ele_file,
+ codes_file),
+ structure=structure)
+ # ZipFile file-like objects don't support seek, so we have to
+ # close and re-open them every time :\
+ spec_file.close()
+ data_ele_file.close()
+ codes_file.close()
+ except Exception as e:
+ raise e
+ logging.error(e)
+
+if __name__ == "__main__":
+ logging.basicConfig(stream=sys.stdout, level=logging.INFO)
+ parser = argparse.ArgumentParser(
+ description="Convert all PyX12 XML files to python modules.")
+ parser.add_argument('zip_file_path', help="The path to the zip file "\
+ "containing the pyX12 X12 spec files to convert")
+ parser.add_argument('-d', '--destination_path', default="parsers",
+ help="The destination path. Defaults to './parsers/'.")
+ parser.add_argument('-s', '--structure', choices=['flat', 'nested'],
+ default="flat", help="The structure of the resulting python "\
+ "class. Nested is easier to read, but may not compile "\
+ "due to too many object instantiations in a single "\
+ "call.")
+ args = parser.parse_args()
+ try:
+ os.makedirs(args.destination_path)
+ except:
+ pass
+ convert_from_zip(args.zip_file_path, args.destination_path, args.structure)

0 comments on commit 67f0526

Please sign in to comment.