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

Read file from URL #521

Merged
merged 3 commits into from
Jun 25, 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 tests/test_tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import ase

from zndraw.tasks import FileIO, get_generator_from_filename


def test_get_generator_from_filename():
file = FileIO(
name="https://raw.githubusercontent.com/LarsSchaaf/Guaranteed-Non-Local-Molecular-Dataset/main/gnl-dataset/GNL-v0.2/gnl-v0.2-test.xyz"
)
generator = get_generator_from_filename(file)

assert isinstance(next(iter(generator)), ase.Atoms)
10 changes: 10 additions & 0 deletions zndraw/tasks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging
import typing as t
import urllib.request
from io import StringIO

import ase.io
import socketio.exceptions
Expand Down Expand Up @@ -45,6 +47,14 @@ def _generator():
raise ImportError(
"You need to install ZnH5MD to use the remote feature (or `pip install zndraw[all]`)."
) from err
elif file_io.name.startswith(("http", "https")):
format = file_io.name.split(".")[-1]
format = format if format != "xyz" else "extxyz"
content = urllib.request.urlopen(file_io.name).read().decode("utf-8")
stream = StringIO(content)

generator = ase.io.iread(stream, format=format)

else:
generator = ase.io.iread(file_io.name)

Expand Down
Loading