Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed writer functions #10

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pyvnt/Container/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ def give_val(self):
res = res + ", "

return res

def get_items(self):
'''
Function to get all the items stored in the object
'''
return self._privateDict.items()

def get_keys(self):
'''
Function to get all the keys stored in the object
'''
return self._privateDict.keys()

def write_out(self, file, indent = 0):
'''
Expand Down
12 changes: 12 additions & 0 deletions pyvnt/Container/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ def size(self):
s = s + len(elem)
return s

def is_a_node(self):
'''
Returns if the list is a list of nodes.
'''
return self.__isNode

def give_val(self):
'''
Returns the list.
Expand All @@ -266,6 +272,12 @@ def give_val(self):

print(res)
return res

def get_elems(self):
'''
Returns the elements of the list.
'''
return self.__values

def check_similar_data(self):
'''
Expand Down
9 changes: 9 additions & 0 deletions pyvnt/Container/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ def add_child(self, node):
'''
self.children += (node, )

def get_data(self):
'''
Function to get the attributes of the current node

Parameter:
None
'''
return self.data


def set_Parent(self, node):
'''
Expand Down
117 changes: 114 additions & 3 deletions pyvnt/Converter/Writer/writer.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,124 @@
from pyvnt.Container import *
from pyvnt.Container.node import *
from pyvnt.Container.key import *
from pyvnt.Container.list import *
from pyvnt.Reference.basic import *
from pyvnt.Reference.error_classes import *
from pyvnt.Reference.dimension_set import *
from pyvnt.Reference.vector import *
from pyvnt.Reference.tensor import *
from pyvnt.utils.make_indent import make_indent
import re

def writeTo(root, path):
'''
Function to write the dictionary object to the file

The root node becomes the filename, and the content of the nodes are then written in the file by traversing through the contents of the node recursively.

Parameters:
Node_C: Dictionary object to be written
path: Path to the file where the dictionary object is to be written

'''
with open(path, "w") as file:
root.write_out(file)
file_name = root.name

ptt = r".txt$"
if re.search(ptt, file_name):
raise ValueError("File name cannot have .txt extension")

with open(path + f"{file_name}.txt", "w") as file: # Creates a file with the same name as the root node

for d in root.get_data():
write_out(d, file)

for child in root.children:
write_out(child, file)
file.write("\n")


def write_out(obj, file, indent = 0, list_in_key = False):
'''
Function to write the current object to the file

Parameters:
file: File object to write the object to
indent: Required indentation needed for the object to be written

'''

if type(obj) == Node_C: # If object is a node
make_indent(file, indent)
file.write(f"{obj.name}\n")
make_indent(file, indent)
file.write("{\n")

for d in obj.get_data():
write_out(d, file, indent+1)

for child in obj.children:
write_out(child, file, indent+1)
file.write("\n")

make_indent(file, indent)
file.write("}\n")

elif type(obj) == Key_C: # If object is a key
col_width = 16
last_elem = list(obj.get_keys())[-1]

make_indent(file, indent)

if len(obj.get_keys()) == 1 and type(list(obj.get_items())[0]) == List_CP:
file.write(f"{obj.name}\n")
for key, val in obj.get_items():
write_out(val, file, indent, True)
else:
file.write(f"{obj.name.ljust(col_width)}")
for key, val in obj.get_items():
write_out(val, file)
if key != last_elem:
file.write(" ")

file.write(";\n")

elif type(obj) == List_CP: # If object is a list
if obj.is_a_node():
make_indent(file, indent)
file.write(f"{obj.name}\n")

make_indent(file, indent)
file.write("(\n")

for child in obj.children:
write_out(child, file, indent+1)
file.write("\n")

make_indent(file, indent)
file.write(")\n")

elif list_in_key:
make_indent(file, indent)
file.write("(\n")
for elem in obj.get_elems():
make_indent(file, indent+1)
for val in elem:
write_out(val, file)
file.write(" ")
file.write("\n")
make_indent(file, indent)
file.write(")")

else:
res = "( "
for elem in obj.get_elems():
for val in elem:
res = res + f"{val.give_val()} "
res += ")"
file.write(res)

elif type(obj) == Int_P or type(obj) == Flt_P or type(obj) == Enm_P or type(obj) == Vector_P or type(obj) == Tensor_P or type(obj) == Dim_Set_P : # If object is a property
file.write(f"{obj.give_val()}")

else:
raise ValueError(f"Object of type {type(obj)} not supported for writing out to file")

Loading