This repository was archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfmparser.py
More file actions
134 lines (125 loc) · 4.5 KB
/
Copy pathdfmparser.py
File metadata and controls
134 lines (125 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""
----------------------------------------------------------
created by: dev.marcio.rocha@gmail.com
date: 26-12-2022
observation: Delphi Form Files parser
----------------------------------------------------------
"""
# -----------------------------------------------
# imports
import utils
from classes import Property
# -----------------------------------------------
# Delphi Form File Parser
class DFMParser():
"""
* Simple Delphi Form File parser, that extracts file properties
* Created by: dev.marcio.rocha@gmail.com
"""
# private declarations
__file_oppened: bool
__file_stream = None
__atributos: list
__fields: list
# public variables
file_path: str
# -----------------------------------------------
# constructor
def __init__(self, **kwargs):
if 'file_path' in kwargs:
self.file_path = kwargs['file_path']
self.__file_oppened = False
self.__file_stream = None
self.__atributos = []
self.__fields = []
# -----------------------------------------------
def open_file(self):
"""
Method that ensire the FileStream is oppened
"""
if not utils.null_or_empty(self.file_path):
self.__file_stream = open(self.file_path, encoding='utf-8')
self.__file_oppened = True
return self
# -----------------------------------------------
def close_file(self):
"""
Method that ensure the FileStream is Closed.
"""
if self.__file_stream is not None:
self.__file_stream.close()
self.__file_oppened = False
self.__file_stream = None
return self
# -----------------------------------------------
def set_file(self, file_path: str):
"""
Method that alters file of FileStream
"""
if self.__file_oppened:
self.close_file()
self.file_path = file_path
return self
# -----------------------------------------------
def parse(self):
"""
Parse attribute section
"""
if not self.__file_oppened:
self.open_file()
lista_atributos = Property(name='Atributos')
atrib_prop = False
item_prop = False
item = {}
fields_prop = False
for line in self.__file_stream:
line = line.replace('\n', '')
if 'atributos' in line.lower():
atrib_prop = True
elif 'item' == line.lower().replace(' ', '').strip():
item_prop = True
elif atrib_prop and item_prop and ('=' in line):
name,value = line.replace(' ', '').replace('\'','').split('=')
item[name] = value
elif atrib_prop and item_prop and ('end' in line.lower()):
item_prop = False
atributo = Property(name=item['Nome'])
for val in item:
if 'nome' not in val.lower():
atributo.new_property(Property(name=val, value=item[val]))
item = {}
lista_atributos.new_property(atributo)
self.__atributos.append(atributo)
elif 'end>' in line.lower():
atrib_prop = False
item_prop = False
item = {}
elif ('object' in line.lower()) and ('field' in line.lower()):
fields_prop = True
item = {}
item['type'] = line.split(' ')[len(line.split(' '))-1]
if fields_prop and ('=' in line):
name, value = line.replace(' ', '').replace('\'','').split('=')
item[name] = value
elif fields_prop and ('end' == line.lower().replace(' ', '').strip()):
fields_prop = False
field = Property(name=item['FieldName'])
for val in item:
if 'fieldname' not in val.lower():
field.new_property(Property(name=val, value=item[val]))
self.__fields.append(field)
item = {}
self.close_file()
return lista_atributos
# -----------------------------------------------
def get_lista_atributos(self):
"""
Returns attributes list
"""
return self.__atributos
# -----------------------------------------------
def get_lista_fields(self):
"""
Returns Fields List
"""
return self.__fields