Package imaker :: Module ecore |
[hide private]
[frames] | no frames]

# Source Code for Module imaker.ecore

     1  #============================================================================
     2  #Name        : ecore.py
     3  #Part of     : Helium
     4
     5  #Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     6  #All rights reserved.
     7  #This component and the accompanying materials are made available
     8  #under the terms of the License "Eclipse Public License v1.0"
     9  #which accompanies this distribution, and is available
    10  #at the URL "http://www.eclipse.org/legal/epl-v10.html".
    11  #
    12  #Initial Contributors:
    13  #Nokia Corporation - initial contribution.
    14  #
    15  #Contributors:
    16  #
    17  #Description:
    18  #===============================================================================
    19
    20  """ Implements few eclipse ecore XML parser helpers. """
    21  import re
    22
    23 -class Reference(object):
    24      """ Reference to real instance. """
    25
    26 -    def __init__(self, node, reference):
    27          """ The constructor. """
    28          self.reference = reference
    29          self.node = node
    30
    31 -    def __getattr__(self, name):
    32          """ Delegate to the reference object.
    33              So the reference object can behave the
    34              same way as the real object.
    35          """
    36          return getattr(self.instance(), name)
    37
    38 -    def instance(self):
    39          """ Retrieve the real object instance from the object hierarchy. """
    40          ref = self.reference
    41          cnode = self.node
    42          if ref.startswith("//"):
    43              cnode = self.node.get_root()
    44              ref = ref[2:]
    45          for pel in ref.split("/"):
    46              res = re.match(r"@([^.]+)(?:\.(\d+))?", pel)
    47              if res != None:
    48                  name = res.group(1)
    49
    50                  if res.group(2) != None:
    51                      cnode = getattr(cnode, name)[int(res.group(2))]
    52                  else:
    53                      cnode = getattr(cnode, name)
    54              else:
    55                  raise Exception("Invalid reference %s" % ref)
    56          return cnode
    57
    58
    59 -class ContainerBase(object):
    60      """ Container class that implements a parent relationship. """
    61 -    def __init__(self, parent=None):
    62          self.parent = parent
    63
    64 -    def get_root(self):
    65          """ Retrieving the root container. """
    66          if self.parent == None:
    67              return self
    68          else:
    69              return self.parent.get_root()
    70