Exemplo n.º 1
0
def monitor_progress(num_files):
    """Watches a log file changes and draws a progress bar
    in the terminal.
    """
    from time import sleep
    import sys

    pbar = ProgressBar(num_files)

    # Try three times to open the file
    for x in range(3):
        try:
            f = open(os.path.join(config.DATA, 'corenlp_log.txt'))
            break
        except IOError:
            sleep(4)
        print "ERROR: Unable to find corenlp_log.txt"


    fname = ''
    while True:
        f.seek(0) # Refresh log.
        try:
            line = f.readlines()[-1]
        except IndexError:
            sleep(1)
            continue
        

        if line and line.strip().startswith('Annotating file'):
            # Once we find the right line, start the pbar
            if not pbar.has_started():
                print "Sending files to StanfordCoreNLP..."
                pbar.start()

            # Ensure corenlp is working on a new file
            new_fname = line.split('/')[-1].split(' ')[0]
            if pbar.has_started() and new_fname != fname:
                fname = new_fname
                pbar.tick()
                
        if pbar.is_done():
            # Stop the thread
            return
        sleep(.1)