Skip to content
Open
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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# generated from manifests external_dependencies
cryptography
paramiko
paramiko<4.0.0
pysftp
153 changes: 153 additions & 0 deletions server_ftp/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
.. image:: https://odoo-community.org/readme-banner-image
:target: https://odoo-community.org/get-involved?utm_source=readme
:alt: Odoo Community Association

==========
Server FTP
==========

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:9bac6cfb37203ffe6deb69b9d21feb8672d2267a252e8befca1b9f4ff61a55ab
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--tools-lightgray.png?logo=github
:target: https://github.com/OCA/server-tools/tree/20.0/server_ftp
:alt: OCA/server-tools
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/server-tools-20-0/server-tools-20-0-server_ftp
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/server-tools&target_branch=20.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module enables connections via FTP, SFTP and FTP_TLS. Operations
such as retrieving a file, checking if a file exists, are all
centralized in an Abstract python class, so the user can operate without
even noticing what protocol they are using.

For more exquisite operations, the user may retrieve a pure connection
object in a straighforward way.

TODOS:

- Secure everything

**Table of contents**

.. contents::
:local:

Installation
============

To install this module, you need to:

1. Have paramiko available ``pip3 install paramiko``
2. Add it as dependency from your main module.

Configuration
=============

To configure this module, you need to:

1. Go to ``Settings/Technical/FTP/Servers``
2. Find your favourite protocol and configure connection settings
3. In the form view of the record, edit file structure.
4. If more folders are needed, just add data records with a proper
xml_id
5. If more servers are needed, just add data records with a proper
xml_id

Usage
=====

To use this module, you need to:

1. Configure and test you ftp/sftp/lsftp connection
2. Fetch the relevant server.ftp.folder record via it's xmlid
3. Code

::

>>> folder = session.env.ref("server_ftp.server_ftp_pull_folder_01_ftp")
>>> folder_server_object = folder.connect()
# folder_server_object is lib.ftp_server.FtpServer object
# You can perform simple operations like ls, put, get, close etc
# with folder_server_object. See lib/abstract_ftp_model.py for more
>>> folder_server_object.listdir()
-rw-rw-r-- 1 1001 1001 0 May 06 14:10 a_file.txt
-rw-rw-r-- 1 1001 1001 0 May 06 14:21 another_file.txt
>>> server = folder_server_object.get_server()
# server is ftplib.FTP object
# so we can do anything in ftplib
>>> server.dir()
-rw-rw-r-- 1 1001 1001 0 May 06 14:10 a_file.txt
-rw-rw-r-- 1 1001 1001 0 May 06 14:21 another_file.txt
>>>
>>> folder_server_object.remove("a_file.txt")
>>> folder_server_object.listdir()
-rw-rw-r-- 1 1001 1001 0 May 06 14:21 another_file.txt
>>> server.dir()
-rw-rw-r-- 1 1001 1001 0 May 06 14:21 another_file.txt
>>> server.delete("another_file.txt")
'250 Delete operation successful.'
>>> folder_server_object.listdir()
>>> server.dir()
>>> folder_server_object.close() == server.quit()

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-tools/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/server-tools/issues/new?body=module:%20server_ftp%0Aversion:%2020.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
-------

* Therp BV

Contributors
------------

- Nikos Tsirintanis ntsirintanis@therp.nl

Other credits
-------------

Therp B.V.

Maintainers
-----------

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/server-tools <https://github.com/OCA/server-tools/tree/20.0/server_ftp>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
3 changes: 3 additions & 0 deletions server_ftp/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from . import lib
from . import models
16 changes: 16 additions & 0 deletions server_ftp/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2020 - 2023 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
{
"name": "Server FTP",
"version": "20.0.1.0.0",
"author": "Therp BV, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/server-tools",
"license": "AGPL-3",
"category": "Tools",
"summary": "Enable connection via various protocols",
"depends": ["base"],
"data": ["security/ir.access.csv", "views/server_ftp_views.xml"],
"demo": ["demo/server_ftp.xml", "demo/server_ftp_folder.xml"],
"external_dependencies": {"python": ["paramiko"]},
"installable": True,
}
10 changes: 10 additions & 0 deletions server_ftp/demo/server_ftp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo noupdate="1">
<record id="ftp_server" model="server.ftp">
<field name="name">FTP Example Server</field>
<field name="server_type">ftp</field>
<field name="host">ftp.example.com</field>
<field name="user">user</field>
<field name="password">password</field>
</record>
</odoo>
15 changes: 15 additions & 0 deletions server_ftp/demo/server_ftp_folder.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<odoo noupdate="1">
<!--- FTP folders-->
<record id="ftp_push_folder" model="server.ftp.folder">
<field name="name">Push folder</field>
<field name="path">/pull_folder</field>
<field name="server_id" ref="ftp_server" />
</record>

<record id="ftp_pull_folder" model="server.ftp.folder">
<field name="name">Pull folder</field>
<field name="path">/push_folder</field>
<field name="server_id" ref="ftp_server" />
</record>
</odoo>
6 changes: 6 additions & 0 deletions server_ftp/lib/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from . import abstract_ftp_server
from . import ftp_server
from . import ftp_tls_server
from . import sftp_server
from . import mock_server
71 changes: 71 additions & 0 deletions server_ftp/lib/abstract_ftp_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Copyright 2020 - 2023 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from abc import ABC, abstractmethod


class AbstractFTPServer(ABC):
def __init__(self):
self.server = None
super().__init__()

def get_server(self):
return self.server

@abstractmethod
def connect(self, host, port, user, password):
"""Connect to object"""
raise NotImplementedError

@abstractmethod
def close(self):
"""Close connection"""
raise NotImplementedError

@abstractmethod
def putfo(self, file_like, name):
"""Transfer file-like object to server

name: filename+extension for file at server
file_like: byte object, content of which
will appear in server as name
"""
raise NotImplementedError

@abstractmethod
def put(self, filepath, name):
"""Transfer file object to server

name: filename+extension for file at server
filepath: path + the actual file you are putting
"""
raise NotImplementedError

@abstractmethod
def getfo(self, file_like, name):
"""Get file from server

name: filename of file to retrieve
file_like: byte object to save containt
of filename
"""
raise NotImplementedError

@abstractmethod
def remove(self, file):
"""Remove file from server"""
raise NotImplementedError

@abstractmethod
def listdir(self, path=""):
"""List in current directory"""
raise NotImplementedError

@abstractmethod
def cd(self, path):
"""Change directory"""
raise NotImplementedError

@abstractmethod
def exists(self, file, path=""):
"""See if file exists in path"""
raise NotImplementedError
73 changes: 73 additions & 0 deletions server_ftp/lib/ftp_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Copyright 2020 - 2023 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from ftplib import FTP, error_perm

from .abstract_ftp_server import AbstractFTPServer


class FTPServer(AbstractFTPServer):
def connect(self, host, port, user, password):
"""Connect to server"""
self.server = FTP()
# Init step by step
self.server.connect(host, port)
self.server.login(user, password)
return self

def close(self):
"""Close connection"""
self.server.quit()
self.server = None

def putfo(self, file_like, name):
"""Transfer file-like object to server

name: filename+extension for file at server
file_like: byte object, content of which
will appear in server as name

Example

bytes_ = b'abc def'
name = "abc.txt"
self.putfo(io.BytesIO(bytes_), name)
"""
self.server.storbinary("STOR " + name, file_like)

def put(self, filepath, name):
"""Transfer file object to server

name: filename+extension for file at server
filepath: path + the actual file you are putting
"""
with open(filepath, "rb") as file:
self.server.storbinary("STOR " + name, file)

def getfo(self, filepath, name):
"""Get file-like object from server

name: containt of file in filepath in bytes
filepath: filename+extension for file at server
"""
self.server.retrbinary("RETR " + filepath, name.write)

def remove(self, file):
"""Remove file from server"""
self.server.delete(file)

def listdir(self, path=""):
"""List directory in path"""
files = []
try:
files = self.server.nlst(path)
except error_perm:
files = []
return files

def cd(self, path):
"""Change directory"""
self.server.cwd(path)

def exists(self, file, path=""):
"""See if file exists in path"""
return file in self.server.nlst(path)
15 changes: 15 additions & 0 deletions server_ftp/lib/ftp_tls_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2020 - 2023 Therp BV <https://therp.nl>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from ftplib import FTP_TLS

from .ftp_server import FTPServer


class FTPTLSServer(FTPServer):
def connect(self, host, port, user, password):
"""Connect to server"""
self.server = FTP_TLS()
# Init step by step
self.server.connect(host, port)
self.server.login(user, password)
return self
Loading
Loading