home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / fnb101.zip / Lib / site-packages / Fnorb / compiler / IDLCompiler.py < prev    next >
Text File  |  1999-06-28  |  6KB  |  168 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/IDLCompiler.py,v $
  29. # Version:      @(#)$RCSfile: IDLCompiler.py,v $ $Revision: 1.7 $
  30. #
  31. #############################################################################
  32. """ The back-end of an IDL compiler for Fnorb! """
  33.  
  34.  
  35. # Standard/built-in modules.
  36. import os
  37.  
  38. # Fnorb modules.
  39. from Fnorb.orb import CORBA
  40.  
  41. # Compiler modules.
  42. import SkelGenerator, StubGenerator
  43.  
  44.  
  45. class Context:
  46.     """ Context specifying the locations of generated files and packages. """
  47.  
  48.     def __init__(self, directory, package, globals):
  49.         """ Constructor.
  50.  
  51.         'directory' is the directory in which to put all of the files and
  52.                 packages generated by the compiler.
  53.  
  54.         'package'   is the name of the package under 'directory' in which to
  55.                 put all of the packages generated by the compiler (this
  56.             parameter must be an instance of the 'Util.PackageName'
  57.             class).
  58.  
  59.         'globals'   is the base name of the sub-packages under 'package' in
  60.                 which to put the stubs and skeletons generated for global
  61.             scope IDL definitions.
  62.  
  63.         """
  64.         self._directory = directory
  65.         self._package = package
  66.     self._globals = globals
  67.  
  68.     return
  69.  
  70.     def create_package_path(self, package):
  71.     """ Create the full pathname of the specified package. """
  72.  
  73.     # Create a valid filename relative to the context's target directory.
  74.     return os.path.join(self._directory, package.join('/'))
  75.  
  76.     def create_import_statement(self, package):
  77.     """ Create a statement to import the specified package.
  78.  
  79.     N.B This method will work for both PackageName instances AND
  80.         --- stringified package names!
  81.  
  82.     """
  83.     if len(self._package) == 0:
  84.         statement = 'import %s' % package
  85.  
  86.     else:
  87.         statement = 'from %s import %s' % (self._package, package)
  88.  
  89.     return statement
  90.     
  91.     #########################################################################
  92.     # Accessors.
  93.     #########################################################################
  94.  
  95.     def directory(self):
  96.     return self._directory
  97.  
  98.     def package(self):
  99.     return self._package
  100.  
  101.     def globals(self):
  102.     return self._globals
  103.  
  104.  
  105. class IDLCompiler:
  106.     """ The back-end of an IDL compiler for Fnorb! """
  107.  
  108.     def compile(self, context, ifr_objects):
  109.     """ Generate code for a list of IR objects.
  110.  
  111.     'context'     is the compilation context giving the directory and 
  112.                       package names in which to put the files and packages
  113.                       generated by the compiler.
  114.     
  115.         'ifr_objects' is a list containing the object references of the IFR
  116.                       objects that we want to generate Python code for.
  117.  
  118.         """
  119.     # Create the stub and skeleton code generators to do the work!
  120.     stub_gen = StubGenerator.StubGenerator()
  121.     skel_gen = SkelGenerator.SkelGenerator()
  122.  
  123.     # If there are global scope definitions that are not modules then we
  124.     # create Python stub and skeleton packages to put them in.
  125.     if self.__have_global_defs(ifr_objects):
  126.         globals     = context.globals()
  127.         global_stub = stub_gen._create_package(context, globals)
  128.         global_skel = skel_gen._create_package(context, globals + '_skel')
  129.  
  130.         # Generate Python for each IFR object.
  131.         for ifr_object in ifr_objects:
  132.         stub_gen.generate(context, global_stub, ifr_object, 0)
  133.         skel_gen.generate(context, global_skel, ifr_object, 0)
  134.  
  135.         # End of the packages.
  136.         stub_gen._end_package(global_stub, 0)
  137.         skel_gen._end_package(global_skel, 0)
  138.  
  139.         global_stub.close()
  140.         global_skel.close()
  141.  
  142.     # Otherwise, there is at least some semblance of order, and there are
  143.     # no global scope definitions other than modules ;^)
  144.     else:
  145.         # Generate Python for each IFR object.
  146.         for ifr_object in ifr_objects:
  147.         stub_gen.generate(context, None, ifr_object, 0)
  148.         skel_gen.generate(context, None, ifr_object, 0)
  149.  
  150.     return
  151.  
  152.     #########################################################################
  153.     # Internal interface.
  154.     #########################################################################
  155.  
  156.     def __have_global_defs(self, ifr_objects):
  157.     """ Are any of the IFR objects NOT module definitions? """
  158.  
  159.     for ifr_object in ifr_objects:
  160.         if ifr_object._get_def_kind() != CORBA.dk_Module:
  161.         return 1
  162.  
  163.     return 0
  164.  
  165. #############################################################################
  166.