ftp.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # vim:set ts=4 sw=4 et:
3 #
4 # Copyright 2014,2015 Vladimir Ermakov.
5 #
6 # This file is part of the mavros package and subject to the license terms
7 # in the top-level LICENSE file of the mavros repository.
8 # https://github.com/mavlink/mavros/tree/master/LICENSE.md
9 
10 __all__ = (
11  'FTPFile',
12  'open',
13  'listdir',
14  'unlink',
15  'mkdir',
16  'rmdir',
17  'rename',
18  'checksum',
19  'reset_server'
20 )
21 
22 import os
23 import rospy
24 import mavros
25 
26 from std_srvs.srv import Empty
27 from mavros_msgs.msg import FileEntry
28 from mavros_msgs.srv import FileOpen, FileClose, FileRead, FileList, FileOpenRequest, \
29  FileMakeDir, FileRemoveDir, FileRemove, FileWrite, FileTruncate, FileRename, \
30  FileChecksum
31 
32 
33 def _get_proxy(service, type):
34  return rospy.ServiceProxy(mavros.get_topic('ftp', service), type)
35 
36 
38  if not ret.success:
39  raise IOError(ret.r_errno, os.strerror(ret.r_errno))
40 
41 
42 class FTPFile(object):
43  """
44  FCU file object.
45  Note that current PX4 firmware only support two connections simultaneously.
46  """
47 
48  def __init__(self, name, mode):
49  self.name = None
50  self.mode = mode
51  self.open(name, mode)
52 
53  def __del__(self):
54  self.close()
55 
56  def open(self, path, mode):
57  """
58  Supported modes:
59  - 'w': write binary
60  - 'r': read binary
61  - 'cw': create excl & write
62  """
63  if mode == 'w' or mode == 'wb':
64  m = FileOpenRequest.MODE_WRITE
65  elif mode == 'r' or mode == 'rb':
66  m = FileOpenRequest.MODE_READ
67  elif mode == 'cw':
68  m = FileOpenRequest.MODE_CREATE
69  else:
70  raise ValueError("Unknown open mode: {}".format(m))
71 
72  open_ = _get_proxy('open', FileOpen)
73  try:
74  ret = open_(file_path=path, mode=m)
75  except rospy.ServiceException as ex:
76  raise IOError(str(ex))
77 
78  _check_raise_errno(ret)
79 
80  self._read = _get_proxy('read', FileRead)
81  self._write = _get_proxy('write', FileWrite)
82 
83  self.name = path
84  self.mode = mode
85  self.size = ret.size
86  self.offset = 0
87 
88  def close(self):
89  if self.closed:
90  return
91 
92  close_ = _get_proxy('close', FileClose)
93  try:
94  ret = close_(file_path=self.name)
95  except rospy.ServiceException as ex:
96  raise IOError(str(ex))
97 
98  self.name = None
99  _check_raise_errno(ret)
100 
101  def read(self, size=1):
102  try:
103  ret = self._read(file_path=self.name, offset=self.offset, size=size)
104  except rospy.ServiceException as ex:
105  raise IOError(str(ex))
106 
107  _check_raise_errno(ret)
108  self.offset += len(ret.data)
109  return bytearray(ret.data)
110 
111  def write(self, bin_data):
112  data_len = len(bin_data)
113  try:
114  ret = self._write(file_path=self.name, offset=self.offset, data=bin_data)
115  except rospy.ServiceException as ex:
116  raise IOError(str(ex))
117 
118  _check_raise_errno(ret)
119  self.offset += data_len
120  if self.offset > self.size:
121  self.size = self.offset
122 
123  def tell(self):
124  return self.offset
125 
126  def seek(self, offset, whence=os.SEEK_SET):
127  if whence is os.SEEK_SET:
128  self.offset = offset
129  elif whence is os.SEEK_END:
130  self.offset = offset + self.size
131  elif whence is os.SEEK_CUR:
132  self.offset += offset
133  else:
134  raise ValueError("Unknown whence")
135 
136  def truncate(self, size=0):
137  truncate_ = _get_proxy('truncate', FileTruncate)
138  try:
139  ret = truncate_(file_path=self.name, length=size)
140  except rospy.ServiceException as ex:
141  raise IOError(str(ex))
142 
143  _check_raise_errno(ret)
144 
145  @property
146  def closed(self):
147  return self.name is None
148 
149  def __enter__(self):
150  return self
151 
152  def __exit__(self, exc_type, exc_value, traceback):
153  self.close()
154 
155 
156 def open(path, mode):
157  """Open file on FCU"""
158  return FTPFile(path, mode)
159 
160 
161 def listdir(path):
162  """List directory :path: contents"""
163  try:
164  list_ = _get_proxy('list', FileList)
165  ret = list_(dir_path=path)
166  except rospy.ServiceException as ex:
167  raise IOError(str(ex))
168 
169  _check_raise_errno(ret)
170  return ret.list
171 
172 
173 def unlink(path):
174  """Remove :path: file"""
175  remove = _get_proxy('remove', FileRemove)
176  try:
177  ret = remove(file_path=path)
178  except rospy.ServiceException as ex:
179  raise IOError(str(ex))
180 
181  _check_raise_errno(ret)
182 
183 
184 def mkdir(path):
185  """Create directory :path:"""
186  mkdir_ = _get_proxy('mkdir', FileMakeDir)
187  try:
188  ret = mkdir_(dir_path=path)
189  except rospy.ServiceException as ex:
190  raise IOError(str(ex))
191 
192  _check_raise_errno(ret)
193 
194 
195 def rmdir(path):
196  """Remove directory :path:"""
197  rmdir_ = _get_proxy('rmdir', FileRemoveDir)
198  try:
199  ret = rmdir_(dir_path=path)
200  except rospy.ServiceException as ex:
201  raise IOError(str(ex))
202 
203  _check_raise_errno(ret)
204 
205 
206 def rename(old_path, new_path):
207  """Rename :old_path: to :new_path:"""
208  rename_ = _get_proxy('rename', FileRename)
209  try:
210  ret = rename_(old_path=old_path, new_path=new_path)
211  except rospy.ServiceException as ex:
212  raise IOError(str(ex))
213 
214  _check_raise_errno(ret)
215 
216 
217 def checksum(path):
218  """Calculate CRC32 for :path:"""
219  checksum_ = _get_proxy('checksum', FileChecksum)
220  try:
221  ret = checksum_(file_path=path)
222  except rospy.ServiceException as ex:
223  raise IOError(str(ex))
224 
225  _check_raise_errno(ret)
226  return ret.crc32
227 
228 
229 def reset_server():
230  reset = _get_proxy('reset', Empty)
231  try:
232  reset()
233  except rospy.ServiceException as ex:
234  raise IOError(str(ex))
def get_topic(args)
Definition: __init__.py:49
def __init__(self, name, mode)
Definition: ftp.py:48
def __del__(self)
Definition: ftp.py:53
def _check_raise_errno(ret)
Definition: ftp.py:37
def _get_proxy(service, type)
Definition: ftp.py:33
def open(self, path, mode)
Definition: ftp.py:56
def close(self)
Definition: ftp.py:88


mavros
Author(s): Vladimir Ermakov
autogenerated on Tue Jun 1 2021 02:36:26