import pyndows
path_to_retrieved_file = ""
with pyndows.connect(...) as machine:
pyndows.get(machine, "shared_folder_name", "/folder/requested_file_name", path_to_retrieved_file)
import pyndows
with pyndows.connect(...) as machine:
description = pyndows.get_file_desc(machine, "shared_folder_name", "/folder/requested_file_name")
import pyndows
file_to_move_path = ""
with pyndows.connect(...) as machine:
pyndows.move(machine, "shared_folder_name", "/folder/destination_file_name", file_to_move_path)
Note that folders will be created if not existing.
You can also provide a custom suffix for the temporary file (.tmp is used by default) via the temp_file_suffix parameter.
import pyndows
with pyndows.connect(...) as machine:
pyndows.rename(machine, "shared_folder_name", "/folder/previous_file_name", "/folder/new_file_name")
import pyndows
with pyndows.connect(...) as machine:
details = pyndows.check("connection identifier", machine)
You can mock remote connections by using samba_mock
pytest
fixture.
2 convenience methods are available:
samba_mock.path(share_folder_name, file_or_folder_path)
returns apathlib.Path
instance that you can use as a replacement for the file on the remote connection.- Use
write_*()
to set the content of a file. - Use
read_*()
to check the content of a file.
- Use
samba_mock.add_callback(method_name, callback)
provides the ability to override the mock default behavior and can be used to send custom exceptions.
Below are a few example of what can be done:
from pyndows.testing import samba_mock, SMBConnectionMock
def test_file_retrieval(samba_mock: SMBConnectionMock):
samba_mock.path("shared_folder_name", "/folder/file_to_retrieve").write_text("File content of path to a file")
# TODO Execute code relying on this file
from pyndows.testing import samba_mock, SMBConnectionMock
def test_file_retrieval(samba_mock: SMBConnectionMock):
# TODO Execute code writing this file
file_content = samba_mock.path("shared_folder_name", "/folder/file_that_was_stored").read_text()
from smb.smb_structs import OperationFailure
from pyndows.testing import samba_mock, SMBConnectionMock
def test_file_retrieval(samba_mock: SMBConnectionMock):
def raise_exception(*args):
raise OperationFailure("Mock for echo failure.", [])
samba_mock.add_callback("echo", raise_exception)
# TODO Execute code calling echo
- python 3.6+ must be installed
- Use pip to install module:
python -m pip install pyndows