Files
DemonEditor/app/ftp.py

82 lines
2.9 KiB
Python
Raw Normal View History

2017-11-06 22:17:43 +03:00
from enum import Enum
2017-10-14 12:24:59 +03:00
from ftplib import FTP
import os
2017-11-06 22:17:43 +03:00
2017-10-14 12:24:59 +03:00
__DATA_FILES_LIST = ("tv", "radio", "lamedb")
2017-11-06 22:17:43 +03:00
class DownloadDataType(Enum):
ALL = 0
BOUQUETS = 1
SATELLITES = 2
def download_data(*, properties, download_type=DownloadDataType.ALL):
2017-11-10 13:38:03 +03:00
with FTP(host=properties["host"], timeout=5) as ftp:
2017-10-14 12:24:59 +03:00
ftp.login(user=properties["user"], passwd=properties["password"])
save_path = properties["data_dir_path"]
files = []
2017-11-06 22:17:43 +03:00
# bouquets section
if download_type is DownloadDataType.ALL or download_type is DownloadDataType.BOUQUETS:
ftp.cwd(properties["services_path"])
ftp.dir(files.append)
for file in files:
name = str(file).strip()
if name.endswith(__DATA_FILES_LIST):
name = name.split()[-1]
with open(save_path + name, 'wb') as f:
ftp.retrbinary('RETR ' + name, f.write)
2017-10-14 12:24:59 +03:00
# satellites.xml section
2017-11-06 22:17:43 +03:00
if download_type is DownloadDataType.ALL or download_type is DownloadDataType.SATELLITES:
ftp.cwd(properties["satellites_xml_path"])
files.clear()
ftp.dir(files.append)
for file in files:
name = str(file).strip()
xml_file = "satellites.xml"
if name.endswith(xml_file):
with open(save_path + xml_file, 'wb') as f:
ftp.retrbinary('RETR ' + xml_file, f.write)
2017-10-26 01:23:05 +03:00
2017-10-14 12:24:59 +03:00
2017-11-06 22:17:43 +03:00
def upload_data(*, properties, download_type=DownloadDataType.ALL, remove_unused=False):
2017-11-10 13:38:03 +03:00
data_path = properties["data_dir_path"]
with FTP(host=properties["host"], timeout=5) as ftp:
ftp.login(user=properties["user"], passwd=properties["password"])
if download_type is DownloadDataType.ALL or download_type is DownloadDataType.SATELLITES:
ftp.cwd(properties["satellites_xml_path"])
file_name = "satellites.xml"
send = send_file(file_name, data_path, ftp)
if download_type == DownloadDataType.SATELLITES:
return send
if download_type is DownloadDataType.ALL or download_type is DownloadDataType.BOUQUETS:
ftp.cwd(properties["services_path"])
if remove_unused:
files = []
ftp.dir(files.append)
for file in files:
name = str(file).strip()
if name.endswith(__DATA_FILES_LIST):
name = name.split()[-1]
ftp.delete(name)
for file_name in os.listdir(data_path):
if file_name == "satellites.xml":
continue
send_file(file_name, data_path, ftp)
def send_file(file_name, path, ftp):
""" Opens the file in binary mode and transfers into receiver """
with open(path + file_name, "rb") as f:
return ftp.storbinary("STOR " + file_name, f)