Beispiel #1
0
    def _getFields(self, xmlClass: Element) -> PyutFields:
        """
        Extracts fields from a DOM element that represents a UML class

        Args:
            xmlClass:
                The DOM version of a UML class

        Returns:
            PyutFields
        """
        pyutFields: PyutFields = cast(PyutFields, [])

        for element in xmlClass.getElementsByTagName(PyutXmlConstants.ELEMENT_MODEL_FIELD):

            xmlField:   Element  = cast(Element, element)
            pyutField: PyutField = PyutField()

            strVis: str                = xmlField.getAttribute(PyutXmlConstants.ATTR_VISIBILITY)
            vis:    PyutVisibilityEnum = PyutVisibilityEnum.toEnum(strVis)

            pyutField.setVisibility(vis)
            xmlParam: Element = xmlField.getElementsByTagName(PyutXmlConstants.ELEMENT_MODEL_PARAM)[0]

            if xmlParam.hasAttribute(PyutXmlConstants.ATTR_DEFAULT_VALUE):
                pyutField.setDefaultValue(xmlParam.getAttribute(PyutXmlConstants.ATTR_DEFAULT_VALUE))
            pyutField.setName(xmlParam.getAttribute(PyutXmlConstants.ATTR_NAME))

            pyutType: PyutType = PyutType(xmlParam.getAttribute(PyutXmlConstants.ATTR_TYPE))
            pyutField.setType(pyutType)

            pyutFields.append(pyutField)

        return pyutFields
Beispiel #2
0
    def _onMethodOk(self, event: Event):
        """
        When button OK from dlgEditMethod is clicked.

        Args:
            event:
        """
        self._pyutMethod.name = self._txtName.GetValue()
        modifiers: PyutModifiers = PyutModifiers([])
        for aModifier in self._txtModifiers.GetValue().split():
            modifiers.append(PyutModifier(aModifier))
        self._pyutMethod.setModifiers(modifiers)

        returnType: PyutType = PyutType(self._txtReturn.GetValue())
        self._pyutMethod.setReturns(returnType)
        self._pyutMethod.setParams(self._pyutMethodCopy.getParams())

        if self._editInterface is False:
            visStr: str = self._rdbVisibility.GetStringSelection()
            visibility: PyutVisibilityEnum = PyutVisibilityEnum.toEnum(visStr)
            self._pyutMethod.setVisibility(visibility)

        self._pyutMethod.sourceCode = self._pyutMethodCopy.sourceCode

        self._setProjectModified()
        # Close dialog
        self.EndModal(OK)
    def _testEnum(self, expectedValue: PyutVisibilityEnum, stringToTest: str,
                  assertMessage: str):

        actualValue: PyutVisibilityEnum = PyutVisibilityEnum.toEnum(
            stringToTest)

        self.assertEqual(expectedValue, actualValue, assertMessage)
Beispiel #4
0
    def _onFieldOk(self, event):
        """
        Activated when button OK from dlgEditField is clicked.

        Args:
            event:  Associated event
        """

        self.fieldToEdit.setName(self._txtFieldName.GetValue().strip())
        from org.pyut.model.PyutType import PyutType

        self.fieldToEdit.setType(
            PyutType(self._txtFieldType.GetValue().strip()))
        visStr: str = self._rdbFieldVisibility.GetStringSelection()
        vis: PyutVisibilityEnum = PyutVisibilityEnum.toEnum(visStr)
        self.fieldToEdit.setVisibility(vis)

        if self._txtFieldDefault.GetValue().strip() != "":
            self.fieldToEdit.setDefaultValue(
                self._txtFieldDefault.GetValue().strip())
        else:
            self.fieldToEdit.setDefaultValue(None)

        # Tell window that its data has been modified
        fileHandling = self._ctrl.getFileHandling()
        project = fileHandling.getCurrentProject()

        if project is not None:
            project.setModified()

        # Close dialog
        self.EndModal(OK)
Beispiel #5
0
    def _onFieldOk (self, event):
        """
        Activated when button OK from dlgEditField is clicked.

        Args:
            event:  Associated event
        """

        self._fieldToEdit.setName(self._txtFieldName.GetValue().strip())
        from org.pyut.model.PyutType import PyutType

        self._fieldToEdit.setType(PyutType(self._txtFieldType.GetValue().strip()))
        visStr: str = self._rdbFieldVisibility.GetStringSelection()
        vis:    PyutVisibilityEnum = PyutVisibilityEnum.toEnum(visStr)
        self._fieldToEdit.setVisibility(vis)

        if self._txtFieldDefault.GetValue().strip() != "":
            self._fieldToEdit.setDefaultValue(self._txtFieldDefault.GetValue().strip())
        else:
            self._fieldToEdit.setDefaultValue(None)

        self._setProjectModified()

        # Close dialog
        self.EndModal(OK)
Beispiel #6
0
    def _xmiVisibility2PyutVisibility(self, visibility: str = "private") -> PyutVisibilityEnum:
        """
        Translates Xmi visibility string to a Pyut visibility enumeration

        Args:
            visibility: the string 'public', 'prviate', or 'protected'

        Returns:  The appropriate enumeration value
        """
        retEnum: PyutVisibilityEnum = PyutVisibilityEnum.toEnum(visibility)
        return retEnum
Beispiel #7
0
    def _getMethods(self, xmlClass: Element) -> PyutMethods:
        """
        Converts XML methods to `PyutMethod`s
        Args:
            xmlClass:  A DOM element that is a UML Class

        Returns:
            A list of `PyutMethod`s associated with the class
        """
        allMethods: PyutMethods = cast(PyutMethods, [])
        for xmlMethod in xmlClass.getElementsByTagName(
                PyutXmlConstants.ELEMENT_MODEL_METHOD):

            pyutMethod: PyutMethod = PyutMethod(
                xmlMethod.getAttribute(PyutXmlConstants.ATTR_NAME))

            strVis: str = xmlMethod.getAttribute(
                PyutXmlConstants.ATTR_VISIBILITY)
            vis: PyutVisibilityEnum = PyutVisibilityEnum.toEnum(strVis)
            pyutMethod.setVisibility(visibility=vis)

            returnElt: Element = xmlMethod.getElementsByTagName(
                PyutXmlConstants.ELEMENT_MODEL_RETURN)[0]
            retTypeStr: str = returnElt.getAttribute(
                PyutXmlConstants.ATTR_TYPE)
            retType: PyutType = PyutType(retTypeStr)
            pyutMethod.setReturns(retType)

            #
            #  Code supports multiple modifiers, but the dialog allows input of only one
            #
            modifiers: NodeList = xmlMethod.getElementsByTagName(
                PyutXmlConstants.ELEMENT_MODEL_MODIFIER)
            for xmlModifier in modifiers:
                xmlModifier: Element = cast(Element, xmlModifier)
                modName: str = xmlModifier.getAttribute(
                    PyutXmlConstants.ATTR_NAME)

                pyutModifier: PyutModifier = PyutModifier(modName)
                pyutMethod.addModifier(pyutModifier)

            methodParameters = []
            for xmlParam in xmlMethod.getElementsByTagName(
                    PyutXmlConstants.ELEMENT_MODEL_PARAM):
                methodParameters.append(self._getParam(xmlParam))

            pyutMethod.setParams(methodParameters)

            allMethods.append(pyutMethod)

        return allMethods
Beispiel #8
0
    def deserialize(cls, serializedData: str,
                    pyutObject: PyutClassCommon) -> PyutClassCommon:
        """

        Args:
            serializedData:
            pyutObject:

        Returns:  The updated PyutClass or PyutInterface

        """

        classDescription = deTokenize("classDescription", serializedData)

        pyutObject.description = classDescription

        methods = eval(deTokenize("methods", serializedData))

        pyutMethods: List[PyutMethod] = []

        for methodProfile in methods:

            # construction of a method
            methodName: str = methodProfile[0]
            methodVisibility: PyutVisibilityEnum = PyutVisibilityEnum.toEnum(
                methodProfile[1])
            methodReturns: PyutType = PyutType(value=methodProfile[2])

            pyutMethod: PyutMethod = PyutMethod(name=methodName,
                                                visibility=methodVisibility,
                                                returns=methodReturns)

            # deserialize method's parameters;  Get the tuple (name, Type, defaultValue)
            params = eval(methodProfile[3])
            for param in params:
                paramName: str = param[0]
                paramType: PyutType = PyutType(param[1])
                paramDefaultValue: str = param[2]

                # creates and add the param to the method
                pyutParam: PyutParam = PyutParam(
                    name=paramName,
                    parameterType=paramType,
                    defaultValue=paramDefaultValue)
                pyutMethod.addParam(pyutParam)

            pyutMethods.append(pyutMethod)

        pyutObject.methods = pyutMethods
        return pyutObject
Beispiel #9
0
    def __safeVisibilityToName(
            self, visibility: Union[str, PyutVisibilityEnum]) -> str:
        """
        Account for old pre V10 code
        Args:
            visibility:

        Returns:
            The visibility name
        """

        if isinstance(visibility, str):
            visStr: str = PyutVisibilityEnum.toEnum(visibility).name
        else:
            visStr: str = visibility.name

        return visStr
    def testToEnumProtectedValue(self):

        val: PyutVisibilityEnum = PyutVisibilityEnum.toEnum('#')
        self.assertEqual(PyutVisibilityEnum.PROTECTED, val,
                         'Protected to enum value fail')
    def testToEnumPublicValue(self):

        val: PyutVisibilityEnum = PyutVisibilityEnum.toEnum('+')
        self.assertEqual(PyutVisibilityEnum.PUBLIC, val,
                         'Public to enum value fail')
    def testToEnumProtectedName(self):

        val: PyutVisibilityEnum = PyutVisibilityEnum.toEnum('protected')
        self.assertEqual(PyutVisibilityEnum.PROTECTED, val,
                         'protected to enum fail')
    def testToEnumPrivateName(self):

        val: PyutVisibilityEnum = PyutVisibilityEnum.toEnum('private')
        self.assertEqual(PyutVisibilityEnum.PRIVATE, val,
                         'Private to enum fail')
    def testToEnumPublicName(self):

        val: PyutVisibilityEnum = PyutVisibilityEnum.toEnum('public')
        self.assertEqual(PyutVisibilityEnum.PUBLIC, val,
                         'Public to enum name fail')
Beispiel #15
0
    def read(self, umlObject, file):
        """
        Read data from file

        format:
        ```Python
        class name
        <<stereotype_optional>>
        +method([param[:type]]*)[:type_return]
        +field[:type][=value_initial]
        ```

        for example:

        ParentClass
        +strMethod(strParam : str = bogus) : str
        +intMethod(intParam = 1) : int
        +floatField : float = 1.0
        +booleanField : bool = True

        Args:
            umlObject:
            file:
        """
        className: str = file.readline().strip()
        pyutClass: PyutClass = umlObject.getPyutObject()
        pyutClass.setName(className)

        # process stereotype if present
        nextStereoType: str = file.readline().strip()
        if nextStereoType[0:2] == "<<":
            pyutClass.setStereotype(nextStereoType[2:-2].strip())
            nextStereoType = file.readline().strip()

        methods = []
        fields = []
        pyutClass.methods = methods
        pyutClass.fields = fields

        # process methods and fields
        visValues: List[str] = PyutVisibilityEnum.values()
        while True:
            if nextStereoType == "":
                break

            # search visibility

            if nextStereoType[0] in visValues:
                visStr = nextStereoType[0]
                vis: PyutVisibilityEnum = PyutVisibilityEnum.toEnum(visStr)
                nextStereoType = nextStereoType[1:]
            else:
                vis: PyutVisibilityEnum = PyutVisibilityEnum.PUBLIC

            pos = nextStereoType.find("(")
            params = []
            if pos != -1:
                # process method
                name = nextStereoType[0:pos].strip()
                nextStereoType = nextStereoType[pos + 1:]
                pos = nextStereoType.find(")")

                # TODO return typ should be PyutType
                returnType: str = ""
                if pos != -1:
                    params = self._findParams(nextStereoType[:pos])
                    nextStereoType = nextStereoType[pos + 1:]
                    pos = nextStereoType.find(":")

                    if pos != -1:
                        returnType = nextStereoType[pos + 1:].strip()
                    else:
                        returnType = ""
                method = PyutMethod(name, vis, returnType)

                method.setParams([PyutParam(x[0], x[1]) for x in params])
                methods.append(method)
            else:
                # process field
                field = self._findParams(nextStereoType)[0]
                if field:
                    fields.append(PyutField(field[0], field[1],
                                            visibility=vis))

            nextStereoType = file.readline().strip()
    def testToEnumPrivateValue(self):

        val: PyutVisibilityEnum = PyutVisibilityEnum.toEnum('-')
        self.assertEqual(PyutVisibilityEnum.PRIVATE, val,
                         'Private to enum value fail')
Beispiel #17
0
    def deserialize(self, serializedData):
        """
        Deserialize the data needed by the deleted OglCass

        Args:
            serializedData: serialized data needed by the command.
        """
        from org.pyut.model.PyutMethod import PyutMethod
        from org.pyut.model.PyutParam import PyutParam
        from org.pyut.model.PyutField import PyutField

        from org.pyut.model.PyutStereotype import PyutStereotype
        from org.pyut.model.PyutModifier import PyutModifier

        # deserialize the data common to all OglObjects
        DeleteOglLinkedObjectCommand.deserialize(self, serializedData)

        # deserialize properties of the OglClass (first level)
        classDescription = deTokenize("classDescription", serializedData)
        classStereotypeName = deTokenize("classStereotypeName", serializedData)
        classShowStereotype = eval(
            deTokenize("classShowStereotype", serializedData))
        classShowMethods = eval(deTokenize("classShowMethods", serializedData))
        classShowFields = eval(deTokenize("classShowFields", serializedData))

        methods = eval(deTokenize("methods", serializedData))
        fields = eval(deTokenize("fields", serializedData))

        # set up the first level properties of the pyutClass
        pyutClass: PyutClass = self._shape.getPyutObject()
        pyutClass.description = classDescription

        if cmp(classStereotypeName, ""):
            pyutStereo = PyutStereotype(classStereotypeName)
            pyutClass.setStereotype(pyutStereo)

        pyutClass.setShowStereotype(classShowStereotype)
        pyutClass.showMethods = classShowMethods
        pyutClass.showFields = classShowFields

        for field in fields:

            fieldName = field[0]
            fieldType = field[1]
            fieldDefaultValue = field[2]
            fieldVisibility = field[3]
            pyutClass.addField(
                PyutField(fieldName, fieldType, fieldDefaultValue,
                          fieldVisibility))

        methodsList = []
        # deserialize methods of the pyutClass
        for methodProfile in methods:

            # construction of a method
            methodName: str = methodProfile[0]
            methodVisibility: PyutVisibilityEnum = PyutVisibilityEnum.toEnum(
                methodProfile[1])
            methodReturns: PyutType = PyutType(value=methodProfile[2])

            method = PyutMethod(name=methodName,
                                visibility=methodVisibility,
                                returns=methodReturns)

            # deserialize method's params so we get a tuple (name, Type, defaultValue)
            params = eval(methodProfile[3])
            for param in params:
                paramName = param[0]

                # construction of the type of the param
                paramType = param[1]
                # pyutType = PyutType(paramType)   Not used
                paramDefaultValue = param[2]

                # creates and add the param to the method
                method.addParam(
                    PyutParam(paramName, paramType, paramDefaultValue))

            # deserialize method's modifiers so we get a list of names
            # that we have to transform into a list of PyutModifiers.
            modifiersNames = eval(methodProfile[4])
            modifiers = []
            for modifierName in modifiersNames:
                modifiers.append(PyutModifier(modifierName))

            # add the modifiers to the method
            method.setModifiers(modifiers)
            # add the method to the list of methods
            methodsList.append(method)

        # add all the methods to the list
        pyutClass.methods = methodsList