Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2@file 

3@brief API to move files using FTP 

4""" 

5import os 

6from ..loghelper import noLOG 

7from .transfer_api import TransferAPI 

8 

9 

10class TransferAPIFile(TransferAPI): 

11 """ 

12 Defines an API to transfer files over another location. 

13 """ 

14 

15 def __init__(self, location, fLOG=noLOG): 

16 """ 

17 @param location location 

18 @param fLOG logging function 

19 """ 

20 TransferAPI.__init__(self, fLOG=fLOG) 

21 self._location = location 

22 

23 def transfer(self, path, data): 

24 """ 

25 It assumes a data holds in memory, 

26 tansfer data to path. 

27 

28 @param data bytes 

29 @param path path to remove location 

30 @return boolean 

31 """ 

32 src = os.path.join(self._location, path) 

33 fol = os.path.dirname(src) 

34 if not os.path.exists(fol): 

35 os.makedirs(fol) 

36 with open(src, "wb") as f: 

37 f.write(data) 

38 return True 

39 

40 def retrieve(self, path, exc=True): 

41 """ 

42 Retrieves data from path. 

43 

44 @param path remove location 

45 @param exc keep exception 

46 @return data 

47 """ 

48 src = os.path.join(self._location, path) 

49 if os.path.exists(src): 

50 with open(src, "rb") as f: 

51 return f.read() 

52 if exc: 

53 raise FileNotFoundError(path) # pragma: no cover 

54 return None