import plac
from ftplib import FTP
import json
from pathlib import Path
from collections import namedtuple
FtpConfig = namedtuple('FtpConfig', 'server username password path')


class ftp:
    commands = ['upload']

    def __init__(self):
        cfg = json.load(open('ftp_config.json', 'r'))
        self.ftp_cfg = FtpConfig(**cfg)

    @plac.pos('filename', '', type=str)
    def upload(self, filename='CARLO.csv'):
        file_path = Path(filename)

        with FTP(self.ftp_cfg.server, self.ftp_cfg.username, self.ftp_cfg.password) as ftp_conn, open(file_path, 'rb') as f:
            ftp_conn.cwd(self.ftp_cfg.path)
            res = ftp_conn.storbinary(f'STOR {file_path.name}', f)
            print(res)


if __name__ == '__main__':
    plac.Interpreter.call(ftp)
