home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / fnb101.zip / Lib / site-packages / Fnorb / compiler / CodeGenerator.py next >
Text File  |  1999-06-28  |  8KB  |  251 lines

  1. #!/usr/bin/env python
  2. #############################################################################
  3. # Copyright (C) DSTC Pty Ltd (ACN 052 372 577) 1997, 1998, 1999
  4. # All Rights Reserved.
  5. #
  6. # The software contained on this media is the property of the DSTC Pty
  7. # Ltd.  Use of this software is strictly in accordance with the
  8. # license agreement in the accompanying LICENSE.HTML file.  If your
  9. # distribution of this software does not contain a LICENSE.HTML file
  10. # then you have no rights to use this software in any manner and
  11. # should contact DSTC at the address below to determine an appropriate
  12. # licensing arrangement.
  13. #      DSTC Pty Ltd
  14. #      Level 7, GP South
  15. #      Staff House Road
  16. #      University of Queensland
  17. #      St Lucia, 4072
  18. #      Australia
  19. #      Tel: +61 7 3365 4310
  20. #      Fax: +61 7 3365 4311
  21. #      Email: enquiries@dstc.edu.au
  22. # This software is being provided "AS IS" without warranty of any
  23. # kind.  In no event shall DSTC Pty Ltd be liable for damage of any
  24. # kind arising out of or in connection with the use or performance of
  25. # this software.
  26. #
  27. # Project:      Fnorb
  28. # File:         $Source: /units/arch/src/Fnorb/compiler/RCS/CodeGenerator.py,v $
  29. # Version:      @(#)$RCSfile: CodeGenerator.py,v $ $Revision: 1.8 $
  30. #
  31. #############################################################################
  32. """ Base class for Python code generators for OMG IDL. """
  33.  
  34.  
  35. # Standard/built-in modules.
  36. import errno, os
  37.  
  38. # Fnorb modules.
  39. from Fnorb.orb import CORBA, Util
  40.  
  41.  
  42. class CodeGenerator:
  43.     """ Base class for Python code generators for OMG IDL. """
  44.  
  45.     def generate(self, context, file, ifr_object, indent):
  46.     """ Generate Python code for an Interface Repository object. """
  47.  
  48.     # Find out the kind of definition that the IR object represents.
  49.     def_kind = ifr_object._get_def_kind()
  50.  
  51.     # Attribute.
  52.     if def_kind == CORBA.dk_Attribute:
  53.         self._generate_attribute(context, file, ifr_object, indent)
  54.  
  55.     # Constant.
  56.     elif def_kind == CORBA.dk_Constant:
  57.         self._generate_constant(context, file, ifr_object, indent)
  58.  
  59.     # Exception.
  60.     elif def_kind == CORBA.dk_Exception:
  61.         self._generate_exception(context, file, ifr_object, indent)
  62.  
  63.     # Interface.
  64.     elif def_kind == CORBA.dk_Interface:
  65.         self._generate_interface(context, file, ifr_object, indent)
  66.  
  67.     # Module.
  68.     elif def_kind == CORBA.dk_Module:
  69.         self._generate_module(context, file, ifr_object, indent)
  70.  
  71.     # Operation.
  72.     elif def_kind == CORBA.dk_Operation:
  73.         self._generate_operation(context, file, ifr_object, indent)
  74.  
  75.     # Structure.
  76.     elif def_kind == CORBA.dk_Struct:
  77.         self._generate_struct(context, file, ifr_object, indent)
  78.  
  79.     # Union.
  80.     elif def_kind == CORBA.dk_Union:
  81.         self._generate_union(context, file, ifr_object, indent)
  82.  
  83.     # Enumeration.
  84.     elif def_kind == CORBA.dk_Enum:
  85.         self._generate_enum(context, file, ifr_object, indent)
  86.  
  87.     # Alias.
  88.     elif def_kind == CORBA.dk_Alias:
  89.         self._generate_alias(context, file, ifr_object, indent)
  90.  
  91.     # Anything else, we just ignore ;^)
  92.     else:
  93.         pass
  94.  
  95.     return
  96.  
  97.     #########################################################################
  98.     # Protected interface.
  99.     #########################################################################
  100.  
  101.     def _indent(self, indent):
  102.     """ Produce the indentation for the specified indent level! """
  103.  
  104.     #       1234 ;^)
  105.     return '    ' * indent
  106.  
  107.     def _create_package(self, context, package, module_id=''):
  108.     """ Create a new Python package. """
  109.  
  110.     # Create the full package name relative to the context's package.
  111.     package = context.package() + Util.PackageName(package)
  112.  
  113.     # Make sure that all of the intermediate packages in the path actually
  114.     # exist (and create them if they don't!).
  115.     for i in range(1, len(package)):
  116.         # Create the pathname of the intermediate package relative to the
  117.         # context's target directory.
  118.         package_path = context.create_package_path(package[:i])
  119.  
  120.         # Create the package directory.
  121.         try:
  122.         os.mkdir(package_path)
  123.  
  124.         # If the package already exists then carry on!
  125.             except os.error, (code, message):
  126.         if code != errno.EEXIST:
  127.             raise os.error, (code, message)
  128.  
  129.             # See if the package already has an '__init__.py' module.
  130.             try:
  131.                 file = open('%s/__init__.py' % package_path, 'r')
  132.                 file.close()
  133.  
  134.         # If it doesn't, then create one.
  135.         except IOError:
  136.                 file = open('%s/__init__.py' % package_path, 'w')
  137.                 file.close()
  138.  
  139.     # Create the pathname of the package relative to the context's target
  140.     # directory.
  141.     package_path = context.create_package_path(package)
  142.  
  143.     # Create the package directory.
  144.     try:
  145.         os.mkdir(package_path)
  146.  
  147.     # If the package already exists then carry on!
  148.         except os.error, (code, message):
  149.         if code != errno.EEXIST:
  150.         raise os.error, (code, message)
  151.  
  152.     # Create/open the package's '__init__' module.
  153.     file = open('%s/__init__.py' % package_path, 'w')
  154.  
  155.     # Documentation string.
  156.     file.write('""" Module: %s\n\n' % module_id)
  157.     file.write("Automagically generated by:-\n")
  158.     file.write("\n")
  159.     file.write("%s\n" % CORBA.ORB_ID)
  160.     file.write("\n")
  161.     file.write('"""\n\n')
  162.  
  163.     # Repository id attribute.
  164.     file.write('_FNORB_ID = "%s"\n\n' % module_id)
  165.  
  166.     # Python 'import' statements.
  167.     file.write("# Fnorb modules.\n")
  168.     file.write("import Fnorb.orb.CORBA\n")
  169.     file.write("import Fnorb.orb.TypeManager\n")
  170.     file.write("import Fnorb.orb.Util\n\n")
  171. ##    file.write("from Fnorb.orb import CORBA, TypeManager, Util\n\n")
  172.  
  173.     return file
  174.  
  175.     def _end_package(self, file, indent):
  176.     """ Add any stuff required at the end of the package!
  177.  
  178.     Currently this is just an EOF marker!
  179.  
  180.     """
  181.     # EOF marker.
  182.     file.write(self._indent(indent))
  183.         file.write('#' * 77)
  184.     file.write('\n')
  185.  
  186.     return
  187.  
  188.     def _write_typecode_constant(self, file, indent, ifr_id, tc, name):
  189.     """ Generate code to add a typecode constant. """
  190.  
  191.     # Create the typecode constant.
  192.     typecode_constant = tc._fnorb_to_constant()
  193.  
  194.     # Generate the code to add the type to the Type Manager.
  195.     file.write(self._indent(indent))
  196.     file.write('Fnorb.orb.TypeManager.TypeManager_init().add_type')
  197.     file.write('("%s", "%s", %s)\n\n' % (ifr_id, typecode_constant, name))
  198.  
  199.     return
  200.  
  201.     def _get_typecode(self, typecode):
  202.     """ Get the typecode 'string' for the given typecode. """
  203.  
  204.     # Primitive typecodes have a '_fnorb_code' method that generates the
  205.     # appropriate string.
  206.     if hasattr(typecode, '_fnorb_code'):
  207.         result = typecode._fnorb_code()
  208.  
  209.     # Otherwise, generate a call to get the typecode from the typecode
  210.     # manager.
  211.     else:
  212.         result = 'Fnorb.orb.CORBA.typecode("%s")' % typecode.id()
  213.  
  214.     return result
  215.  
  216.     # Override these methods to generate the code for each IFR object that
  217.     # requires mapping to a Python data structure.
  218.     def _generate_attribute(self, context, file, ifr_object, indent):
  219.     pass
  220.  
  221.     def _generate_constant(self, context, file, ifr_object, indent):
  222.     pass
  223.  
  224.     def _generate_exception(self, context, file, ifr_object, indent):
  225.     pass
  226.  
  227.     def _generate_interface(self, context, file, ifr_object, indent):
  228.     pass
  229.  
  230.     def _generate_module(self, context, file, ifr_object, indent):
  231.     pass
  232.  
  233.     def _generate_operation(self, context, file, ifr_object, indent):
  234.     pass
  235.  
  236.     def _generate_struct(self, context, file, ifr_object, indent):
  237.     pass
  238.  
  239.     def _generate_union(self, context, file, ifr_object, indent):
  240.     pass
  241.  
  242.     def _generate_enum(self, context, file, ifr_object, indent):
  243.     pass
  244.  
  245.     def _generate_alias(self, context, file, ifr_object, indent):
  246.     pass
  247.  
  248. #############################################################################
  249.