프로젝트

일반

사용자정보

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

hytos / DTI_PID / DTI_PID / Configs.py @ 4d03a31c

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

1
# coding: utf-8
2
"""
3
    This is Config class
4
"""
5

    
6
from AppDocData import AppDocData
7
from CodeTables import CodeTable
8

    
9
class LineNoConfig:
10
    DELIMITER = '"'
11

    
12
    def __init__(self, unit, delimiter, value):
13
        self.unit = unit
14
        self.delimiter = delimiter
15
        self.value = value
16

    
17
    def parse(self, text):
18
        """ return true if successfully parse given text else return false """
19
        import re
20

    
21
        try:
22
            appDocData = AppDocData.instance()
23
            configs = self.value.split(LineNoConfig.DELIMITER)
24

    
25
            res = []
26

    
27
            _text = text.replace(' ','')
28

    
29
            index = sorted(list(set([_text.find('\n', i) for i in range(len(_text)) if _text.find('\n', i) != -1])), reverse=True)
30
            if index:
31
                _text = _text.replace('\n', '')
32
                
33
            for propertyName in configs:
34
                lineProp = appDocData.getLinePropertiesByUID(propertyName)
35
                # Line property인 경우
36
                if lineProp:
37
                    lineType = lineProp[0].AttributeType
38

    
39
                    if lineType == 'Code Table':
40
                        tableName = lineProp[0].Attribute
41
                        found = CodeTable.instance(tableName).find_starts_with(_text, self.unit)
42
                        if not found: return (False,)
43
                        _text = _text[len(found):len(_text)]
44
                        res.append(found)
45
                    elif lineType == 'Int':
46
                        if lineProp[0].Expression:
47
                            if _text.startswith(lineProp[0].Expresssion):
48
                                _text = _text[len(lineProp[0].Expression):len(_text)]
49
                                res.append(lineProp[0].Expression)
50
                            else:
51
                                return (False,)
52
                        elif lineProp[0].Length:
53
                            _text = _text[:lineProp[0].Length].replace('o', '0').replace('O', '0') + _text[lineProp[0].Length:]
54
                            match = re.search(re.compile('^[0-9]{{{}}}'.format(lineProp[0].Length)), _text)
55
                            if match:
56
                                _text = _text[len(match.group(match.start())):len(_text)]
57
                                res.append(match.group(match.start()))
58
                            else:
59
                                return (False,)
60
                        else:
61
                            match = re.search(re.compile('^[0-9]+'), _text)
62
                            if match:
63
                                _text = _text[len(match.group(match.start())):len(_text)]
64
                                res.append(match.group(match.start()))
65
                            else:
66
                                return (False,)
67
                    elif lineType == 'String':
68
                        if lineProp[0].Expression:
69
                            if lineProp[0].startswith(lineProp[0].Expression):
70
                                text = _text[len(lineProp[0].Expression):len(_text)]
71
                                res.append(lineProp[0].Expression)
72
                            else:
73
                                return (False,)
74
                        elif lineProp[0].Length:
75
                            if len(_text) >= lineProp[0].Length:
76
                                res.append(_text[:lineProp[0].Length])
77
                                _text = _text[lineProp[0].Length:len(_text)]
78
                            else:
79
                                return (False,)
80
                        else:
81
                            res.append(_text)
82
                            _text = ''
83
                # 못찾은 경우 (ex. delimiter)
84
                else:
85
                    if propertyName:
86
                        if _text.startswith(propertyName):
87
                            _text = _text[len(propertyName):len(_text)]
88
                            res.append(propertyName)
89
                        else:
90
                            return (False,)
91
                    else:
92
                        return (False,)
93
            return (True, res, index) if not _text else (False,)
94
        except Exception as ex:
95
            import sys
96
            from App import App
97
            from AppDocData import MessageType
98

    
99
            message = 'error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)
100
            App.mainWnd().addMessage.emit(MessageType.Error, message)
101

    
102
            return (False,)
103

    
104
    @staticmethod
105
    def instance():
106
        res = []
107

    
108
        delimiter = '!-!'
109

    
110
        appDocData = AppDocData.instance()
111
        configs = appDocData.getConfigs('Line No', 'Size Unit')
112
        if 1 == len(configs):
113
            sizeUnits = configs[0].value.split(delimiter)
114
        else: return None
115
        configs = appDocData.getConfigs('Line No', 'Delimiter')
116
        if 1 == len(configs):
117
            lineInsideDelimiters = configs[0].value.split(delimiter)
118
        else: return None
119
        configs = appDocData.getConfigs('Line No', 'Configuration')
120
        if len(configs) == 1 and configs[0].value is not None:
121
            lineConfigs = configs[0].value.split(delimiter)
122
        else: return None
123

    
124
        if len(sizeUnits) == len(lineInsideDelimiters) and len(sizeUnits) == len(lineConfigs):
125
            for i in range(len(sizeUnits)):
126
                res.append(LineNoConfig(sizeUnits[i], lineInsideDelimiters[i], lineConfigs[i]))
127

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