mk-fg / python-onedrive

Obsolete python/cli module for MS SkyDrive/OneDrive's old API, do not use for new projects
Do What The F*ck You Want To Public License
200 stars 32 forks source link

add callback function to put_bits method #60

Closed W4RH4WK closed 9 years ago

W4RH4WK commented 9 years ago

It is no problem to do a chunked upload using the put_bits method, but I need to do some tasks between chunk uploads (status update and similar). This pull-requests adds an optional callback to the method which will take transferred bytes and total bytes as arguments.

def my_callback(bytes_transferred, bytes_total):
    print "uploaded: {}%".format(bytes_transferred * 100 / bytes_total)
mk-fg commented 9 years ago

Thanks!

I think it might be worth giving callback keyword a more descriptive name, like "chunk_callback" and passing it a session id, so that it can also be used to e.g. store that to resume download after process restart, and can be just left unused in case of simple progress report.

I'll probably add these on top.

mk-fg commented 9 years ago

In a8a44ab I've changed "callback -> chunk_callback" and made code pass it the following stuff:

chunk_callback(
  bytes_uploaded=c, bytes_count=src_len,
  chunk_n=n, chunk_count=cn, bits_session_id=bits_sid )

Idea behind using keywords is that you can easily ignore unneeded ones in callbacks, which is not the case with positional args, e.g. in case of progress-callback above, you can have:

def my_callback(bytes_uploaded, bytes_count, **kws):

And not worry about all the unneeded stuff and/or possible future additions there.

Though now that I see naming of the keywords in your callback, I think I'll also change "uploaded -> transferred" and "count -> total", as it makes a lot more sense, I just couldn't come up with the right words in a8a44ab ;)

mk-fg commented 9 years ago

Though now that I see naming of the keywords in your callback, I think I'll also change "uploaded -> transferred" and "count -> total"

18518c1

W4RH4WK commented 9 years ago

great, thank you