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

connection object for filepass #7

Merged
merged 7 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
73 changes: 27 additions & 46 deletions cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,36 @@

import graypy

from src.filepass import file_pass
from src.filepass import ConnectionDetails, file_pass


def main():
# Load Environmental Variables

# GREYLOG_SERVER
# GREYLOG_PORT
# INTEGRATION_NAME
# Connection objects
from_conn = ConnectionDetails(
method=os.environ.get("FROMMETHOD"),
user=os.environ.get("FROMUSER"),
password=os.environ.get("FROMPW"),
server=os.environ.get("FROMSVR"),
port=os.environ.get("FROMPORT"),
dir=os.environ.get("FROMDIR"),
share=os.environ.get("FROMSMBSHARE"),
)

to_conn = ConnectionDetails(
method=os.environ.get("TOMETHOD"),
user=os.environ.get("TOUSER"),
password=os.environ.get("TOPW"),
server=os.environ.get("TOSVR"),
port=os.environ.get("TOPORT"),
dir=os.environ.get("TODIR"),
share=os.environ.get("TOSMBSHARE"),
sparul93 marked this conversation as resolved.
Show resolved Hide resolved
)

from_user = os.environ.get("FROMUSER")
from_pw = os.environ.get("FROMPW")
from_svr = os.environ.get("FROMSVR")
from_port = os.environ.get("FROMPORT")
from_dir = os.environ.get("FROMDIR")
from_share = os.environ.get("FROMSMBSHARE")
from_method = os.environ.get("FROMMETHOD")
from_delete = os.environ.get("FROMDELETE")
from_filter = os.environ.get("FROMFILEFILTER")

to_user = os.environ.get("TOUSER")
to_pw = os.environ.get("TOPW")
to_svr = os.environ.get("TOSVR")
to_port = os.environ.get("TOPORT")
to_dir = os.environ.get("TODIR")
to_share = os.environ.get("TOSMBSHARE")
to_method = os.environ.get("TOMETHOD")
to_delete = os.environ.get("TODELETE")
# Environment variable added for filename change in single file mode
new_filename = os.environ.get("NEW_FILENAME")

filepass_logger = logging.getLogger("filepass_logger")
Expand All @@ -51,19 +52,11 @@ def main():
logger = logging.LoggerAdapter(
filepass_logger,
{
"from_method": from_method,
"from_user": from_user,
"from_svr": from_svr,
"from_port": from_port,
"from_share": from_share,
"from_dir": from_dir,
"from_conn": from_conn,
"from_delete": from_delete,
"from_filter": from_filter,
"to_method": to_method,
"to_user": to_user,
"to_svr": to_svr,
"to_port": to_port,
"to_share": to_share,
"to_dir": to_dir,
"to_conn": to_conn,
"to_delete": to_delete,
"integration": "filepass",
"filepass_name": os.environ.get("INTEGRATION_NAME"),
"new_filename": new_filename,
Expand All @@ -72,22 +65,10 @@ def main():
try:
file_pass(
logger,
from_user,
from_pw,
from_svr,
from_port,
from_dir,
from_share,
from_method,
from_conn,
from_delete,
from_filter,
to_user,
to_pw,
to_svr,
to_port,
to_dir,
to_share,
to_method,
to_conn,
to_delete,
new_filename,
)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "filepass"
version = "0.4.0"
version = "0.5.0"
authors = [
{ name="Orlund Norstrom", email="[email protected]" },
{ name="Marco Lussetti", email="[email protected]" },
Expand Down
18 changes: 11 additions & 7 deletions src/filepass/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from .filepass import (
file_pass,
local_connection,
osfs_connection,
sftp_connection,
smb_connection,
)
from .filepass import file_pass, osfs_connection, sftp_connection, smb_connection
from .filepass_config import ConnectionDetails

__all__ = [
"ConnectionDetails",
"sftp_connection",
"smb_connection",
"local_connection",
"osfs_connection",
"file_pass",
sparul93 marked this conversation as resolved.
Show resolved Hide resolved
]
121 changes: 53 additions & 68 deletions src/filepass/filepass.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,58 @@
import fs.smbfs
from fs.walk import Walker

from .filepass_config import ConnectionDetails


# File Transfer Types
def sftp_connection(logger, user, pw, svr, port, dir):
logger.debug("sftp://{}:{}@{}:{}{}".format(user, "passwordhere", svr, port, dir))
fs_conn = fs.open_fs("sftp://{}:{}@{}:{}{}".format(user, pw, svr, port, dir))
def sftp_connection(logger, conn_details: ConnectionDetails):
logger.debug(
"sftp://{}:{}@{}:{}{}".format(
conn_details.user,
"passwordhere",
conn_details.server,
conn_details.port,
conn_details.dir,
)
)
fs_conn = fs.open_fs(
"sftp://{}:{}@{}:{}{}".format(
conn_details.user,
conn_details.password,
conn_details.server,
conn_details.port,
conn_details.dir,
)
)
return fs_conn


def smb_connection(logger, user, pw, svr, port, smbshare, dir):
def smb_connection(logger, conn_details: ConnectionDetails):
logger.debug(
"smb://{}:{}@{}:{}/{}".format(user, "passwordhere", svr, port, smbshare + dir)
"smb://{}:{}@{}:{}/{}".format(
conn_details.user,
"passwordhere",
conn_details.server,
conn_details.port,
conn_details.share + conn_details.dir,
)
)
fs_conn = fs.open_fs(
"smb://{}:{}@{}:{}/{}?direct-tcp=True&name-port=139&timeout=15&domain=".format(
user, pw, svr, port, smbshare + dir
conn_details.user,
conn_details.password,
conn_details.server,
conn_details.port,
conn_details.share + conn_details.dir,
)
)
# timeout, name - port, direct - tcp, hostname, and domain.
return fs_conn


def local_connection(logger, dir: str):
"""Establishes a connection to a directory on the local filesystem"""
logger.debug("local connection to {}".format(dir))
fs_conn = fs.open_fs("{}".format(dir))
return fs_conn


def osfs_connection(logger, dir):
logger.debug("osfs dir: {}".format(dir))
fs_conn = fs.open_fs(dir)
def osfs_connection(logger, conn_details: ConnectionDetails):
logger.debug("osfs/local connection to dir: {}".format(conn_details.dir))
fs_conn = fs.open_fs(conn_details.dir)
return fs_conn


Expand All @@ -45,65 +66,29 @@ def transfer_file(from_fs, to_fs, filename, should_rename=False, new_filename=No

def file_pass(
logger,
from_user,
from_pw,
from_svr,
from_port,
from_dir,
from_share,
from_method,
from_conn: ConnectionDetails,
from_delete,
from_filter,
to_user,
to_pw,
to_svr,
to_port,
to_dir,
to_share,
to_method,
to_conn: ConnectionDetails,
to_delete,
new_filename=None,
):
# From File System
if from_method == "sftp":
logger.debug("Create from SFTP connection")
from_fs = sftp_connection(
logger, from_user, from_pw, from_svr, from_port, from_dir
)
connection_functions = {
"sftp": sftp_connection,
"smb": smb_connection,
"local": osfs_connection,
"osfs": osfs_connection,
sparul93 marked this conversation as resolved.
Show resolved Hide resolved
}

if from_method == "smb":
logger.debug("Create from SMB connection")
from_fs = smb_connection(
logger, from_user, from_pw, from_svr, from_port, from_share, from_dir
)

if from_method == "local":
logger.debug("Create from local/osfs connection")
from_fs = local_connection(logger, from_dir)

if from_method == "osfs":
logger.debug("Create osfs connection")
from_fs = osfs_connection(logger, from_dir)

# To File System
if to_method == "sftp":
logger.debug("Create to SFTP connection")
to_fs = sftp_connection(logger, to_user, to_pw, to_svr, to_port, to_dir)

if to_method == "smb":
logger.debug("Create to SMB connection")
to_fs = smb_connection(
logger, to_user, to_pw, to_svr, to_port, to_share, to_dir
)

if to_method == "local":
logger.debug("Create from local connection")
to_fs = local_connection(logger, to_dir)

if to_method == "osfs":
logger.debug("Create to OSFS connection")
to_fs = osfs_connection(logger, to_dir)
from_fs = connection_functions[from_conn.method](logger, from_conn)
to_fs = connection_functions[to_conn.method](logger, to_conn)

logger.debug(
f"Establishing connection 'from' server: {from_conn.server}, using method: {from_conn.method} \n Directory selected: {from_conn.dir}"
sparul93 marked this conversation as resolved.
Show resolved Hide resolved
)
logger.debug(
f"Establishing connection 'to' server: {to_conn.server}, using method: {to_conn.method} \n Directory selected: {to_conn.dir}"
)
# Do the move
walker = Walker(filter=[from_filter], ignore_errors=True, max_depth=1)
# Create a list of files to be transferred based on the filter.
Expand Down
18 changes: 18 additions & 0 deletions src/filepass/filepass_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class ConnectionDetails:
def __init__(
self,
method,
sparul93 marked this conversation as resolved.
Show resolved Hide resolved
user=None,
password=None,
server=None,
port=None,
dir=None,
sparul93 marked this conversation as resolved.
Show resolved Hide resolved
share=None,
):
self.method = method
self.user = user
sparul93 marked this conversation as resolved.
Show resolved Hide resolved
self.password = password
sparul93 marked this conversation as resolved.
Show resolved Hide resolved
self.server = server
sparul93 marked this conversation as resolved.
Show resolved Hide resolved
self.port = port
sparul93 marked this conversation as resolved.
Show resolved Hide resolved
self.dir = dir
self.share = share
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add at least a basic level of error checking

e.g. if something is defined as SMB, then share would be mandatory, right?

If you have ssh, then you need a username and a password.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Raise Value error to ensure all mandatory fields are provided by user.

Loading