forked from piskvorky/smart_open
-
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.
Support for reading and writing files directly to/from ftp (piskvorky…
- Loading branch information
1 parent
d79ab9f
commit c01272b
Showing
22 changed files
with
724 additions
and
1,275 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,3 +65,4 @@ target/ | |
|
||
# env files | ||
.env | ||
.venv |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Quickstart | ||
|
||
Clone the repo and use a python installation to create a venv: | ||
|
||
```sh | ||
git clone [email protected]:RaRe-Technologies/smart_open.git | ||
cd smart_open | ||
python -m venv .venv | ||
``` | ||
|
||
Activate the venv to start working and install test deps: | ||
|
||
```sh | ||
.venv/bin/activate | ||
pip install -e ".[test]" | ||
``` | ||
|
||
Tests should pass: | ||
|
||
```sh | ||
pytest | ||
``` | ||
|
||
Thats it! When you're done, deactivate the venv: | ||
|
||
```sh | ||
deactivate | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from __future__ import unicode_literals | ||
import pytest | ||
from smart_open import open | ||
|
||
|
||
@pytest.fixture(params=[("ftp", 21), ("ftps", 90)]) | ||
def server_info(request): | ||
return request.param | ||
|
||
def test_nonbinary(server_info): | ||
server_type = server_info[0] | ||
port_num = server_info[1] | ||
file_contents = "Test Test \n new test \n another tests" | ||
appended_content1 = "Added \n to end" | ||
|
||
with open(f"{server_type}://user:123@localhost:{port_num}/file", "w") as f: | ||
f.write(file_contents) | ||
|
||
with open(f"{server_type}://user:123@localhost:{port_num}/file", "r") as f: | ||
read_contents = f.read() | ||
assert read_contents == file_contents | ||
|
||
with open(f"{server_type}://user:123@localhost:{port_num}/file", "a") as f: | ||
f.write(appended_content1) | ||
|
||
with open(f"{server_type}://user:123@localhost:{port_num}/file", "r") as f: | ||
read_contents = f.read() | ||
assert read_contents == file_contents + appended_content1 | ||
|
||
def test_binary(server_info): | ||
server_type = server_info[0] | ||
port_num = server_info[1] | ||
file_contents = b"Test Test \n new test \n another tests" | ||
appended_content1 = b"Added \n to end" | ||
|
||
with open(f"{server_type}://user:123@localhost:{port_num}/file2", "wb") as f: | ||
f.write(file_contents) | ||
|
||
with open(f"{server_type}://user:123@localhost:{port_num}/file2", "rb") as f: | ||
read_contents = f.read() | ||
assert read_contents == file_contents | ||
|
||
with open(f"{server_type}://user:123@localhost:{port_num}/file2", "ab") as f: | ||
f.write(appended_content1) | ||
|
||
with open(f"{server_type}://user:123@localhost:{port_num}/file2", "rb") as f: | ||
read_contents = f.read() | ||
assert read_contents == file_contents + appended_content1 | ||
|
||
def test_line_endings_non_binary(server_info): | ||
server_type = server_info[0] | ||
port_num = server_info[1] | ||
B_CLRF = b'\r\n' | ||
CLRF = '\r\n' | ||
file_contents = f"Test Test {CLRF} new test {CLRF} another tests{CLRF}" | ||
|
||
with open(f"{server_type}://user:123@localhost:{port_num}/file3", "w") as f: | ||
f.write(file_contents) | ||
|
||
with open(f"{server_type}://user:123@localhost:{port_num}/file3", "r") as f: | ||
for line in f: | ||
assert not CLRF in line | ||
|
||
with open(f"{server_type}://user:123@localhost:{port_num}/file3", "rb") as f: | ||
for line in f: | ||
assert B_CLRF in line | ||
|
||
def test_line_endings_binary(server_info): | ||
server_type = server_info[0] | ||
port_num = server_info[1] | ||
B_CLRF = b'\r\n' | ||
CLRF = '\r\n' | ||
file_contents = f"Test Test {CLRF} new test {CLRF} another tests{CLRF}".encode('utf-8') | ||
|
||
with open(f"{server_type}://user:123@localhost:{port_num}/file4", "wb") as f: | ||
f.write(file_contents) | ||
|
||
with open(f"{server_type}://user:123@localhost:{port_num}/file4", "r") as f: | ||
for line in f: | ||
assert not CLRF in line | ||
|
||
with open(f"{server_type}://user:123@localhost:{port_num}/file4", "rb") as f: | ||
for line in f: | ||
assert B_CLRF in line |
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2022 Radim Rehurek <[email protected]> | ||
# | ||
# This code is distributed under the terms and conditions | ||
# from the MIT License (MIT). | ||
# | ||
|
||
import os | ||
import tempfile | ||
import pytest | ||
|
||
import smart_open | ||
import smart_open.ssh | ||
|
||
|
||
def explode(*args, **kwargs): | ||
raise RuntimeError("this function should never have been called") | ||
|
||
|
||
@pytest.mark.skipif("SMART_OPEN_SSH" not in os.environ, reason="this test only works on the dev machine") | ||
def test(): | ||
with smart_open.open("ssh://misha@localhost/Users/misha/git/smart_open/README.rst") as fin: | ||
readme = fin.read() | ||
|
||
assert 'smart_open — utils for streaming large files in Python' in readme | ||
|
||
# | ||
# Ensure the cache is being used | ||
# | ||
assert ('localhost', 'misha') in smart_open.ssh._SSH | ||
|
||
try: | ||
connect_ssh = smart_open.ssh._connect_ssh | ||
smart_open.ssh._connect_ssh = explode | ||
|
||
with smart_open.open("ssh://misha@localhost/Users/misha/git/smart_open/howto.md") as fin: | ||
howto = fin.read() | ||
|
||
assert 'How-to Guides' in howto | ||
finally: | ||
smart_open.ssh._connect_ssh = connect_ssh |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[tool.pytest.ini_options] | ||
testpaths = ["smart_open"] |
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
Oops, something went wrong.