Skip to content

Commit

Permalink
Cleaned up file printing functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
smelandr committed Oct 27, 2017
1 parent 86c3920 commit 05620e0
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions nmrstarlib/nmrstarlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ def print_file(self, f=sys.stdout, file_format="nmrstar", tw=3):
self.print_saveframe(saveframe, f, file_format, tw)
print(u"\nsave_\n\n", file=f)


elif file_format is "json":
print(self._to_json(), file=f)

Expand All @@ -343,28 +344,24 @@ def print_saveframe(self, sf, f=sys.stdout, file_format="nmrstar", tw=3):
:rtype: :py:obj:`None`
"""
if file_format is "nmrstar":
if sf == u"data":
print(u"{}_{}\n".format(sf, self[sf]), file=f)
else:
for sftag in self[sf].keys():
# handle loops
if sftag[:5] == "loop_":
print(u"\n{}loop_".format(tw * u" "), file=f)
self.print_loop(sf, sftag, f, file_format, tw * 2)
print(u"\n{}stop_".format(tw * u" "), file=f)

# handle the NMR-Star "multiline string"
# if self[sf][sftag][0] == u";":
elif self[sf][sftag].endswith(u"\n"):
print(u"{}_{}".format(tw * u" ", sftag), file=f)
print(u";\n{};\n".format(self[sf][sftag]), file=f)

elif len(self[sf][sftag].split()) > 1:
# need to escape value with quotes (i.e. u"'{}'".format()) if value consists of two or more words
print(u"{}_{}\t {}".format(tw * u" ", sftag, u"'{}'".format(self[sf][sftag])), file=f)
for sftag in self[sf].keys():
# handle loops
if sftag[:5] == "loop_":
print(u"\n{}loop_".format(tw * u" "), file=f)
self.print_loop(sf, sftag, f, file_format, tw * 2)
print(u"\n{}stop_".format(tw * u" "), file=f)

# handle the NMR-Star "multiline string"
elif self[sf][sftag].endswith(u"\n"):
print(u"{}_{}".format(tw * u" ", sftag), file=f)
print(u";\n{};".format(self[sf][sftag]), file=f)

elif len(self[sf][sftag].split()) > 1:
# need to escape value with quotes (i.e. u"'{}'".format()) if value consists of two or more words
print(u"{}_{}\t {}".format(tw * u" ", sftag, u"'{}'".format(self[sf][sftag])), file=f)

else:
print(u"{}_{}\t {}".format(tw * u" ", sftag, self[sf][sftag]), file=f)
else:
print(u"{}_{}\t {}".format(tw * u" ", sftag, self[sf][sftag]), file=f)

elif file_format is "json":
print(json.dumps(self[sf], sort_keys=False, indent=4), file=f)
Expand Down

0 comments on commit 05620e0

Please sign in to comment.