프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

hytos / DTI_PID / DTI_PID / SymbolAttr.py @ 0b04ae07

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

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

    
4
class SymbolProp:
5
    """ This is symbol property class """
6

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

    
10
        self.UID = uuid.uuid4() if UID is None else uuid.UUID(UID)
11
        self.Freeze = Freeze
12
        self.Attribute = Attribute
13
        self.AttributeType = AttributType
14
        self.DisplayAttribute = DisplayAttribute if DisplayAttribute else Attribute
15
        self.Length = Length 
16
        self.Expression = Expression 
17

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

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

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

    
28
        from EngineeringAbstractItem import QEngineeringAbstractItem
29
        from SymbolSvgItem import SymbolSvgItem
30
        from EngineeringLineItem import QEngineeringLineItem
31

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

    
37
        return False
38

    
39
    def parse_record(self, record):
40
        """ parse record for property """
41
        import uuid
42
        
43
        self.UID = uuid.UUID(record['UID'])
44
        self.Freeze = record['Freeze'] == 'True' if record['Freeze'] else False
45
        self.Attribute = record['Attribute']
46
        self.AttributeType = record['AttributeType']
47
        self.DisplayAttribute = record['DisplayAttribute']
48
        self.Expression = record['Expression']
49
        self.Length = record['Length']
50

    
51
    def parse_xml(self, node):
52
        """ parse xml node for property """
53
        import uuid
54
        
55
        self.UID = uuid.UUID(node.attrib['UID'])
56
        self.Freeze = node.attrib['Freeze'] == 'True' if 'Freeze' in node.attrib else False
57
        self.Attribute = node.attrib['Attribute']
58
        self.AttributeType = node.attrib['AttributeType']
59
        self.DisplayAttribute = node.attrib['DisplayAttribute']
60
        self.Expression = node.attrib['Expression']
61
        self.Length = node.attrib['Length']
62

    
63
    @staticmethod
64
    def is_valid_uuid(value, version=4):
65
        import uuid
66
        """
67
        Check if uuid_to_test is a valid UUID.
68

69
        Parameters
70
        ----------
71
        uuid_to_test : str
72
        version : {1, 2, 3, 4}
73

74
        Returns
75
        -------
76
        `True` if uuid_to_test is a valid UUID, otherwise `False`.
77

78
        Examples
79
        --------
80
        >>> is_valid_uuid('c9bf9e57-1685-4c89-bafb-ff5af830be8a')
81
        True
82
        >>> is_valid_uuid('c9bf9e58')
83
        False
84
        """
85
        try:
86
            uuid_obj = uuid.UUID(value)#, version=version)
87
        except:
88
            return False
89

    
90
        return str(uuid_obj) == value 
91

    
92
    def parse_value(self, text):
93
        """ parse value of property """
94
        import uuid
95

    
96
        if self.AttributeType == 'Boolean':
97
            return True if text and text == 'True' else False
98
        else:
99
            return uuid.UUID(text) if text and SymbolProp.is_valid_uuid(text) else text if text else ''
100

    
101
    def toXml(self):
102
        """ generate xml code for symbol property """
103
        from xml.etree.ElementTree import Element, SubElement, dump, ElementTree
104

    
105
        node = Element('PROPERTY')
106
        node.attrib['UID'] = str(self.UID) if self.UID else ''
107
        node.attrib['Freeze'] = str(self.Freeze)
108
        node.attrib['Attribute'] = self.Attribute if self.Attribute is not None else ''
109
        node.attrib['AttributeType'] = self.AttributeType if self.AttributeType is not None else ''
110
        node.attrib['DisplayAttribute'] = self.DisplayAttribute if self.DisplayAttribute else ''
111
        node.attrib['Expression'] = self.Expression if self.Expression else ''
112
        node.attrib['Length'] = str(self.Length) if self.Length else ''
113

    
114
        return node
115

    
116
class SymbolAttr(SymbolProp):
117
    """ This is symbol attribute class """
118
    def __init__(self):
119
        import uuid
120
        SymbolProp.__init__(self, None, None, None)
121

    
122
        self.UID = uuid.uuid4()
123
        self.Freeze = False
124
        self.Attribute = None
125
        self.DisplayAttribute = None
126
        self.AttributeType = None
127
        self.AttrAt = None
128
        self.Expression = None
129
        self.Target = None
130
        self.Length = None
131
        self.AssocItem = None
132
        self.IsProp = 0  # default value is 0
133
        self.Codes = None
134

    
135
    @staticmethod
136
    def from_record(record):
137
        """ parse record for property """
138
        import uuid
139
        
140
        attr = SymbolAttr()
141
        attr.UID = uuid.UUID(record['UID'])
142
        attr.Freeze = record['Freeze'] == 'True' if record['Freeze'] else False
143
        attr.Attribute = record['Attribute']
144
        attr.DisplayAttribute = record['DisplayAttribute']
145
        attr.AttributeType = record['AttributeType']
146
        attr.AttrAt = record['AttrAt']
147
        attr.Expression = record['Expression']
148
        attr.Length = record['Length'] if 'Length' in record else 0
149
        attr.IsProp = int(record['Property']) if record['Property'] else 0
150
        attr.AssocItem = uuid.UUID(record['Association_UID']) if record['Association_UID'] and record['Association_UID'] != 'None' else None
151

    
152
        return attr
153

    
154
    @staticmethod
155
    def fromXml(node):
156
        """ generate SymbolAttr instance from xml node """
157
        import uuid
158

    
159
        attr = SymbolAttr()
160
        attr.UID = uuid.UUID(node.attrib['UID']) if 'UID' in node.attrib and node.attrib['UID'] != '' else uuid.uuid4()
161
        attr.Freeze = node.attrib['Freeze'] == 'True' if 'Freeze' in node.attrib else False
162
        attr.Attribute = node.attrib['Attribute']
163
        attr.DisplayAttribute = node.attrib['DisplayAttribute']
164
        attr.AttributeType = node.attrib['AttributeType']
165
        attr.AttrAt = node.attrib['AttrAt']
166
        attr.Expression = node.attrib['Expression']
167
        attr.Length = node.attrib['Length']
168
        attr.AssocItem = uuid.UUID(node.attrib['AssocItem']) if 'AssocItem' in node.attrib and node.attrib['AssocItem'] != '' else None
169

    
170
        return attr
171

    
172
    def toXml(self):
173
        """ generate xml code for symbol attribute """
174
        from xml.etree.ElementTree import Element, SubElement, dump, ElementTree
175

    
176
        node = Element('ATTRIBUTE')
177
        node.attrib['Freeze'] = str(self.Freeze)
178
        node.attrib['UID'] = str(self.UID) if self.UID is not None else ''
179
        node.attrib['Attribute'] = self.Attribute if self.Attribute is not None else ''
180
        node.attrib['DisplayAttribute'] = self.DisplayAttribute if self.DisplayAttribute is not None else ''
181
        node.attrib['AttributeType'] = self.AttributeType if self.AttributeType is not None else ''
182
        node.attrib['AttrAt'] = str(self.AttrAt) if self.AttrAt is not None else ''
183
        node.attrib['Expression'] = self.Expression if self.Expression is not None else ''
184
        node.attrib['Length'] = str(self.Length) if self.Length is not None else ''
185
        node.attrib['AssocItem'] = str(self.AssocItem) if self.AssocItem is not None else ''
186

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