home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / fnb101.zip / Lib / site-packages / Fnorb / compiler / StubGenerator.py < prev   
Text File  |  1999-06-28  |  23KB  |  725 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/StubGenerator.py,v $
  29. # Version:      @(#)$RCSfile: StubGenerator.py,v $ $Revision: 1.9 $
  30. #
  31. #############################################################################
  32. """ Python stub generator for CORBA IDL. """
  33.  
  34.  
  35. # Standard/built-in modules.
  36. import string
  37.  
  38. # Fnorb modules.
  39. from Fnorb.orb import CORBA, Util
  40.  
  41. # Compiler modules.
  42. import CodeGenerator
  43.  
  44.  
  45. class StubGenerator(CodeGenerator.CodeGenerator):
  46.     """ Python stub generator for CORBA IDL. """
  47.  
  48.     #########################################################################
  49.     # CodeGenerator protected interface.
  50.     #########################################################################
  51.  
  52.     def _generate_attribute(self, context, file, ifr_object, indent):
  53.     """ Generate Python code for an IDL attribute. """
  54.  
  55.     # Get the description of the attribute.
  56.     description = ifr_object.describe()
  57.     attribute_desc = description.value.value()
  58.  
  59.         #####################################################################
  60.     # Accessor.
  61.         #####################################################################
  62.  
  63.     # Method header.
  64.     file.write(self._indent(indent))
  65.     file.write('def _get_%s(self):\n' % attribute_desc.name)
  66.  
  67.     # Indent.
  68.     indent = indent + 1
  69.  
  70.     # Documentation string.
  71.     file.write(self._indent(indent))
  72.     file.write('""" Attribute: %s """\n\n' % attribute_desc.id)
  73.  
  74.     # Create the output typecode.
  75.     file.write(self._indent(indent))
  76.     file.write('# Typecode for the attribute value.\n')
  77.     file.write(self._indent(indent))
  78.     file.write('outputs = []\n')
  79.     file.write(self._indent(indent))
  80.     file.write('outputs.append(')
  81.     file.write(self._get_typecode(attribute_desc.type))
  82.     file.write(')\n\n')
  83.  
  84.     # Boiler plate.
  85.     file.write(self._indent(indent))
  86.     file.write('# Create a request object.\n')
  87.     file.write(self._indent(indent))
  88.     file.write('request = self._create_request(')
  89.     file.write('"_get_%s", ' % attribute_desc.name)
  90.     file.write('[], outputs, [])\n\n')
  91.     file.write(self._indent(indent))
  92.     file.write('# Make the request!\n')
  93.     file.write(self._indent(indent))
  94.     file.write('request.invoke()\n\n')
  95.  
  96.     file.write(self._indent(indent))
  97.     file.write('# Return the attribute value.\n')
  98.     file.write(self._indent(indent))
  99.     file.write('return request.results()\n\n')
  100.  
  101.     # Outdent.
  102.     indent = indent - 1
  103.  
  104.         #####################################################################
  105.     # Modifier (unless the attribute is read only ;^).
  106.         #####################################################################
  107.  
  108.     if attribute_desc.mode != CORBA.ATTR_READONLY:
  109.         # Method header.
  110.         file.write(self._indent(indent))
  111.         file.write('def _set_%s(self, _value):\n' % attribute_desc.name)
  112.  
  113.         # Indent.
  114.         indent = indent + 1
  115.  
  116.         # Documentation string.
  117.         file.write(self._indent(indent))
  118.         file.write('""" Attribute: %s """\n\n' % attribute_desc.id)
  119.  
  120.         # Create the input typecodes.
  121.         file.write(self._indent(indent))
  122.         file.write("# Typecode for the attribute value.\n")
  123.         file.write(self._indent(indent))
  124.         file.write('inputs = []\n')
  125.  
  126.         file.write(self._indent(indent))
  127.         file.write('inputs.append(')
  128.         file.write(self._get_typecode(attribute_desc.type))
  129.         file.write(')\n\n')
  130.  
  131.         # Boiler plate.
  132.         file.write(self._indent(indent))
  133.         file.write('# Create a request object.\n')
  134.         file.write(self._indent(indent))
  135.         file.write('request = self._create_request(')
  136.         file.write('"_set_%s", ' % attribute_desc.name)
  137.         file.write('inputs, [], [])\n\n')
  138.         file.write(self._indent(indent))
  139.         file.write('# Make the request!\n')
  140.         file.write(self._indent(indent))
  141.         file.write('request.invoke(_value)\n\n')
  142.  
  143.         file.write(self._indent(indent))
  144.         file.write('return\n\n')
  145.  
  146.     return
  147.  
  148.     def _generate_constant(self, context, file, ifr_object, indent):
  149.     """ Generate Python code to represent an IDL constant. """
  150.  
  151.     # Get the description of the constant.
  152.     description = ifr_object.describe()
  153.     const_desc = description.value.value()
  154.  
  155.     # Make sure that the constant name is not a Python keyword.
  156.     py_const_name = Util.python_name(const_desc.name)
  157.  
  158.     # Repository id comment.
  159.     file.write(self._indent(indent))
  160.     file.write('# Constant: %s\n' % const_desc.id)
  161.  
  162.     # Create and initialise a corresponding Python variable.
  163.     file.write(self._indent(indent))
  164.     file.write('%s = %s' % (py_const_name, repr(const_desc.value.value())))
  165.     file.write('\n\n')
  166.  
  167.     # Get the constant's typecode.
  168.     typecode = const_desc.type
  169.  
  170.     # Add a typecode constant to the stub.
  171.     self._write_typecode_constant(file, indent, const_desc.id, typecode,
  172.                       py_const_name)
  173.  
  174.     return
  175.  
  176.     def _generate_exception(self, context, file, ifr_object, indent):
  177.     """ Generate Python code to represent an IDL exception. """
  178.  
  179.     # Get the description of the exception.
  180.     description = ifr_object.describe()
  181.     exception_desc = description.value.value()
  182.  
  183.     # Make sure that the structure name is not a Python keyword.
  184.     py_exception_name = Util.python_name(exception_desc.name)
  185.  
  186.     # Get the members of the exception.
  187.     members = ifr_object._get_members()
  188.  
  189.     # Class header (inheriting from CORBA.UserException).
  190.     file.write(self._indent(indent))
  191.     file.write('class %s(Fnorb.orb.CORBA.UserException):\n' \
  192.            % py_exception_name)
  193.     # Indent.
  194.     indent = indent + 1
  195.  
  196.     # Documentation string.
  197.     file.write(self._indent(indent))
  198.     file.write('""" Exception: %s """\n\n' % exception_desc.id)
  199.  
  200.     # Repository id attribute.
  201.     file.write(self._indent(indent))
  202.     file.write('_FNORB_ID = "%s"\n\n' % exception_desc.id)
  203.  
  204.     # Create a constructor parameter for every member of the exception.
  205.     parameters = ['self']
  206.     for member in members:
  207.         parameters.append('_' + member.name)
  208.  
  209.     # Method header for the constructor.
  210.     file.write(self._indent(indent))
  211.     file.write('def __init__(%s):\n' % string.join(parameters, ', '))
  212.  
  213.     # Indent.
  214.     indent = indent + 1
  215.  
  216.     # Documentation string.
  217.     file.write(self._indent(indent))
  218.     file.write('""" Constructor. """\n\n')
  219.  
  220.     # Initialise every member of the exception in the constructor body.
  221.     for member in members:
  222.         # Make sure that the member name is not a Python keyword.
  223.         py_member_name = Util.python_name(member.name)
  224.         file.write(self._indent(indent))
  225.         file.write("self.%s = _%s\n" % (py_member_name, member.name))
  226.  
  227.     # Return statement.
  228.     file.write(self._indent(indent))
  229.     file.write('return\n\n')
  230.         
  231.     # Outdent.
  232.     indent = indent - 1
  233.  
  234.     # Create the __getinitargs__ method.
  235.     if len(members) > 0:
  236.         # Method header.
  237.         file.write(self._indent(indent))
  238.         file.write('def __getinitargs__(self):\n')
  239.  
  240.         # Indent.
  241.         indent = indent + 1
  242.  
  243.         # Documentation string.
  244.         file.write(self._indent(indent))
  245.         file.write('""" ')
  246.         file.write('Return the constructor arguments for unpickling.')
  247.         file.write(' """\n\n')
  248.  
  249.         # Special case for the singleton tuple ;^)
  250.         if len(members) == 1:
  251.         # Make sure that the member name is not a Python keyword.
  252.         py_member_name = Util.python_name(members[0].name)
  253.         file.write(self._indent(indent))
  254.         file.write('return (self.%s,)\n\n' % py_member_name) 
  255.  
  256.         else:
  257.         results = []
  258.         for member in members:
  259.             # Make sure that the member name is not a Python keyword.
  260.             py_member_name = Util.python_name(member.name)
  261.             results.append('self.%s' % py_member_name)
  262.  
  263.         file.write(self._indent(indent))
  264.         file.write('return (%s)\n\n' % string.join(results, ', '))
  265.  
  266.         # Outdent.
  267.         indent = indent - 1
  268.         
  269.     # Outdent.
  270.     indent = indent - 1
  271.  
  272.     # Get the exceptions's typecode.
  273.     typecode = ifr_object._get_type()
  274.  
  275.     # Add a typecode constant to the stub.
  276.     self._write_typecode_constant(file, indent, exception_desc.id,
  277.                       typecode, py_exception_name)
  278.  
  279.     return
  280.  
  281.     def _generate_interface(self, context, file, ifr_object, indent):
  282.     """ Generate Python code to represent an IDL interface. """
  283.  
  284.     # Get the description of the interface.
  285.     description = ifr_object.describe()
  286.     interface_desc = description.value.value()
  287.  
  288.     # Make sure that the interface name is not a Python keyword.
  289.     py_interface_name = Util.python_name(interface_desc.name)
  290.  
  291.     # Get the scoped name of the interface.
  292.     interface_scoped_name =Util.ScopedName(ifr_object._get_absolute_name())
  293.  
  294.     # Fix up any clashes with Python keywords.
  295.     interface_scoped_name.pythonise()
  296.  
  297.     # Base interfaces.
  298.     bases = ['Fnorb.orb.CORBA.Object']
  299.     packages = []
  300.  
  301.     for base in ifr_object._get_base_interfaces():
  302.         # Get the scoped name of the base interface.
  303.         base_scoped_name = Util.ScopedName(base._get_absolute_name())
  304.  
  305.         # Fix up any clashes with Python keywords.
  306.         base_scoped_name.pythonise()
  307.  
  308.         # If the base interface is defined in a different IDL module (ie.
  309.         # in a different Python package).
  310.         if base_scoped_name[:-1] != interface_scoped_name[:-1]:
  311.         # If the base interface is defined at the global scope then
  312.         # add the name of the global package.
  313.         if len(base_scoped_name) == 1:
  314.             base_scoped_name.insert(0, context.globals())
  315.  
  316.         # Use the full scoped name in the Python class header.
  317.         base_python_name = base_scoped_name.join('.')
  318.  
  319.         # Add the Python package that the base interface is defined in
  320.         # to the list of packages to import.
  321.         packages.append(base_scoped_name[:-1].join('.'))
  322.  
  323.         # Otherwise, the base interface is defined in the same IDL module
  324.         # as the interface itself, so we just use the base interface name
  325.         # in the Python class header.
  326.         else:
  327.         base_python_name = base_scoped_name[-1]
  328.  
  329.         # Add to the list of base classes for the class header.
  330.         bases.append(base_python_name)
  331.         
  332.     # Import base interface packages.
  333.     if len(packages) > 0:
  334.         file.write(self._indent(indent))
  335.         file.write('# Import base interface packages.\n')
  336.         for package in packages:
  337.         file.write(self._indent(indent))
  338.         file.write(context.create_import_statement(package))
  339.         file.write('\n')
  340.  
  341.         file.write('\n')
  342.  
  343.     # Class header.
  344.     file.write(self._indent(indent))
  345.     file.write('class %s' % py_interface_name)
  346.     file.write('(%s):\n' % string.join(bases, ', '))
  347.  
  348.     # Indent.
  349.     indent = indent + 1
  350.     
  351.     # Documentation string.
  352.     file.write(self._indent(indent))
  353.     file.write('""" Interface: %s """\n\n' % interface_desc.id)
  354.  
  355.     # Repository id attribute.
  356.     file.write(self._indent(indent))
  357.     file.write('_FNORB_ID = "%s"\n\n' % interface_desc.id)
  358.  
  359.     # Generate code for every definition contained in the interface
  360.     # (ignoring inherited definitions).
  361.     contents = ifr_object.contents(CORBA.dk_all, 1)
  362.     if len(contents) > 0:
  363.         for contained in contents:
  364.         self.generate(context, file, contained, indent)
  365.  
  366.     # Empty interface.
  367.     else:
  368.         file.write(self._indent(indent))
  369.         file.write('pass\n\n')
  370.  
  371.     # Outdent.
  372.     indent = indent - 1
  373.  
  374.     # Get the interface's typecode.
  375.     typecode = ifr_object._get_type()
  376.  
  377.     # Add a typecode constant to the stub.
  378.     self._write_typecode_constant(file, indent, interface_desc.id,
  379.                       typecode, py_interface_name)
  380.  
  381.     return
  382.  
  383.     def _generate_module(self, context, file, ifr_object, indent):
  384.     """ Generate Python code to represent an IDL module. """
  385.  
  386.     # Get the description of the module.
  387.     description = ifr_object.describe()
  388.     module_desc = description.value.value()
  389.  
  390.     # Get the scoped name of the module.
  391.     package = Util.ScopedName(ifr_object._get_absolute_name())
  392.  
  393.     # Fix up any clashes with Python keywords.
  394.     package.pythonise()
  395.  
  396.     # Create a Python package to represent the IDL module.
  397.     file = self._create_package(context, package.join('/'), module_desc.id)
  398.  
  399.     # Generate code for every definition contained in the module.
  400.     for contained in ifr_object.contents(CORBA.dk_all, 0):
  401.         self.generate(context, file, contained, 0)
  402.  
  403.     # End of the package.
  404.     self._end_package(file, indent)
  405.  
  406.     # All done!
  407.     file.close()
  408.  
  409.     return
  410.  
  411.     def _generate_operation(self, context, file, ifr_object, indent):
  412.     """ Generate Python code to represent an IDL operation. """
  413.  
  414.     # Get the description of the operation.
  415.     description = ifr_object.describe()
  416.     operation_desc = description.value.value()
  417.  
  418.     # Make sure that the operation name is not a Python keyword.
  419.     py_operation_name = Util.python_name(operation_desc.name)
  420.  
  421.     # Method header.
  422.     file.write(self._indent(indent))
  423.     file.write('def %s(self, *args, **kw):\n' % py_operation_name)
  424.  
  425.     # Indent.
  426.     indent = indent + 1
  427.  
  428.     # Documentation string.
  429.     file.write(self._indent(indent))
  430.     file.write('""" Operation: %s """\n\n' % operation_desc.id)
  431.  
  432.     # Create the input typecodes.
  433.     file.write(self._indent(indent))
  434.     file.write("# Typecodes for 'in' and 'inout' parameters.\n")
  435.     file.write(self._indent(indent))
  436.     file.write('inputs = []\n')
  437.  
  438.     for p in operation_desc.parameters:
  439.         if p.mode == CORBA.PARAM_IN or p.mode == CORBA.PARAM_INOUT:
  440.         file.write(self._indent(indent))
  441.         file.write('inputs.append(%s)' % self._get_typecode(p.type))
  442.         file.write('\n')
  443.  
  444.     file.write('\n')
  445.         
  446.     # Create the output typecodes.
  447.     file.write(self._indent(indent))
  448.     file.write("# Typecodes for the result, 'inout' and 'out' parameters.")
  449.     file.write('\n')
  450.     file.write(self._indent(indent))
  451.     file.write('outputs = []\n')
  452.  
  453.     # The result.
  454.     if operation_desc.result.kind() != CORBA.tk_void:
  455.         file.write(self._indent(indent))
  456.         file.write('outputs.append(')
  457.         file.write(self._get_typecode(operation_desc.result))
  458.         file.write(')\n')
  459.  
  460.     # 'inout' and 'out' parameters.
  461.     for p in operation_desc.parameters:
  462.         if p.mode == CORBA.PARAM_INOUT or p.mode == CORBA.PARAM_OUT:
  463.         file.write(self._indent(indent))
  464.         file.write('outputs.append(%s)' % self._get_typecode(p.type))
  465.         file.write('\n')
  466.  
  467.     file.write('\n')
  468.  
  469.     # Create the exception typecodes.
  470.     file.write(self._indent(indent))
  471.     file.write("# Typecodes for user exceptions.\n")
  472.     file.write(self._indent(indent))
  473.     file.write('exceptions = []\n')
  474.  
  475.     for ex in operation_desc.exceptions:
  476.         file.write(self._indent(indent))
  477.         file.write('exceptions.append(Fnorb.orb.CORBA.typecode("%s"))\n' \
  478.                % ex.id)
  479.  
  480.     file.write('\n')
  481.     
  482.     # Boiler plate.
  483.     file.write(self._indent(indent))
  484.     file.write('# Create a request object.\n')
  485.     file.write(self._indent(indent))
  486.     file.write('request = self._create_request("')
  487.     file.write(operation_desc.name)
  488.     file.write('", inputs, outputs, exceptions)\n\n')
  489.     file.write(self._indent(indent))
  490.     file.write('# Make the request!\n')
  491.  
  492.     if operation_desc.mode == CORBA.OP_ONEWAY:
  493.         file.write(self._indent(indent))
  494.         file.write('apply(request.invoke_oneway, args, kw)\n\n')
  495.         file.write(self._indent(indent))
  496.         file.write('return\n\n')
  497.     
  498.     else:
  499.         file.write(self._indent(indent))
  500.         file.write('apply(request.invoke, args, kw)\n\n')
  501.         file.write(self._indent(indent))
  502.         file.write('# Return the results.\n')
  503.         file.write(self._indent(indent))
  504.         file.write('return request.results()\n\n')
  505.  
  506.     return
  507.             
  508.     def _generate_struct(self, context, file, ifr_object, indent):
  509.     """ Generate Python code to represent an IDL structure. """
  510.  
  511.     # Get the description of the structure.
  512.     description = ifr_object.describe()
  513.     struct_desc = description.value.value()
  514.  
  515.     # Make sure that the structure name is not a Python keyword.
  516.     py_struct_name = Util.python_name(struct_desc.name)
  517.  
  518.     # Get the members of the structure.
  519.     members = ifr_object._get_members()
  520.  
  521.     # Class header.
  522.     file.write(self._indent(indent))
  523.     file.write('class %s:\n' % py_struct_name)
  524.  
  525.     # Indent.
  526.     indent = indent + 1
  527.  
  528.     # Documentation string.
  529.     file.write(self._indent(indent))
  530.     file.write('""" Struct: %s """\n\n' % struct_desc.id)
  531.  
  532.     # Repository id attribute.
  533.     file.write(self._indent(indent))
  534.     file.write('_FNORB_ID = "%s"\n\n' % struct_desc.id)
  535.  
  536.     # Create a constructor parameter for every member of the structure.
  537.     parameters = ['self']
  538.     for member in members:
  539.         parameters.append('_' + member.name)
  540.  
  541.     # Method header for the constructor.
  542.     file.write(self._indent(indent))
  543.     file.write('def __init__(%s):\n' % string.join(parameters, ', '))
  544.  
  545.     # Indent.
  546.     indent = indent + 1
  547.  
  548.     # Documentation string.
  549.     file.write(self._indent(indent))
  550.     file.write('""" Constructor. """\n\n')
  551.  
  552.     # Initialise every member of the structure in the constructor body.
  553.     for member in members:
  554.         # Make sure that the member name is not a Python keyword.
  555.         py_member_name = Util.python_name(member.name)
  556.         file.write(self._indent(indent))
  557.         file.write("self.%s = _%s\n" % (py_member_name, member.name))
  558.  
  559.     # Return statement.
  560.     file.write(self._indent(indent))
  561.     file.write('return\n\n')
  562.         
  563.     # Outdent.
  564.     indent = indent - 1
  565.  
  566.     # Create the __getinitargs__ method.
  567.     if len(members) > 0:
  568.         # Method header.
  569.         file.write(self._indent(indent))
  570.         file.write('def __getinitargs__(self):\n')
  571.  
  572.         # Indent.
  573.         indent = indent + 1
  574.  
  575.         # Documentation string.
  576.         file.write(self._indent(indent))
  577.         file.write('""" ')
  578.         file.write('Return the constructor arguments for unpickling.')
  579.         file.write(' """\n\n')
  580.  
  581.         # Special case for the singleton tuple ;^)
  582.         if len(members) == 1:
  583.         # Make sure that the member name is not a Python keyword.
  584.         py_member_name = Util.python_name(members[0].name)
  585.         file.write(self._indent(indent))
  586.         file.write('return (self.%s,)\n\n' % py_member_name) 
  587.  
  588.         else:
  589.         results = []
  590.         for member in members:
  591.             # Make sure that the member name is not a Python keyword.
  592.             py_member_name = Util.python_name(member.name)
  593.             results.append('self.%s' % py_member_name)
  594.  
  595.         file.write(self._indent(indent))
  596.         file.write('return (%s)\n\n' % string.join(results, ', '))
  597.  
  598.         # Outdent.
  599.         indent = indent - 1
  600.  
  601.     # Outdent.
  602.     indent = indent - 1
  603.  
  604.     # Get the structure's typecode.
  605.     typecode = ifr_object._get_type()
  606.  
  607.     # Add a typecode constant to the stub.
  608.     self._write_typecode_constant(file, indent, struct_desc.id, typecode,
  609.                       py_struct_name)
  610.  
  611.     return
  612.  
  613.     def _generate_union(self, context, file, ifr_object, indent):
  614.     """ Generate Python code to represent an IDL union. """
  615.  
  616.     # Get the description of the structure.
  617.     description = ifr_object.describe()
  618.     union_desc = description.value.value()
  619.  
  620.     # Make sure that the union name is not a Python keyword.
  621.     py_union_name = Util.python_name(union_desc.name)
  622.  
  623.     # Class header.
  624.     file.write(self._indent(indent))
  625.     file.write('class %s(Fnorb.orb.Util.Union):\n' % py_union_name)
  626.  
  627.     # Indent.
  628.     indent = indent + 1
  629.  
  630.     # Documentation string.
  631.     file.write(self._indent(indent))
  632.     file.write('""" Union: %s """\n\n' % union_desc.id)
  633.  
  634.     # Repository id attribute.
  635.     file.write(self._indent(indent))
  636.     file.write('_FNORB_ID = "%s"\n\n' % union_desc.id)
  637.  
  638.     file.write(self._indent(indent))
  639.     file.write('pass\n\n')
  640.  
  641.     # Outdent.
  642.     indent = indent - 1
  643.  
  644.     # Get the unions's typecode.
  645.     typecode = ifr_object._get_type()
  646.  
  647.     # Add a typecode constant to the stub.
  648.     self._write_typecode_constant(file, indent, union_desc.id, typecode,
  649.                       py_union_name)
  650.  
  651.     return
  652.  
  653.     def _generate_enum(self, context, file, ifr_object, indent):
  654.     """ Generate Python code to represent an IDL enumeration. """
  655.  
  656.     # Get the description of the enumeration.
  657.     description = ifr_object.describe()
  658.     enum_desc = description.value.value()
  659.  
  660.     # Make sure that the enumeration name is not a Python keyword.
  661.     py_enum_name = Util.python_name(enum_desc.name)
  662.  
  663.     # Get the members of the enumeration.
  664.     members = ifr_object._get_members()
  665.  
  666.     # Repository id comment.
  667.     file.write(self._indent(indent))
  668.     file.write('# Enum: %s\n' % enum_desc.id)
  669.  
  670.     # Create an Util.EnumMember instance for every member of the
  671.     # enumeration.
  672.     i = 0
  673.     for member in members:
  674.         # Make sure that the member name is not a Python keyword.
  675.         py_member_name = Util.python_name(member)
  676.         file.write(self._indent(indent))
  677.         file.write('%s = ' % py_member_name)
  678.         file.write('Fnorb.orb.Util.EnumMember("%s", %d)\n' % (member, i))
  679.         i = i + 1
  680.  
  681.     # Use the enumeration name to create an instance of the 'Util.Enum'
  682.     # class to emulate a list containing all of the enumeration members.
  683.     file.write(self._indent(indent))
  684.     py_member_names = string.join(map(Util.python_name, members), ', ')
  685.     file.write('%s = Fnorb.orb.Util.Enum("%s", [%s])\n\n' \
  686.            % (py_enum_name, enum_desc.id, py_member_names))
  687.  
  688.     # Get the enum's typecode.
  689.     typecode = ifr_object._get_type()
  690.  
  691.     # Add a typecode constant to the stub.
  692.     self._write_typecode_constant(file, indent, enum_desc.id, typecode,
  693.                       py_enum_name)
  694.            
  695.     return
  696.  
  697.     def _generate_alias(self, context, file, ifr_object, indent):
  698.     """ Generate Python code to represent an IDL alias. """
  699.  
  700.     # Get the description of the alias.
  701.     description = ifr_object.describe()
  702.     alias_desc = description.value.value()
  703.  
  704.     # Repository id comment.
  705.     file.write(self._indent(indent))
  706.     file.write('# Alias: %s\n' % alias_desc.id)
  707.  
  708.     # 'Unwind' the alias until we find a 'real' definition!
  709.     type_def = ifr_object._get_original_type_def()
  710.     while type_def._get_def_kind() == CORBA.dk_Alias:
  711.         type_def = type_def._get_original_type_def()
  712.  
  713.     # Get the 'real' typecode.
  714.     typecode = type_def._get_type()
  715.  
  716.     # Add a typecode constant to the stub.
  717.     self._write_typecode_constant(file, indent, alias_desc.id, typecode,
  718.                       'None')
  719.  
  720.     return
  721.  
  722. #############################################################################
  723.