From 0040f6a8950f7367b84513d425c37f9e416512f7 Mon Sep 17 00:00:00 2001 From: ThaHobbyist Date: Fri, 26 Jul 2024 17:20:36 +0530 Subject: [PATCH] Fixed writer functions --- pyvnt/Container/key.py | 12 ++++ pyvnt/Container/list.py | 12 ++++ pyvnt/Container/node.py | 9 +++ pyvnt/Converter/Writer/writer.py | 117 ++++++++++++++++++++++++++++++- 4 files changed, 147 insertions(+), 3 deletions(-) diff --git a/pyvnt/Container/key.py b/pyvnt/Container/key.py index e4420eb..f3f5f22 100755 --- a/pyvnt/Container/key.py +++ b/pyvnt/Container/key.py @@ -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): ''' diff --git a/pyvnt/Container/list.py b/pyvnt/Container/list.py index cad3501..01f97c3 100644 --- a/pyvnt/Container/list.py +++ b/pyvnt/Container/list.py @@ -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. @@ -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): ''' diff --git a/pyvnt/Container/node.py b/pyvnt/Container/node.py index 6579c98..87d1683 100755 --- a/pyvnt/Container/node.py +++ b/pyvnt/Container/node.py @@ -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): ''' diff --git a/pyvnt/Converter/Writer/writer.py b/pyvnt/Converter/Writer/writer.py index 16564f1..62f1048 100644 --- a/pyvnt/Converter/Writer/writer.py +++ b/pyvnt/Converter/Writer/writer.py @@ -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) \ No newline at end of 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") + \ No newline at end of file