| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from __future__ import absolute_import, print_function, unicode_literals |
| 5 |
|
| 6 |
from optparse import OptionParser |
| 7 |
import os.path |
| 8 |
import sys |
| 9 |
import dbus |
| 10 |
import dbus.service |
| 11 |
import dbus.mainloop.glib |
| 12 |
try: |
| 13 |
from gi.repository import GObject |
| 14 |
except ImportError: |
| 15 |
import gobject as GObject |
| 16 |
|
| 17 |
BUS_NAME='org.bluez.obex' |
| 18 |
PATH = '/org/bluez/obex' |
| 19 |
CLIENT_INTERFACE='org.bluez.obex.Client1' |
| 20 |
SESSION_INTERFACE='org.bluez.obex.Session1' |
| 21 |
FILE_TRASNFER_INTERFACE='org.bluez.obex.FileTransfer1' |
| 22 |
TRANSFER_INTERFACE='org.bluez.obex.Transfer1' |
| 23 |
|
| 24 |
def parse_options(): |
| 25 |
parser.add_option("-d", "--device", dest="device", |
| 26 |
help="Device to connect", metavar="DEVICE") |
| 27 |
parser.add_option("-c", "--chdir", dest="new_dir", |
| 28 |
help="Change current directory to DIR", metavar="DIR") |
| 29 |
parser.add_option("-l", "--list", action="store_true", dest="list_dir", |
| 30 |
help="List the current directory") |
| 31 |
parser.add_option("-g", "--get", dest="get_file", |
| 32 |
help="Get FILE", metavar="FILE") |
| 33 |
parser.add_option("-p", "--put", dest="put_file", |
| 34 |
help="Put FILE", metavar="FILE") |
| 35 |
parser.add_option("-y", "--copy", dest="copy_file", |
| 36 |
help="Copy FILE", metavar="FILE") |
| 37 |
parser.add_option("-m", "--move", dest="move_file", |
| 38 |
help="Move FILE", metavar="FILE") |
| 39 |
parser.add_option("-n", "--destname", dest="dest_file", |
| 40 |
help="Destination FILE", metavar="FILE") |
| 41 |
parser.add_option("-r", "--remove", dest="remove_file", |
| 42 |
help="Remove FILE", metavar="FILE") |
| 43 |
parser.add_option("-v", "--verbose", action="store_true", |
| 44 |
dest="verbose") |
| 45 |
|
| 46 |
return parser.parse_args() |
| 47 |
|
| 48 |
class FtpClient: |
| 49 |
def __init__(self, session_path, verbose=False): |
| 50 |
self.transferred = 0 |
| 51 |
self.transfer_path = None |
| 52 |
self.transfer_size = 0 |
| 53 |
self.verbose = verbose |
| 54 |
bus = dbus.SessionBus() |
| 55 |
obj = bus.get_object(BUS_NAME, session_path) |
| 56 |
self.session = dbus.Interface(obj, SESSION_INTERFACE) |
| 57 |
self.ftp = dbus.Interface(obj, FILE_TRASNFER_INTERFACE) |
| 58 |
bus.add_signal_receiver(self.properties_changed, |
| 59 |
dbus_interface="org.freedesktop.DBus.Properties", |
| 60 |
signal_name="PropertiesChanged", |
| 61 |
path_keyword="path") |
| 62 |
|
| 63 |
def create_transfer_reply(self, path, properties): |
| 64 |
self.transfer_path = path |
| 65 |
self.transfer_size = properties["Size"] |
| 66 |
if self.verbose: |
| 67 |
print("Transfer created: %s" % path) |
| 68 |
|
| 69 |
def generic_reply(self): |
| 70 |
if self.verbose: |
| 71 |
print("Operation succeeded") |
| 72 |
|
| 73 |
def error(self, err): |
| 74 |
print(err) |
| 75 |
mainloop.quit() |
| 76 |
|
| 77 |
def properties_changed(self, interface, properties, invalidated, path): |
| 78 |
if path != self.transfer_path: |
| 79 |
return |
| 80 |
|
| 81 |
if "Status" in properties and \ |
| 82 |
(properties['Status'] == 'complete' or \ |
| 83 |
properties['Status'] == 'error'): |
| 84 |
if self.verbose: |
| 85 |
print("Transfer %s" % properties['Status']) |
| 86 |
mainloop.quit() |
| 87 |
return |
| 88 |
|
| 89 |
if "Transferred" not in properties: |
| 90 |
return |
| 91 |
|
| 92 |
value = properties["Transferred"] |
| 93 |
speed = (value - self.transferred) / 1000 |
| 94 |
print("Transfer progress %d/%d at %d kBps" % (value, |
| 95 |
self.transfer_size, |
| 96 |
speed)) |
| 97 |
self.transferred = value |
| 98 |
|
| 99 |
def change_folder(self, new_dir): |
| 100 |
for node in new_dir.split("/"): |
| 101 |
self.ftp.ChangeFolder(node) |
| 102 |
|
| 103 |
def list_folder(self): |
| 104 |
for i in self.ftp.ListFolder(): |
| 105 |
if i["Type"] == "folder": |
| 106 |
print("%s/" % (i["Name"])) |
| 107 |
else: |
| 108 |
print("%s" % (i["Name"])) |
| 109 |
|
| 110 |
def put_file(self, filename): |
| 111 |
self.ftp.PutFile(os.path.abspath(filename), |
| 112 |
os.path.basename(filename), |
| 113 |
reply_handler=self.create_transfer_reply, |
| 114 |
error_handler=self.error) |
| 115 |
|
| 116 |
def get_file(self, filename): |
| 117 |
self.ftp.GetFile(os.path.abspath(filename), |
| 118 |
os.path.basename(filename), |
| 119 |
reply_handler=self.create_transfer_reply, |
| 120 |
error_handler=self.error) |
| 121 |
|
| 122 |
def remove_file(self, filename): |
| 123 |
self.ftp.Delete(filename, |
| 124 |
reply_handler=self.generic_reply, |
| 125 |
error_handler=self.error) |
| 126 |
|
| 127 |
def move_file(self, filename, destname): |
| 128 |
self.ftp.MoveFile(filename, destname, |
| 129 |
reply_handler=self.generic_reply, |
| 130 |
error_handler=self.error) |
| 131 |
|
| 132 |
def copy_file(self, filename, destname): |
| 133 |
self.ftp.CopyFile(filename, destname, |
| 134 |
reply_handler=self.generic_reply, |
| 135 |
error_handler=self.error) |
| 136 |
|
| 137 |
if __name__ == '__main__': |
| 138 |
|
| 139 |
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 140 |
|
| 141 |
parser = OptionParser() |
| 142 |
|
| 143 |
(options, args) = parse_options() |
| 144 |
|
| 145 |
if not options.device: |
| 146 |
parser.print_help() |
| 147 |
sys.exit(0) |
| 148 |
|
| 149 |
bus = dbus.SessionBus() |
| 150 |
mainloop = GObject.MainLoop() |
| 151 |
|
| 152 |
client = dbus.Interface(bus.get_object(BUS_NAME, PATH,), |
| 153 |
CLIENT_INTERFACE) |
| 154 |
|
| 155 |
print("Creating Session") |
| 156 |
path = client.CreateSession(options.device, { "Target": "ftp" }) |
| 157 |
|
| 158 |
ftp_client = FtpClient(path, options.verbose) |
| 159 |
|
| 160 |
if options.new_dir: |
| 161 |
ftp_client.change_folder(options.new_dir) |
| 162 |
|
| 163 |
if options.list_dir: |
| 164 |
ftp_client.list_folder() |
| 165 |
|
| 166 |
if options.get_file: |
| 167 |
ftp_client.get_file(options.get_file) |
| 168 |
|
| 169 |
if options.put_file: |
| 170 |
ftp_client.put_file(options.put_file) |
| 171 |
|
| 172 |
if options.move_file: |
| 173 |
ftp_client.move_file(options.move_file, options.dest_file) |
| 174 |
|
| 175 |
if options.copy_file: |
| 176 |
ftp_client.copy_file(options.copy_file, options.dest_file) |
| 177 |
|
| 178 |
if options.remove_file: |
| 179 |
ftp_client.remove_file(options.remove_file) |
| 180 |
|
| 181 |
mainloop.run() |