Exemple #1
0
 def _init_obj_attrs(self, obj):
     """
     Initialize obj attributes.
     Args:
         obj(object): A python object to set attributes to.
     """
     for attr in obj.__class__._tx_attrs.values():
         if attr.mult in [MULT_ZEROORMORE, MULT_ONEORMORE]:
             # list
             setattr(obj, attr.name, [])
         elif attr.cls.__name__ in BASE_TYPE_NAMES:
             # Instantiate base python type
             if self.auto_init_attributes:
                 setattr(obj, attr.name,
                         python_type(attr.cls.__name__)())
             else:
                 # See https://github.com/textX/textX/issues/11
                 if attr.bool_assignment:
                     # Only ?= assignments shall have default
                     # value of False.
                     setattr(obj, attr.name, False)
                 else:
                     # Set base type attribute to None initially
                     # in order to be able to detect if an optional
                     # values are given in the model. Default values
                     # can be specified using object processors.
                     setattr(obj, attr.name, None)
         else:
             # Reference to other obj
             setattr(obj, attr.name, None)
Exemple #2
0
    def _init_obj_attrs(self, obj, user=False):
        """
        Initialize obj attributes.
        Args:
            obj(object): A python object to set attributes to.
            user(bool): If this object is a user object mangle attribute names.
        """
        for attr in obj.__class__._tx_attrs.values():

            if user:
                # Mangle name to prvent name clashing
                attr_name = "_txa_%s" % attr.name
            else:
                attr_name = attr.name

            if attr.mult in [MULT_ZEROORMORE, MULT_ONEORMORE]:
                # list
                setattr(obj, attr_name, [])
            elif attr.cls.__name__ in BASE_TYPE_NAMES:
                # Instantiate base python type
                if self.auto_init_attributes:
                    setattr(obj, attr_name,
                            python_type(attr.cls.__name__)())
                else:
                    # See https://github.com/textX/textX/issues/11
                    if attr.bool_assignment:
                        # Only ?= assignments shall have default
                        # value of False.
                        setattr(obj, attr_name, False)
                    else:
                        # Set base type attribute to None initially
                        # in order to be able to detect if an optional
                        # values are given in the model. Default values
                        # can be specified using object processors.
                        setattr(obj, attr_name, None)
            else:
                # Reference to other obj
                setattr(obj, attr_name, None)