프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / DTI_PID / SymbolAttr.py @ deb37242

이력 | 보기 | 이력해설 | 다운로드 (5.92 KB)

1
# coding: utf-8
2
"""
3
    This is Symbol Attribute module
4
"""
5

    
6
class SymbolProp:
7
    """ This is symbol property class """
8

    
9
    def __init__(self, UID, Attribute, AttributType, Freeze=False, DisplayAttribute=None, Length=None, Expression=None):
10
        import uuid
11

    
12
        self.UID = uuid.uuid4() if UID is None else uuid.UUID(UID, version=4)
13
        self.Freeze = Freeze
14
        self.Attribute = Attribute
15
        self.AttributeType = AttributType
16
        self.DisplayAttribute = DisplayAttribute if DisplayAttribute else Attribute
17
        self.Length = Length 
18
        self.Expression = Expression 
19

    
20
    @property
21
    def is_selectable(self):
22
        """ return if attribute is selectable """
23
        from SymbolAttrEditorDialog import QSymbolAttrEditorDialog
24

    
25
        return (self.AttributeType in [key for key,value in QSymbolAttrEditorDialog.SYMBOL_ATTR_DATA_TYPES.items() if value == -1])
26

    
27
    def match_type(self, item):
28
        """ check if given item's attribute type matches """
29

    
30
        from EngineeringAbstractItem import QEngineeringAbstractItem
31
        from SymbolSvgItem import SymbolSvgItem
32
        from EngineeringLineItem import QEngineeringLineItem
33

    
34
        if self.AttributeType == 'Comp Item':
35
            return issubclass(type(item), SymbolSvgItem) or type(item) is QEngineeringLineItem
36
        elif self.AttributeType == QEngineeringAbstractItem.assoc_type(item):
37
            return True
38

    
39
        return False
40

    
41
    def parse_xml(self, node):
42
        """ parse xml node for property """
43
        import uuid
44
        
45
        self.UID = uuid.UUID(node.attrib['UID'], version=4)
46
        self.Freeze = node.attrib['Freeze'] == 'True' if 'Freeze' in node.attrib else False
47
        self.Attribute = node.attrib['Attribute']
48
        self.AttributeType = node.attrib['AttributeType']
49
        self.DisplayAttribute = node.attrib['DisplayAttribute']
50
        self.Expression = node.attrib['Expression']
51
        self.Length = node.attrib['Length']
52

    
53
    @staticmethod
54
    def is_valid_uuid(value, version=4):
55
        import uuid
56
        """
57
        Check if uuid_to_test is a valid UUID.
58

59
        Parameters
60
        ----------
61
        uuid_to_test : str
62
        version : {1, 2, 3, 4}
63

64
        Returns
65
        -------
66
        `True` if uuid_to_test is a valid UUID, otherwise `False`.
67

68
        Examples
69
        --------
70
        >>> is_valid_uuid('c9bf9e57-1685-4c89-bafb-ff5af830be8a')
71
        True
72
        >>> is_valid_uuid('c9bf9e58')
73
        False
74
        """
75
        try:
76
            uuid_obj = uuid.UUID(value, version=version)
77
        except:
78
            return False
79

    
80
        return str(uuid_obj) == value 
81

    
82
    def parse_value(self, text):
83
        """ parse value of property """
84
        import uuid
85

    
86
        if self.AttributeType == 'Boolean':
87
            return True if text and text == 'True' else False
88
        else:
89
            return uuid.UUID(text, version=4) if text and SymbolProp.is_valid_uuid(text) else text if text else ''
90

    
91
    def toXml(self):
92
        """ generate xml code for symbol property """
93
        from xml.etree.ElementTree import Element, SubElement, dump, ElementTree
94

    
95
        node = Element('PROPERTY')
96
        node.attrib['UID'] = str(self.UID) if self.UID else ''
97
        node.attrib['Freeze'] = str(self.Freeze)
98
        node.attrib['Attribute'] = self.Attribute if self.Attribute is not None else ''
99
        node.attrib['AttributeType'] = self.AttributeType if self.AttributeType is not None else ''
100
        node.attrib['DisplayAttribute'] = self.DisplayAttribute if self.DisplayAttribute else ''
101
        node.attrib['Expression'] = self.Expression if self.Expression else ''
102
        node.attrib['Length'] = str(self.Length) if self.Length else ''
103

    
104
        return node
105

    
106
class SymbolAttr(SymbolProp):
107
    """ This is symbol attribute class """
108
    def __init__(self):
109
        import uuid
110
        SymbolProp.__init__(self, None, None, None)
111

    
112
        self.UID = uuid.uuid4()
113
        self.Freeze = False
114
        self.Attribute = None
115
        self.DisplayAttribute = None
116
        self.AttributeType = None
117
        self.AttrAt = None
118
        self.Expression = None
119
        self.Target = None
120
        self.Length = None
121
        self.AssocItem = None
122
        self.IsProp = None
123

    
124
    @staticmethod
125
    def fromXml(node):
126
        """ generate SymbolAttr instance from xml node """
127
        import uuid
128

    
129
        attr = SymbolAttr()
130
        attr.UID = uuid.UUID(node.attrib['UID'], version=4) if 'UID' in node.attrib and node.attrib['UID'] != '' else uuid.uuid4()
131
        attr.Freeze = node.attrib['Freeze'] == 'True' if 'Freeze' in node.attrib else False
132
        attr.Attribute = node.attrib['Attribute']
133
        attr.DisplayAttribute = node.attrib['DisplayAttribute']
134
        attr.AttributeType = node.attrib['AttributeType']
135
        attr.AttrAt = node.attrib['AttrAt']
136
        attr.Expression = node.attrib['Expression']
137
        attr.Length = node.attrib['Length']
138
        attr.AssocItem = uuid.UUID(node.attrib['AssocItem'], version=4) if 'AssocItem' in node.attrib and node.attrib['AssocItem'] != '' else None
139

    
140
        return attr
141

    
142
    def toXml(self):
143
        """ generate xml code for symbol attribute """
144
        from xml.etree.ElementTree import Element, SubElement, dump, ElementTree
145

    
146
        node = Element('ATTRIBUTE')
147
        node.attrib['Freeze'] = str(self.Freeze)
148
        node.attrib['UID'] = str(self.UID) if self.UID is not None else ''
149
        node.attrib['Attribute'] = self.Attribute if self.Attribute is not None else ''
150
        node.attrib['DisplayAttribute'] = self.DisplayAttribute if self.DisplayAttribute is not None else ''
151
        node.attrib['AttributeType'] = self.AttributeType if self.AttributeType is not None else ''
152
        node.attrib['AttrAt'] = str(self.AttrAt) if self.AttrAt is not None else ''
153
        node.attrib['Expression'] = self.Expression if self.Expression is not None else ''
154
        node.attrib['Length'] = str(self.Length) if self.Length is not None else ''
155
        node.attrib['AssocItem'] = str(self.AssocItem) if self.AssocItem is not None else ''
156

    
157
        return node
클립보드 이미지 추가 (최대 크기: 500 MB)