Examples¶
Configuration files¶
; application configuration: example/configs/filetransfer.cfg
[global]
jobs_dir = example/configs/jobs
hosts_cfg = example/configs/hosts.cfg
[logging]
log_dir = example/logs
[sftp]
known_hosts = example/ssh/known_hosts
key_rsa_file = example/ssh/rsa_key
[mail]
host = smtp.example.com:587
security = STARTTLS
user = transferuser
password = topsecret
from_addr = transferuser@example.com
[notify]
error = admin@example.com
; job configuration: example/configs/jobs/job001.cfg
[job]
name = Example file transfer
[source]
path = example/data
files = *.txt, *.csv
recursive = yes
[target]
host_id = other-company-sftp
path = path/on/server
temp = ext:.tmp
[notify]
done = user@example.com
; hosts configuration: example/configs/hosts.cfg
[other-company-sftp]
type = SFTP
host = other-company.com
user = user_xyz
key_type = RSA
[example-ftps]
type = FTPS
host = ftp.example.com
user = ftpuser
password = secret
encrypt_data = no
filetransfer --config example/configs/filetransfer.cfg job001
configure()¶
from filetransfer import (configure, Terminated, Error,
set_sigterm_handler)
set_sigterm_handler()
try:
exc = None
run, _ = configure('example/configs/filetransfer.cfg', 'job001')
try:
... # e.g. prepare data that will be transferred
except BaseException as ex:
exc = ex
_, code = run(exc)
raise SystemExit(code)
except (Error, Terminated) as ex:
raise SystemExit(ex.code)
except Exception as ex:
raise SystemExit(9)
extra configuration¶
; job configuration: example/configs/jobs/job002.cfg
; sections for FileTransfer configuration omitted
[x:DEFAULT]
quuz = 47
[x:foo]
bar = 42
>>> from filetransfer import configure
>>> run, extra = configure('example/configs/filetransfer.cfg',
... 'job002', defaults={'quux':'1729'})
>>> extra['foo']['bar']
'42'
>>> extra['foo']['quuz']
'47'
>>> extra['foo']['quux']
'1729'
>>> type(extra)
configparser.ConfigParser
transfer()¶
from filetransfer import transfer
...
src_cfg = {'path': 'example/data',
'files': '*.txt, *.csv',
'recursive': 'yes'}
tgt_cfg = {'type': 'sftp',
'host': 'other-company.com',
'user': 'user_xyz',
'known_hosts': 'example/ssh/known_hosts',
'key_type': 'RSA',
'key_file': 'example/ssh/rsa_key',
'path': 'path/on/server',
'temp': 'ext:.tmp'}
result = transfer(src_cfg, tgt_cfg)
...
or
; config.ini
[source]
path: example/data
files: *.txt, *.csv
recursive: yes
[target]
type: sftp
host: other-company.com
user: user_xyz
known_hosts: example/ssh/known_hosts
key_type: RSA
key_file: example/ssh/rsa_key
path: path/on/server
temp: ext:.tmp
from configparser import ConfigParser
from filetransfer import transfer
...
cfg=ConfigParser()
with open('config.ini') as fh:
cfg.read_file(fh)
result = transfer(cfg)
...