Skip to content

Commit

Permalink
Fixed changes in api for converter and tutorial.
Browse files Browse the repository at this point in the history
  • Loading branch information
smelandr committed Jan 3, 2017
1 parent 752392b commit d8baaca
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Import `nmrstarlib` library and create generator function that will yield
>>> from nmrstarlib import nmrstarlib
>>>
>>> # "path": path_to_file / path_to_dir / path_to_archive / bmrb_id / file_url
>>> starfile_gen = nmrstarlib.read_files(["path"])
>>> starfile_gen = nmrstarlib.read_files("path")
>>>
>>> for starfile in starfile_gen:
... print(starfile.bmrbid) # print BMRB id of StarFile
Expand Down
10 changes: 5 additions & 5 deletions docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ directory of NMR-STAR files, archive of NMR-STAR files or BMRB id:

>>> from nmrstarlib import nmrstarlib
>>>
>>> single_starfile = nmrstarlib.read_files(["bmr18569.str"]) # single NMR-STAR file
>>> single_starfile = nmrstarlib.read_files("bmr18569.str") # single NMR-STAR file
>>>
>>> starfiles = nmrstarlib.read_files(["bmr18569.str", "bmr336.str"]) # several NMR-STAR files
>>> starfiles = nmrstarlib.read_files("bmr18569.str", "bmr336.str") # several NMR-STAR files
>>>
>>> dir_starfiles = nmrstarlib.read_files(["starfiles_dir"]) # directory of NMR-STAR files
>>> dir_starfiles = nmrstarlib.read_files("starfiles_dir") # directory of NMR-STAR files
>>>
>>> arch_starfiles = nmrstarlib.read_files(["starfiles.zip"]) # archive of NMR-STAR files
>>> arch_starfiles = nmrstarlib.read_files("starfiles.zip") # archive of NMR-STAR files
>>>
>>> url_starfile = nmrstarlib.read_files(["18569"]) # BMRB id of NMR-STAR file
>>> url_starfile = nmrstarlib.read_files("18569") # BMRB id of NMR-STAR file
>>>

Processing StarFile generator
Expand Down
38 changes: 19 additions & 19 deletions nmrstarlib/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def __init__(self, from_path, to_path, from_format="nmrstar", to_format="json"):
:param str from_format: Input format: `nmrstar` or `json`.
:param str to_format: Output format: `nmrstar` or `json`.
:return: None
:rtype: None
:rtype: :py:obj:`None`
"""
self.from_path = os.path.normpath(from_path)
self.to_path = os.path.normpath(to_path)
Expand All @@ -139,7 +139,7 @@ def convert(self):
"""Convert file(s) from NMR-STAR format to JSON format or from JSON format to NMR-STAR format.
:return: None
:rtype: None
:rtype: :py:obj:`None`
"""
if not os.path.exists(os.path.dirname(self.to_path)):
dirname = os.path.dirname(self.to_path)
Expand All @@ -164,7 +164,7 @@ def _many_to_many(self):
"""Perform many-to-many files conversion.
:return: None
:rtype: None
:rtype: :py:obj:`None`
"""
if not self.to_path_compression:
self._to_dir()
Expand All @@ -182,7 +182,7 @@ def _one_to_one(self):
"""Perform one-to-one file conversion.
:return: None
:rtype: None
:rtype: :py:obj:`None`
"""
if not self.to_path_compression:
self._to_textfile()
Expand All @@ -200,9 +200,9 @@ def _to_dir(self):
"""Convert files to directory.
:return: None
:rtype: None
:rtype: :py:obj:`None`
"""
for starfile in nmrstarlib.read_files([self.from_path]):
for starfile in nmrstarlib.read_files(self.from_path):
outpath = self._outputpath(starfile.source)

if not os.path.exists(os.path.dirname(outpath)):
Expand All @@ -215,18 +215,18 @@ def _to_zipfile(self):
"""Convert files to zip archive.
:return: None
:rtype: None
:rtype: :py:obj:`None`
"""
with zipfile.ZipFile(self.to_path, mode="w", compression=zipfile.ZIP_DEFLATED) as outfile:
for starfile in nmrstarlib.read_files([self.from_path]):
for starfile in nmrstarlib.read_files(self.from_path):
outpath = self._outputpath(starfile.source, archive=True)
outfile.writestr(outpath, starfile.writestr(self.to_format))

def _to_tarfile(self):
"""Convert files to tar archive.
:return: None
:rtype: None
:rtype: :py:obj:`None`
"""
if self.to_path_compression == "tar":
tar_mode = "w"
Expand All @@ -238,7 +238,7 @@ def _to_tarfile(self):
tar_mode = "w"

with tarfile.open(self.to_path, mode=tar_mode) as outfile:
for starfile in nmrstarlib.read_files([self.from_path]):
for starfile in nmrstarlib.read_files(self.from_path):
outpath = self._outputpath(starfile.source, archive=True)
info = tarfile.TarInfo(outpath)
data = starfile.writestr(self.to_format).encode()
Expand All @@ -249,40 +249,40 @@ def _to_bz2file(self):
"""Convert file to bz2-compressed file.
:return: None
:rtype: None
:rtype: :py:obj:`None`
"""
with bz2.BZ2File(self.to_path, mode="wb") as outfile:
for starfile in nmrstarlib.read_files([self.from_path]):
for starfile in nmrstarlib.read_files(self.from_path):
outfile.write(starfile.writestr(self.to_format).encode())

def _to_gzipfile(self):
"""Convert file to gzip-compressed file.
:return: None
:rtype: None
:rtype: :py:obj:`None`
"""
with gzip.GzipFile(self.to_path, mode="wb") as outfile:
for starfile in nmrstarlib.read_files([self.from_path]):
for starfile in nmrstarlib.read_files(self.from_path):
outfile.write(starfile.writestr(self.to_format).encode())

def _to_textfile(self):
"""Convert file to regular text file.
:return: None
:rtype: None
:rtype: :py:obj:`None`
"""
to_path = self.to_path if self.to_path.endswith(self.nmrstar_extension[self.to_format]) \
else self.to_path + self.nmrstar_extension[self.to_format]
with open(to_path, mode="w") as outfile:
for starfile in nmrstarlib.read_files([self.from_path]):
for starfile in nmrstarlib.read_files(self.from_path):
outfile.write(starfile.writestr(self.to_format))

def _outputpath(self, inputpath, archive=False):
"""Construct an output path string from an input path string.
:param str inputpath: Input path string.
:return: Output path string.
:rtype: str
:rtype: :py:class:`str`
"""
indirpath, fname = os.path.split(os.path.abspath(os.path.normpath(inputpath)))

Expand All @@ -294,8 +294,8 @@ def _outputpath(self, inputpath, archive=False):
outparts = inparts[len(commonparts):]

if archive:
outdirpath = os.path.join(*outparts)
outdirpath = os.path.join(*outparts) if outparts else ""
else:
outdirpath = os.path.join(self.to_path, *outparts)

return os.path.join(outdirpath, fname + self.nmrstar_extension[self.to_format])
return os.path.join(outdirpath, fname + self.nmrstar_extension[self.to_format])

0 comments on commit d8baaca

Please sign in to comment.