-
Notifications
You must be signed in to change notification settings - Fork 0
/
ulftp.py
63 lines (50 loc) · 1.85 KB
/
ulftp.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
from ftplib import FTP
from os.path import splitext
from tempfile import NamedTemporaryFile
class UlFtp:
def __init__(self):
self.ftp = None
def open(self, addr):
self.ftp = FTP(addr)
self.ftp.login()
def quit(self):
self.ftp.quit()
def get_latest_printer_file(self, dir=None):
"""
Gets the latest printer file from the U2 to a tempfile in the given directory
:param dir: directory to save files to (None to use a default temp directory)
:return: path to the tempfile
"""
self.ftp.cwd("/Usb0")
latest_n = 0
latest_filename = None
for filename, facts in self.ftp.mlsd():
if filename.startswith("printer-"):
try:
n = int(splitext(filename)[0].split("-")[1])
except ValueError:
continue
if n > latest_n:
latest_n = n
latest_filename = filename
tf = NamedTemporaryFile(suffix=".png", delete=False, dir=dir)
print(" retrieving {} to {}".format(latest_filename, tf.name))
self.ftp.retrbinary("RETR {}".format(latest_filename), tf.write)
return tf.name
def get_file(self, filename):
"""
Gets the given file from /Usb0, and saves to the current local directory
:param filename: filename, including extension
"""
self.ftp.cwd("/Usb0")
tf = open(filename, "wb")
self.ftp.retrbinary("RETR {}".format(filename), tf.write)
def send(self, filepath, filename):
"""
Sends the given file to /Usb0 root
:param filepath: path to local file
:param filename: remote filename
"""
self.ftp.cwd("/Usb0")
fp = open(filepath, 'rb')
self.ftp.storbinary("STOR {}".format(filename), fp)