1+ import os , sys
2+ 3+ 4+ def get_libs (base_dir ):
5+ retlist = []
6+ for r , d , f in os .walk (base_dir ):
7+ for file in f :
8+ if '.pdl' in file :
9+ itemobj = os .path .relpath (f'{ r } /{ file } ' )
10+ retlist .append (itemobj )
11+ 12+ return retlist
13+ 14+ 15+ def get_lib_info (library ):
16+ if not os .path .exists (library ):
17+ raise FileNotFoundError (f'ERROR: Package pdlparse cannot find file { library } .' )
18+ 19+ with open (library , 'r' ) as file :
20+ file_content = file .read ()
21+ if '=' or '{' in file_content :
22+ var_count = 0
23+ class_count = 0
24+ for line in file_content .splitlines ():
25+ if '=' in line :
26+ var_count += 1
27+ if '{' in line :
28+ class_count += 1
29+ else :
30+ pass
31+ 32+ else :
33+ raise Exception (f'ERROR: Package pdlparse cannot find vars or classes in { library } .' )
34+ 35+ return f'variable# = { var_count } class# = { class_count } '
36+ 37+ 38+ def get_main_lib (base_dir ):
39+ if not os .path .exists (base_dir ):
40+ raise FileNotFoundError (f'ERROR: Package pdlparse cannot find file { base_dir } .' )
41+ 42+ retlist = []
43+ for r , d , f in os .walk (base_dir ):
44+ for file in f :
45+ if 'main' in file :
46+ if '.pdl' in file :
47+ itemobj = os .path .relpath (f'{ r } /{ file } ' )
48+ retlist .append (itemobj )
49+ else :
50+ pass
51+ 52+ if len (retlist ) == 0 :
53+ retlist .append ('No main library found.' )
54+ return retlist
55+ 56+ 57+ def read_lib (library , aloud = False ):
58+ if not os .path .exists (library ):
59+ raise FileNotFoundError (f'ERROR: Package pdlparse cannot find file { library } .' )
60+ 61+ with open (library , 'r' ) as file :
62+ retlist = []
63+ file_content = file .read ()
64+ 65+ for line in file_content .splitlines ():
66+ if aloud != False :
67+ retlist .append (line )
68+ else :
69+ print (line )
70+ if aloud != False :
71+ return retlist
72+ 73+ 74+ def format_lib (library ):
75+ if not os .path .exists (library ):
76+ raise FileNotFoundError (f'ERROR: Package pdlparse cannot find file { library } .' )
77+ 78+ with open (library , 'r' ) as file :
79+ file_content = file .read ()
80+ 81+ for line in file_content .splitlines ():
82+ if '}' in line :
83+ index = line .find ('}' )
84+ if not ';' in line :
85+ with open (library , 'w' ) as edit :
86+ replacement = file_content .replace ('}' , '};' )
87+ edit .write (replacement )
88+ if '{' in line :
89+ index = line .find ('{' )
90+ name = line [0 :index - 1 ]
91+ if not 'class' in line :
92+ with open (library , 'w' ) as edit :
93+ replacement = file_content .replace (f'{ name } ' , f'class { name } ' )
94+ edit .write (replacement )
95+ else :
96+ pass
97+
0 commit comments