11import os , sys
22
3- def get_variable (file ):
4- with open (file , 'r' ) as Fin :
3+ '''
4+ __KEYWORDS__
5+ class - a place to store variables and data
6+ priv - declares a variable as exclusive to .pdl file
7+ new - creates a copy of variable similar to handle
8+ __TYPES__
9+ str - declares a variable as a string
10+ int - declares a variable as integer
11+ lst - uses brackets and commas to create list's
12+ flt - a floating point number
13+ raw - used to declare all back-slashes to be ignored
14+ bol - true, false, null
15+ __COMMENTS__
16+ // - line comment
17+ /* - start block
18+ */ - end block
19+ '''
20+
21+
22+ class utility :
23+ def typeProcessor (assumed_type ):
24+ if 'str' in assumed_type :
25+ return 'string'
26+ elif 'int' in assumed_type :
27+ return 'integer'
28+ elif 'lst' in assumed_type :
29+ return 'array'
30+ elif 'bol' in assumed_type :
31+ return 'boolean'
32+ elif 'raw' in assumed_type :
33+ return 'raw_string'
34+ elif 'flt' in assumed_type :
35+ return 'floating_point'
36+ else :
37+ raise Exception ('ENGINE-ERROR: A fatal function was passed a incomprehensible argument' )
38+
39+ def findClass (library , var , type , value ):
40+ # Searches for variables class and returns it
41+ if not os .path .exists (library ):
42+ raise FileNotFoundError (f'ERROR: Package pdlparse cannot find file { library } .' )
43+
44+ with open (library , 'r+' ) as file :
45+ file_content = file .read ()
46+ if var in file_content :
47+ for line in file_content .split ('\n ' ):
48+ if '{' in line :
49+ class_index = file_content .splitlines ().index (line )
50+ index = line .find ('{' )
51+ indextr = line .find ('class' )
52+ if var and type and value in line :
53+ line_index = file_content .splitlines ().index (line )
54+ if line_index > class_index :
55+ line_diffrence = line_index - class_index
56+ else :
57+ line_diffrence = class_index - line_index
58+ target_line = file_content .splitlines ()[line_index - line_diffrence ]
59+ class_name = target_line [indextr :index - 1 ]
60+
61+ return class_name .replace ('class ' , '' )
62+
63+
64+ def scrap_library (library ):
65+ # Get all variables in library
66+ if not os .path .exists (library ):
67+ raise FileNotFoundError (f'ERROR: Package pdlparse cannot find file { library } .' )
68+
69+ with open (library , 'r+' ) as file :
70+ retlist = []
71+ file_content = file .read ()
72+ for line in file_content .split ('\n ' ):
73+ for fline in file_content .split ('\n ' ):
74+ if '=' in fline :
75+ index = fline .find ('=' )
76+ if fline [index + 2 :].isdigit () or "'" not in fline [index + 2 :]:
77+ indexer = fline [index + 2 :]
78+ else :
79+ indexer = fline [index + 3 :]
80+ found = fline .find ('=' )
81+ value = indexer .replace ("'" , '' )
82+ type = utility .typeProcessor (fline [0 :5 ])
83+ raw_type = fline [0 :5 ]
84+ var = fline [5 :found - 1 ].replace (' ' , '' )
85+ class_name = utility .findClass (library , var , raw_type , value )
86+ retlist .append (f'{ class_name } .{ var } = { value } , { type } ' )
87+ return retlist
88+
89+
90+ def get_variable (library , variable ):
91+ # Get a variables value based on its name
92+ if not os .path .exists (library ):
93+ raise FileNotFoundError (f'ERROR: Package pdlparse cannot find file { library } .' )
94+
95+ with open (library , 'r' ) as Fin :
96+ content = Fin .read ()
97+ if variable in content :
98+ with open (library , 'r+' ) as file :
99+ file_content = file .read ()
100+ for line in file_content .split ('\n ' ):
101+ if variable in line :
102+ if '=' in line :
103+ index = line .find ('=' )
104+ value = line [index + 3 :].replace ("'" , '' )
105+ type = utility .typeProcessor (line [0 :5 ])
106+ return str (f'{ value } , { type } ' )
107+ else :
108+ raise Exception (f'ERROR: Package pdlparse cannot find { variable } in { library } .' )
109+
110+
111+ def filter_file (library , word ):
112+ # Remove given word from library
113+ if not os .path .exists (library ):
114+ raise FileNotFoundError (f'ERROR: Package pdlparse cannot find file { library } .' )
115+ with open (library , "r+" ) as Fin :
116+ content = Fin .read ()
117+ if word in content :
118+ with open (library , "w+" ) as Fout :
119+ write_item = content .replace (word , "" )
120+ Fout .write (write_item )
121+ else :
122+ raise Exception (f'ERROR: { word } cannot be found in { library } ' )
123+
124+
125+ def get_strings (library ):
126+ # Get all strings from library
127+ if not os .path .exists (library ):
128+ raise FileNotFoundError (f'ERROR: Package pdlparse cannot find file { library } .' )
129+
130+ with open (library , 'r+' ) as file :
131+ retlist = []
132+ file_content = file .read ()
133+ for line in file_content .split ('\n ' ):
134+ if "'" and "str" in line :
135+ index = line .find ("'" )
136+ string = line [index :].replace ("'" , "" )
137+ retlist .append (string )
138+ return retlist
139+
140+
141+ def get_class (library , target_class ):
142+ # Get all variables in given class
143+ if not os .path .exists (library ):
144+ raise FileNotFoundError (f'ERROR: Package pdlparse cannot find file { library } .' )
145+
146+ with open (library , 'r' ) as Fin :
147+ content = Fin .read ()
148+ if target_class in content :
149+ with open (library , 'r+' ) as file :
150+ file_content = file .read ()
151+ for line in file_content .split ('\n ' ):
152+ if target_class in line :
153+ if '{' in line :
154+ retlist = []
155+ for fline in file_content .split ('\n ' ):
156+ if '=' in fline :
157+ index = fline .find ('=' )
158+ if fline [index + 2 :].isdigit () or "'" not in fline [index + 2 :]:
159+ indexer = fline [index + 2 :]
160+ else :
161+ indexer = fline [index + 3 :]
162+ found = fline .find ('=' )
163+ value = indexer .replace ("'" , '' )
164+ type = utility .typeProcessor (fline [0 :5 ])
165+ var = fline [5 :found - 1 ].replace (' ' , '' )
166+ retlist .append (f'{ var } = { value } , { type } ' )
167+ if '};' in fline :
168+ file .close ()
169+ return retlist
170+ else :
171+ raise Exception (f'ERROR: Package pdlparse cannot find { target_class } in { library } .' )
172+
173+
174+ def get_type (library , target_type ):
175+ # Get all variables with given type
176+ if not os .path .exists (library ):
177+ raise FileNotFoundError (f'ERROR: Package pdlparse cannot find file { library } .' )
178+
179+ with open (library , 'r' ) as Fin :
180+ content = Fin .read ()
181+ if target_type in content :
182+ with open (library , 'r+' ) as file :
183+ file_content = file .read ()
184+ for line in file_content .split ('\n ' ):
185+ if target_type in line :
186+ retlist = []
187+ for fline in file_content .split ('\n ' ):
188+ if '=' in fline :
189+ index = fline .find ('=' )
190+ if fline [index + 2 :].isdigit () or "'" not in fline [index + 2 :]:
191+ indexer = fline [index + 2 :]
192+ else :
193+ indexer = fline [index + 3 :]
194+ found = fline .find ('=' )
195+ value = indexer .replace ("'" , '' )
196+ var = fline [5 :found - 1 ].replace (' ' , '' )
197+ if fline [0 :5 ].replace (' ' , '' ) == target_type :
198+ retlist .append (f'{ var } = { value } ' )
199+ else :
200+ pass
201+ return retlist
202+ else :
203+ raise Exception (f'ERROR: Package pdlparse cannot find { target_type } in { library } .' )
204+
205+
206+ def get_value (library , target_value ):
207+ # Get information about a variable based on its value
208+ if not os .path .exists (library ):
209+ raise FileNotFoundError (f'ERROR: Package pdlparse cannot find file { library } .' )
210+
211+ with open (library , 'r' ) as Fin :
5212 content = Fin .read ()
213+ if target_value in content :
214+ with open (library , 'r+' ) as file :
215+ file_content = file .read ()
216+ for line in file_content .split ('\n ' ):
217+ if target_value in line :
218+ retlist = []
219+ for fline in file_content .split ('\n ' ):
220+ if '=' in fline :
221+ index = fline .find ('=' )
222+ if fline [index + 2 :].isdigit () or "'" not in fline [index + 2 :]:
223+ indexer = fline [index + 2 :]
224+ else :
225+ indexer = fline [index + 3 :]
226+ found = fline .find ('=' )
227+ value = indexer .replace ("'" , '' )
228+ var = fline [5 :found - 1 ].replace (' ' , '' )
229+ type = fline [0 :5 ]
230+ class_name = utility .findClass (library , var , type , value )
231+ if value == target_value :
232+ retlist .append (f'{ class_name } .{ var } = { value } ' )
233+ else :
234+ pass
235+ return retlist
236+ else :
237+ raise Exception (f'ERROR: Package pdlparse cannot find { target_value } in { library } .' )
238+
239+
240+ def get_comments (library ):
241+ # Gets all comments from given library
242+ if not os .path .exists (library ):
243+ raise FileNotFoundError (f'ERROR: Package pdlparse cannot find file { library } .' )
244+
245+ with open (library , 'r+' ) as file :
246+ retlist = []
247+ file_content = file .read ()
248+ for line in file_content .split ('\n ' ):
249+ if "//" in line :
250+ index = line .find ("//" )
251+ string = line [index :].replace ("// " , "" )
252+ retlist .append (string )
253+ return retlist
6254
7-
0 commit comments