-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cae3274
commit 0040f6a
Showing
4 changed files
with
147 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|