-
Notifications
You must be signed in to change notification settings - Fork 1
/
bizerba_ftp.py
231 lines (183 loc) · 5.82 KB
/
bizerba_ftp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#! python3
# coding: utf-8
"""Download files needed by the Bizerba scales from an FTP server.
Autor:
Rémy Taymans <[email protected]>
Current Maintainer:
Rémy Taymans <[email protected]>
Creation: 10 oct 2017
Last change: 11 oct 2017
Usage:
Write in the configuration file:
- ftp address
- ftp user
- ftp password
- ftp directory for csv
- ftp directory for old csv
- ftp directory for image
- local folder for csv
- local folder for image
- location of the log file
Then run this script.
"""
from ftplib import FTP
import logging
import os
import configparser
__author__ = "Rémy Taymans"
__copyright__ = "Copyright 2017, Rémy Taymans"
__credits__ = ["Rémy Taymans"]
__license__ = "GPLv3"
__version__ = "0.2"
__maintainer__ = "Rémy Taymans"
__email__ = "[email protected]"
__status__ = "Development"
CONFIG_FILENAME = "bizerba.conf"
LOG_FILENAME = "bizerba.log"
def main():
"""Program start here"""
# Read config file
config = get_config()
# Open config file
try:
logfilename = config.get('log', 'filename').strip('"')
except configparser.Error:
logfilename = LOG_FILENAME
logging.basicConfig(
filename=logfilename,
format='%(asctime)s - %(levelname)s : %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=20 # Loglevel INFO
)
logging.info("Check the config file")
# Check config file
if not check_config(config):
logging.critical("The config file is not properly writed")
exit(1)
logging.info("Config file check passed successfully")
logging.info("Starts running script")
# Connect to FTP
logging.info('Open connection with FTP server')
ftp = FTP(config['ftp'].get('address').strip('"'))
ftp.login(
user=config['ftp'].get('user').strip('"'),
passwd=config['ftp'].get('password').strip('"')
)
# Set encoding
ftp.encoding = "cp1252"
# Get images frist than files
get_image_files(ftp, config)
get_csv_files(ftp, config)
try:
ftp.quit()
logging.info('Quit connection')
except Exception:
ftp.close()
logging.info('Close connection')
def get_config(config_filename=CONFIG_FILENAME):
"""Return the config file as an object"""
config = configparser.ConfigParser()
config.read(config_filename)
return config
def check_config(config):
"""Check that the config file have all the required field"""
try:
config.get('ftp', 'address')
config.get('ftp', 'user')
config.get('ftp', 'password')
config.get('ftp', 'csv_dir')
config.get('ftp', 'backup_csv_dir')
config.get('ftp', 'image_dir')
except configparser.Error as err:
logging.error(err)
return False
try:
config.get('local', 'csv_dir')
config.get('local', 'image_dir')
except configparser.Error as err:
logging.error(err)
return False
try:
config.get('log', 'filename')
except configparser.Error as err:
logging.error(err)
return False
return True
def remove_hidden_files(files):
"""Remove hidden file form the list given in args"""
new_files = []
for f in files:
if not f.startswith('.'):
new_files.append(f)
return new_files
def keep_only_csv(files):
"""Remove all the files that are not csv in the list given in
args"""
new_files = []
for f in files:
f_low = f.lower()
if f_low.endswith('.csv'):
new_files.append(f)
return new_files
def get_csv_files(ftp, config, move_csv_to_backup=True):
"""Download the csv files from the right directory on the ftp server
into the right local directory. If move_csv_to_backup is True, the
csv file on the ftp are moved from their original place to the
backup dir.
"""
# Change local working directory to CSV import directory
os.chdir(config['local'].get('csv_dir').strip('"'))
logging.info('Working in %s directory', os.getcwd())
# Get CSV files
ftp.cwd(config['ftp'].get('csv_dir').strip('"'))
files = ftp.nlst()
files = remove_hidden_files(files)
files = keep_only_csv(files)
for f in files:
logging.info('Writing %s', f)
get_text_file_from_ftp(ftp, f)
if move_csv_to_backup:
logging.info(
'Move %s to %s/%s',
f,
config['ftp'].get('backup_csv_dir').strip('"'),
f
)
ftp.rename(
f,
'%s/%s' % (
config['ftp'].get('backup_csv_dir').strip('"'),
f
)
)
def get_image_files(ftp, config):
"""Download the image files from the right directory on the ftp
server into the right local dirctory.
"""
# Change local working directory to image import directory
os.chdir(config['local'].get('image_dir').strip('"'))
logging.info('Working in %s directory', os.getcwd())
# Get images
ftp.cwd(config['ftp'].get('image_dir').strip('"'))
files = ftp.nlst()
files = remove_hidden_files(files)
for f in files:
logging.info('Writing %s', f)
get_binary_file_from_ftp(ftp, f)
def get_text_file_from_ftp(ftp, f):
"""Download a text file from an existing FTP connection.
The transfer is in cp1252.
"""
with open(f, 'w', encoding="cp1252") as local_file:
ftp.retrlines(
'RETR %s' % f,
lambda s, w=local_file.write: w(s+'\n')
)
def get_binary_file_from_ftp(ftp, f):
"""Download a file from an existing FTP connection.
The transfer is done in binary mode.
"""
with open(f, 'wb') as local_file:
ftp.retrbinary('RETR %s' % f, local_file.write)
if __name__ == "__main__":
main()