0

How can i parse what other function print? IE. function prints

131072 of 1093419 downloaded
393216 of 1093419 downloaded
786432 of 1093419 downloaded
1089536 of 1093419 downloaded

And i would like to create a gauge (http://wiki.wxpython.org/wxGauge%20Widget) out of this data.

5
  • use regular expressions Commented Jun 22, 2013 at 13:43
  • 2
    Are you asking about how to capture the STDOUT of another process? Is this a child process of your Python program? Commented Jun 22, 2013 at 13:44
  • This is from mega.py library (github.com/richardasaurus/mega.py) Commented Jun 22, 2013 at 13:46
  • 1
    I would suggest opening an issue suggesting that it may be useful to provide a callback for some sort of progress function, rather than the library outputting to stdout. If you can, make the patch yourself. Commented Jun 22, 2013 at 13:50
  • First, you modify the mega module accordingly. Then, you create the patch with the diff command to submit it to the author(s) of the module. As for how to modify the module, I'd start off with adding a function parameter that contains a callback which is invoked instead of the print() output. Commented Jun 22, 2013 at 14:05

1 Answer 1

2

As @AlexChamberlain says, the best way you have to do what you want is to patch the mega library so you can use its output and make good use of it, you can change the code easily as follows:

in the file mega/mega.py you change the download_file method so it calls a callback on all results instead of printing them:

def download_file(self, file_handle, file_key, dest_path=None, dest_filename=None, is_public=False, file=None, callback=None):

Then you use that callback at line 496. Instead of that line:

print('{0} of {1} downloaded'.format(file_info.st_size, file_size))

you add:

if callback:
    callback(file_info.st_size, file_size)
else:
    print('{0} of {1} downloaded'.format(file_info.st_size, file_size))

And then you have to change the prototypes of all functions that call download_file(), e.g. for download():

def download(self, file, dest_path=None, dest_filename=None, callback=None):
    """
    Download a file by it's file object
    """
    self.download_file(None, None, file=file[1], dest_path=dest_path, dest_filename=dest_filename, is_public=False, callback=callback)

And finally you can use download as follows:

def myupdater(current, total):
    print "downloaded {0}/{1} so far".format(current, total)

mega.download('xxx', '/tmp', 'foo', callback=myupdater)

of course my answer is not complete (you'll have to do it for download_url and find if you shall apply the same pattern for other features, like upload). But I hope you get the idea, so you can be proud of committing a patch to an opensource project!

N.B.: to create a patch, you shall look at this document at github's.

HTH

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.