def setpath(self, name="", create_dir=False, to_parent=False, header_list=()): """setpath(self, name = "", create_dir = False, to_parent = False, header_list = ()) Requests a change to the server's current directory for the session to the directory with the specified name, and returns the response. This method is also used to perform other actions, such as navigating to the parent directory (set to_parent to True) and creating a new directory (set create_dir to True). Additional headers can be sent by passing a sequence as the header_list keyword argument. These will be sent after the name information. """ header_list = list(header_list) if name is not None: header_list = [headers.Name(name)] + header_list max_length = self.remote_info.max_packet_length flags = 0 if not create_dir: flags |= requests.Set_Path.DontCreateDir if to_parent: flags |= requests.Set_Path.NavigateToParent request = requests.Set_Path((flags, 0)) response = self._send_headers(request, header_list, max_length) return response
def _get(self, name=None, header_list=()): header_list = list(header_list) if name is not None: header_list = [headers.Name(name)] + header_list max_length = self.remote_info.max_packet_length request = requests.Get() response = self._send_headers(request, header_list, max_length) yield response if not isinstance(response, responses.Continue) and \ not isinstance(response, responses.Success): return # Retrieve the file data. file_data = [] request = requests.Get_Final() while isinstance(response, responses.Continue): self.socket.sendall(request.encode()) response = self.response_handler.decode(self.socket) yield response
def put(self, name, file_data, header_list = ()): """put(self, name, file_data, header_list = ()) Sends a file with the given name, containing the file_data specified, to the server for storage in the current directory for the session, and returns the response. Additional headers can be sent by passing a sequence as the header_list keyword argument. These will be sent after the name and file length information associated with the name and file_data supplied. """ header_list = [ headers.Name(name), headers.Length(len(file_data)) ] + list(header_list) max_length = self.remote_info.max_packet_length request = requests.Put() self.state = common.PUT self.state_put = common.PUT_HEADER self.file_data = file_data self.pending_headers = header_list self._send_headers(request, max_length)
def get_message(self, message_id): response = self.get(header_list=[ self.connection_id, headers.Name(message_id), headers.Type("x-bt/message"), headers.App_Parameters("\x0A\x01\x14\x01") ]) if isinstance(response, responses.FailureResponse): raise Exception() header, data = response return data
def get(self, socket, request): name = "" type = "" for header in request.header_data: print("header is " + header) if isinstance(header, headers.Name): name = header.decode().strip(b"\x00") print("Receiving request for %s" % name) elif isinstance(header, headers.Type): type = header.decode().strip(b"\x00") print("Type %s" % type) path = os.path.abspath(os.path.join(self.directory, name)) if os.path.isdir(path) or type == "x-obex/folder-listing": details = {} if path.startswith(self.directory): l = os.listdir(path) s = '<?xml version="1.0"?>\n<folder-listing>\n' for i in l: objpath = os.path.join(path, i) if os.path.isdir(objpath): details[i] = (i, os.stat(objpath)[stat.ST_CTIME]) s += ' <folder name="%s" created="%s" />' % details[i] else: details[i] = (i, os.stat(objpath)[stat.ST_CTIME], os.stat(objpath)[stat.ST_SIZE]) s += ' <file name="%s" created="%s" size="%s" />' % details[ i] s += "</folder-listing>\n" print(s) response = responses.Success() response_headers = [ headers.Name(name.encode("utf8")), headers.Length(len(s)), headers.Body(s.encode("utf8")) ] self.send_response(socket, response, response_headers) else: self._reject(socket) else: self._reject(socket)
def delete(self, name, header_list=()): """delete(self, name, header_list = ()) Requests the deletion of the file with the specified name from the current directory and returns the server's response. """ header_list = [headers.Name(name)] + list(header_list) max_length = self.remote_info.max_packet_length request = requests.Put_Final() return self._send_headers(request, header_list, max_length)
def _put(self, name, file_data, header_list = ()): header_list = [ headers.Name(name), headers.Length(len(file_data)) ] + list(header_list) max_length = self.remote_info.max_packet_length request = requests.Put() response = self._send_headers(request, header_list, max_length) yield response if not isinstance(response, responses.Continue): return # Send the file data. # The optimum size is the maximum packet length accepted by the # remote device minus three bytes for the header ID and length # minus three bytes for the request. optimum_size = max_length - 3 - 3 i = 0 while i < len(file_data): data = file_data[i:i+optimum_size] i += len(data) if i < len(file_data): request = requests.Put() request.add_header(headers.Body(data, False), max_length) self.socket.sendall(request.encode()) response = self.response_handler.decode(self.socket) yield response if not isinstance(response, responses.Continue): return else: request = requests.Put_Final() request.add_header(headers.End_Of_Body(data, False), max_length) self.socket.sendall(request.encode()) response = self.response_handler.decode(self.socket) yield response if not isinstance(response, responses.Success): return