Skip to content

Commit 40e77d6

Browse files
authored
change error message and add new functions to new file
1 parent d921c41 commit 40e77d6

File tree

6 files changed

+94
-47
lines changed

6 files changed

+94
-47
lines changed

PDL/example.pdl

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
# - Comment
2-
# https://macromates.com/manual/en/language_grammars
3-
# https://code.visualstudio.com/api/language-extensions/syntax-highlight-guide
4-
5-
food {
6-
str(pizza = 'Peporoni') # pizza variable within food class
7-
str(bread = 'rye') # bread variable within food class
8-
str(cheese = 'mozzarella') # cheese variable within food class
9-
}
10-
11-
class2 {
12-
int(variable = '1N')
13-
}
1+
class test {
2+
str name = 'charlie'
3+
int number = 2
4+
// `raw` means the parser ignores back-slash's
5+
raw file_path = /'C:/source/calc/add.py'/
6+
list array = ['Pizza', 'Taco', 'Burger']
7+
bool torf = true
8+
str namehandle = new name
9+
10+
'''
11+
Dope docstring
12+
'''
13+
// This is a comment
14+
/* This is a
15+
block comment */
16+
};

PDL/package/pdlparse/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#https://www.freecodecamp.org/news/build-your-first-python-package/
22
from parse import scrap_library, get_variable, filter_file, get_strings, get_class, get_type, get_value, get_comments
33
from library import get_libs, get_lib_info, get_main_lib, read_lib, format_lib, rm_library, remove_comments
4-
from new import create_lib, create_lib_script, create_manifest
4+
from new import create_lib, create_lib_script, create_manifest, create_class, create_lib_id

PDL/package/pdlparse/library.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def get_libs(dir):
1616
def get_lib_info(library):
1717
# Returns information on the given library
1818
if not os.path.exists(library):
19-
raise FileNotFoundError(f'ERROR: Package pdlparse cannot find file {library}.')
19+
raise FileNotFoundError(f'[ERROR] Package pdlparse cannot find file {library}.')
2020

2121
with open(library, 'r') as file:
2222
file_content = file.read()
@@ -32,14 +32,14 @@ def get_lib_info(library):
3232
pass
3333

3434
else:
35-
raise Exception(f'ERROR: Package pdlparse cannot find vars or classes in {library}.')
35+
raise Exception(f'[ERROR] Package pdlparse cannot find vars or classes in {library}.')
3636
return f'variable# = {var_count} class# = {class_count}'
3737

3838

3939
def get_main_lib(base_dir):
4040
# Finds main library if there are any
4141
if not os.path.exists(base_dir):
42-
raise FileNotFoundError(f'ERROR: Package pdlparse cannot find file {base_dir}.')
42+
raise FileNotFoundError(f'[ERROR] Package pdlparse cannot find file {base_dir}.')
4343

4444
retlist = []
4545
for r, d, f in os.walk(base_dir):
@@ -59,7 +59,7 @@ def get_main_lib(base_dir):
5959
def read_lib(library, aloud=False):
6060
# Reads the given library aloud or returns it
6161
if not os.path.exists(library):
62-
raise FileNotFoundError(f'ERROR: Package pdlparse cannot find file {library}.')
62+
raise FileNotFoundError(f'[ERROR] Package pdlparse cannot find file {library}.')
6363

6464
with open(library, 'r') as file:
6565
retlist = []
@@ -77,7 +77,7 @@ def read_lib(library, aloud=False):
7777
def format_lib(library):
7878
# Fixes any soft-errors found in library
7979
if not os.path.exists(library):
80-
raise FileNotFoundError(f'ERROR: Package pdlparse cannot find file {library}.')
80+
raise FileNotFoundError(f'[ERROR] Package pdlparse cannot find file {library}.')
8181

8282
with open(library, 'r') as file:
8383
file_content = file.read()
@@ -136,18 +136,18 @@ def format_lib(library):
136136
def rm_library(library):
137137
# Burry the library 6-feet under. (delete it)
138138
if not os.path.exists(library):
139-
raise FileNotFoundError(f'ERROR: Package pdlparse cannot find file {library}.')
139+
raise FileNotFoundError(f'[ERROR] Package pdlparse cannot find file {library}.')
140140

141141
try:
142142
os.remove(library)
143143
except Exception as e:
144-
raise Exception(f'ERROR: An unknown error occurred during runtime \n{e}\n')
144+
raise Exception(f'[ERROR] An unknown error occurred during runtime \n{e}\n')
145145

146146

147147
def remove_comments(library):
148148
# Removes all comments from a library
149149
if not os.path.exists(library):
150-
raise FileNotFoundError(f'ERROR: Package pdlparse cannot find file {library}.')
150+
raise FileNotFoundError(f'[ERROR] Package pdlparse cannot find file {library}.')
151151

152152
with open(library, 'r+') as file:
153153
retlist = []

PDL/package/pdlparse/new.py

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import os, sys
2+
import string
3+
import random
24

35

46
def create_lib(path, name):
7+
# Creates new library
58
if not os.path.exists(path):
6-
raise FileNotFoundError(f'ERROR: Package pdlparse cannot find {path}.')
7-
9+
raise FileNotFoundError(f'[ERROR] Package pdlparse cannot find {path}.')
10+
811
new_file = f'{path}/{name}.pdl'
912
with open(new_file, 'w') as file:
1013
file.write('''/*
@@ -29,23 +32,25 @@ class - a place to store variables and data
2932

3033

3134
def create_lib_script(library, script_name):
35+
# Automaticly creates a library script
3236
if not os.path.exists(library):
33-
raise FileNotFoundError(f'ERROR: Package pdlparse cannot find {library}')
37+
raise FileNotFoundError(f'[ERROR] Package pdlparse cannot find {library}')
3438
file_path = os.path.dirname(library)
3539
if not '.' in script_name:
3640
new_file = f'{file_path}/{script_name}.py'
3741
else:
38-
raise Exception(f'ERROR: Package pdlparse found `.` in {script_name} variable')
39-
42+
raise Exception(f'[ERROR] Package pdlparse found `.` in {script_name} variable')
43+
4044
with open(new_file, 'w') as new:
4145
new.write("import os, sys \n")
42-
new.write("import pdlparse as pdl \n")
46+
new.write("import pdlparse as pdl \n\n")
4347
new.write(f"variables = pdl.parse.scrap_library('{library}')")
4448

4549

4650
def create_manifest(dir):
51+
# Create a new manifest with all files in folder
4752
if not os.path.exists(dir):
48-
raise FileNotFoundError(f'ERROR: Package pdlparse cannot find {dir}')
53+
raise FileNotFoundError(f'[ERROR] Package pdlparse cannot find {dir}')
4954
new_file = f'{dir}/MANIFEST'
5055

5156
with open(new_file, 'a') as file:
@@ -62,3 +67,43 @@ def create_manifest(dir):
6267
file.write('-_LIBS_-\n')
6368
for item in libs:
6469
file.write(f'{item}\n')
70+
71+
72+
def create_class(library, class_name):
73+
# Create a new class in given library
74+
if not os.path.exists(library):
75+
raise FileNotFoundError(f'[ERROR] Package pdlparse cannot find {library}')
76+
start_bracket = '{'
77+
end_bracket = '}'
78+
79+
try:
80+
with open(library, 'a+') as file:
81+
file.write(f"""
82+
class {class_name} {start_bracket}
83+
str placeholder = 'Replace me'
84+
{end_bracket};
85+
""")
86+
file.close()
87+
except Exception as e:
88+
raise Exception(f'[ERROR] An unknown runtime error occurred. \n{e}\n')
89+
90+
91+
def create_lib_id(library):
92+
if not os.path.exists(library):
93+
raise FileNotFoundError(f'[ERROR] Package pdlparse cannot find {library}')
94+
if os.path.getsize(library) == 0:
95+
raise Exception(f'[ERROR] Given library {library} is too small.')
96+
alphabet = string.ascii_letters + string.digits
97+
lenght_list = [6, 8, 16]
98+
id = []
99+
100+
for i in range(random.choice(lenght_list)):
101+
id.append(random.choice(alphabet))
102+
103+
with open(library, 'r') as file:
104+
content = file.read()
105+
lines = content.splitlines()
106+
first_line = lines[0]
107+
content = content.replace(first_line, f"{''.join(id)}")
108+
with open(library, 'w') as edit:
109+
edit.write(content)

PDL/package/pdlparse/parse.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ def typeProcessor(assumed_type):
3434
elif 'flt' in assumed_type:
3535
return 'floating_point'
3636
else:
37-
raise Exception('ENGINE-ERROR: A fatal function was passed a incomprehensible argument')
37+
raise Exception('ENGINE-[ERROR] A fatal function was passed a incomprehensible argument')
3838

3939
def findClass(library, var, type, value):
4040
# Searches for variables class and returns it
4141
if not os.path.exists(library):
42-
raise FileNotFoundError(f'ERROR: Package pdlparse cannot find file {library}.')
42+
raise FileNotFoundError(f'[ERROR] Package pdlparse cannot find file {library}.')
4343

4444
with open(library, 'r+') as file:
4545
file_content = file.read()
@@ -64,7 +64,7 @@ def findClass(library, var, type, value):
6464
def scrap_library(library):
6565
# Get all variables in library
6666
if not os.path.exists(library):
67-
raise FileNotFoundError(f'ERROR: Package pdlparse cannot find file {library}.')
67+
raise FileNotFoundError(f'[ERROR] Package pdlparse cannot find file {library}.')
6868

6969
with open(library, 'r+') as file:
7070
retlist = []
@@ -90,7 +90,7 @@ def scrap_library(library):
9090
def get_variable(library, variable):
9191
# Get a variables value based on its name
9292
if not os.path.exists(library):
93-
raise FileNotFoundError(f'ERROR: Package pdlparse cannot find file {library}.')
93+
raise FileNotFoundError(f'[ERROR] Package pdlparse cannot find file {library}.')
9494

9595
with open(library, 'r') as Fin:
9696
content = Fin.read()
@@ -105,27 +105,27 @@ def get_variable(library, variable):
105105
type = utility.typeProcessor(line[0:5])
106106
return str(f'{value}, {type}')
107107
else:
108-
raise Exception(f'ERROR: Package pdlparse cannot find {variable} in {library}.')
108+
raise Exception(f'[ERROR] Package pdlparse cannot find {variable} in {library}.')
109109

110110

111111
def filter_file(library, word):
112112
# Remove given word from library
113113
if not os.path.exists(library):
114-
raise FileNotFoundError(f'ERROR: Package pdlparse cannot find file {library}.')
114+
raise FileNotFoundError(f'[ERROR] Package pdlparse cannot find file {library}.')
115115
with open(library, "r+") as Fin:
116116
content = Fin.read()
117117
if word in content:
118118
with open(library, "w+") as Fout:
119119
write_item = content.replace(word, "")
120120
Fout.write(write_item)
121121
else:
122-
raise Exception(f'ERROR: {word} cannot be found in {library}')
122+
raise Exception(f'[ERROR] {word} cannot be found in {library}')
123123

124124

125125
def get_strings(library):
126126
# Get all strings from library
127127
if not os.path.exists(library):
128-
raise FileNotFoundError(f'ERROR: Package pdlparse cannot find file {library}.')
128+
raise FileNotFoundError(f'[ERROR] Package pdlparse cannot find file {library}.')
129129

130130
with open(library, 'r+') as file:
131131
retlist = []
@@ -141,7 +141,7 @@ def get_strings(library):
141141
def get_class(library, target_class):
142142
# Get all variables in given class
143143
if not os.path.exists(library):
144-
raise FileNotFoundError(f'ERROR: Package pdlparse cannot find file {library}.')
144+
raise FileNotFoundError(f'[ERROR] Package pdlparse cannot find file {library}.')
145145

146146
with open(library, 'r') as Fin:
147147
content = Fin.read()
@@ -168,13 +168,13 @@ def get_class(library, target_class):
168168
file.close()
169169
return retlist
170170
else:
171-
raise Exception(f'ERROR: Package pdlparse cannot find {target_class} in {library}.')
171+
raise Exception(f'[ERROR] Package pdlparse cannot find {target_class} in {library}.')
172172

173173

174174
def get_type(library, target_type):
175175
# Get all variables with given type
176176
if not os.path.exists(library):
177-
raise FileNotFoundError(f'ERROR: Package pdlparse cannot find file {library}.')
177+
raise FileNotFoundError(f'[ERROR] Package pdlparse cannot find file {library}.')
178178

179179
with open(library, 'r') as Fin:
180180
content = Fin.read()
@@ -200,13 +200,13 @@ def get_type(library, target_type):
200200
pass
201201
return retlist
202202
else:
203-
raise Exception(f'ERROR: Package pdlparse cannot find {target_type} in {library}.')
203+
raise Exception(f'[ERROR] Package pdlparse cannot find {target_type} in {library}.')
204204

205205

206206
def get_value(library, target_value):
207207
# Get information about a variable based on its value
208208
if not os.path.exists(library):
209-
raise FileNotFoundError(f'ERROR: Package pdlparse cannot find file {library}.')
209+
raise FileNotFoundError(f'[ERROR] Package pdlparse cannot find file {library}.')
210210

211211
with open(library, 'r') as Fin:
212212
content = Fin.read()
@@ -234,13 +234,13 @@ def get_value(library, target_value):
234234
pass
235235
return retlist
236236
else:
237-
raise Exception(f'ERROR: Package pdlparse cannot find {target_value} in {library}.')
237+
raise Exception(f'[ERROR] Package pdlparse cannot find {target_value} in {library}.')
238238

239239

240240
def get_comments(library):
241241
# Gets all comments from given library
242242
if not os.path.exists(library):
243-
raise FileNotFoundError(f'ERROR: Package pdlparse cannot find file {library}.')
243+
raise FileNotFoundError(f'[ERROR] Package pdlparse cannot find file {library}.')
244244

245245
with open(library, 'r+') as file:
246246
retlist = []

PDL/package/setup.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# https://www.freecodecamp.org/news/build-your-first-python-package/
21
from setuptools import setup, find_packages
32

43
VERSION = '1.0'
@@ -11,7 +10,7 @@
1110

1211
# Setting up
1312
setup(
14-
name="PDLparse",
13+
name="pdlparse",
1514
version=VERSION,
1615
author="Cooper ransom",
1716
author_email="Cooperransom08@outlook.com",
@@ -20,7 +19,7 @@
2019
packages=find_packages(),
2120
install_requires=[],
2221

23-
keywords=['python', 'data', 'library', 'minimalistic', 'like-toml'],
22+
keywords=['python', 'data', 'library', 'minimalistic', 'data', 'like-toml'],
2423
classifiers= [
2524
"Development Status :: 1 - Beta",
2625
"Intended Audience :: Developers",

0 commit comments

Comments
 (0)